init
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Cms\Model\Page;
|
||||
|
||||
use Magento\Cms\Model\Page;
|
||||
use Magento\Cms\Model\PageFactory;
|
||||
use Magento\Cms\Model\Page\CustomLayout\Data\CustomLayoutSelected;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Magento\Framework\View\Result\PageFactory as PageResultFactory;
|
||||
use Magento\Framework\View\Model\Layout\MergeFactory;
|
||||
use Magento\Framework\View\Model\Layout\Merge;
|
||||
|
||||
/**
|
||||
* Test the manager.
|
||||
*/
|
||||
class CustomLayoutManagerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var CustomLayoutRepositoryInterface
|
||||
*/
|
||||
private $repo;
|
||||
|
||||
/**
|
||||
* @var CustomLayoutManagerInterface
|
||||
*/
|
||||
private $manager;
|
||||
|
||||
/**
|
||||
* @var PageFactory
|
||||
*/
|
||||
private $pageFactory;
|
||||
|
||||
/**
|
||||
* @var PageResultFactory
|
||||
*/
|
||||
private $resultFactory;
|
||||
|
||||
/**
|
||||
* @var IdentityMap
|
||||
*/
|
||||
private $identityMap;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$objectManager = Bootstrap::getObjectManager();
|
||||
$this->resultFactory = $objectManager->get(PageResultFactory::class);
|
||||
//Mocking available list of files for the page.
|
||||
$handles = [
|
||||
'cms_page_view_selectable_page100_select1',
|
||||
'cms_page_view_selectable_page100_select2'
|
||||
];
|
||||
$processor = $this->getMockBuilder(Merge::class)->disableOriginalConstructor()->getMock();
|
||||
$processor->method('getAvailableHandles')->willReturn($handles);
|
||||
$processorFactory = $this->getMockBuilder(MergeFactory::class)->disableOriginalConstructor()->getMock();
|
||||
$processorFactory->method('create')->willReturn($processor);
|
||||
|
||||
$this->manager = $objectManager->create(
|
||||
CustomLayoutManagerInterface::class,
|
||||
['layoutProcessorFactory' => $processorFactory]
|
||||
);
|
||||
$this->repo = $objectManager->create(
|
||||
CustomLayoutRepositoryInterface::class,
|
||||
['manager' => $this->manager]
|
||||
);
|
||||
$this->pageFactory = $objectManager->get(PageFactory::class);
|
||||
$this->identityMap = $objectManager->get(IdentityMap::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a page's custom layout.
|
||||
*
|
||||
* @magentoDataFixture Magento/Cms/_files/pages.php
|
||||
* @throws \Throwable
|
||||
* @return void
|
||||
*/
|
||||
public function testCustomLayoutUpdate(): void
|
||||
{
|
||||
/** @var Page $page */
|
||||
$page = $this->pageFactory->create(['customLayoutRepository' => $this->repo]);
|
||||
$page->load('page100', 'identifier');
|
||||
$pageId = (int)$page->getId();
|
||||
$this->identityMap->add($page);
|
||||
//Set file ID
|
||||
$this->repo->save(new CustomLayoutSelected($pageId, 'select2'));
|
||||
|
||||
//Test handles
|
||||
$result = $this->resultFactory->create();
|
||||
$this->manager->applyUpdate($result, $this->repo->getFor($pageId));
|
||||
$this->identityMap->remove((int)$page->getId());
|
||||
$this->assertContains(
|
||||
'cms_page_view_selectable_page100_select2',
|
||||
$result->getLayout()->getUpdate()->getHandles()
|
||||
);
|
||||
}
|
||||
}
|
||||
+160
@@ -0,0 +1,160 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Cms\Model\Page;
|
||||
|
||||
use Magento\Cms\Model\Page;
|
||||
use Magento\Cms\Model\PageFactory;
|
||||
use Magento\Cms\Model\Page\CustomLayout\Data\CustomLayoutSelected;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Framework\Exception\NoSuchEntityException;
|
||||
use Magento\Framework\View\Model\Layout\Merge;
|
||||
use Magento\Framework\View\Model\Layout\MergeFactory;
|
||||
use Magento\TestFramework\Cms\Model\CustomLayoutManager;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Test the repository.
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
||||
*/
|
||||
class CustomLayoutRepositoryTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var CustomLayoutRepositoryInterface
|
||||
*/
|
||||
private $repo;
|
||||
|
||||
/**
|
||||
* @var PageFactory
|
||||
*/
|
||||
private $pageFactory;
|
||||
|
||||
/**
|
||||
* @var CustomLayoutManager
|
||||
*/
|
||||
private $fakeManager;
|
||||
|
||||
/**
|
||||
* @var IdentityMap
|
||||
*/
|
||||
private $identityMap;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$objectManager = Bootstrap::getObjectManager();
|
||||
$objectManager->configure([
|
||||
'preferences' => [
|
||||
\Magento\Cms\Model\Page\CustomLayoutManagerInterface::class =>
|
||||
\Magento\TestFramework\Cms\Model\CustomLayoutManager::class
|
||||
]
|
||||
]);
|
||||
$this->fakeManager = $objectManager->get(CustomLayoutManager::class);
|
||||
$this->repo = $objectManager->create(CustomLayoutRepositoryInterface::class, ['manager' => $this->fakeManager]);
|
||||
$this->pageFactory = $objectManager->get(PageFactory::class);
|
||||
$this->identityMap = $objectManager->get(IdentityMap::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create page instance.
|
||||
*
|
||||
* @param string $id
|
||||
* @return Page
|
||||
*/
|
||||
private function createPage(string $id): Page
|
||||
{
|
||||
$page = $this->pageFactory->create(['customLayoutRepository' => $this->repo]);
|
||||
$page->load($id, 'identifier');
|
||||
$this->identityMap->add($page);
|
||||
|
||||
return $page;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a page's custom layout.
|
||||
*
|
||||
* @magentoDataFixture Magento/Cms/_files/pages.php
|
||||
* @return void
|
||||
*/
|
||||
public function testCustomLayout(): void
|
||||
{
|
||||
$page = $this->createPage('page100');
|
||||
$pageId = (int)$page->getId();
|
||||
$this->fakeManager->fakeAvailableFiles($pageId, ['select1', 'select2']);
|
||||
|
||||
//Invalid file ID
|
||||
$exceptionRaised = null;
|
||||
try {
|
||||
$this->repo->save(new CustomLayoutSelected($pageId, 'some_file'));
|
||||
} catch (\Throwable $exception) {
|
||||
$exceptionRaised = $exception;
|
||||
}
|
||||
$this->assertNotEmpty($exceptionRaised);
|
||||
$this->assertInstanceOf(\InvalidArgumentException::class, $exceptionRaised);
|
||||
|
||||
//Set file ID
|
||||
$this->repo->save(new CustomLayoutSelected($pageId, 'select2'));
|
||||
|
||||
//Test saved
|
||||
$saved = $this->repo->getFor($pageId);
|
||||
$this->assertEquals('select2', $saved->getLayoutFileId());
|
||||
|
||||
//Removing custom file
|
||||
$this->repo->deleteFor($pageId);
|
||||
|
||||
//Test saved
|
||||
$notFound = false;
|
||||
try {
|
||||
$this->repo->getFor($pageId);
|
||||
} catch (NoSuchEntityException $exception) {
|
||||
$notFound = true;
|
||||
}
|
||||
$this->assertTrue($notFound);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that layout updates are saved with save method.
|
||||
*
|
||||
* @magentoDataFixture Magento/Cms/_files/pages.php
|
||||
* @return void
|
||||
*/
|
||||
public function testSaved(): void
|
||||
{
|
||||
$page = $this->createPage('page100');
|
||||
$this->fakeManager->fakeAvailableFiles((int)$page->getId(), ['select1', 'select2']);
|
||||
|
||||
//Special no-update instruction
|
||||
$page->setData('layout_update_selected', '_no_update_');
|
||||
$page->save();
|
||||
$this->assertNull($page->getData('layout_update_selected'));
|
||||
|
||||
//Existing file update
|
||||
$page->setData('layout_update_selected', 'select1');
|
||||
$page->save();
|
||||
/** @var Page $page */
|
||||
$page = $this->pageFactory->create();
|
||||
$page->load('page100', 'identifier');
|
||||
$this->assertEquals('select1', $page->getData('layout_update_selected'));
|
||||
$this->assertEquals('select1', $this->repo->getFor((int)$page->getId())->getLayoutFileId());
|
||||
|
||||
//Invalid file
|
||||
$caught = null;
|
||||
$page->setData('layout_update_selected', 'nonExisting');
|
||||
try {
|
||||
$page->save();
|
||||
} catch (\Throwable $exception) {
|
||||
$caught = $exception;
|
||||
}
|
||||
$this->assertInstanceOf(LocalizedException::class, $caught);
|
||||
$this->assertEquals($caught->getMessage(), 'Invalid Custom Layout Update selected');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,176 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Cms\Model\Page;
|
||||
|
||||
use Magento\Cms\Api\GetPageByIdentifierInterface;
|
||||
use Magento\Framework\App\Request\Http as HttpRequest;
|
||||
use Magento\Framework\App\RequestInterface;
|
||||
use Magento\Framework\ObjectManagerInterface;
|
||||
use Magento\TestFramework\Cms\Model\CustomLayoutManager;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Test pages data provider.
|
||||
*
|
||||
* @magentoAppArea adminhtml
|
||||
*/
|
||||
class DataProviderTest extends TestCase
|
||||
{
|
||||
private $providerData = [
|
||||
'name' => 'test',
|
||||
'primaryFieldName' => 'page_id',
|
||||
'requestFieldName' => 'page_id',
|
||||
];
|
||||
|
||||
/**
|
||||
* @var DataProvider
|
||||
*/
|
||||
private $provider;
|
||||
|
||||
/**
|
||||
* @var GetPageByIdentifierInterface
|
||||
*/
|
||||
private $repo;
|
||||
|
||||
/**
|
||||
* @var CustomLayoutManager
|
||||
*/
|
||||
private $filesFaker;
|
||||
|
||||
/**
|
||||
* @var HttpRequest
|
||||
*/
|
||||
private $request;
|
||||
|
||||
/**
|
||||
* @var ObjectManagerInterface
|
||||
*/
|
||||
private $objectManager;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = Bootstrap::getObjectManager();
|
||||
$this->objectManager->configure([
|
||||
'preferences' => [CustomLayoutManagerInterface::class => CustomLayoutManager::class]
|
||||
]);
|
||||
$this->repo = $this->objectManager->get(GetPageByIdentifierInterface::class);
|
||||
$this->filesFaker = $this->objectManager->get(CustomLayoutManager::class);
|
||||
$this->request = $this->objectManager->get(HttpRequest::class);
|
||||
$this->provider = $this->objectManager->create(
|
||||
DataProvider::class,
|
||||
array_merge($this->providerData, ['customLayoutManager' => $this->filesFaker])
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that custom layout date is handled properly.
|
||||
*
|
||||
* @magentoDataFixture Magento/Cms/_files/pages_with_layout_xml.php
|
||||
* @dataProvider customLayoutDataProvider
|
||||
*
|
||||
* @param string $identifier
|
||||
* @param string|null $layoutUpdateSelected
|
||||
* @return void
|
||||
*/
|
||||
public function testCustomLayoutData(string $identifier, ?string $layoutUpdateSelected): void
|
||||
{
|
||||
$page = $this->repo->execute($identifier, 0);
|
||||
|
||||
$request = $this->objectManager->create(RequestInterface::class);
|
||||
$request->setParam('page_id', $page->getId());
|
||||
|
||||
$provider = $this->objectManager->create(
|
||||
DataProvider::class,
|
||||
array_merge($this->providerData, ['request' => $request])
|
||||
);
|
||||
|
||||
$data = $provider->getData();
|
||||
$pageData = $data[$page->getId()];
|
||||
|
||||
$this->assertEquals($layoutUpdateSelected, $pageData['layout_update_selected']);
|
||||
}
|
||||
|
||||
/**
|
||||
* DataProvider for testCustomLayoutData
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function customLayoutDataProvider(): array
|
||||
{
|
||||
return [
|
||||
['test_custom_layout_page_1', '_existing_'],
|
||||
['test_custom_layout_page_2', null],
|
||||
['test_custom_layout_page_3', 'test_selected'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that proper meta for custom layout field is returned.
|
||||
*
|
||||
* @return void
|
||||
* @throws \Throwable
|
||||
* @magentoDataFixture Magento/Cms/_files/pages_with_layout_xml.php
|
||||
*/
|
||||
public function testCustomLayoutMeta(): void
|
||||
{
|
||||
//Testing a page without layout xml
|
||||
$page = $this->repo->execute('test_custom_layout_page_3', 0);
|
||||
$this->filesFaker->fakeAvailableFiles((int)$page->getId(), ['test1', 'test2']);
|
||||
$this->request->setParam('page_id', $page->getId());
|
||||
|
||||
$meta = $this->provider->getMeta();
|
||||
$this->assertArrayHasKey('design', $meta);
|
||||
$this->assertArrayHasKey('children', $meta['design']);
|
||||
$this->assertArrayHasKey('custom_layout_update_select', $meta['design']['children']);
|
||||
$this->assertArrayHasKey('arguments', $meta['design']['children']['custom_layout_update_select']);
|
||||
$this->assertArrayHasKey('data', $meta['design']['children']['custom_layout_update_select']['arguments']);
|
||||
$this->assertArrayHasKey(
|
||||
'options',
|
||||
$meta['design']['children']['custom_layout_update_select']['arguments']['data']
|
||||
);
|
||||
$expectedList = [
|
||||
['label' => 'No update', 'value' => '_no_update_'],
|
||||
['label' => 'test1', 'value' => 'test1'],
|
||||
['label' => 'test2', 'value' => 'test2']
|
||||
];
|
||||
$metaList = $meta['design']['children']['custom_layout_update_select']['arguments']['data']['options'];
|
||||
sort($expectedList);
|
||||
sort($metaList);
|
||||
$this->assertEquals($expectedList, $metaList);
|
||||
|
||||
//Page with old layout xml
|
||||
$page = $this->repo->execute('test_custom_layout_page_1', 0);
|
||||
$this->filesFaker->fakeAvailableFiles((int)$page->getId(), ['test3']);
|
||||
$this->request->setParam('page_id', $page->getId());
|
||||
|
||||
$meta = $this->provider->getMeta();
|
||||
$this->assertArrayHasKey('design', $meta);
|
||||
$this->assertArrayHasKey('children', $meta['design']);
|
||||
$this->assertArrayHasKey('custom_layout_update_select', $meta['design']['children']);
|
||||
$this->assertArrayHasKey('arguments', $meta['design']['children']['custom_layout_update_select']);
|
||||
$this->assertArrayHasKey('data', $meta['design']['children']['custom_layout_update_select']['arguments']);
|
||||
$this->assertArrayHasKey(
|
||||
'options',
|
||||
$meta['design']['children']['custom_layout_update_select']['arguments']['data']
|
||||
);
|
||||
$expectedList = [
|
||||
['label' => 'No update', 'value' => '_no_update_'],
|
||||
['label' => 'Use existing layout update XML', 'value' => '_existing_'],
|
||||
['label' => 'test3', 'value' => 'test3'],
|
||||
];
|
||||
$metaList = $meta['design']['children']['custom_layout_update_select']['arguments']['data']['options'];
|
||||
sort($expectedList);
|
||||
sort($metaList);
|
||||
$this->assertEquals($expectedList, $metaList);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user