|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Algolia\AlgoliaSearch\Test\Integration\Page; |
| 4 | + |
| 5 | +use Algolia\AlgoliaSearch\Exceptions\AlgoliaException; |
| 6 | +use Algolia\AlgoliaSearch\Exceptions\ExceededRetriesException; |
| 7 | +use Algolia\AlgoliaSearch\Helper\ConfigHelper; |
| 8 | +use Algolia\AlgoliaSearch\Test\Integration\MultiStoreTestCase; |
| 9 | +use Algolia\AlgoliaSearch\Model\Indexer\Page; |
| 10 | +use Magento\Cms\Api\PageRepositoryInterface; |
| 11 | +use Magento\Cms\Api\Data\PageInterface; |
| 12 | +use Magento\Cms\Model\ResourceModel\Page\CollectionFactory; |
| 13 | +use Magento\Framework\Exception\CouldNotSaveException; |
| 14 | +use Magento\Framework\Exception\LocalizedException; |
| 15 | +use Magento\Framework\Exception\NoSuchEntityException; |
| 16 | +use Magento\Store\Api\Data\StoreInterface; |
| 17 | + |
| 18 | +/** |
| 19 | + * @magentoDataFixture Magento/Store/_files/second_website_with_two_stores.php |
| 20 | + * @magentoDbIsolation disabled |
| 21 | + * @magentoAppIsolation enabled |
| 22 | + */ |
| 23 | +class MultiStorePagesTest extends MultiStoreTestCase |
| 24 | +{ |
| 25 | + /** @var Page */ |
| 26 | + protected $pagesIndexer; |
| 27 | + |
| 28 | + /** @var PageRepositoryInterface */ |
| 29 | + protected $pageRepository; |
| 30 | + |
| 31 | + /** @var CollectionFactory */ |
| 32 | + private $pageCollectionFactory; |
| 33 | + |
| 34 | + const ABOUT_US_PAGE_ID = 7; |
| 35 | + |
| 36 | + public function setUp():void |
| 37 | + { |
| 38 | + parent::setUp(); |
| 39 | + |
| 40 | + $this->pagesIndexer = $this->objectManager->get(Page::class); |
| 41 | + $this->pageRepository = $this->objectManager->get(PageRepositoryInterface::class); |
| 42 | + $this->pageCollectionFactory = $this->objectManager->get(CollectionFactory::class); |
| 43 | + |
| 44 | + $this->pagesIndexer->executeFull(); |
| 45 | + $this->algoliaHelper->waitLastTask(); |
| 46 | + } |
| 47 | + |
| 48 | + /*** |
| 49 | + * @magentoDbIsolation disabled |
| 50 | + * |
| 51 | + * @throws ExceededRetriesException |
| 52 | + * @throws AlgoliaException |
| 53 | + */ |
| 54 | + public function testMultiStorePageIndices() |
| 55 | + { |
| 56 | + // Check that every store has the right number of pages |
| 57 | + foreach ($this->storeManager->getStores() as $store) { |
| 58 | + $this->assertNbOfRecordsPerStore( |
| 59 | + $store->getCode(), |
| 60 | + 'pages', |
| 61 | + $store->getCode() === 'fixture_second_store' ? // we excluded 2 pages on setupStore() |
| 62 | + $this->assertValues->expectedExcludePages : |
| 63 | + $this->assertValues->expectedPages |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + $defaultStore = $this->storeRepository->get('default'); |
| 68 | + $fixtureSecondStore = $this->storeRepository->get('fixture_second_store'); |
| 69 | + |
| 70 | + try { |
| 71 | + $aboutUsPage = $this->loadPage(self::ABOUT_US_PAGE_ID); |
| 72 | + } catch (\Exception $e) { |
| 73 | + $this->markTestIncomplete('Page could not be found.'); |
| 74 | + } |
| 75 | + |
| 76 | + // Setting the page only for default store |
| 77 | + $aboutUsPage->setStores([$defaultStore->getId()]); |
| 78 | + $this->pageRepository->save($aboutUsPage); |
| 79 | + |
| 80 | + $this->pagesIndexer->execute([self::ABOUT_US_PAGE_ID]); |
| 81 | + $this->algoliaHelper->waitLastTask(); |
| 82 | + |
| 83 | + $this->assertNbOfRecordsPerStore( |
| 84 | + $defaultStore->getCode(), |
| 85 | + 'pages', |
| 86 | + $this->assertValues->expectedPages |
| 87 | + ); |
| 88 | + |
| 89 | + $this->assertNbOfRecordsPerStore( |
| 90 | + $fixtureSecondStore->getCode(), |
| 91 | + 'pages', |
| 92 | + $this->assertValues->expectedExcludePages - 1 |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Loads page by id. |
| 98 | + * |
| 99 | + * @param int $pageId |
| 100 | + * |
| 101 | + * @return PageInterface |
| 102 | + * @throws LocalizedException |
| 103 | + */ |
| 104 | + private function loadPage(int $pageId): PageInterface |
| 105 | + { |
| 106 | + return $this->pageRepository->getById($pageId); |
| 107 | + } |
| 108 | + |
| 109 | + protected function resetPage(PageInterface $page): void |
| 110 | + { |
| 111 | + $page->setStores([0]); |
| 112 | + $this->pageRepository->save($page); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * @param StoreInterface $store |
| 117 | + * @param bool $enableInstantSearch |
| 118 | + * |
| 119 | + * @return void |
| 120 | + * @throws AlgoliaException |
| 121 | + * @throws LocalizedException |
| 122 | + * @throws NoSuchEntityException |
| 123 | + */ |
| 124 | + protected function setupStore(StoreInterface $store, bool $enableInstantSearch = false): void |
| 125 | + { |
| 126 | + // Exclude 2 pages on second store |
| 127 | + $excludedPages = $store->getCode() === 'fixture_second_store' ? |
| 128 | + [['attribute' => 'no-route'], ['attribute' => 'home']]: |
| 129 | + []; |
| 130 | + |
| 131 | + $this->setConfig( |
| 132 | + ConfigHelper::EXCLUDED_PAGES, |
| 133 | + $this->getSerializer()->serialize($excludedPages), |
| 134 | + $store->getCode() |
| 135 | + ); |
| 136 | + |
| 137 | + parent::setupStore($store, $enableInstantSearch); |
| 138 | + } |
| 139 | + |
| 140 | + public function tearDown(): void |
| 141 | + { |
| 142 | + // Restore page in case DB is not cleaned up |
| 143 | + $aboutUsPage = $this->loadPage(self::ABOUT_US_PAGE_ID); |
| 144 | + $this->resetPage($aboutUsPage); |
| 145 | + |
| 146 | + parent::tearDown(); |
| 147 | + } |
| 148 | +} |
0 commit comments