Skip to content

Commit 6564ef3

Browse files
authored
MAGE-1056: Add MultiStore category tests (#1623)
* MAGE-1056: add category tests * MAGE-1056: improved updateCategory() * MAGE-1056: improved assertAlgoliaRecordValues() * MAGE-1056: check category disabled * MAGE-1056: add category config tests * MAGE-1056: reorganize the folder structure * MAGE-1056: update products tests * MAGE-1056: fix products tests * MAGE-1056: update tearDown
1 parent 531e867 commit 6564ef3

13 files changed

+391
-142
lines changed

Test/Integration/AssertValues/Magento247.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
class Magento247
66
{
7-
public $productsOnStockCount = 182;
7+
public $productsOnStockCount = 180;
88
public $productsOutOfStockCount = 183;
99
public $lastJobDataSize = 13;
1010
public $expectedCategory = 17;

Test/Integration/CategoriesIndexingTest.php renamed to Test/Integration/Category/CategoriesIndexingTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<?php
22

3-
namespace Algolia\AlgoliaSearch\Test\Integration;
3+
namespace Algolia\AlgoliaSearch\Test\Integration\Category;
44

55
use Algolia\AlgoliaSearch\Model\Indexer\Category;
6+
use Algolia\AlgoliaSearch\Test\Integration\IndexingTestCase;
67

78
class CategoriesIndexingTest extends IndexingTestCase
89
{
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
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+
}

Test/Integration/ConfigTest.php renamed to Test/Integration/Config/ConfigTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
<?php
22

3-
namespace Algolia\AlgoliaSearch\Test\Integration;
3+
namespace Algolia\AlgoliaSearch\Test\Integration\Config;
44

55
use Algolia\AlgoliaSearch\Exceptions\AlgoliaException;
66
use Algolia\AlgoliaSearch\Model\IndicesConfigurator;
7+
use Algolia\AlgoliaSearch\Test\Integration\TestCase;
78

89
class ConfigTest extends TestCase
910
{
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Test\Integration\Config;
4+
5+
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
6+
use Algolia\AlgoliaSearch\Test\Integration\MultiStoreTestCase;
7+
8+
/**
9+
* @magentoDataFixture Magento/Store/_files/second_website_with_two_stores.php
10+
*/
11+
class MultiStoreConfigTest extends MultiStoreTestCase
12+
{
13+
14+
const ADDITIONAL_ATTRIBUTE = 'additional_attribute';
15+
16+
public function testMultiStoreIndicesCreation()
17+
{
18+
$websites = $this->storeManager->getWebsites();
19+
$stores = $this->storeManager->getStores();
20+
21+
// Check that stores and websites are properly created
22+
$this->assertEquals(count($websites), 2);
23+
$this->assertEquals(count($stores), 3);
24+
25+
foreach ($stores as $store) {
26+
$this->setupStore($store);
27+
}
28+
29+
$indicesCreatedByTest = 0;
30+
$indices = $this->algoliaHelper->listIndexes();
31+
32+
foreach ($indices['items'] as $index) {
33+
$name = $index['name'];
34+
35+
if (mb_strpos($name, $this->indexPrefix) === 0) {
36+
$indicesCreatedByTest++;
37+
}
38+
}
39+
40+
// Check that the configuration created the appropriate number of indices (4 per store => 3*4=12)
41+
$this->assertEquals($indicesCreatedByTest, 12);
42+
43+
44+
$defaultStore = $this->storeRepository->get('default');
45+
$fixtureSecondStore = $this->storeRepository->get('fixture_second_store');
46+
47+
// Change category configuration at store level (attributes and ranking)
48+
$attributesFromConfig = $this->configHelper->getCategoryAdditionalAttributes($defaultStore->getId());
49+
$attributesFromConfigAlt = $attributesFromConfig;
50+
$attributesFromConfigAlt[] = [
51+
"attribute" => self::ADDITIONAL_ATTRIBUTE,
52+
"searchable" => "1",
53+
"order" => "unordered",
54+
"retrievable" => "1",
55+
];
56+
57+
$this->setConfig(
58+
ConfigHelper::CATEGORY_ATTRIBUTES,
59+
json_encode($attributesFromConfigAlt),
60+
$fixtureSecondStore->getCode())
61+
;
62+
63+
$rankingsFromConfig = $this->configHelper->getCategoryCustomRanking($defaultStore->getId());
64+
$rankingsFromConfigAlt = $rankingsFromConfig;
65+
$rankingsFromConfigAlt[] = [
66+
"attribute" => self::ADDITIONAL_ATTRIBUTE,
67+
"order" => "desc",
68+
];
69+
70+
$this->setConfig(
71+
ConfigHelper::CATEGORY_CUSTOM_RANKING,
72+
json_encode($rankingsFromConfigAlt),
73+
$fixtureSecondStore->getCode())
74+
;
75+
76+
$this->indicesConfigurator->saveConfigurationToAlgolia($fixtureSecondStore->getId());
77+
78+
$defaultIndexSettings = $this->algoliaHelper->getSettings($this->indexPrefix . 'default_categories');
79+
$fixtureIndexSettings = $this->algoliaHelper->getSettings($this->indexPrefix . 'fixture_second_store_categories');
80+
81+
$attributeFromConfig = 'unordered(' . self::ADDITIONAL_ATTRIBUTE . ')';
82+
$this->assertNotContains($attributeFromConfig, $defaultIndexSettings['searchableAttributes']);
83+
$this->assertContains($attributeFromConfig, $fixtureIndexSettings['searchableAttributes']);
84+
85+
$rankingFromConfig = 'desc(' . self::ADDITIONAL_ATTRIBUTE . ')';
86+
$this->assertNotContains($rankingFromConfig, $defaultIndexSettings['customRanking']);
87+
$this->assertContains($rankingFromConfig, $fixtureIndexSettings['customRanking']);
88+
}
89+
}

Test/Integration/MultiStoreCategoriesTest.php

Lines changed: 0 additions & 34 deletions
This file was deleted.

Test/Integration/MultiStoreConfigTest.php

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)