Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAGE-1084: Multistore Replicas tests #1663

Open
wants to merge 4 commits into
base: release/3.15.0-dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
230 changes: 230 additions & 0 deletions Test/Integration/Product/MultiStoreReplicaTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
<?php

namespace Algolia\AlgoliaSearch\Test\Integration\Product;

use Algolia\AlgoliaSearch\Api\Product\ReplicaManagerInterface;
use Algolia\AlgoliaSearch\Console\Command\ReplicaRebuildCommand;
use Algolia\AlgoliaSearch\Console\Command\ReplicaSyncCommand;
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
use Algolia\AlgoliaSearch\Service\Product\SortingTransformer;
use Algolia\AlgoliaSearch\Test\Integration\MultiStoreTestCase;
use Algolia\AlgoliaSearch\Test\Integration\Product\Traits\ReplicaAssertionsTrait;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\Store\Api\Data\StoreInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* @magentoDataFixture Magento/Store/_files/second_website_with_two_stores.php
*/
class MultiStoreReplicaTest extends MultiStoreTestCase
{
use ReplicaAssertionsTrait;

const COLOR_ATTR = 'color';
const CREATED_AT_ATTR = 'created_at';
const ASC_DIR = 'asc';
const DESC_DIR = 'desc';

protected ?ReplicaManagerInterface $replicaManager = null;

protected ?SerializerInterface $serializer = null;

protected function setUp(): void
{
parent::setUp();
$this->replicaManager = $this->objectManager->get(ReplicaManagerInterface::class);
$this->serializer = $this->objectManager->get(SerializerInterface::class);

$stores = $this->storeManager->getStores();

foreach ($stores as $store) {
$this->setupStore($store, true);
}
}

public function testMultiStoreReplicaConfig()
{
$defaultStore = $this->storeRepository->get('default');
$fixtureSecondStore = $this->storeRepository->get('fixture_second_store');

// Check replica config for created_at desc
$this->checkReplicaIsStandard($defaultStore, self::CREATED_AT_ATTR, self::DESC_DIR);
$this->checkReplicaIsStandard($fixtureSecondStore, self::CREATED_AT_ATTR, self::DESC_DIR);

// add color asc sorting (virtual on a single store)
$this->addSortingByStore($defaultStore, self::COLOR_ATTR, self::ASC_DIR);
$this->addSortingByStore($fixtureSecondStore, self::COLOR_ATTR, self::ASC_DIR,true);

// Check replica config for color asc
$this->checkReplicaIsStandard($defaultStore, self::COLOR_ATTR, self::ASC_DIR);
$this->checkReplicaIsVirtual($fixtureSecondStore, self::COLOR_ATTR, self::ASC_DIR);

$this->resetAllSortings();
}

public function testReplicaCommands()
{
$defaultStore = $this->storeRepository->get('default');
$fixtureSecondStore = $this->storeRepository->get('fixture_second_store');

$defaultIndexName = $this->indexPrefix . $defaultStore->getCode() . '_products';
$fixtureIndexName = $this->indexPrefix . $fixtureSecondStore->getCode() . '_products';

// Update store config for fixture only
$this->mockSortUpdate('price', 'desc', ['virtualReplica' => 1], $fixtureSecondStore);

$defaultSortings = $this->objectManager->get(SortingTransformer::class)->getSortingIndices(
$defaultStore->getId(),
null,
null,
true
);

$fixtureSortings = $this->objectManager->get(SortingTransformer::class)->getSortingIndices(
$fixtureSecondStore->getId(),
null,
null,
true
);

// Executing commands - Start
$syncCmd = $this->objectManager->get(ReplicaSyncCommand::class);
$this->mockProperty($syncCmd, 'output', OutputInterface::class);
$syncCmd->syncReplicas();
$this->algoliaHelper->waitLastTask($defaultStore->getId());
$this->algoliaHelper->waitLastTask($fixtureSecondStore->getId());

$rebuildCmd = $this->objectManager->get(ReplicaRebuildCommand::class);
$this->invokeMethod(
$rebuildCmd,
'execute',
[
$this->createMock(InputInterface::class),
$this->createMock(OutputInterface::class)
]
);
$this->algoliaHelper->waitLastTask($defaultStore->getId());
$this->algoliaHelper->waitLastTask($fixtureSecondStore->getId());
// Executing commands - End

$currentDefaultSettings = $this->algoliaHelper->getSettings($defaultIndexName, $defaultStore->getId());
$currentFixtureSettings = $this->algoliaHelper->getSettings($fixtureIndexName, $fixtureSecondStore->getId());

$this->assertArrayHasKey('replicas', $currentDefaultSettings);
$this->assertArrayHasKey('replicas', $currentFixtureSettings);

$defaultReplicas = $currentDefaultSettings['replicas'];
$fixtureReplicas = $currentFixtureSettings['replicas'];

$this->assertEquals(count($defaultSortings), count($defaultReplicas));
$this->assertEquals(count($fixtureSortings), count($fixtureReplicas));

$this->assertSortToReplicaConfigParity(
$defaultIndexName,
$defaultSortings,
$defaultReplicas,
$defaultStore->getId()
);

$this->assertSortToReplicaConfigParity(
$fixtureIndexName,
$fixtureSortings,
$fixtureReplicas,
$fixtureSecondStore->getId()
);
}

protected function checkReplicaIsStandard(StoreInterface $store, $sortAttr, $sortDir)
{
$this->checkReplicaConfigByStore($store, $sortAttr, $sortDir, 'standard');
}

protected function checkReplicaIsVirtual(StoreInterface $store, $sortAttr, $sortDir)
{
$this->checkReplicaConfigByStore($store, $sortAttr, $sortDir, 'virtual');
}

protected function checkReplicaConfigByStore(StoreInterface $store, $sortAttr, $sortDir, $type)
{
$indexName = $this->indexPrefix . $store->getCode() . '_products';

$settings = $this->algoliaHelper->getSettings($indexName, $store->getId());

$this->assertArrayHasKey('replicas', $settings);

$replicaIndexName = $indexName . '_' . $sortAttr . '_' . $sortDir;

$type === 'virtual' ?
$this->assertTrue($this->isVirtualReplica($settings['replicas'], $replicaIndexName)) :
$this->assertTrue($this->isStandardReplica($settings['replicas'], $replicaIndexName));

$replicaSettings = $this->assertReplicaIndexExists($indexName, $replicaIndexName, $store->getId());

$type === 'virtual' ?
$this->assertVirtualReplicaRanking($replicaSettings, "$sortDir($sortAttr)"):
$this->assertStandardReplicaRanking($replicaSettings, "$sortDir($sortAttr)");
}

protected function addSortingByStore(StoreInterface $store, $attr, $dir, $isVirtual = false)
{
$sorting = $this->configHelper->getSorting($store->getId());
$newSorting = [
'attribute' => $attr,
'sort' => $dir,
'sortLabel' => $attr,
];

if ($isVirtual) {
$newSorting['virtualReplica'] = 1;
}

$sorting[] = $newSorting;

$this->setConfig(
ConfigHelper::SORTING_INDICES,
$this->serializer->serialize($sorting),
$store->getCode()
);

$this->assertSortingAttribute($attr, $dir);
$this->indicesConfigurator->saveConfigurationToAlgolia($store->getId());
$this->algoliaHelper->waitLastTask($store->getId());
}

protected function resetAllSortings()
{
$stores = $this->storeManager->getStores();

foreach ($stores as $store) {
$this->setConfig(
ConfigHelper::SORTING_INDICES,
[
[
'attribute' => 'price',
'sort' => 'asc',
'sortLabel' => 'Lowest Price'
],
[
'attribute' => 'price',
'sort' => 'desc',
'sortLabel' => 'Highest Price'
],
[
'attribute' => 'created_at',
'sort' => 'desc',
'sortLabel' => 'Newest first'
]
],
$store->getCode()
);
}
}

protected function tearDown(): void
{
$this->resetAllSortings();

parent::tearDown();
}
}
Loading