Skip to content

Commit b4c2449

Browse files
authored
Merge pull request #4273 from magento-plankton/MC-17013
[plankton] PR
2 parents 8c0b621 + 529750a commit b4c2449

File tree

3 files changed

+93
-13
lines changed

3 files changed

+93
-13
lines changed

dev/tests/integration/testsuite/Magento/Framework/View/Layout/MergeTest.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ class MergeTest extends \PHPUnit\Framework\TestCase
4040
*/
4141
protected $_cache;
4242

43+
/**
44+
* @var \Magento\Framework\Serialize\SerializerInterface|\PHPUnit_Framework_MockObject_MockObject
45+
*/
46+
protected $serializer;
47+
4348
/**
4449
* @var \PHPUnit_Framework_MockObject_MockObject
4550
*/
@@ -68,7 +73,8 @@ class MergeTest extends \PHPUnit\Framework\TestCase
6873
protected function setUp()
6974
{
7075
$files = [];
71-
foreach (glob(__DIR__ . '/_mergeFiles/layout/*.xml') as $filename) {
76+
$fileDriver = new \Magento\Framework\Filesystem\Driver\File();
77+
foreach ($fileDriver->readDirectory(__DIR__ . '/_mergeFiles/layout/') as $filename) {
7278
$files[] = new \Magento\Framework\View\File($filename, 'Magento_Widget');
7379
}
7480
$fileSource = $this->getMockForAbstractClass(\Magento\Framework\View\File\CollectorInterface::class);
@@ -94,6 +100,8 @@ protected function setUp()
94100

95101
$this->_cache = $this->getMockForAbstractClass(\Magento\Framework\Cache\FrontendInterface::class);
96102

103+
$this->serializer = $this->getMockForAbstractClass(\Magento\Framework\Serialize\SerializerInterface::class);
104+
97105
$this->_theme = $this->createMock(\Magento\Theme\Model\Theme::class);
98106
$this->_theme->expects($this->any())->method('isPhysical')->will($this->returnValue(true));
99107
$this->_theme->expects($this->any())->method('getArea')->will($this->returnValue('area'));
@@ -129,6 +137,7 @@ function ($filename) use ($fileDriver) {
129137
'resource' => $this->_resource,
130138
'appState' => $this->_appState,
131139
'cache' => $this->_cache,
140+
'serializer' => $this->serializer,
132141
'theme' => $this->_theme,
133142
'validator' => $this->_layoutValidator,
134143
'logger' => $this->_logger,
@@ -264,9 +273,16 @@ public function testLoadFileSystemWithPageLayout()
264273

265274
public function testLoadCache()
266275
{
276+
$cacheValue = [
277+
"pageLayout" => "1column",
278+
"layout" => self::FIXTURE_LAYOUT_XML
279+
];
280+
267281
$this->_cache->expects($this->at(0))->method('load')
268-
->with('LAYOUT_area_STORE20_100c6a4ccd050e33acef0553f24ef399961')
269-
->will($this->returnValue(self::FIXTURE_LAYOUT_XML));
282+
->with('LAYOUT_area_STORE20_100c6a4ccd050e33acef0553f24ef399961_page_layout_merged')
283+
->will($this->returnValue(json_encode($cacheValue)));
284+
285+
$this->serializer->expects($this->once())->method('unserialize')->willReturn($cacheValue);
270286

271287
$this->assertEmpty($this->_model->getHandles());
272288
$this->assertEmpty($this->_model->asString());
@@ -413,7 +429,8 @@ public function testLoadWithInvalidLayout()
413429
->willThrowException(new \Exception('Layout is invalid.'));
414430

415431
$suffix = md5(implode('|', $this->_model->getHandles()));
416-
$cacheId = "LAYOUT_{$this->_theme->getArea()}_STORE{$this->scope->getId()}_{$this->_theme->getId()}{$suffix}";
432+
$cacheId = "LAYOUT_{$this->_theme->getArea()}_STORE{$this->scope->getId()}"
433+
. "_{$this->_theme->getId()}{$suffix}_page_layout_merged";
417434
$messages = $this->_layoutValidator->getMessages();
418435

419436
// Testing error message is logged with logger

lib/internal/Magento/Framework/View/Model/Layout/Merge.php

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
*/
66
namespace Magento\Framework\View\Model\Layout;
77

8+
use Magento\Framework\App\ObjectManager;
89
use Magento\Framework\App\State;
910
use Magento\Framework\Config\Dom\ValidationException;
1011
use Magento\Framework\Filesystem\DriverPool;
1112
use Magento\Framework\Filesystem\File\ReadFactory;
13+
use Magento\Framework\Serialize\SerializerInterface;
1214
use Magento\Framework\View\Model\Layout\Update\Validator;
1315

1416
/**
@@ -41,7 +43,7 @@ class Merge implements \Magento\Framework\View\Layout\ProcessorInterface
4143
/**
4244
* Cache id suffix for page layout
4345
*/
44-
const PAGE_LAYOUT_CACHE_SUFFIX = 'page_layout';
46+
const PAGE_LAYOUT_CACHE_SUFFIX = 'page_layout_merged';
4547

4648
/**
4749
* @var \Magento\Framework\View\Design\ThemeInterface
@@ -53,6 +55,11 @@ class Merge implements \Magento\Framework\View\Layout\ProcessorInterface
5355
*/
5456
private $scope;
5557

58+
/**
59+
* @var SerializerInterface
60+
*/
61+
private $serializer;
62+
5663
/**
5764
* In-memory cache for loaded layout updates
5865
*
@@ -168,6 +175,7 @@ class Merge implements \Magento\Framework\View\Layout\ProcessorInterface
168175
* @param ReadFactory $readFactory,
169176
* @param \Magento\Framework\View\Design\ThemeInterface $theme Non-injectable theme instance
170177
* @param string $cacheSuffix
178+
* @param SerializerInterface|null $serializer
171179
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
172180
*/
173181
public function __construct(
@@ -181,7 +189,8 @@ public function __construct(
181189
\Psr\Log\LoggerInterface $logger,
182190
ReadFactory $readFactory,
183191
\Magento\Framework\View\Design\ThemeInterface $theme = null,
184-
$cacheSuffix = ''
192+
$cacheSuffix = '',
193+
SerializerInterface $serializer = null
185194
) {
186195
$this->theme = $theme ?: $design->getDesignTheme();
187196
$this->scope = $scopeResolver->getScope();
@@ -193,6 +202,7 @@ public function __construct(
193202
$this->logger = $logger;
194203
$this->readFactory = $readFactory;
195204
$this->cacheSuffix = $cacheSuffix;
205+
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class);
196206
}
197207

198208
/**
@@ -425,12 +435,12 @@ public function load($handles = [])
425435

426436
$this->addHandle($handles);
427437

428-
$cacheId = $this->getCacheId();
429-
$cacheIdPageLayout = $cacheId . '_' . self::PAGE_LAYOUT_CACHE_SUFFIX;
438+
$cacheId = $this->getCacheId() . '_' . self::PAGE_LAYOUT_CACHE_SUFFIX;
430439
$result = $this->_loadCache($cacheId);
431-
if ($result) {
432-
$this->addUpdate($result);
433-
$this->pageLayout = $this->_loadCache($cacheIdPageLayout);
440+
if ($result !== false && $result !== null) {
441+
$data = $this->serializer->unserialize($result);
442+
$this->pageLayout = $data["pageLayout"];
443+
$this->addUpdate($data["layout"]);
434444
foreach ($this->getHandles() as $handle) {
435445
$this->allHandles[$handle] = $this->handleProcessed;
436446
}
@@ -443,8 +453,13 @@ public function load($handles = [])
443453

444454
$layout = $this->asString();
445455
$this->_validateMergedLayout($cacheId, $layout);
446-
$this->_saveCache($layout, $cacheId, $this->getHandles());
447-
$this->_saveCache((string)$this->pageLayout, $cacheIdPageLayout, $this->getHandles());
456+
457+
$data = [
458+
"pageLayout" => (string)$this->pageLayout,
459+
"layout" => $layout
460+
];
461+
$this->_saveCache($this->serializer->serialize($data), $cacheId, $this->getHandles());
462+
448463
return $this;
449464
}
450465

lib/internal/Magento/Framework/View/Test/Unit/Model/Layout/MergeTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
use Magento\Framework\Phrase;
1111
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1212

13+
/**
14+
* Class MergeTest
15+
*
16+
* @package Magento\Framework\View\Test\Unit\Model\Layout
17+
*/
1318
class MergeTest extends \PHPUnit\Framework\TestCase
1419
{
1520
/**
@@ -27,11 +32,21 @@ class MergeTest extends \PHPUnit\Framework\TestCase
2732
*/
2833
private $scope;
2934

35+
/**
36+
* @var \Magento\Framework\Cache\FrontendInterface|\PHPUnit_Framework_MockObject_MockObject
37+
*/
38+
private $cache;
39+
3040
/**
3141
* @var \Magento\Framework\View\Model\Layout\Update\Validator|\PHPUnit_Framework_MockObject_MockObject
3242
*/
3343
private $layoutValidator;
3444

45+
/**
46+
* @var \Magento\Framework\Serialize\SerializerInterface|\PHPUnit_Framework_MockObject_MockObject
47+
*/
48+
private $serializer;
49+
3550
/**
3651
* @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
3752
*/
@@ -47,10 +62,12 @@ protected function setUp()
4762
$this->objectManagerHelper = new ObjectManager($this);
4863

4964
$this->scope = $this->getMockForAbstractClass(\Magento\Framework\Url\ScopeInterface::class);
65+
$this->cache = $this->getMockForAbstractClass(\Magento\Framework\Cache\FrontendInterface::class);
5066
$this->layoutValidator = $this->getMockBuilder(\Magento\Framework\View\Model\Layout\Update\Validator::class)
5167
->disableOriginalConstructor()
5268
->getMock();
5369
$this->logger = $this->getMockForAbstractClass(\Psr\Log\LoggerInterface::class);
70+
$this->serializer = $this->getMockForAbstractClass(\Magento\Framework\Serialize\SerializerInterface::class);
5471
$this->appState = $this->getMockBuilder(\Magento\Framework\App\State::class)
5572
->disableOriginalConstructor()
5673
->getMock();
@@ -59,9 +76,11 @@ protected function setUp()
5976
\Magento\Framework\View\Model\Layout\Merge::class,
6077
[
6178
'scope' => $this->scope,
79+
'cache' => $this->cache,
6280
'layoutValidator' => $this->layoutValidator,
6381
'logger' => $this->logger,
6482
'appState' => $this->appState,
83+
'serializer' => $this->serializer,
6584
]
6685
);
6786
}
@@ -92,4 +111,33 @@ public function testValidateMergedLayoutThrowsException()
92111

93112
$this->model->load();
94113
}
114+
115+
/**
116+
* Test that merged layout is saved to cache if it wasn't cached before.
117+
*/
118+
public function testSaveToCache()
119+
{
120+
$this->scope->expects($this->once())->method('getId')->willReturn(1);
121+
$this->cache->expects($this->once())->method('save');
122+
123+
$this->model->load();
124+
}
125+
126+
/**
127+
* Test that merged layout is not re-saved to cache when it was loaded from cache.
128+
*/
129+
public function testNoSaveToCacheWhenCachePresent()
130+
{
131+
$cacheValue = [
132+
"pageLayout" => "1column",
133+
"layout" => "<body></body>"
134+
];
135+
136+
$this->scope->expects($this->once())->method('getId')->willReturn(1);
137+
$this->cache->expects($this->once())->method('load')->willReturn(json_encode($cacheValue));
138+
$this->serializer->expects($this->once())->method('unserialize')->willReturn($cacheValue);
139+
$this->cache->expects($this->never())->method('save');
140+
141+
$this->model->load();
142+
}
95143
}

0 commit comments

Comments
 (0)