Skip to content

Commit 3db94ce

Browse files
committed
MC-41018: Minor changes in CMS page validation
1 parent ff591a3 commit 3db94ce

File tree

3 files changed

+98
-2
lines changed

3 files changed

+98
-2
lines changed

app/code/Magento/Cms/Controller/Adminhtml/Page/PostDataProcessor.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\Framework\App\ObjectManager;
1111
use Magento\Framework\Config\Dom\ValidationException;
1212
use Magento\Framework\Config\Dom\ValidationSchemaException;
13+
use Magento\Cms\Model\Page\CustomLayout\CustomLayoutValidator;
1314

1415
/**
1516
* Controller helper for user input.
@@ -36,23 +37,32 @@ class PostDataProcessor
3637
*/
3738
private $validationState;
3839

40+
/**
41+
* @var CustomLayoutValidator
42+
*/
43+
private $customLayoutValidator;
44+
3945
/**
4046
* @param \Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter
4147
* @param \Magento\Framework\Message\ManagerInterface $messageManager
4248
* @param \Magento\Framework\View\Model\Layout\Update\ValidatorFactory $validatorFactory
43-
* @param DomValidationState $validationState
49+
* @param DomValidationState|null $validationState
50+
* @param CustomLayoutValidator|null $customLayoutValidator
4451
*/
4552
public function __construct(
4653
\Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter,
4754
\Magento\Framework\Message\ManagerInterface $messageManager,
4855
\Magento\Framework\View\Model\Layout\Update\ValidatorFactory $validatorFactory,
49-
DomValidationState $validationState = null
56+
DomValidationState $validationState = null,
57+
CustomLayoutValidator $customLayoutValidator = null
5058
) {
5159
$this->dateFilter = $dateFilter;
5260
$this->messageManager = $messageManager;
5361
$this->validatorFactory = $validatorFactory;
5462
$this->validationState = $validationState
5563
?: ObjectManager::getInstance()->get(DomValidationState::class);
64+
$this->customLayoutValidator = $customLayoutValidator
65+
?: ObjectManager::getInstance()->get(CustomLayoutValidator::class);
5666
}
5767

5868
/**
@@ -146,6 +156,9 @@ private function validateData($data, $layoutXmlValidator)
146156
) {
147157
return false;
148158
}
159+
if (!$this->customLayoutValidator->validate($data)) {
160+
return false;
161+
}
149162
} catch (ValidationException $e) {
150163
return false;
151164
} catch (ValidationSchemaException $e) {
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Cms\Model\Page\CustomLayout;
9+
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Framework\Message\ManagerInterface;
12+
use Magento\Cms\Model\PageFactory;
13+
14+
class CustomLayoutValidator
15+
{
16+
/**
17+
* @var PageFactory
18+
*/
19+
protected $pageFactory;
20+
21+
/**
22+
* @var ManagerInterface
23+
*/
24+
private $messageManager;
25+
26+
/**
27+
* CustomLayoutValidator constructor.
28+
* @param PageFactory $pageFactory
29+
* @param ManagerInterface $messageManager
30+
*/
31+
public function __construct(
32+
PageFactory $pageFactory,
33+
ManagerInterface $messageManager
34+
) {
35+
$this->pageFactory = $pageFactory;
36+
$this->messageManager = $messageManager;
37+
}
38+
39+
/**
40+
* Validates layout update and custom layout update for CMS page
41+
*
42+
* @param array $data
43+
* @return bool
44+
* @throws LocalizedException
45+
*/
46+
public function validate(array $data)
47+
{
48+
$layoutUpdate = isset($data['layout_update_xml']) ? $data['layout_update_xml'] : null;
49+
$customLayoutUpdate = isset($data['custom_layout_update_xml']) ? $data['custom_layout_update_xml'] : null;
50+
$page = $this->pageFactory->create();
51+
$page->load($data['page_id']);
52+
$oldLayoutUpdate = $page->getId() ? $page->getLayoutUpdateXml() : null;
53+
$olCustomLayoutUpdate = $page->getId() ? $page->getCustomLayoutUpdateXml() : null;
54+
if ($layoutUpdate && $oldLayoutUpdate !== $layoutUpdate
55+
|| $customLayoutUpdate && $olCustomLayoutUpdate !== $customLayoutUpdate
56+
) {
57+
throw new LocalizedException(__('Custom layout update text cannot be changed, only removed'));
58+
}
59+
return true;
60+
}
61+
}

dev/tests/integration/testsuite/Magento/Cms/Controller/Adminhtml/PageDesignTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Magento\UrlRewrite\Model\ResourceModel\UrlRewriteCollection;
2121
use Magento\UrlRewrite\Model\ResourceModel\UrlRewriteCollectionFactory;
2222
use Magento\UrlRewrite\Model\UrlRewrite;
23+
use Magento\Cms\Controller\Adminhtml\Page\PostDataProcessor;
2324

2425
/**
2526
* Test the saving CMS pages design via admin area interface.
@@ -64,6 +65,8 @@ class PageDesignTest extends AbstractBackendController
6465
*/
6566
private $pagesToDelete = [];
6667

68+
private $postDaraProcessor;
69+
6770
/**
6871
* @inheritDoc
6972
*/
@@ -81,6 +84,7 @@ protected function setUp(): void
8184
$this->pageRetriever = Bootstrap::getObjectManager()->get(GetPageByIdentifierInterface::class);
8285
$this->scopeConfig = Bootstrap::getObjectManager()->get(ScopeConfigInterface::class);
8386
$this->pagesToDelete = [];
87+
$this->postDaraProcessor = Bootstrap::getObjectManager()->get(PostDataProcessor::class);
8488
}
8589

8690
/**
@@ -295,4 +299,22 @@ public function testSaveWithInlavidIdentifier(): void
295299
MessageInterface::TYPE_ERROR
296300
);
297301
}
302+
303+
/**
304+
* Test create CMS page with invalid layout update
305+
*
306+
* @return void
307+
*/
308+
public function testSaveWithCustomLayoutUpdate(): void
309+
{
310+
$requestData = [
311+
PageInterface::IDENTIFIER => 'randomidentified',
312+
PageInterface::TITLE => 'page title',
313+
PageInterface::CUSTOM_THEME => '1',
314+
PageInterface::PAGE_LAYOUT => 'empty',
315+
PageInterface::CUSTOM_LAYOUT_UPDATE_XML => '<container />',
316+
PageInterface::LAYOUT_UPDATE_XML => '<container />',
317+
];
318+
$this->assertFalse($this->postDaraProcessor->validate($requestData));
319+
}
298320
}

0 commit comments

Comments
 (0)