Skip to content

Commit 3cc51dc

Browse files
Fix PageCache: async rendering of blocks can corrupt layout cache #8554 #9050 #9560
Adapted PageCache and Framework tests
1 parent c6e2b39 commit 3cc51dc

File tree

3 files changed

+36
-20
lines changed

3 files changed

+36
-20
lines changed

app/code/Magento/PageCache/Test/Unit/Controller/Block/EsiTest.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ class EsiTest extends \PHPUnit\Framework\TestCase
4040
protected $layoutMock;
4141

4242
/**
43-
* @var \Magento\Framework\View\Layout\ProcessorInterface|\PHPUnit_Framework_MockObject_MockObject
43+
* @var \Magento\Framework\View\Layout\LayoutCacheKeyInterface|\PHPUnit_Framework_MockObject_MockObject
4444
*/
45-
protected $layoutProcessorMock;
45+
protected $layoutCacheKeyMock;
4646

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

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

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

73-
$this->layoutMock->expects($this->any())->method('getUpdate')->will($this->returnValue($this->layoutProcessorMock));
74-
7573
$contextMock->expects($this->any())->method('getRequest')->will($this->returnValue($this->requestMock));
7674
$contextMock->expects($this->any())->method('getResponse')->will($this->returnValue($this->responseMock));
7775
$contextMock->expects($this->any())->method('getView')->will($this->returnValue($this->viewMock));
@@ -85,7 +83,8 @@ protected function setUp()
8583
'context' => $contextMock,
8684
'translateInline' => $this->translateInline,
8785
'jsonSerializer' => new \Magento\Framework\Serialize\Serializer\Json(),
88-
'base64jsonSerializer' => new \Magento\Framework\Serialize\Serializer\Base64Json()
86+
'base64jsonSerializer' => new \Magento\Framework\Serialize\Serializer\Base64Json(),
87+
'layoutCacheKey' => $this->layoutCacheKeyMock
8988
]
9089
);
9190
}
@@ -113,12 +112,10 @@ public function testExecute($blockClass, $shouldSetHeaders)
113112

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

116-
$this->layoutMock->expects($this->at(0))
117-
->method('getUpdate')
118-
->will($this->returnValue($this->layoutProcessorMock));
119-
$this->layoutProcessorMock->expects($this->at(0))
120-
->method('addCacheKey')
121-
->willReturnSelf();
115+
$this->layoutMock->expects($this->never())
116+
->method('getUpdate');
117+
$this->layoutCacheKeyMock->expects($this->atLeastOnce())
118+
->method('addCacheKeys');
122119

123120
$this->layoutMock->expects($this->once())
124121
->method('getBlock')

app/code/Magento/PageCache/Test/Unit/Controller/Block/RenderTest.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ class RenderTest extends \PHPUnit\Framework\TestCase
4949
*/
5050
protected $layoutProcessorMock;
5151

52+
/**
53+
* @var \Magento\Framework\View\Layout\LayoutCacheKeyInterface|\PHPUnit_Framework_MockObject_MockObject
54+
*/
55+
protected $layoutCacheKeyMock;
56+
5257
/**
5358
* Set up before test
5459
*/
@@ -58,6 +63,7 @@ protected function setUp()
5863
->disableOriginalConstructor()->getMock();
5964

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

6268
$contextMock = $this->getMockBuilder(\Magento\Framework\App\Action\Context::class)
6369
->disableOriginalConstructor()->getMock();
@@ -86,7 +92,8 @@ protected function setUp()
8692
'context' => $contextMock,
8793
'translateInline' => $this->translateInline,
8894
'jsonSerializer' => new \Magento\Framework\Serialize\Serializer\Json(),
89-
'base64jsonSerializer' => new \Magento\Framework\Serialize\Serializer\Base64Json()
95+
'base64jsonSerializer' => new \Magento\Framework\Serialize\Serializer\Base64Json(),
96+
'layoutCacheKey' => $this->layoutCacheKeyMock
9097
]
9198
);
9299
}
@@ -96,6 +103,8 @@ public function testExecuteNotAjax()
96103
$this->requestMock->expects($this->once())->method('isAjax')->will($this->returnValue(false));
97104
$this->requestMock->expects($this->once())->method('setActionName')->will($this->returnValue('noroute'));
98105
$this->requestMock->expects($this->once())->method('setDispatched')->will($this->returnValue(false));
106+
$this->layoutCacheKeyMock->expects($this->never())
107+
->method('addCacheKeys');
99108
$this->action->execute();
100109
}
101110

@@ -113,6 +122,8 @@ public function testExecuteNoParams()
113122
->method('getParam')
114123
->with($this->equalTo('handles'), $this->equalTo(''))
115124
->will($this->returnValue(''));
125+
$this->layoutCacheKeyMock->expects($this->never())
126+
->method('addCacheKeys');
116127
$this->action->execute();
117128
}
118129

@@ -158,17 +169,15 @@ public function testExecute()
158169
->will($this->returnValue(base64_encode(json_encode($handles))));
159170
$this->viewMock->expects($this->once())->method('loadLayout')->with($this->equalTo($handles));
160171
$this->viewMock->expects($this->any())->method('getLayout')->will($this->returnValue($this->layoutMock));
172+
$this->layoutMock->expects($this->never())
173+
->method('getUpdate');
174+
$this->layoutCacheKeyMock->expects($this->atLeastOnce())
175+
->method('addCacheKeys');
161176
$this->layoutMock->expects($this->at(0))
162-
->method('getUpdate')
163-
->will($this->returnValue($this->layoutProcessorMock));
164-
$this->layoutProcessorMock->expects($this->at(0))
165-
->method('addCacheKey')
166-
->willReturnSelf();
167-
$this->layoutMock->expects($this->at(1))
168177
->method('getBlock')
169178
->with($this->equalTo($blocks[0]))
170179
->will($this->returnValue($blockInstance1));
171-
$this->layoutMock->expects($this->at(2))
180+
$this->layoutMock->expects($this->at(1))
172181
->method('getBlock')
173182
->with($this->equalTo($blocks[1]))
174183
->will($this->returnValue($blockInstance2));

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ class MergeTest extends \PHPUnit\Framework\TestCase
4242
*/
4343
private $appState;
4444

45+
/**
46+
* @var \Magento\Framework\View\Layout\LayoutCacheKeyInterface|\PHPUnit_Framework_MockObject_MockObject
47+
*/
48+
protected $layoutCacheKeyMock;
49+
4550
protected function setUp()
4651
{
4752
$this->objectManagerHelper = new ObjectManager($this);
@@ -54,6 +59,7 @@ protected function setUp()
5459
$this->appState = $this->getMockBuilder(\Magento\Framework\App\State::class)
5560
->disableOriginalConstructor()
5661
->getMock();
62+
$this->layoutCacheKeyMock = $this->getMockForAbstractClass(\Magento\Framework\View\Layout\LayoutCacheKeyInterface::class);
5763

5864
$this->model = $this->objectManagerHelper->getObject(
5965
\Magento\Framework\View\Model\Layout\Merge::class,
@@ -62,6 +68,7 @@ protected function setUp()
6268
'layoutValidator' => $this->layoutValidator,
6369
'logger' => $this->logger,
6470
'appState' => $this->appState,
71+
'layoutCacheKey' => $this->layoutCacheKeyMock
6572
]
6673
);
6774
}
@@ -76,6 +83,9 @@ public function testValidateMergedLayoutThrowsException()
7683
'Please correct the XSD data and try again.',
7784
];
7885
$this->scope->expects($this->once())->method('getId')->willReturn(1);
86+
$this->layoutCacheKeyMock->expects($this->once())
87+
->method('getCacheKeys')
88+
->willReturn([]);
7989
$this->layoutValidator->expects($this->once())
8090
->method('isValid')
8191
->willThrowException(

0 commit comments

Comments
 (0)