Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AssertCategoryPage.php
Go to the documentation of this file.
1 <?php
8 
11 use Magento\Catalog\Test\Page\Category\CatalogCategoryView;
12 use Magento\Mtf\Client\BrowserInterface;
13 use Magento\Mtf\Constraint\AbstractConstraint;
14 
19 class AssertCategoryPage extends AbstractConstraint
20 {
26  protected $visibleCmsBlockMode = [
27  'Static block only',
28  'Static block and products'
29  ];
30 
36  protected $categoryViewPage;
37 
43  protected $browser;
44 
50  protected $category;
51 
60  public function processAssert(
61  Category $category,
62  CatalogCategoryView $categoryView,
63  BrowserInterface $browser
64  ) {
65  $this->browser = $browser;
66  $this->category = $category;
67  $this->categoryViewPage = $categoryView;
68  $this->browser->open($this->getCategoryUrl($category));
69  $categoryData = $this->prepareFixtureData($category->getData());
70  $diff = $this->verifyGeneralInformation($categoryData);
71  $diff = array_merge($diff, $this->verifyContent($categoryData));
72  $diff = array_merge($diff, $this->verifyDisplaySettings($categoryData));
73  $diff = array_merge($diff, $this->verifySearchEngineOptimization($categoryData));
74  \PHPUnit\Framework\Assert::assertEmpty(
75  $diff,
76  "Category settings on Storefront page are different.\n" . implode(' ', $diff)
77  );
78  }
79 
86  protected function prepareFixtureData(array $data)
87  {
88  if (isset($data['id'])) {
89  unset($data['id']);
90  }
91  return $data;
92  }
93 
100  protected function getCategoryUrl(Category $category)
101  {
102  $categoryUrlKey = [];
103  while ($category) {
104  $categoryUrlKey[] = $category->hasData('url_key')
105  ? strtolower($category->getUrlKey())
106  : trim(strtolower(preg_replace('#[^0-9a-z%]+#i', '-', $category->getName())), '-');
107 
108  $category = $category->getDataFieldConfig('parent_id')['source']->getParentCategory();
109  if ($category !== null && 1 == $category->getParentId()) {
110  $category = null;
111  }
112  }
113 
114  return $_ENV['app_frontend_url'] . implode('/', array_reverse($categoryUrlKey)) . '.html';
115  }
116 
125  protected function verifyGeneralInformation(array $categoryData)
126  {
127  $errorMessage = [];
128 
129  if (isset($categoryData['include_in_menu']) && $categoryData['include_in_menu'] == 'Yes') {
130  if (!$this->categoryViewPage->getTopmenu()->isCategoryVisible($categoryData['name'])) {
131  $errorMessage[] = 'Category is not visible in the navigation pane.';
132  }
133  }
134  if (isset($categoryData['include_in_menu']) && $categoryData['include_in_menu'] == 'No') {
135  if ($this->categoryViewPage->getTopmenu()->isCategoryVisible($categoryData['name'])) {
136  $errorMessage[] = 'Category is visible in the navigation pane.';
137  }
138  }
139 
140  if (isset($categoryData['name'])) {
141  $title = $this->categoryViewPage->getTitleBlock()->getTitle();
142  if ($categoryData['name'] != $title) {
143  $errorMessage[] = 'Wrong category name.'
144  . "\nExpected: " . $categoryData['name']
145  . "\nActual: " . $title;
146  }
147  }
148 
149  return $errorMessage;
150  }
151 
160  protected function verifyContent(array $categoryData)
161  {
162  $errorMessage = [];
163 
164  if (!$this->categoryViewPage->getViewBlock()->isVisible()) {
165  $errorMessage[] =
166  'Category Content is not visible.'
167  . "Skipped verifying Content settings for category {$categoryData['name']}.";
168  return $errorMessage;
169  }
170 
171  if (isset($categoryData['description'])) {
172  $description = $this->categoryViewPage->getViewBlock()->getDescription();
173  if ($categoryData['description'] != $description) {
174  $errorMessage[] = 'Wrong category description.'
175  . "\nExpected: " . $categoryData['description']
176  . "\nActual: " . $description;
177  }
178  }
179 
180  if (isset($categoryData['landing_page'])
181  && isset($categoryData['display_mode'])
182  && in_array($categoryData['display_mode'], $this->visibleCmsBlockMode)
183  ) {
185  $sourceLandingPage = $this->category->getDataFieldConfig('landing_page')['source'];
186  $fixtureContent = $sourceLandingPage->getCmsBlock()->getContent();
187  $pageContent = $this->categoryViewPage->getViewBlock()->getContent();
188 
189  if ($fixtureContent != $pageContent) {
190  $errorMessage[] = 'Wrong category landing page content.'
191  . "\nExpected: " . $fixtureContent
192  . "\nActual: " . $pageContent;
193  }
194  }
195 
196  return $errorMessage;
197  }
198 
207  protected function verifyDisplaySettings(array $categoryData)
208  {
209  $errorMessage = [];
210 
211  //TODO: verify display_mode
212 
213  if (isset($categoryData['default_sort_by'])) {
214  $expected = $categoryData['default_sort_by'];
215  $actual = $this->categoryViewPage->getTopToolbar()->getSelectSortType();
216  if ($expected != $actual) {
217  $errorMessage[] = 'Wrong sorting type.'
218  . "\nExpected: " . $expected
219  . "\nActual: " . $actual;
220  }
221  }
222 
223  if (isset($categoryData['available_sort_by'])) {
224  $availableSortType = array_filter(
225  $categoryData['available_sort_by'],
226  function (&$value) {
227  return $value !== '-' && ucfirst($value);
228  }
229  );
230  if ($availableSortType) {
231  $expected = array_values($availableSortType);
232  $actual = $this->categoryViewPage->getTopToolbar()->getSortType();
233  if ($expected != $actual) {
234  $errorMessage[] = 'Wrong available sorting type.'
235  . "\nExpected: " . implode(PHP_EOL, $expected)
236  . "\nActual: " . implode(PHP_EOL, $actual);
237  }
238  }
239  }
240 
241  // TODO: verify Layered Navigation Price Step
242 
243  return $errorMessage;
244  }
245 
254  protected function verifySearchEngineOptimization(array $categoryData)
255  {
256  $errorMessage = [];
257 
258  $categoryUrl = $this->getCategoryUrl($this->category);
259  if ($categoryUrl != $this->browser->getUrl()) {
260  $errorMessage[] = 'Wrong page URL.'
261  . "\nExpected: " . $categoryUrl
262  . "\nActual: " . $this->browser->getUrl();
263  }
264 
265  if (isset($categoryData['meta_title'])) {
266  $actual = $this->browser->getTitle();
267  if ($categoryData['meta_title'] != $actual) {
268  $errorMessage[] = 'Wrong page title.'
269  . "\nExpected: " . $categoryData['meta_title']
270  . "\nActual: " . $actual;
271  }
272  }
273 
274  return $errorMessage;
275  }
276 
282  public function toString()
283  {
284  return 'Category data on category page equals to passed from fixture.';
285  }
286 }
$title
Definition: default.phtml:14
$value
Definition: gender.phtml:16
processAssert(Category $category, CatalogCategoryView $categoryView, BrowserInterface $browser)