diff --git a/app/code/Magento/Ui/Model/Manager.php b/app/code/Magento/Ui/Model/Manager.php index 348b7d398800d71bc763eecb719313b89f44252e..a2a4f05eb8bad7b2a4e379b552e245df9c00e13c 100644 --- a/app/code/Magento/Ui/Model/Manager.php +++ b/app/code/Magento/Ui/Model/Manager.php @@ -1,6 +1,6 @@ <?php /** - * Copyright © 2016 Magento. All rights reserved. + * Copyright © 2017 Magento. All rights reserved. * See COPYING.txt for license details. */ namespace Magento\Ui\Model; @@ -17,6 +17,8 @@ use Magento\Framework\View\Element\UiComponent\Config\ManagerInterface; use Magento\Framework\View\Element\UiComponent\Config\Provider\Component\Definition as ComponentDefinition; use Magento\Framework\View\Element\UiComponent\Config\ReaderFactory; use Magento\Framework\View\Element\UiComponent\Config\UiReaderInterface; +use Magento\Framework\Serialize\SerializerInterface; +use Magento\Framework\App\ObjectManager; /** * Class Manager @@ -94,6 +96,11 @@ class Manager implements ManagerInterface */ protected $uiReader; + /** + * @var SerializerInterface + */ + private $serializer; + /** * @param ComponentDefinition $componentConfigProvider * @param DomMergerInterface $domMerger @@ -102,6 +109,7 @@ class Manager implements ManagerInterface * @param AggregatedFileCollectorFactory $aggregatedFileCollectorFactory * @param CacheInterface $cache * @param InterpreterInterface $argumentInterpreter + * @param SerializerInterface|null $serializer */ public function __construct( ComponentDefinition $componentConfigProvider, @@ -110,7 +118,8 @@ class Manager implements ManagerInterface ArrayObjectFactory $arrayObjectFactory, AggregatedFileCollectorFactory $aggregatedFileCollectorFactory, CacheInterface $cache, - InterpreterInterface $argumentInterpreter + InterpreterInterface $argumentInterpreter, + SerializerInterface $serializer = null ) { $this->componentConfigProvider = $componentConfigProvider; $this->domMerger = $domMerger; @@ -120,6 +129,7 @@ class Manager implements ManagerInterface $this->aggregatedFileCollectorFactory = $aggregatedFileCollectorFactory; $this->cache = $cache; $this->argumentInterpreter = $argumentInterpreter; + $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class); } /** @@ -164,9 +174,14 @@ class Manager implements ManagerInterface $cachedPool = $this->cache->load($cacheID); if ($cachedPool === false) { $this->prepare($name); - $this->cache->save($this->componentsPool->serialize(), $cacheID); + $this->cache->save( + $this->serializer->serialize($this->componentsPool->getArrayCopy()), + $cacheID + ); } else { - $this->componentsPool->unserialize($cachedPool); + $this->componentsPool->exchangeArray( + $this->serializer->unserialize($cachedPool) + ); } $this->componentsData->offsetSet($name, $this->componentsPool); $this->componentsData->offsetSet($name, $this->evaluateComponentArguments($this->getData($name))); diff --git a/app/code/Magento/Ui/Test/Unit/Model/ManagerTest.php b/app/code/Magento/Ui/Test/Unit/Model/ManagerTest.php index 37f97af4597ac069312dbb1e1fb404e36cbdf12f..0e50950cc953eb4a24a2b36ff114a588b700d573 100644 --- a/app/code/Magento/Ui/Test/Unit/Model/ManagerTest.php +++ b/app/code/Magento/Ui/Test/Unit/Model/ManagerTest.php @@ -1,6 +1,6 @@ <?php /** - * Copyright © 2016 Magento. All rights reserved. + * Copyright © 2017 Magento. All rights reserved. * See COPYING.txt for license details. */ @@ -75,6 +75,9 @@ class ManagerTest extends \PHPUnit_Framework_TestCase */ protected $aggregatedFileCollectorFactory; + /** @var \Magento\Framework\Serialize\SerializerInterface|\PHPUnit_Framework_MockObject_MockObject */ + private $serializer; + protected function setUp() { $this->componentConfigProvider = $this->getMockBuilder( @@ -105,6 +108,24 @@ class ManagerTest extends \PHPUnit_Framework_TestCase ->getMockForAbstractClass(); $this->argumentInterpreter = $this->getMockBuilder(\Magento\Framework\Data\Argument\InterpreterInterface::class) ->getMockForAbstractClass(); + $this->serializer = $this->getMockBuilder( + \Magento\Framework\Serialize\SerializerInterface::class + )->getMockForAbstractClass(); + $this->serializer->expects($this->any()) + ->method('serialize') + ->willReturnCallback( + function ($value) { + return json_encode($value); + } + ); + $this->serializer->expects($this->any()) + ->method('unserialize') + ->willReturnCallback( + function ($value) { + return json_decode($value, true); + } + ); + $this->manager = new Manager( $this->componentConfigProvider, $this->domMerger, @@ -112,7 +133,8 @@ class ManagerTest extends \PHPUnit_Framework_TestCase $this->arrayObjectFactory, $this->aggregatedFileCollectorFactory, $this->cacheConfig, - $this->argumentInterpreter + $this->argumentInterpreter, + $this->serializer ); } @@ -192,7 +214,7 @@ class ManagerTest extends \PHPUnit_Framework_TestCase [ 'test_component1', new \ArrayObject(), - $cachedData->serialize(), + json_encode($cachedData->getArrayCopy()), [], [ 'test_component1' => [ diff --git a/lib/internal/Magento/Framework/App/Http/Context.php b/lib/internal/Magento/Framework/App/Http/Context.php index 4146dac725f03a70971e85d6a29b27803e795412..59ab6f7602c3f4cc3369584ff5fa09f9629fae6e 100644 --- a/lib/internal/Magento/Framework/App/Http/Context.php +++ b/lib/internal/Magento/Framework/App/Http/Context.php @@ -1,6 +1,6 @@ <?php /** - * Copyright © 2016 Magento. All rights reserved. + * Copyright © 2017 Magento. All rights reserved. * See COPYING.txt for license details. */ namespace Magento\Framework\App\Http; @@ -27,6 +27,16 @@ class Context */ protected $default = []; + /** + * @param array $data + * @param array $default + */ + public function __construct(array $data = [], array $default = []) + { + $this->data = $data; + $this->default = $default; + } + /** * Data setter * @@ -99,4 +109,17 @@ class Context } return null; } + + /** + * Get data and default data in "key-value" format + * + * @return array + */ + public function toArray() + { + return [ + 'data' => $this->data, + 'default' => $this->default + ]; + } } diff --git a/lib/internal/Magento/Framework/App/PageCache/Kernel.php b/lib/internal/Magento/Framework/App/PageCache/Kernel.php index 8a83592bc322e86c046b68480f815878f3de8855..02d6646c67d0f76ee4a527bf29e7d0f165a8f822 100644 --- a/lib/internal/Magento/Framework/App/PageCache/Kernel.php +++ b/lib/internal/Magento/Framework/App/PageCache/Kernel.php @@ -1,12 +1,10 @@ <?php /** - * Copyright © 2016 Magento. All rights reserved. + * Copyright © 2017 Magento. All rights reserved. * See COPYING.txt for license details. */ namespace Magento\Framework\App\PageCache; -use Magento\Framework\App\ObjectManager; - /** * Builtin cache processor */ @@ -34,19 +32,76 @@ class Kernel */ private $fullPageCache; + /** + * @var \Magento\Framework\Serialize\SerializerInterface + */ + private $serializer; + + /** + * @var \Magento\Framework\App\Http\Context + */ + private $context; + + /** + * @var \Magento\Framework\App\Http\ContextFactory + */ + private $contextFactory; + + /** + * @var \Magento\Framework\App\Response\HttpFactory + */ + private $httpFactory; + /** * @param Cache $cache * @param Identifier $identifier * @param \Magento\Framework\App\Request\Http $request + * @param \Magento\Framework\App\Http\Context|null $context + * @param \Magento\Framework\App\Http\ContextFactory|null $contextFactory + * @param \Magento\Framework\App\Response\HttpFactory|null $httpFactory + * @param \Magento\Framework\Serialize\SerializerInterface|null $serializer */ public function __construct( \Magento\Framework\App\PageCache\Cache $cache, \Magento\Framework\App\PageCache\Identifier $identifier, - \Magento\Framework\App\Request\Http $request + \Magento\Framework\App\Request\Http $request, + \Magento\Framework\App\Http\Context $context = null, + \Magento\Framework\App\Http\ContextFactory $contextFactory = null, + \Magento\Framework\App\Response\HttpFactory $httpFactory = null, + \Magento\Framework\Serialize\SerializerInterface $serializer = null ) { $this->cache = $cache; $this->identifier = $identifier; $this->request = $request; + + if ($context) { + $this->context = $context; + } else { + $this->context = \Magento\Framework\App\ObjectManager::getInstance()->get( + \Magento\Framework\App\Http\Context::class + ); + } + if ($contextFactory) { + $this->contextFactory = $contextFactory; + } else { + $this->contextFactory = \Magento\Framework\App\ObjectManager::getInstance()->get( + \Magento\Framework\App\Http\ContextFactory::class + ); + } + if ($httpFactory) { + $this->httpFactory = $httpFactory; + } else { + $this->httpFactory = \Magento\Framework\App\ObjectManager::getInstance()->get( + \Magento\Framework\App\Response\HttpFactory::class + ); + } + if ($serializer) { + $this->serializer = $serializer; + } else { + $this->serializer = \Magento\Framework\App\ObjectManager::getInstance()->get( + \Magento\Framework\Serialize\SerializerInterface::class + ); + } } /** @@ -57,7 +112,12 @@ class Kernel public function load() { if ($this->request->isGet() || $this->request->isHead()) { - return unserialize($this->getCache()->load($this->identifier->getValue())); + $responseData = $this->serializer->unserialize($this->getCache()->load($this->identifier->getValue())); + if (!$responseData) { + return false; + } + + return $this->buildResponse($responseData); } return false; } @@ -84,11 +144,63 @@ class Kernel if (!headers_sent()) { header_remove('Set-Cookie'); } - $this->getCache()->save(serialize($response), $this->identifier->getValue(), $tags, $maxAge); + + $this->getCache()->save( + $this->serializer->serialize($this->getPreparedData($response)), + $this->identifier->getValue(), + $tags, + $maxAge + ); } } } + /** + * Get prepared data for storage in the cache. + * + * @param \Magento\Framework\App\Response\Http $response + * @return array + */ + private function getPreparedData(\Magento\Framework\App\Response\Http $response) + { + return [ + 'content' => $response->getContent(), + 'status_code' => $response->getStatusCode(), + 'headers' => $response->getHeaders()->toArray(), + 'context' => $this->context->toArray() + ]; + + } + + /** + * Build response using response data. + * + * @param array $responseData + * @return \Magento\Framework\App\Response\Http + */ + private function buildResponse($responseData) + { + $context = $this->contextFactory->create( + [ + 'data' => $responseData['context']['data'], + 'default' => $responseData['context']['default'] + ] + ); + + $response = $this->httpFactory->create( + [ + 'context' => $context + ] + ); + $response->setStatusCode($responseData['status_code']); + $response->setContent($responseData['content']); + foreach ($responseData['headers'] as $headerKey => $headerValue) { + $response->setHeader($headerKey, $headerValue, true); + } + + return $response; + } + /** * TODO: Workaround to support backwards compatibility, will rework to use Dependency Injection in MAGETWO-49547 * @@ -97,7 +209,9 @@ class Kernel private function getCache() { if (!$this->fullPageCache) { - $this->fullPageCache = ObjectManager::getInstance()->get(\Magento\PageCache\Model\Cache\Type::class); + $this->fullPageCache = \Magento\Framework\App\ObjectManager::getInstance()->get( + \Magento\PageCache\Model\Cache\Type::class + ); } return $this->fullPageCache; } diff --git a/lib/internal/Magento/Framework/App/Test/Unit/Http/ContextTest.php b/lib/internal/Magento/Framework/App/Test/Unit/Http/ContextTest.php index 05f589e0dc61a9299f717aeab0905f8b205fdafe..8ce4f006861e8d3cc518df48a0882cda22a15b14 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/Http/ContextTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/Http/ContextTest.php @@ -1,6 +1,6 @@ <?php /** - * Copyright © 2016 Magento. All rights reserved. + * Copyright © 2017 Magento. All rights reserved. * See COPYING.txt for license details. */ @@ -64,4 +64,19 @@ class ContextTest extends \PHPUnit_Framework_TestCase ksort($data); $this->assertEquals(sha1(serialize($data)), $this->object->getVaryString()); } + + public function testToArray() + { + $newObject = new \Magento\Framework\App\Http\Context(['key' => 'value']); + + $newObject->setValue('key1', 'value1', 'default1'); + $newObject->setValue('key2', 'value2', 'default2'); + $this->assertEquals( + [ + 'data' => ['key' => 'value', 'key1' => 'value1', 'key2' => 'value2'], + 'default' => ['key1' => 'default1', 'key2' => 'default2'] + ], + $newObject->toArray() + ); + } } diff --git a/lib/internal/Magento/Framework/App/Test/Unit/PageCache/KernelTest.php b/lib/internal/Magento/Framework/App/Test/Unit/PageCache/KernelTest.php index 81b828b3f938c45333a5ac8b096d8b23d013a81a..7e65b174abdd97a3adc231b415c96a62f634e194 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/PageCache/KernelTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/PageCache/KernelTest.php @@ -1,12 +1,17 @@ <?php /** - * Copyright © 2016 Magento. All rights reserved. + * Copyright © 2017 Magento. All rights reserved. * See COPYING.txt for license details. */ namespace Magento\Framework\App\Test\Unit\PageCache; use \Magento\Framework\App\PageCache\Kernel; +use \Magento\Framework\App\Http\ContextFactory; +use \Magento\Framework\App\Response\HttpFactory; +/** + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + */ class KernelTest extends \PHPUnit_Framework_TestCase { /** @var Kernel */ @@ -27,39 +32,136 @@ class KernelTest extends \PHPUnit_Framework_TestCase /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\PageCache\Model\Cache\Type */ private $fullPageCacheMock; + /** @var \Magento\Framework\App\Response\Http|\PHPUnit_Framework_MockObject_MockObject */ + private $httpResponseMock; + + /** @var ContextFactory|\PHPUnit_Framework_MockObject_MockObject */ + private $contextFactoryMock; + + /** @var HttpFactory|\PHPUnit_Framework_MockObject_MockObject */ + private $httpFactoryMock; + + /** @var \Magento\Framework\Serialize\SerializerInterface|\PHPUnit_Framework_MockObject_MockObject */ + private $serializer; + + /** @var \Magento\Framework\App\Http\Context|\PHPUnit_Framework_MockObject_MockObject */ + private $contextMock; + /** * Setup */ protected function setUp() { + $headersMock = $this->getMock(\Zend\Http\Headers::class, [], [], '', false); $this->cacheMock = $this->getMock(\Magento\Framework\App\PageCache\Cache::class, [], [], '', false); $this->fullPageCacheMock = $this->getMock(\Magento\PageCache\Model\Cache\Type::class, [], [], '', false); - $this->identifierMock = - $this->getMock(\Magento\Framework\App\PageCache\Identifier::class, [], [], '', false); + $this->contextMock = $this->getMock(\Magento\Framework\App\Http\Context::class, [], [], '', false); + $this->httpResponseMock = $this->getMock(\Magento\Framework\App\Response\Http::class, [], [], '', false); + $this->identifierMock = $this->getMock(\Magento\Framework\App\PageCache\Identifier::class, [], [], '', false); $this->requestMock = $this->getMock(\Magento\Framework\App\Request\Http::class, [], [], '', false); - $this->kernel = new Kernel($this->cacheMock, $this->identifierMock, $this->requestMock); + $this->serializer = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class, [], [], '', false); + $this->responseMock = $this->getMock(\Magento\Framework\App\Response\Http::class, [], [], '', false); + $this->contextFactoryMock = $this->getMock(ContextFactory::class, ['create'], [], '', false); + $this->httpFactoryMock = $this->getMock(HttpFactory::class, ['create'], [], '', false); + $this->responseMock->expects($this->any())->method('getHeaders')->willReturn($headersMock); + + $this->kernel = new Kernel( + $this->cacheMock, + $this->identifierMock, + $this->requestMock, + $this->contextMock, + $this->contextFactoryMock, + $this->httpFactoryMock, + $this->serializer + ); $reflection = new \ReflectionClass(\Magento\Framework\App\PageCache\Kernel::class); $reflectionProperty = $reflection->getProperty('fullPageCache'); $reflectionProperty->setAccessible(true); $reflectionProperty->setValue($this->kernel, $this->fullPageCacheMock); + } - $this->responseMock = $this->getMockBuilder( - \Magento\Framework\App\Response\Http::class - )->setMethods( - ['getHeader', 'getHttpResponseCode', 'setNoCacheHeaders', 'clearHeader', '__wakeup'] - )->disableOriginalConstructor()->getMock(); + /** + * @dataProvider dataProviderForResultWithCachedData + * @param string $id + * @param mixed $cache + * @param bool $isGet + * @param bool $isHead + */ + public function testLoadWithCachedData($id, $cache, $isGet, $isHead) + { + $this->serializer->expects($this->once()) + ->method('unserialize') + ->willReturnCallback( + function ($value) { + return json_decode($value, true); + } + ); + + $this->contextFactoryMock + ->expects($this->once()) + ->method('create') + ->with( + [ + 'data' => ['context_data'], + 'default' => ['context_default_data'] + ] + ) + ->willReturn($this->contextMock); + + $this->httpFactoryMock + ->expects($this->once()) + ->method('create') + ->with(['context' => $this->contextMock]) + ->willReturn($this->httpResponseMock); + + $this->requestMock->expects($this->once())->method('isGet')->will($this->returnValue($isGet)); + $this->requestMock->expects($this->any())->method('isHead')->will($this->returnValue($isHead)); + $this->fullPageCacheMock->expects( + $this->any() + )->method( + 'load' + )->with( + $this->equalTo($id) + )->will( + $this->returnValue(json_encode($cache)) + ); + $this->httpResponseMock->expects($this->once())->method('setStatusCode')->with($cache['status_code']); + $this->httpResponseMock->expects($this->once())->method('setContent')->with($cache['content']); + $this->httpResponseMock->expects($this->once())->method('setHeader')->with(0, 'header', true); + $this->identifierMock->expects($this->any())->method('getValue')->will($this->returnValue($id)); + $this->assertEquals($this->httpResponseMock, $this->kernel->load()); + } + + /** + * @return array + */ + public function dataProviderForResultWithCachedData() + { + $data = [ + 'context' => [ + 'data' => ['context_data'], + 'default' => ['context_default_data'] + ], + 'status_code' => 'status_code', + 'content' => 'content', + 'headers' => ['header'] + ]; + + return [ + ['existing key', $data, true, false], + ['existing key', $data, false, true], + ]; } /** - * @dataProvider loadProvider - * @param mixed $expected + * @dataProvider dataProviderForResultWithoutCachedData * @param string $id * @param mixed $cache * @param bool $isGet * @param bool $isHead */ - public function testLoad($expected, $id, $cache, $isGet, $isHead) + public function testLoadWithoutCachedData($id, $cache, $isGet, $isHead) { $this->requestMock->expects($this->once())->method('isGet')->will($this->returnValue($isGet)); $this->requestMock->expects($this->any())->method('isHead')->will($this->returnValue($isHead)); @@ -70,31 +172,21 @@ class KernelTest extends \PHPUnit_Framework_TestCase )->with( $this->equalTo($id) )->will( - $this->returnValue(serialize($cache)) + $this->returnValue(json_encode($cache)) ); $this->identifierMock->expects($this->any())->method('getValue')->will($this->returnValue($id)); - $this->assertEquals($expected, $this->kernel->load()); + $this->assertEquals(false, $this->kernel->load()); } /** * @return array */ - public function loadProvider() + public function dataProviderForResultWithoutCachedData() { - $data = [1, 2, 3]; return [ - [$data, 'existing key', $data, true, false], - [$data, 'existing key', $data, false, true], - [ - new \Magento\Framework\DataObject($data), - 'existing key', - new \Magento\Framework\DataObject($data), - true, - false - ], - [false, 'existing key', $data, false, false], - [false, 'non existing key', false, true, false], - [false, 'non existing key', false, false, false] + ['existing key', [], false, false], + ['non existing key', false, true, false], + ['non existing key', false, false, false] ]; } @@ -104,6 +196,14 @@ class KernelTest extends \PHPUnit_Framework_TestCase */ public function testProcessSaveCache($httpCode, $at) { + $this->serializer->expects($this->once()) + ->method('serialize') + ->willReturnCallback( + function ($value) { + return json_encode($value); + } + ); + $cacheControlHeader = \Zend\Http\Header\CacheControl::fromString( 'Cache-Control: public, max-age=100, s-maxage=100' ); diff --git a/lib/internal/Magento/Framework/View/Layout.php b/lib/internal/Magento/Framework/View/Layout.php index 3334967c8da8327cb1de7eb396fb46978fc0c08c..ac3938c94c5c7432ba8095ea98c29162afb8b954 100755 --- a/lib/internal/Magento/Framework/View/Layout.php +++ b/lib/internal/Magento/Framework/View/Layout.php @@ -1,6 +1,6 @@ <?php /** - * Copyright © 2016 Magento. All rights reserved. + * Copyright © 2017 Magento. All rights reserved. * See COPYING.txt for license details. */ namespace Magento\Framework\View; @@ -8,11 +8,13 @@ namespace Magento\Framework\View; use Magento\Framework\Cache\FrontendInterface; use Magento\Framework\Event\ManagerInterface; use Magento\Framework\Message\ManagerInterface as MessageManagerInterface; +use Magento\Framework\Serialize\SerializerInterface; use Magento\Framework\View\Layout\Element; use Magento\Framework\View\Layout\ScheduledStructure; use Magento\Framework\App\State as AppState; use Psr\Log\LoggerInterface as Logger; use Magento\Framework\Exception\LocalizedException; +use Magento\Framework\App\ObjectManager; /** * Layout model @@ -165,6 +167,11 @@ class Layout extends \Magento\Framework\Simplexml\Config implements \Magento\Fra */ protected $logger; + /** + * @var SerializerInterface + */ + private $serializer; + /** * @param Layout\ProcessorFactory $processorFactory * @param ManagerInterface $eventManager @@ -179,6 +186,7 @@ class Layout extends \Magento\Framework\Simplexml\Config implements \Magento\Fra * @param \Magento\Framework\App\State $appState * @param \Psr\Log\LoggerInterface $logger * @param bool $cacheable + * @param SerializerInterface|null $serializer */ public function __construct( Layout\ProcessorFactory $processorFactory, @@ -193,10 +201,12 @@ class Layout extends \Magento\Framework\Simplexml\Config implements \Magento\Fra Layout\Generator\ContextFactory $generatorContextFactory, AppState $appState, Logger $logger, - $cacheable = true + $cacheable = true, + SerializerInterface $serializer = null ) { $this->_elementClass = \Magento\Framework\View\Layout\Element::class; $this->_renderingOutput = new \Magento\Framework\DataObject(); + $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class); $this->_processorFactory = $processorFactory; $this->_eventManager = $eventManager; @@ -308,12 +318,19 @@ class Layout extends \Magento\Framework\Simplexml\Config implements \Magento\Fra $cacheId = 'structure_' . $this->getUpdate()->getCacheId(); $result = $this->cache->load($cacheId); if ($result) { - $this->readerContext = unserialize($result); + $data = $this->serializer->unserialize($result); + $this->getReaderContext()->getPageConfigStructure()->populateWithArray($data['pageConfigStructure']); + $this->getReaderContext()->getScheduledStructure()->populateWithArray($data['scheduledStructure']); } else { \Magento\Framework\Profiler::start('build_structure'); $this->readerPool->interpret($this->getReaderContext(), $this->getNode()); \Magento\Framework\Profiler::stop('build_structure'); - $this->cache->save(serialize($this->getReaderContext()), $cacheId, $this->getUpdate()->getHandles()); + + $data = [ + 'pageConfigStructure' => $this->getReaderContext()->getPageConfigStructure()->__toArray(), + 'scheduledStructure' => $this->getReaderContext()->getScheduledStructure()->__toArray(), + ]; + $this->cache->save($this->serializer->serialize($data), $cacheId, $this->getUpdate()->getHandles()); } $generatorContext = $this->generatorContextFactory->create( diff --git a/lib/internal/Magento/Framework/View/Layout/ScheduledStructure.php b/lib/internal/Magento/Framework/View/Layout/ScheduledStructure.php index cf1981335d6be9a6b12b38944623d727abe6fdeb..117fb8278145f89f22674b7ab25d21fab57946ff 100644 --- a/lib/internal/Magento/Framework/View/Layout/ScheduledStructure.php +++ b/lib/internal/Magento/Framework/View/Layout/ScheduledStructure.php @@ -1,6 +1,6 @@ <?php /** - * Copyright © 2016 Magento. All rights reserved. + * Copyright © 2017 Magento. All rights reserved. * See COPYING.txt for license details. */ namespace Magento\Framework\View\Layout; @@ -19,6 +19,23 @@ class ScheduledStructure const ELEMENT_IS_AFTER = 'isAfter'; /**#@-*/ + /** + * Map of class properties. + * + * @var array + */ + private $serializableProperties = [ + 'scheduledStructure', + 'scheduledData', + 'scheduledElements', + 'scheduledMoves', + 'scheduledRemoves', + 'scheduledIfconfig', + 'scheduledPaths', + 'elementsToSort', + 'brokenParent', + ]; + /** * Information about structural elements, scheduled for creation * @@ -84,18 +101,10 @@ class ScheduledStructure /** * @param array $data - * - * @SuppressWarnings(PHPMD.NPathComplexity) */ public function __construct(array $data = []) { - $this->scheduledStructure = isset($data['scheduledStructure']) ? $data['scheduledStructure'] : []; - $this->scheduledData = isset($data['scheduledData']) ? $data['scheduledData'] : []; - $this->scheduledElements = isset($data['scheduledElements']) ? $data['scheduledElements'] : []; - $this->scheduledMoves = isset($data['scheduledMoves']) ? $data['scheduledMoves'] : []; - $this->scheduledRemoves = isset($data['scheduledRemoves']) ? $data['scheduledRemoves'] : []; - $this->scheduledIfconfig = isset($data['scheduledIfconfig']) ? $data['scheduledIfconfig'] : []; - $this->scheduledPaths = isset($data['scheduledPaths']) ? $data['scheduledPaths'] : []; + $this->populateWithArray($data); } /** @@ -531,4 +540,44 @@ class ScheduledStructure $this->scheduledElements = []; $this->scheduledStructure = []; } + + /** + * Reformat 'Layout scheduled structure' to array. + * + * @return array + */ + public function __toArray() + { + $result = []; + foreach ($this->serializableProperties as $property) { + $result[$property] = $this->{$property}; + } + + return $result; + } + + /** + * Update 'Layout scheduled structure' data. + * + * @param array $data + * @return void + */ + public function populateWithArray(array $data) + { + foreach ($this->serializableProperties as $property) { + $this->{$property} = $this->getArrayValueByKey($property, $data); + } + } + + /** + * Get value from array by key. + * + * @param string $key + * @param array $array + * @return array + */ + private function getArrayValueByKey($key, array $array) + { + return isset($array[$key]) ? $array[$key] : []; + } } diff --git a/lib/internal/Magento/Framework/View/Page/Config/Structure.php b/lib/internal/Magento/Framework/View/Page/Config/Structure.php index 4ab89319f6d82aeeaad6e637076ed7e181fab2d4..d7fe402044e8ffd589e2669eb0c0611cdb4b7f61 100644 --- a/lib/internal/Magento/Framework/View/Page/Config/Structure.php +++ b/lib/internal/Magento/Framework/View/Page/Config/Structure.php @@ -1,6 +1,6 @@ <?php /** - * Copyright © 2016 Magento. All rights reserved. + * Copyright © 2017 Magento. All rights reserved. * See COPYING.txt for license details. */ @@ -12,6 +12,22 @@ namespace Magento\Framework\View\Page\Config; */ class Structure { + /** + * Map of class properties. + * + * @var array + */ + private $serializableProperties = [ + 'assets', + 'removeAssets', + 'title', + 'metadata', + 'elementAttributes', + 'removeElementAttributes', + 'bodyClasses', + 'isBodyClassesDeleted', + ]; + /** * Information assets elements on page * @@ -194,4 +210,44 @@ class Structure { return $this->assets; } + + /** + * Reformat 'Page config structure' to array. + * + * @return array + */ + public function __toArray() + { + $result = []; + foreach ($this->serializableProperties as $property) { + $result[$property] = $this->{$property}; + } + + return $result; + } + + /** + * Update 'Page config structure' data. + * + * @param array $data + * @return void + */ + public function populateWithArray(array $data) + { + foreach ($this->serializableProperties as $property) { + $this->{$property} = $this->getArrayValueByKey($property, $data); + } + } + + /** + * Get value from array by key. + * + * @param string $key + * @param array $array + * @return array + */ + private function getArrayValueByKey($key, array $array) + { + return isset($array[$key]) ? $array[$key] : []; + } } diff --git a/lib/internal/Magento/Framework/View/Test/Unit/LayoutTest.php b/lib/internal/Magento/Framework/View/Test/Unit/LayoutTest.php index 413b1e4cd1ac83008600dbf062d038796a408642..640ece658edaa0620c3c470206914b431cf403bc 100755 --- a/lib/internal/Magento/Framework/View/Test/Unit/LayoutTest.php +++ b/lib/internal/Magento/Framework/View/Test/Unit/LayoutTest.php @@ -1,10 +1,12 @@ <?php /** - * Copyright © 2016 Magento. All rights reserved. + * Copyright © 2017 Magento. All rights reserved. * See COPYING.txt for license details. */ namespace Magento\Framework\View\Test\Unit; +use Magento\Framework\Serialize\SerializerInterface; + /** * @SuppressWarnings(PHPMD.TooManyFields) * @SuppressWarnings(PHPMD.CouplingBetweenObjects) @@ -81,6 +83,16 @@ class LayoutTest extends \PHPUnit_Framework_TestCase */ protected $readerContextMock; + /** + * @var \Magento\Framework\View\Page\Config\Structure|\PHPUnit_Framework_MockObject_MockObject + */ + private $pageConfigStructure; + + /** + * @var \Magento\Framework\View\Layout\ScheduledStructure|\PHPUnit_Framework_MockObject_MockObject + */ + private $layoutScheduledSructure; + /** * @var \Magento\Framework\View\Layout\Generator\ContextFactory|\PHPUnit_Framework_MockObject_MockObject */ @@ -96,6 +108,11 @@ class LayoutTest extends \PHPUnit_Framework_TestCase */ protected $loggerMock; + /** + * @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $serializer; + protected function setUp() { $this->structureMock = $this->getMockBuilder(\Magento\Framework\View\Layout\Data\Structure::class) @@ -141,9 +158,22 @@ class LayoutTest extends \PHPUnit_Framework_TestCase $this->readerContextFactoryMock = $this->getMockBuilder( \Magento\Framework\View\Layout\Reader\ContextFactory::class )->disableOriginalConstructor()->getMock(); + + $this->pageConfigStructure = $this->getMockBuilder(\Magento\Framework\View\Page\Config\Structure::class) + ->setMethods(['__toArray', 'populateWithArray']) + ->getMock(); + $this->layoutScheduledSructure = $this->getMockBuilder(\Magento\Framework\View\Layout\ScheduledStructure::class) + ->setMethods(['__toArray', 'populateWithArray']) + ->getMock(); $this->readerContextMock = $this->getMockBuilder(\Magento\Framework\View\Layout\Reader\Context::class) + ->setMethods(['getPageConfigStructure', 'getScheduledStructure']) ->disableOriginalConstructor() ->getMock(); + $this->readerContextMock->expects($this->any())->method('getPageConfigStructure') + ->willReturn($this->pageConfigStructure); + $this->readerContextMock->expects($this->any())->method('getScheduledStructure') + ->willReturn($this->layoutScheduledSructure); + $this->generatorContextFactoryMock = $this->getMockBuilder( \Magento\Framework\View\Layout\Generator\ContextFactory::class ) @@ -154,6 +184,15 @@ class LayoutTest extends \PHPUnit_Framework_TestCase ->getMock(); $this->loggerMock = $this->getMockBuilder(\Psr\Log\LoggerInterface::class) ->getMock(); + $this->serializer = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); + $this->serializer->expects($this->any())->method('serialize') + ->willReturnCallback(function ($value) { + return json_encode($value); + }); + $this->serializer->expects($this->any())->method('unserialize') + ->willReturnCallback(function ($value) { + return json_decode($value, true); + }); $this->model = new \Magento\Framework\View\Layout( $this->processorFactoryMock, @@ -168,7 +207,8 @@ class LayoutTest extends \PHPUnit_Framework_TestCase $this->generatorContextFactoryMock, $this->appStateMock, $this->loggerMock, - true + true, + $this->serializer ); } @@ -735,9 +775,32 @@ class LayoutTest extends \PHPUnit_Framework_TestCase ->with($this->readerContextMock, $xml) ->willReturnSelf(); + $pageConfigStructureData = [ + 'field_1' => 123, + 'field_2' => 'text', + 'field_3' => [ + 'field_3_1' => '1244', + 'field_3_2' => null, + 'field_3_3' => false, + ] + ]; + $this->pageConfigStructure->expects($this->any())->method('__toArray') + ->willReturn($pageConfigStructureData); + + $layoutScheduledStructureData = [ + 'field_1' => 1283, + 'field_2' => 'text_qwertyuiop[]asdfghjkl;' + ]; + $this->layoutScheduledSructure->expects($this->any())->method('__toArray') + ->willReturn($layoutScheduledStructureData); + $data = [ + 'pageConfigStructure' => $pageConfigStructureData, + 'scheduledStructure' => $layoutScheduledStructureData + ]; + $this->cacheMock->expects($this->once()) ->method('save') - ->with(serialize($this->readerContextMock), 'structure_' . $layoutCacheId, $handles) + ->with(json_encode($data), 'structure_' . $layoutCacheId, $handles) ->willReturn(true); $generatorContextMock = $this->getMockBuilder(\Magento\Framework\View\Layout\Generator\Context::class) @@ -774,6 +837,9 @@ class LayoutTest extends \PHPUnit_Framework_TestCase $xml = simplexml_load_string('<layout/>', \Magento\Framework\View\Layout\Element::class); $this->model->setXml($xml); + $this->readerContextFactoryMock->expects($this->once()) + ->method('create') + ->willReturn($this->readerContextMock); $themeMock = $this->getMockForAbstractClass(\Magento\Framework\View\Design\ThemeInterface::class); $this->themeResolverMock->expects($this->once()) ->method('get') @@ -787,14 +853,33 @@ class LayoutTest extends \PHPUnit_Framework_TestCase ->method('getCacheId') ->willReturn($layoutCacheId); - $readerContextMock = $this->getMockBuilder(\Magento\Framework\View\Layout\Reader\Context::class) - ->disableOriginalConstructor() - ->getMock(); + $pageConfigStructureData = [ + 'field_1' => 123, + 'field_2' => 'text', + 'field_3' => [ + 'field_3_1' => '1244', + 'field_3_2' => null, + 'field_3_3' => false, + ] + ]; + $this->pageConfigStructure->expects($this->once())->method('populateWithArray') + ->with($pageConfigStructureData); + + $layoutScheduledStructureData = [ + 'field_1' => 1283, + 'field_2' => 'text_qwertyuiop[]asdfghjkl;' + ]; + $this->layoutScheduledSructure->expects($this->once())->method('populateWithArray') + ->with($layoutScheduledStructureData); + $data = [ + 'pageConfigStructure' => $pageConfigStructureData, + 'scheduledStructure' => $layoutScheduledStructureData + ]; $this->cacheMock->expects($this->once()) ->method('load') ->with('structure_' . $layoutCacheId) - ->willReturn(serialize($readerContextMock)); + ->willReturn(json_encode($data)); $this->readerPoolMock->expects($this->never()) ->method('interpret'); @@ -811,7 +896,7 @@ class LayoutTest extends \PHPUnit_Framework_TestCase $this->generatorPoolMock->expects($this->once()) ->method('process') - ->with($readerContextMock, $generatorContextMock) + ->with($this->readerContextMock, $generatorContextMock) ->willReturn(true); $elements = [