|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\PageBuilder\Api; |
| 10 | + |
| 11 | +use Magento\Catalog\Model\ResourceModel\Category\Collection; |
| 12 | +use Magento\Cms\Api\Data\PageInterface; |
| 13 | +use Magento\Cms\Model\PageRepository; |
| 14 | +use Magento\Framework\Api\SearchCriteriaBuilder; |
| 15 | +use Magento\Framework\Exception\CouldNotSaveException; |
| 16 | +use Magento\Framework\ObjectManagerInterface; |
| 17 | +use Magento\TestFramework\Helper\Bootstrap; |
| 18 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 19 | + |
| 20 | +class CMSContentProductListingTest extends GraphQlAbstract |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var ObjectManagerInterface |
| 24 | + */ |
| 25 | + private ObjectManagerInterface $objectManager; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var SearchCriteriaBuilder |
| 29 | + */ |
| 30 | + private SearchCriteriaBuilder $searchCriteriaBuilder; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var PageRepository |
| 34 | + */ |
| 35 | + private PageRepository $pageRepository; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var Collection |
| 39 | + */ |
| 40 | + private Collection $categoryCollection; |
| 41 | + |
| 42 | + /** |
| 43 | + * @inheritDoc |
| 44 | + */ |
| 45 | + protected function setUp(): void |
| 46 | + { |
| 47 | + $this->objectManager = $objectManager = Bootstrap::getObjectManager(); |
| 48 | + $this->searchCriteriaBuilder = $objectManager->get(SearchCriteriaBuilder::class); |
| 49 | + $this->pageRepository = $objectManager->get(PageRepository::class); |
| 50 | + $this->categoryCollection = $this->objectManager->get(Collection::class); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Check if product listing is sorted by position |
| 55 | + * |
| 56 | + * @magentoDataFixture Magento/Cms/Fixtures/page_list.php |
| 57 | + * @magentoApiDataFixture Magento/Catalog/_files/category_with_three_products.php |
| 58 | + * @return void |
| 59 | + * @throws CouldNotSaveException |
| 60 | + */ |
| 61 | + public function testCMSContentProductListingSortOrder(): void |
| 62 | + { |
| 63 | + $category = $this->categoryCollection->addFieldToFilter( |
| 64 | + 'name', |
| 65 | + 'Category 999' |
| 66 | + )->getFirstItem(); |
| 67 | + $categoryId = $category->getId(); |
| 68 | + $content = '<style>#html-body [data-pb-style=E4B30DS]{justify-content:flex-start;display:flex;' . |
| 69 | + 'flex-direction:column;background-position:left top;background-size:cover;background-repeat:no-repeat;' . |
| 70 | + 'background-attachment:scroll}</style><div data-content-type="row" data-appearance="contained" ' . |
| 71 | + 'data-element="main"><div data-enable-parallax="0" data-parallax-speed="0.5" data-background-images="{}" ' . |
| 72 | + 'data-background-type="image" data-video-loop="true" data-video-play-only-visible="true" ' . |
| 73 | + 'data-video-lazy-load="true" data-video-fallback-src="" data-element="inner" ' . |
| 74 | + 'data-pb-style="E4B30DS"><div data-content-type="products" data-appearance="grid" ' . |
| 75 | + 'data-element="main">{{widget type="Magento\CatalogWidget\Block\Product\ProductsList" ' . |
| 76 | + 'template="Magento_CatalogWidget::product/widget/content/grid.phtml" anchor_text="" id_path="" ' . |
| 77 | + 'show_pager="0" products_count="5" condition_option="category_ids" condition_option_value="' . $categoryId |
| 78 | + .'"type_name="Catalog Products List" conditions_encoded="^[`1`:^[`aggregator`:`all`,`new_child`:``,' . |
| 79 | + '`type`:`Magento||CatalogWidget||Model||Rule||Condition||Combine`,`value`:`1`^],`1--1`:^[`operator`:`==`,' . |
| 80 | + '`type`:`Magento||CatalogWidget||Model||Rule||Condition||Product`,`attribute`:`category_ids`,' . |
| 81 | + '`value`:`' . $categoryId . '`^]^]" sort_order="position"}}</div></div></div>'; |
| 82 | + $page = $this->getPageByTitle('Page with 1column layout'); |
| 83 | + $page->setContent($content); |
| 84 | + $this->pageRepository->save($page); |
| 85 | + |
| 86 | + $productPositions = $category->getProductsPosition(); |
| 87 | + $products = array_keys($productPositions); |
| 88 | + $index = 2; |
| 89 | + foreach ($products as $product) { |
| 90 | + $productPositions[$product] = $index; |
| 91 | + $index--; |
| 92 | + } |
| 93 | + |
| 94 | + $category->setPostedProducts($productPositions); |
| 95 | + $category->save(); |
| 96 | + |
| 97 | + $query = $this->getQuery($page->getIdentifier(), ['title', 'content']); |
| 98 | + $response = $this->graphQlQueryWithResponseHeaders($query); |
| 99 | + $position1 = strpos($response['body']['cmsPage']['content'], '/simple-product2.html'); |
| 100 | + $position2 = strpos($response['body']['cmsPage']['content'], '/simple-product-with-price-20.html'); |
| 101 | + $position3 = strpos($response['body']['cmsPage']['content'], '/simple-product-with-price-10.html'); |
| 102 | + $this->assertTrue($position1 < $position2 && $position2 < $position3); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Retrieve a page by its title |
| 107 | + * |
| 108 | + * @param string $title |
| 109 | + * @return PageInterface |
| 110 | + */ |
| 111 | + private function getPageByTitle(string $title): PageInterface |
| 112 | + { |
| 113 | + $searchCriteria = $this->searchCriteriaBuilder |
| 114 | + ->addFilter('title', $title) |
| 115 | + ->create(); |
| 116 | + |
| 117 | + $pages = $this->pageRepository->getList($searchCriteria)->getItems(); |
| 118 | + |
| 119 | + /** @var PageInterface $page */ |
| 120 | + $page = reset($pages); |
| 121 | + |
| 122 | + return $page; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Create GraphQL CMS page query |
| 127 | + * |
| 128 | + * @param string $identifier |
| 129 | + * @param array $fields |
| 130 | + * @return string |
| 131 | + */ |
| 132 | + private function getQuery(string $identifier, array $fields = ['title']): string |
| 133 | + { |
| 134 | + $fields = implode(PHP_EOL, $fields); |
| 135 | + |
| 136 | + return <<<QUERY |
| 137 | +{ |
| 138 | + cmsPage(identifier: "$identifier") { |
| 139 | + $fields |
| 140 | + } |
| 141 | +} |
| 142 | +QUERY; |
| 143 | + } |
| 144 | +} |
0 commit comments