|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\Catalog\Test\Constraint; |
| 8 | + |
| 9 | +use Magento\Catalog\Test\Fixture\Category; |
| 10 | +use Magento\Catalog\Test\Page\Adminhtml\CatalogCategoryEdit; |
| 11 | +use Magento\Catalog\Test\Page\Adminhtml\CatalogCategoryIndex; |
| 12 | +use Magento\Mtf\Constraint\AbstractConstraint; |
| 13 | + |
| 14 | +/** |
| 15 | + * Assert that category products grid filter works correctly. |
| 16 | + */ |
| 17 | +class AssertCategoryProductsGridFilter extends AbstractConstraint |
| 18 | +{ |
| 19 | + /** |
| 20 | + * Grid columns for tests |
| 21 | + * |
| 22 | + * @var array |
| 23 | + */ |
| 24 | + private $testFilterColumns = [ |
| 25 | + 'visibility', |
| 26 | + ]; |
| 27 | + |
| 28 | + /** |
| 29 | + * Assert that category products grid filter works correctly. |
| 30 | + * |
| 31 | + * @param CatalogCategoryIndex $catalogCategoryIndex |
| 32 | + * @param CatalogCategoryEdit $catalogCategoryEdit |
| 33 | + * @param Category $category |
| 34 | + * @return void |
| 35 | + */ |
| 36 | + public function processAssert( |
| 37 | + CatalogCategoryIndex $catalogCategoryIndex, |
| 38 | + CatalogCategoryEdit $catalogCategoryEdit, |
| 39 | + Category $category |
| 40 | + ) { |
| 41 | + $catalogCategoryIndex->getTreeCategories()->selectCategory($category, true); |
| 42 | + $categoryProducts = $category->getDataFieldConfig('category_products')['source']->getProducts(); |
| 43 | + $catalogCategoryEdit->getEditForm()->openSection('category_products'); |
| 44 | + |
| 45 | + foreach ($this->testFilterColumns as $field) { |
| 46 | + $this->testGridFilter($categoryProducts, $catalogCategoryEdit, $field); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * @param array $categoryProducts |
| 52 | + * @param CatalogCategoryEdit $catalogCategoryEdit |
| 53 | + * @param string $filterField |
| 54 | + */ |
| 55 | + private function testGridFilter(array $categoryProducts, CatalogCategoryEdit $catalogCategoryEdit, $filterField) |
| 56 | + { |
| 57 | + $productsByFilter = []; |
| 58 | + foreach ($categoryProducts as $product) { |
| 59 | + $filterValue = $product->getData($filterField); |
| 60 | + if (!isset($productsByFilter[$filterValue])) { |
| 61 | + $productsByFilter[$filterValue] = []; |
| 62 | + } |
| 63 | + $productsByFilter[$filterValue][] = $product; |
| 64 | + } |
| 65 | + |
| 66 | + $productsFieldset = $catalogCategoryEdit->getEditForm()->getSection('category_products'); |
| 67 | + foreach ($productsByFilter as $filterValue => $products) { |
| 68 | + $productsFieldset->getProductGrid()->search([ |
| 69 | + 'in_category' => 'Yes', |
| 70 | + $filterField => $filterValue, |
| 71 | + ]); |
| 72 | + |
| 73 | + $expectedRows = []; |
| 74 | + foreach ($products as $product) { |
| 75 | + $expectedRows[] = $product->getName(); |
| 76 | + } |
| 77 | + $gridRows = $productsFieldset->getProductGrid()->getRowsData(['name']); |
| 78 | + $actualRows = array_column($gridRows, 'name'); |
| 79 | + sort($expectedRows); |
| 80 | + sort($actualRows); |
| 81 | + |
| 82 | + \PHPUnit_Framework_Assert::assertEquals( |
| 83 | + $expectedRows, |
| 84 | + $actualRows, |
| 85 | + "Category products grid filter '$filterField' does not work correctly" |
| 86 | + ); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Returns a string representation of the object. |
| 92 | + * |
| 93 | + * @return string |
| 94 | + */ |
| 95 | + public function toString() |
| 96 | + { |
| 97 | + return 'Category products grid filter works correctly'; |
| 98 | + } |
| 99 | +} |
0 commit comments