Skip to content

Commit

Permalink
Fix PageCache: async rendering of blocks can corrupt layout cache mag…
Browse files Browse the repository at this point in the history
…ento#8554 magento#9050 magento#9560

Adapted PageCache and Framework tests
  • Loading branch information
adrian-martinez-interactiv4 committed Sep 22, 2017
1 parent c6e2b39 commit 3cc51dc
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 20 deletions.
21 changes: 9 additions & 12 deletions app/code/Magento/PageCache/Test/Unit/Controller/Block/EsiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ class EsiTest extends \PHPUnit\Framework\TestCase
protected $layoutMock;

/**
* @var \Magento\Framework\View\Layout\ProcessorInterface|\PHPUnit_Framework_MockObject_MockObject
* @var \Magento\Framework\View\Layout\LayoutCacheKeyInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $layoutProcessorMock;
protected $layoutCacheKeyMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Translate\InlineInterface
Expand All @@ -57,7 +57,7 @@ protected function setUp()
$this->layoutMock = $this->getMockBuilder(\Magento\Framework\View\Layout::class)
->disableOriginalConstructor()->getMock();

$this->layoutProcessorMock = $this->getMockForAbstractClass(\Magento\Framework\View\Layout\ProcessorInterface::class);
$this->layoutCacheKeyMock = $this->getMockForAbstractClass(\Magento\Framework\View\Layout\LayoutCacheKeyInterface::class);

$contextMock =
$this->getMockBuilder(\Magento\Framework\App\Action\Context::class)
Expand All @@ -70,8 +70,6 @@ protected function setUp()
$this->viewMock = $this->getMockBuilder(\Magento\Framework\App\View::class)
->disableOriginalConstructor()->getMock();

$this->layoutMock->expects($this->any())->method('getUpdate')->will($this->returnValue($this->layoutProcessorMock));

$contextMock->expects($this->any())->method('getRequest')->will($this->returnValue($this->requestMock));
$contextMock->expects($this->any())->method('getResponse')->will($this->returnValue($this->responseMock));
$contextMock->expects($this->any())->method('getView')->will($this->returnValue($this->viewMock));
Expand All @@ -85,7 +83,8 @@ protected function setUp()
'context' => $contextMock,
'translateInline' => $this->translateInline,
'jsonSerializer' => new \Magento\Framework\Serialize\Serializer\Json(),
'base64jsonSerializer' => new \Magento\Framework\Serialize\Serializer\Base64Json()
'base64jsonSerializer' => new \Magento\Framework\Serialize\Serializer\Base64Json(),
'layoutCacheKey' => $this->layoutCacheKeyMock
]
);
}
Expand Down Expand Up @@ -113,12 +112,10 @@ public function testExecute($blockClass, $shouldSetHeaders)

$this->viewMock->expects($this->once())->method('getLayout')->will($this->returnValue($this->layoutMock));

$this->layoutMock->expects($this->at(0))
->method('getUpdate')
->will($this->returnValue($this->layoutProcessorMock));
$this->layoutProcessorMock->expects($this->at(0))
->method('addCacheKey')
->willReturnSelf();
$this->layoutMock->expects($this->never())
->method('getUpdate');
$this->layoutCacheKeyMock->expects($this->atLeastOnce())
->method('addCacheKeys');

$this->layoutMock->expects($this->once())
->method('getBlock')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ class RenderTest extends \PHPUnit\Framework\TestCase
*/
protected $layoutProcessorMock;

/**
* @var \Magento\Framework\View\Layout\LayoutCacheKeyInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $layoutCacheKeyMock;

/**
* Set up before test
*/
Expand All @@ -58,6 +63,7 @@ protected function setUp()
->disableOriginalConstructor()->getMock();

$this->layoutProcessorMock = $this->getMockForAbstractClass(\Magento\Framework\View\Layout\ProcessorInterface::class);
$this->layoutCacheKeyMock = $this->getMockForAbstractClass(\Magento\Framework\View\Layout\LayoutCacheKeyInterface::class);

$contextMock = $this->getMockBuilder(\Magento\Framework\App\Action\Context::class)
->disableOriginalConstructor()->getMock();
Expand Down Expand Up @@ -86,7 +92,8 @@ protected function setUp()
'context' => $contextMock,
'translateInline' => $this->translateInline,
'jsonSerializer' => new \Magento\Framework\Serialize\Serializer\Json(),
'base64jsonSerializer' => new \Magento\Framework\Serialize\Serializer\Base64Json()
'base64jsonSerializer' => new \Magento\Framework\Serialize\Serializer\Base64Json(),
'layoutCacheKey' => $this->layoutCacheKeyMock
]
);
}
Expand All @@ -96,6 +103,8 @@ public function testExecuteNotAjax()
$this->requestMock->expects($this->once())->method('isAjax')->will($this->returnValue(false));
$this->requestMock->expects($this->once())->method('setActionName')->will($this->returnValue('noroute'));
$this->requestMock->expects($this->once())->method('setDispatched')->will($this->returnValue(false));
$this->layoutCacheKeyMock->expects($this->never())
->method('addCacheKeys');
$this->action->execute();
}

Expand All @@ -113,6 +122,8 @@ public function testExecuteNoParams()
->method('getParam')
->with($this->equalTo('handles'), $this->equalTo(''))
->will($this->returnValue(''));
$this->layoutCacheKeyMock->expects($this->never())
->method('addCacheKeys');
$this->action->execute();
}

Expand Down Expand Up @@ -158,17 +169,15 @@ public function testExecute()
->will($this->returnValue(base64_encode(json_encode($handles))));
$this->viewMock->expects($this->once())->method('loadLayout')->with($this->equalTo($handles));
$this->viewMock->expects($this->any())->method('getLayout')->will($this->returnValue($this->layoutMock));
$this->layoutMock->expects($this->never())
->method('getUpdate');
$this->layoutCacheKeyMock->expects($this->atLeastOnce())
->method('addCacheKeys');
$this->layoutMock->expects($this->at(0))
->method('getUpdate')
->will($this->returnValue($this->layoutProcessorMock));
$this->layoutProcessorMock->expects($this->at(0))
->method('addCacheKey')
->willReturnSelf();
$this->layoutMock->expects($this->at(1))
->method('getBlock')
->with($this->equalTo($blocks[0]))
->will($this->returnValue($blockInstance1));
$this->layoutMock->expects($this->at(2))
$this->layoutMock->expects($this->at(1))
->method('getBlock')
->with($this->equalTo($blocks[1]))
->will($this->returnValue($blockInstance2));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ class MergeTest extends \PHPUnit\Framework\TestCase
*/
private $appState;

/**
* @var \Magento\Framework\View\Layout\LayoutCacheKeyInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $layoutCacheKeyMock;

protected function setUp()
{
$this->objectManagerHelper = new ObjectManager($this);
Expand All @@ -54,6 +59,7 @@ protected function setUp()
$this->appState = $this->getMockBuilder(\Magento\Framework\App\State::class)
->disableOriginalConstructor()
->getMock();
$this->layoutCacheKeyMock = $this->getMockForAbstractClass(\Magento\Framework\View\Layout\LayoutCacheKeyInterface::class);

$this->model = $this->objectManagerHelper->getObject(
\Magento\Framework\View\Model\Layout\Merge::class,
Expand All @@ -62,6 +68,7 @@ protected function setUp()
'layoutValidator' => $this->layoutValidator,
'logger' => $this->logger,
'appState' => $this->appState,
'layoutCacheKey' => $this->layoutCacheKeyMock
]
);
}
Expand All @@ -76,6 +83,9 @@ public function testValidateMergedLayoutThrowsException()
'Please correct the XSD data and try again.',
];
$this->scope->expects($this->once())->method('getId')->willReturn(1);
$this->layoutCacheKeyMock->expects($this->once())
->method('getCacheKeys')
->willReturn([]);
$this->layoutValidator->expects($this->once())
->method('isValid')
->willThrowException(
Expand Down

0 comments on commit 3cc51dc

Please sign in to comment.