Skip to content

Commit ba2981c

Browse files
mihai.comanmihai.coman
authored andcommitted
ICP-11 added unit tests
1 parent 5dc5d4d commit ba2981c

10 files changed

+834
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rocketweb\CmsImportExport\Test\Integration;
6+
7+
use Magento\Framework\Exception\FileSystemException;
8+
use Magento\Store\Model\StoreManagerInterface;
9+
use Magento\TestFramework\App\Filesystem;
10+
use Magento\Framework\App\Filesystem\DirectoryList;
11+
use Magento\Framework\Filesystem\Directory\WriteInterface;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
use PHPUnit\Framework\TestCase;
14+
use RocketWeb\CmsImportExport\Model\Service\DumpCmsDataService;
15+
16+
/**
17+
* @magentoAppIsolation disabled
18+
* @magentoDbIsolation disabled
19+
* @magentoAppArea adminhtml
20+
*/
21+
class ExportByStoreScopeEntitiesTest extends TestCase
22+
{
23+
protected ?DumpCmsDataService $exporter;
24+
protected ?string $exportDirPath;
25+
protected ?WriteInterface $varDirectory;
26+
27+
/**
28+
* @return void
29+
* @throws FileSystemException
30+
*/
31+
protected function setUp(): void
32+
{
33+
$objectManager = Bootstrap::getObjectManager();
34+
$fileSystem = $objectManager->create(Filesystem::class);
35+
$this->exporter = $objectManager->create(DumpCmsDataService::class);
36+
$this->varDirectory = $fileSystem->getDirectoryWrite(DirectoryList::VAR_DIR);
37+
$this->exportDirPath = $this->varDirectory->getAbsolutePath() . 'sync_cms_data';
38+
$storeManager = $objectManager->create(StoreManagerInterface::class);
39+
if ($storeManager->getStore()->getId() != '0') {
40+
$storeManager->setCurrentStore(0);
41+
}
42+
}
43+
44+
public function getExecuteCases(): array
45+
{
46+
return [
47+
['block', 'import_gopher_cms_block_multistore', ['default', 'second_store_view']],
48+
['page', 'import_gopher_cms_page_multistore', ['default', 'second_store_view']],
49+
];
50+
}
51+
52+
/**
53+
* @param string $type
54+
* @param string $identifier
55+
* @param array $scopes
56+
* @return void
57+
* @throws FileSystemException
58+
* @magentoDataFixture Rocketweb_CmsImportExport::_files/multiple_websites_with_store_groups_stores.php
59+
* @dataProvider getExecuteCases
60+
* @magentoDataFixture Rocketweb_CmsImportExport::_files/multi_store_block.php
61+
* @magentoDataFixture Rocketweb_CmsImportExport::_files/multi_store_page.php
62+
*/
63+
public function testCmsExportedCorrectlyByScope(
64+
string $type,
65+
string $identifier,
66+
array $scopes
67+
) {
68+
$this->exporter->execute([$type], [$identifier], false);
69+
//validate that the export folder exists
70+
self::assertTrue(
71+
$this->varDirectory->isExist($this->exportDirPath),
72+
__CLASS__ . ' Export directory does not exist'
73+
);
74+
75+
$filename = sprintf(
76+
"$identifier---%s.json",
77+
implode('---', $scopes)
78+
);
79+
$filepath = sprintf(
80+
'%s/%s/%s',
81+
$this->exportDirPath,
82+
$type === 'block' ? 'cms/blocks' : 'cms/pages',
83+
$filename
84+
);
85+
//validate file was created successfully
86+
self::assertTrue(
87+
$this->varDirectory->isFile($filepath),
88+
__CLASS__ . " $filename does not exist"
89+
);
90+
91+
$decoded = json_decode(
92+
$this->varDirectory->readFile($filepath),
93+
true
94+
);
95+
//validate file structure was created successfully
96+
self::assertArrayHasKey(
97+
'identifier',
98+
$decoded,
99+
__CLASS__ . " $filename does not have the correct structure"
100+
);
101+
self::assertTrue(
102+
$decoded['identifier'] === $identifier,
103+
__CLASS__ . " Invalid identifiers, file: {$decoded['identifier']}, provided $identifier"
104+
);
105+
}
106+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rocketweb\CmsImportExport\Test\Integration;
6+
7+
use Magento\Framework\Exception\FileSystemException;
8+
use Magento\TestFramework\App\Filesystem;
9+
use Magento\Framework\App\Filesystem\DirectoryList;
10+
use Magento\Framework\Filesystem\Directory\WriteInterface;
11+
use Magento\TestFramework\Helper\Bootstrap;
12+
use PHPUnit\Framework\TestCase;
13+
use RocketWeb\CmsImportExport\Model\Service\DumpCmsDataService;
14+
15+
/**
16+
* @magentoAppIsolation enabled
17+
* @magentoDbIsolation enabled
18+
*/
19+
class ExportEntitiesTest extends TestCase
20+
{
21+
protected ?DumpCmsDataService $exporter;
22+
protected ?string $exportDirPath;
23+
protected ?WriteInterface $varDirectory;
24+
25+
protected function setUp(): void
26+
{
27+
$objectManager = Bootstrap::getObjectManager();
28+
$fileSystem = $objectManager->create(Filesystem::class);
29+
$this->exporter = $objectManager->create(DumpCmsDataService::class);
30+
$this->varDirectory = $fileSystem->getDirectoryWrite(DirectoryList::VAR_DIR);
31+
$this->exportDirPath = $this->varDirectory->getAbsolutePath() . 'sync_cms_data';
32+
}
33+
34+
public function getExecuteCases(): array
35+
{
36+
return [
37+
['block'],
38+
['page'],
39+
];
40+
}
41+
42+
/**
43+
* @return void
44+
* @throws FileSystemException
45+
*/
46+
protected function tearDown(): void
47+
{
48+
if ($this->varDirectory->isExist($this->exportDirPath))
49+
$this->varDirectory->delete($this->exportDirPath);
50+
}
51+
52+
/**
53+
* @param string $type
54+
* @return void
55+
* @throws FileSystemException
56+
* @dataProvider getExecuteCases
57+
* @magentoDataFixture Magento/Cms/_files/block.php
58+
* @magentoDataFixture Magento/Cms/_files/noroute.php
59+
*/
60+
public function testCmsExportedCorrectly(string $type)
61+
{
62+
$this->exporter->execute([$type], null, false);
63+
//validate that the export folder exists
64+
self::assertTrue(
65+
$this->varDirectory->isExist($this->exportDirPath)
66+
);
67+
if ($type === 'block') {
68+
$filepath = sprintf(
69+
'%s/%s/%s',
70+
$this->exportDirPath,
71+
'cms/blocks',
72+
'fixture_block---default.json'
73+
);
74+
//validate file was created successfully
75+
self::assertTrue(
76+
$this->varDirectory->isFile($filepath)
77+
);
78+
$decoded = json_decode(
79+
$this->varDirectory->readFile($filepath),
80+
true
81+
);
82+
//validate file structure was created successfully
83+
self::assertArrayHasKey('identifier', $decoded);
84+
self::assertTrue($decoded['identifier'] === 'fixture_block');
85+
}
86+
87+
if ($type === 'page') {
88+
$filepath = sprintf(
89+
'%s/%s/%s',
90+
$this->exportDirPath,
91+
'cms/pages',
92+
'no-route---_all_.json'
93+
);
94+
//validate file was created successfully
95+
self::assertTrue(
96+
$this->varDirectory->isFile($filepath)
97+
);
98+
$decoded = json_decode(
99+
$this->varDirectory->readFile($filepath),
100+
true
101+
);
102+
//validate file structure was created successfully
103+
self::assertArrayHasKey('identifier', $decoded);
104+
self::assertTrue($decoded['identifier'] === 'no-route');
105+
}
106+
}
107+
}
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
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

Comments
 (0)