Skip to content

Commit f6c326b

Browse files
committed
Merge remote-tracking branch 'tango-ce/MAGETWO-52215' into MAGETWO-52779
2 parents 0228243 + 82d7789 commit f6c326b

File tree

2 files changed

+187
-58
lines changed

2 files changed

+187
-58
lines changed

app/code/Magento/Cms/Model/Page.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77

88
use Magento\Cms\Api\Data\PageInterface;
99
use Magento\Cms\Model\ResourceModel\Page as ResourceCmsPage;
10+
use Magento\Framework\App\Config\ScopeConfigInterface;
1011
use Magento\Framework\DataObject\IdentityInterface;
12+
use Magento\Framework\Exception\LocalizedException;
1113
use Magento\Framework\Model\AbstractModel;
14+
use Magento\Cms\Helper\Page as PageHelper;
1215

1316
/**
1417
* Cms Page Model
@@ -49,6 +52,11 @@ class Page extends AbstractModel implements PageInterface, IdentityInterface
4952
*/
5053
protected $_eventPrefix = 'cms_page';
5154

55+
/**
56+
* @var ScopeConfigInterface
57+
*/
58+
private $scopeConfig;
59+
5260
/**
5361
* Initialize resource model
5462
*
@@ -526,4 +534,44 @@ public function setIsActive($isActive)
526534
{
527535
return $this->setData(self::IS_ACTIVE, $isActive);
528536
}
537+
538+
/**
539+
* {@inheritdoc}
540+
*/
541+
public function beforeSave()
542+
{
543+
$originalIdentifier = $this->getOrigData('identifier');
544+
$currentIdentifier = $this->getIdentifier();
545+
546+
if (!$this->getId() || $originalIdentifier === $currentIdentifier) {
547+
return parent::beforeSave();
548+
}
549+
550+
switch ($originalIdentifier) {
551+
case $this->getScopeConfig()->getValue(PageHelper::XML_PATH_NO_ROUTE_PAGE):
552+
throw new LocalizedException(
553+
__('This identifier is reserved for "CMS No Route Page" in configuration.')
554+
);
555+
case $this->getScopeConfig()->getValue(PageHelper::XML_PATH_HOME_PAGE):
556+
throw new LocalizedException(__('This identifier is reserved for "CMS Home Page" in configuration.'));
557+
case $this->getScopeConfig()->getValue(PageHelper::XML_PATH_NO_COOKIES_PAGE):
558+
throw new LocalizedException(
559+
__('This identifier is reserved for "CMS No Cookies Page" in configuration.')
560+
);
561+
}
562+
563+
return parent::beforeSave();
564+
}
565+
566+
/**
567+
* @return ScopeConfigInterface
568+
*/
569+
private function getScopeConfig()
570+
{
571+
if (null === $this->scopeConfig) {
572+
$this->scopeConfig = \Magento\Framework\App\ObjectManager::getInstance()->get(ScopeConfigInterface::class);
573+
}
574+
575+
return $this->scopeConfig;
576+
}
529577
}

app/code/Magento/Cms/Test/Unit/Model/PageTest.php

Lines changed: 139 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -5,96 +5,99 @@
55
*/
66
namespace Magento\Cms\Test\Unit\Model;
77

8+
use Magento\Cms\Model\Page;
9+
use Magento\Framework\App\Config\ScopeConfigInterface;
10+
use Magento\Framework\Event\ManagerInterface;
11+
use Magento\Framework\Model\Context;
12+
use Magento\Cms\Model\ResourceModel\Page as PageResource;
13+
use Magento\Framework\Model\ResourceModel\AbstractResource;
14+
815
/**
916
* @covers \Magento\Cms\Model\Page
17+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1018
*/
1119
class PageTest extends \PHPUnit_Framework_TestCase
1220
{
1321
/**
14-
* @var \Magento\Cms\Model\Page|\PHPUnit_Framework_MockObject_MockObject
22+
* @var \Magento\Cms\Model\Page
1523
*/
16-
protected $thisMock;
24+
protected $model;
1725

1826
/**
19-
* @var \Magento\Backend\Block\Template\Context
27+
* @var \Magento\Backend\Block\Template\Context|\PHPUnit_Framework_MockObject_MockObject
2028
*/
21-
protected $context;
29+
protected $contextMock;
2230

2331
/**
24-
* @var \Magento\Framework\Event\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
32+
* @var ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
2533
*/
2634
protected $eventManagerMock;
2735

2836
/**
29-
* @var \Magento\Cms\Model\ResourceModel\Page|\PHPUnit_Framework_MockObject_MockObject
37+
* @var PageResource|\PHPUnit_Framework_MockObject_MockObject
3038
*/
3139
protected $resourcePageMock;
3240

41+
/**
42+
* @var AbstractResource|\PHPUnit_Framework_MockObject_MockObject
43+
*/
44+
protected $resourcesMock;
45+
46+
/**
47+
* @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
48+
*/
49+
protected $scopeConfigMock;
50+
3351
protected function setUp()
3452
{
35-
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
36-
$this->eventManagerMock = $this->getMockBuilder('Magento\Framework\Event\ManagerInterface')
53+
$this->eventManagerMock = $this->getMockBuilder(ManagerInterface::class)
3754
->disableOriginalConstructor()
3855
->getMock();
39-
$this->context = $objectManager->getObject(
40-
'Magento\Framework\Model\Context',
41-
[
42-
'eventDispatcher' => $this->eventManagerMock
43-
]
44-
);
45-
$this->resourcePageMock = $this->getMockBuilder('Magento\Cms\Model\ResourceModel\Page')
56+
$this->contextMock = $this->getMockBuilder(Context::class)
4657
->disableOriginalConstructor()
47-
->setMethods(
48-
[
49-
'getIdFieldName',
50-
'checkIdentifier',
51-
]
52-
)
5358
->getMock();
54-
$this->thisMock = $this->getMockBuilder('Magento\Cms\Model\Page')
55-
->setConstructorArgs(
56-
[
57-
$this->context,
58-
$this->getMockBuilder('Magento\Framework\Registry')
59-
->disableOriginalConstructor()
60-
->getMock(),
61-
$this->getMockBuilder('Magento\Framework\Model\ResourceModel\AbstractResource')
62-
->disableOriginalConstructor()
63-
->setMethods(
64-
[
65-
'_construct',
66-
'getConnection',
67-
]
68-
)
69-
->getMockForAbstractClass(),
70-
$this->getMockBuilder('Magento\Framework\Data\Collection\AbstractDb')
71-
->disableOriginalConstructor()
72-
->getMockForAbstractClass(),
73-
]
74-
)
75-
->setMethods(
76-
[
77-
'_construct',
78-
'_getResource',
79-
'load',
80-
]
81-
)
59+
$this->resourcePageMock = $this->getMockBuilder(PageResource::class)
60+
->disableOriginalConstructor()
61+
->setMethods(['getIdFieldName', 'checkIdentifier'])
8262
->getMock();
63+
$this->eventManagerMock = $this->getMockBuilder(ManagerInterface::class)
64+
->disableOriginalConstructor()
65+
->getMock();
66+
$this->resourcesMock = $this->getMockBuilder(AbstractResource::class)
67+
->setMethods(['getIdFieldName', 'load', 'checkIdentifier'])
68+
->getMockForAbstractClass();
69+
$this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class)
70+
->getMockForAbstractClass();
71+
72+
$this->contextMock->expects($this->any())
73+
->method('getEventDispatcher')
74+
->willReturn($this->eventManagerMock);
75+
$this->resourcePageMock->expects($this->any())
76+
->method('getResources')
77+
->willReturn($this->resourcesMock);
8378

84-
$this->thisMock->expects($this->any())
85-
->method('_getResource')
86-
->willReturn($this->resourcePageMock);
87-
$this->thisMock->expects($this->any())
88-
->method('load')
89-
->willReturnSelf();
79+
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
80+
81+
$this->model = $objectManager->getObject(
82+
Page::class,
83+
[
84+
'context' => $this->contextMock,
85+
'resource' => $this->resourcesMock,
86+
]
87+
);
88+
$objectManager->setBackwardCompatibleProperty(
89+
$this->model,
90+
'scopeConfig',
91+
$this->scopeConfigMock
92+
);
9093
}
9194

9295
/**
9396
* @covers \Magento\Cms\Model\Page::noRoutePage
9497
*/
9598
public function testNoRoutePage()
9699
{
97-
$this->assertEquals($this->thisMock, $this->thisMock->noRoutePage());
100+
$this->assertEquals($this->model, $this->model->noRoutePage());
98101
}
99102

100103
/**
@@ -106,11 +109,89 @@ public function testCheckIdentifier()
106109
$storeId = 2;
107110
$fetchOneResult = 'some result';
108111

109-
$this->resourcePageMock->expects($this->atLeastOnce())
112+
$this->resourcesMock->expects($this->atLeastOnce())
110113
->method('checkIdentifier')
111114
->with($identifier, $storeId)
112115
->willReturn($fetchOneResult);
113116

114-
$this->assertInternalType('string', $this->thisMock->checkIdentifier($identifier, $storeId));
117+
$this->assertInternalType('string', $this->model->checkIdentifier($identifier, $storeId));
118+
}
119+
120+
/**
121+
* @expectedException \Magento\Framework\Exception\LocalizedException
122+
* @expectedExceptionMessage This identifier is reserved for "CMS No Route Page" in configuration.
123+
*/
124+
public function testBeforeSave404Identifier()
125+
{
126+
$this->model->setId(1);
127+
$this->model->setOrigData('identifier', 'no-route');
128+
$this->model->setIdentifier('no-route2');
129+
130+
$this->scopeConfigMock->expects($this->once())
131+
->method('getValue')
132+
->willReturnMap(
133+
[
134+
[
135+
\Magento\Cms\Helper\Page::XML_PATH_NO_ROUTE_PAGE,
136+
ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
137+
null,
138+
'no-route'
139+
]
140+
]
141+
);
142+
143+
$this->model->beforeSave();
144+
}
145+
146+
/**
147+
* @expectedException \Magento\Framework\Exception\LocalizedException
148+
* @expectedExceptionMessage This identifier is reserved for "CMS Home Page" in configuration.
149+
*/
150+
public function testBeforeSaveHomeIdentifier()
151+
{
152+
$this->model->setId(1);
153+
$this->model->setOrigData('identifier', 'home');
154+
$this->model->setIdentifier('home2');
155+
156+
$this->scopeConfigMock->expects($this->atLeastOnce())
157+
->method('getValue')
158+
->willReturnMap(
159+
[
160+
[
161+
\Magento\Cms\Helper\Page::XML_PATH_HOME_PAGE,
162+
ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
163+
null,
164+
'home'
165+
]
166+
]
167+
);
168+
169+
$this->model->beforeSave();
170+
}
171+
172+
/**
173+
* @expectedException \Magento\Framework\Exception\LocalizedException
174+
* @expectedExceptionMessage This identifier is reserved for "CMS No Cookies Page" in configuration.
175+
*/
176+
public function testBeforeSaveNoCookiesIdentifier()
177+
{
178+
$this->model->setId(1);
179+
$this->model->setOrigData('identifier', 'no-cookies');
180+
$this->model->setIdentifier('no-cookies2');
181+
182+
$this->scopeConfigMock->expects($this->atLeastOnce())
183+
->method('getValue')
184+
->willReturnMap(
185+
[
186+
[
187+
\Magento\Cms\Helper\Page::XML_PATH_NO_COOKIES_PAGE,
188+
ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
189+
null,
190+
'no-cookies'
191+
]
192+
]
193+
);
194+
195+
$this->model->beforeSave();
115196
}
116197
}

0 commit comments

Comments
 (0)