|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Algolia\AlgoliaSearch\Test\Integration\Category; |
| 4 | + |
| 5 | +use Algolia\AlgoliaSearch\Exceptions\AlgoliaException; |
| 6 | +use Algolia\AlgoliaSearch\Exceptions\ExceededRetriesException; |
| 7 | +use Algolia\AlgoliaSearch\Model\Indexer\Category; |
| 8 | +use Algolia\AlgoliaSearch\Test\Integration\MultiStoreTestCase; |
| 9 | +use Magento\Catalog\Api\CategoryRepositoryInterface; |
| 10 | +use Magento\Catalog\Api\Data\CategoryInterface; |
| 11 | +use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory; |
| 12 | +use Magento\Framework\Exception\CouldNotSaveException; |
| 13 | +use Magento\Framework\Exception\NoSuchEntityException; |
| 14 | + |
| 15 | +/** |
| 16 | + * @magentoDataFixture Magento/Store/_files/second_website_with_two_stores.php |
| 17 | + * @magentoDbIsolation disabled |
| 18 | + * @magentoAppIsolation enabled |
| 19 | + */ |
| 20 | +class MultiStoreCategoriesTest extends MultiStoreTestCase |
| 21 | +{ |
| 22 | + /** @var Category */ |
| 23 | + protected $categoriesIndexer; |
| 24 | + |
| 25 | + /** @var CategoryRepositoryInterface */ |
| 26 | + protected $categoryRepository; |
| 27 | + |
| 28 | + /** @var CollectionFactory */ |
| 29 | + private $categoryCollectionFactory; |
| 30 | + |
| 31 | + const BAGS_CATEGORY_ID = 4; |
| 32 | + const BAGS_CATEGORY_NAME = "Bags"; |
| 33 | + const BAGS_CATEGORY_NAME_ALT = "Bags Alt"; |
| 34 | + |
| 35 | + public function setUp():void |
| 36 | + { |
| 37 | + parent::setUp(); |
| 38 | + |
| 39 | + $this->categoriesIndexer = $this->objectManager->get(Category::class); |
| 40 | + $this->categoryRepository = $this->objectManager->get(CategoryRepositoryInterface::class); |
| 41 | + $this->categoryCollectionFactory = $this->objectManager->get(CollectionFactory::class); |
| 42 | + |
| 43 | + |
| 44 | + $this->categoriesIndexer->executeFull(); |
| 45 | + $this->algoliaHelper->waitLastTask(); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @throws CouldNotSaveException |
| 50 | + * @throws ExceededRetriesException |
| 51 | + * @throws AlgoliaException |
| 52 | + * @throws NoSuchEntityException |
| 53 | + */ |
| 54 | + public function testMultiStoreCategoryIndices() |
| 55 | + { |
| 56 | + // Check that every store has the right number of categories |
| 57 | + foreach ($this->storeManager->getStores() as $store) { |
| 58 | + $this->assertNbOfRecordsPerStore($store->getCode(), 'categories', $this->assertValues->expectedCategory); |
| 59 | + } |
| 60 | + |
| 61 | + $defaultStore = $this->storeRepository->get('default'); |
| 62 | + $fixtureSecondStore = $this->storeRepository->get('fixture_second_store'); |
| 63 | + |
| 64 | + $bagsCategory = $this->loadCategory(self::BAGS_CATEGORY_ID, $defaultStore->getId()); |
| 65 | + |
| 66 | + $this->assertEquals(self::BAGS_CATEGORY_NAME, $bagsCategory->getName()); |
| 67 | + |
| 68 | + // Change a category name at store level |
| 69 | + $bagsCategoryAlt = $this->updateCategory( |
| 70 | + self::BAGS_CATEGORY_ID, |
| 71 | + $fixtureSecondStore->getId(), |
| 72 | + ['name' => self::BAGS_CATEGORY_NAME_ALT] |
| 73 | + ); |
| 74 | + |
| 75 | + $this->assertEquals(self::BAGS_CATEGORY_NAME, $bagsCategory->getName()); |
| 76 | + $this->assertEquals(self::BAGS_CATEGORY_NAME_ALT, $bagsCategoryAlt->getName()); |
| 77 | + |
| 78 | + $this->categoriesIndexer->execute([self::BAGS_CATEGORY_ID]); |
| 79 | + $this->algoliaHelper->waitLastTask(); |
| 80 | + |
| 81 | + $this->assertAlgoliaRecordValues( |
| 82 | + $this->indexPrefix . 'default_categories', |
| 83 | + (string) self::BAGS_CATEGORY_ID, |
| 84 | + ['name' => self::BAGS_CATEGORY_NAME] |
| 85 | + ); |
| 86 | + |
| 87 | + $this->assertAlgoliaRecordValues( |
| 88 | + $this->indexPrefix . 'fixture_second_store_categories', |
| 89 | + (string) self::BAGS_CATEGORY_ID, |
| 90 | + ['name' => self::BAGS_CATEGORY_NAME_ALT] |
| 91 | + ); |
| 92 | + |
| 93 | + // Disable this category at store level |
| 94 | + $bagsCategoryAlt = $this->updateCategory( |
| 95 | + self::BAGS_CATEGORY_ID, |
| 96 | + $fixtureSecondStore->getId(), |
| 97 | + ['is_active' => 0] |
| 98 | + ); |
| 99 | + |
| 100 | + $this->categoriesIndexer->execute([self::BAGS_CATEGORY_ID]); |
| 101 | + $this->algoliaHelper->waitLastTask(); |
| 102 | + |
| 103 | + $this->assertNbOfRecordsPerStore( |
| 104 | + $defaultStore->getCode(), |
| 105 | + 'categories', |
| 106 | + $this->assertValues->expectedCategory |
| 107 | + ); |
| 108 | + |
| 109 | + $this->assertNbOfRecordsPerStore( |
| 110 | + $fixtureSecondStore->getCode(), |
| 111 | + 'categories', |
| 112 | + $this->assertValues->expectedCategory - 1 |
| 113 | + ); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Loads category by name. |
| 118 | + * |
| 119 | + * @param int $categoryId |
| 120 | + * @param int $storeId |
| 121 | + * |
| 122 | + * @return CategoryInterface |
| 123 | + * @throws NoSuchEntityException |
| 124 | + */ |
| 125 | + private function loadCategory(int $categoryId, int $storeId): CategoryInterface |
| 126 | + { |
| 127 | + return $this->categoryRepository->get($categoryId, $storeId); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * @param int $categoryId |
| 132 | + * @param int $storeId |
| 133 | + * @param array $values |
| 134 | + * |
| 135 | + * @return CategoryInterface |
| 136 | + * @throws CouldNotSaveException |
| 137 | + * @throws NoSuchEntityException |
| 138 | + * |
| 139 | + * @see Magento\Catalog\Block\Product\ListProduct\SortingTest |
| 140 | + */ |
| 141 | + private function updateCategory(int $categoryId, int $storeId, array $values): CategoryInterface |
| 142 | + { |
| 143 | + $oldStoreId = $this->storeManager->getStore()->getId(); |
| 144 | + $this->storeManager->setCurrentStore($storeId); |
| 145 | + $category = $this->loadCategory($categoryId, $storeId); |
| 146 | + foreach ($values as $attribute => $value) { |
| 147 | + $category->setData($attribute, $value); |
| 148 | + } |
| 149 | + $categoryAlt = $this->categoryRepository->save($category); |
| 150 | + $this->storeManager->setCurrentStore($oldStoreId); |
| 151 | + |
| 152 | + return $categoryAlt; |
| 153 | + } |
| 154 | + |
| 155 | + public function tearDown(): void |
| 156 | + { |
| 157 | + $defaultStore = $this->storeRepository->get('default'); |
| 158 | + |
| 159 | + // Restore category name in case DB is not cleaned up |
| 160 | + $this->updateCategory( |
| 161 | + self::BAGS_CATEGORY_ID, |
| 162 | + $defaultStore->getId(), |
| 163 | + [ |
| 164 | + 'name' => self::BAGS_CATEGORY_NAME, |
| 165 | + 'is_active' => 1 |
| 166 | + ] |
| 167 | + ); |
| 168 | + |
| 169 | + parent::tearDown(); |
| 170 | + } |
| 171 | +} |
0 commit comments