|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rocketweb\CmsImportExport\Test\Integration; |
| 6 | + |
| 7 | +use Magento\Cms\Api\BlockRepositoryInterface; |
| 8 | +use Magento\Cms\Api\PageRepositoryInterface; |
| 9 | +use Magento\Cms\Model\Block; |
| 10 | +use Magento\Cms\Model\BlockFactory; |
| 11 | +use Magento\Cms\Model\Page; |
| 12 | +use Magento\Cms\Model\PageFactory; |
| 13 | +use Magento\Framework\Exception\FileSystemException; |
| 14 | +use Magento\Store\Api\StoreRepositoryInterface; |
| 15 | +use Magento\Store\Model\StoreManagerInterface; |
| 16 | +use Magento\TestFramework\App\Filesystem; |
| 17 | +use Magento\Framework\App\Filesystem\DirectoryList; |
| 18 | +use Magento\Framework\Filesystem\Directory\WriteInterface; |
| 19 | +use Magento\TestFramework\Helper\Bootstrap; |
| 20 | +use PHPUnit\Framework\TestCase; |
| 21 | +use RocketWeb\CmsImportExport\Model\Service\ImportCmsDataService; |
| 22 | + |
| 23 | +/** |
| 24 | + * @magentoAppIsolation disabled |
| 25 | + * @magentoDbIsolation disabled |
| 26 | + */ |
| 27 | +class ImportByStoreScopeEntitiesTest extends TestCase |
| 28 | +{ |
| 29 | + protected ?ImportCmsDataService $importer; |
| 30 | + protected ?string $exportDirPath; |
| 31 | + protected ?BlockRepositoryInterface $blockRepository; |
| 32 | + protected ?PageRepositoryInterface $pageRepository; |
| 33 | + protected ?StoreRepositoryInterface $storeRepository; |
| 34 | + protected ?WriteInterface $varDirectory; |
| 35 | + protected ?BlockFactory $blockFactory; |
| 36 | + protected ?PageFactory $pageFactory; |
| 37 | + protected ?StoreManagerInterface $storeManager; |
| 38 | + /** |
| 39 | + * @return void |
| 40 | + * @throws FileSystemException |
| 41 | + */ |
| 42 | + protected function setUp(): void |
| 43 | + { |
| 44 | + $objectManager = Bootstrap::getObjectManager(); |
| 45 | + $fileSystem = $objectManager->create(Filesystem::class); |
| 46 | + $this->importer = $objectManager->create(ImportCmsDataService::class); |
| 47 | + $this->varDirectory = $fileSystem->getDirectoryWrite(DirectoryList::VAR_DIR); |
| 48 | + $this->blockRepository = $objectManager->create(BlockRepositoryInterface::class); |
| 49 | + $this->storeRepository = $objectManager->create(StoreRepositoryInterface::class); |
| 50 | + $this->pageRepository = $objectManager->create(PageRepositoryInterface::class); |
| 51 | + $this->blockFactory = $objectManager->create(BlockFactory::class); |
| 52 | + $this->pageFactory = $objectManager->create(PageFactory::class); |
| 53 | + $this->storeManager = $objectManager->create(StoreManagerInterface::class); |
| 54 | + $this->exportDirPath = $this->varDirectory->getAbsolutePath() . 'sync_cms_data'; |
| 55 | + } |
| 56 | + |
| 57 | + public function getExecuteCases(): array |
| 58 | + { |
| 59 | + return [ |
| 60 | + ['block', 'import_gopher_cms_block_multistore', ['second_store_view']], |
| 61 | + ['page', 'import_gopher_cms_page_multistore', ['second_store_view']], |
| 62 | + ]; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @param string $type |
| 67 | + * @param string $identifier |
| 68 | + * @param array $scopes |
| 69 | + * @return void |
| 70 | + * @magentoAppArea adminhtml |
| 71 | + * @magentoDataFixture Rocketweb_CmsImportExport::_files/multiple_websites_with_store_groups_stores.php |
| 72 | + * @dataProvider getExecuteCases |
| 73 | + * @throws \Exception |
| 74 | + */ |
| 75 | + public function testCmsImportedCorrectlyByScope( |
| 76 | + string $type, |
| 77 | + string $identifier, |
| 78 | + array $scopes |
| 79 | + ) { |
| 80 | + // @codingStandardsIgnoreStart |
| 81 | + //remove blocks / pages first if they exist |
| 82 | + try { |
| 83 | + if ($type === 'block') { |
| 84 | + $block = $this->blockFactory->create(); |
| 85 | + $block->load($identifier, 'identifier'); |
| 86 | + $this->blockRepository->delete($block); |
| 87 | + } else { |
| 88 | + $page = $this->pageFactory->create(); |
| 89 | + $page->load($identifier, 'identifier'); |
| 90 | + $this->pageRepository->delete($page); |
| 91 | + } |
| 92 | + } catch (\Exception $e) { //means they don't exist, move on |
| 93 | + } |
| 94 | + |
| 95 | + $selectedStore = $this->storeRepository->get(current($scopes)); |
| 96 | + $this->checkAndCreateFiles($type, $identifier, $scopes); |
| 97 | + if ($this->storeManager->getStore()->getId() != '0') { |
| 98 | + $this->storeManager->setCurrentStore(0); |
| 99 | + } |
| 100 | + $this->importer->execute([$type], [$identifier], false); |
| 101 | + if ($type === 'block') { |
| 102 | + /** @var Block $page */ |
| 103 | + $block = $this->blockFactory->create(); |
| 104 | + $block->load($identifier, 'identifier'); |
| 105 | + self::assertIsObject($block); |
| 106 | + self::assertNotEmpty($block->getId()); |
| 107 | + self::assertContains($selectedStore->getId(), $block->getStores()); |
| 108 | + self::assertEquals($block->getIdentifier(), $identifier); |
| 109 | + } else { |
| 110 | + /** @var Page $page */ |
| 111 | + $page = $this->pageFactory->create(); |
| 112 | + $page->load($identifier, 'identifier'); |
| 113 | + self::assertIsObject($page); |
| 114 | + self::assertNotEmpty($page->getId()); |
| 115 | + self::assertContains($selectedStore->getId(), $page->getStores()); |
| 116 | + self::assertEquals($page->getIdentifier(), $identifier); |
| 117 | + } |
| 118 | + // @codingStandardsIgnoreEnd |
| 119 | + } |
| 120 | + |
| 121 | + private function checkAndCreateFiles(string $type, string $identifier, array $scopes): void |
| 122 | + { |
| 123 | + if (!$this->varDirectory->isExist($this->exportDirPath)) { |
| 124 | + $this->varDirectory->create($this->exportDirPath); |
| 125 | + } |
| 126 | + |
| 127 | + $blockContent = '{"title":"CMS Block Title","identifier":"import_gopher_cms_block_multistore","stores":["second_store_view"],"is_active":true,"is_tailwindcss_jit_enabled":"1"}'; |
| 128 | + $blockHtmlContent = '<h1>Fixture Block Title</h1> |
| 129 | +<a href="{{store url=""}}">store url</a> |
| 130 | +<p>Config value: "{{config path="web/unsecure/base_url"}}".</p> |
| 131 | +<p>Custom variable: "{{customvar code="variable_code"}}".</p> |
| 132 | +'; |
| 133 | + $pageContent = '{"title":"Cms Page 100","is_active":true,"page_layout":"1column","identifier":"import_gopher_cms_page_multistore","stores":["second_store_view"],"content_heading":"<h2>Cms Page 100 Title<\/h2>","is_tailwindcss_jit_enabled":"1"}'; |
| 134 | + $pageHtmlContent = '<h1>Cms Page 100 Title</h1>'; |
| 135 | + $jsonFilename = sprintf( |
| 136 | + "$identifier---%s.json", |
| 137 | + implode('---', $scopes) |
| 138 | + ); |
| 139 | + $htmlFilename = sprintf( |
| 140 | + "$identifier---%s.html", |
| 141 | + implode('---', $scopes) |
| 142 | + ); |
| 143 | + $jsonFilePath = $this->exportDirPath . ($type === 'block' ? '/cms/blocks/' : '/cms/pages/') . $jsonFilename; |
| 144 | + $htmlFilePath = $this->exportDirPath . ($type === 'block' ? '/cms/blocks/' : '/cms/pages/') . $htmlFilename; |
| 145 | + if (!$this->varDirectory->isFile($jsonFilePath)) { |
| 146 | + $this->varDirectory->writeFile( |
| 147 | + $jsonFilePath, |
| 148 | + $type === 'block' ? $blockContent : $pageContent |
| 149 | + ); |
| 150 | + } |
| 151 | + |
| 152 | + if (!$this->varDirectory->isFile($htmlFilePath)) { |
| 153 | + $this->varDirectory->writeFile( |
| 154 | + $htmlFilePath, |
| 155 | + $type === 'block' ? $blockHtmlContent : $pageHtmlContent |
| 156 | + ); |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments