Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ProcessLayoutRenderElementTest.php
Go to the documentation of this file.
1 <?php
9 
11 
12 class ProcessLayoutRenderElementTest extends \PHPUnit\Framework\TestCase
13 {
15  private $_model;
16 
18  private $entitySpecificHandlesListMock;
19 
21  private $_configMock;
22 
24  private $_blockMock;
25 
27  private $_layoutMock;
28 
30  private $_observerMock;
31 
33  private $_transport;
34 
38  protected function setUp()
39  {
40  $this->_configMock = $this->createPartialMock(\Magento\PageCache\Model\Config::class, ['getType', 'isEnabled']);
41  $this->entitySpecificHandlesListMock = $this->createMock(EntitySpecificHandlesList::class);
42 
43  $this->_model = new \Magento\PageCache\Observer\ProcessLayoutRenderElement(
44  $this->_configMock,
45  $this->entitySpecificHandlesListMock,
46  new \Magento\Framework\Serialize\Serializer\Json(),
47  new \Magento\Framework\Serialize\Serializer\Base64Json()
48  );
49  $this->_observerMock = $this->createPartialMock(\Magento\Framework\Event\Observer::class, ['getEvent']);
50  $this->_layoutMock = $this->createPartialMock(
51  \Magento\Framework\View\Layout::class,
52  ['isCacheable', 'getBlock', 'getUpdate', 'getHandles']
53  );
54  $this->_blockMock = $this->getMockForAbstractClass(
55  \Magento\Framework\View\Element\AbstractBlock::class,
56  [],
57  '',
58  false,
59  true,
60  true,
61  ['getData', 'isScopePrivate', 'getNameInLayout', 'getUrl']
62  );
63  $this->_transport = new \Magento\Framework\DataObject(['output' => 'test output html']);
64  }
65 
74  public function testExecute(
75  $cacheState,
76  $varnishIsEnabled,
77  $scopeIsPrivate,
78  $blockTtl,
79  $expectedOutput
80  ) {
81  $eventMock = $this->createPartialMock(
82  \Magento\Framework\Event::class,
83  ['getLayout', 'getElementName', 'getTransport']
84  );
85  $this->_observerMock->expects($this->once())->method('getEvent')->will($this->returnValue($eventMock));
86  $eventMock->expects($this->once())->method('getLayout')->will($this->returnValue($this->_layoutMock));
87  $this->_configMock->expects($this->any())->method('isEnabled')->will($this->returnValue($cacheState));
88 
89  if ($cacheState) {
90  $eventMock->expects($this->once())
91  ->method('getElementName')
92  ->will($this->returnValue('blockName'));
93 
94  $eventMock->expects($this->once())
95  ->method('getTransport')
96  ->will($this->returnValue($this->_transport));
97 
98  $this->_layoutMock->expects($this->once())
99  ->method('isCacheable')
100  ->will($this->returnValue(true));
101 
102  $this->_layoutMock->expects($this->any())
103  ->method('getUpdate')
104  ->will($this->returnSelf());
105 
106  $this->_layoutMock->expects($this->any())
107  ->method('getHandles')
108  ->will($this->returnValue(['default', 'catalog_product_view', 'catalog_product_view_id_1']));
109 
110  $this->entitySpecificHandlesListMock->expects($this->any())
111  ->method('getHandles')
112  ->will($this->returnValue(['catalog_product_view_id_1']));
113 
114  $this->_layoutMock->expects($this->once())
115  ->method('getBlock')
116  ->will($this->returnValue($this->_blockMock));
117 
118  if ($varnishIsEnabled) {
119  $this->_blockMock->expects($this->once())
120  ->method('getData')
121  ->with('ttl')
122  ->will($this->returnValue($blockTtl));
123  $this->_blockMock->expects($this->any())
124  ->method('getUrl')
125  ->with(
126  'page_cache/block/esi',
127  ['blocks' => '[null]',
128  'handles' => 'WyJkZWZhdWx0IiwiY2F0YWxvZ19wcm9kdWN0X3ZpZXciXQ==']
129  )
130  ->will(
131  $this->returnValue(
132  'page_cache/block/wrapesi/with/handles/WyJkZWZhdWx0IiwiY2F0YWxvZ19wcm9kdWN0X3ZpZXciXQ=='
133  )
134  );
135  }
136  if ($scopeIsPrivate) {
137  $this->_blockMock->expects($this->once())
138  ->method('getNameInLayout')
139  ->will($this->returnValue('testBlockName'));
140  $this->_blockMock->expects($this->once())
141  ->method('isScopePrivate')
142  ->will($this->returnValue($scopeIsPrivate));
143  }
144  $this->_configMock->expects($this->any())->method('getType')->will($this->returnValue($varnishIsEnabled));
145  }
146  $this->_model->execute($this->_observerMock);
147 
148  $this->assertEquals($expectedOutput, $this->_transport['output']);
149  }
150 
151  public function testExecuteWithBase64Encode()
152  {
153  $expectedOutput = '<esi:include src="page_cache/block/wrapesi/with/handles/YW5kL290aGVyL3N0dWZm" />';
154  $eventMock = $this->createPartialMock(
155  \Magento\Framework\Event::class,
156  ['getLayout', 'getElementName', 'getTransport']
157  );
158  $expectedUrl = 'page_cache/block/wrapesi/with/handles/' . base64_encode('and/other/stuff');
159 
160  $this->_observerMock->expects($this->once())->method('getEvent')->will($this->returnValue($eventMock));
161  $eventMock->expects($this->once())->method('getLayout')->will($this->returnValue($this->_layoutMock));
162  $this->_configMock->expects($this->any())->method('isEnabled')->will($this->returnValue(true));
163 
164  $eventMock->expects($this->once())
165  ->method('getElementName')
166  ->will($this->returnValue('blockName'));
167 
168  $eventMock->expects($this->once())
169  ->method('getTransport')
170  ->will($this->returnValue($this->_transport));
171 
172  $this->_layoutMock->expects($this->once())
173  ->method('isCacheable')
174  ->will($this->returnValue(true));
175 
176  $this->_layoutMock->expects($this->any())
177  ->method('getUpdate')
178  ->will($this->returnSelf());
179 
180  $this->_layoutMock->expects($this->any())
181  ->method('getHandles')
182  ->will($this->returnValue([]));
183 
184  $this->_layoutMock->expects($this->once())
185  ->method('getBlock')
186  ->will($this->returnValue($this->_blockMock));
187 
188  $this->entitySpecificHandlesListMock->expects($this->any())
189  ->method('getHandles')
190  ->will($this->returnValue(['catalog_product_view_id_1']));
191 
192  $this->_blockMock->expects($this->once())
193  ->method('getData')
194  ->with('ttl')
195  ->will($this->returnValue(100));
196  $this->_blockMock->expects($this->any())
197  ->method('getUrl')
198  ->will($this->returnValue($expectedUrl));
199 
200  $this->_blockMock->expects($this->once())
201  ->method('getNameInLayout')
202  ->will($this->returnValue('testBlockName'));
203 
204  $this->_configMock->expects($this->any())->method('getType')->will($this->returnValue(true));
205 
206  $this->_model->execute($this->_observerMock);
207 
208  $this->assertEquals($expectedOutput, $this->_transport['output']);
209  }
210 
217  {
218  return [
219  'full_page type and Varnish enabled, public scope, ttl is set' => [
220  true,
221  true,
222  false,
223  360,
224  '<esi:include src="page_cache/block/wrapesi/with/handles/'
225  . 'WyJkZWZhdWx0IiwiY2F0YWxvZ19wcm9kdWN0X3ZpZXciXQ==" />',
226  ],
227  'full_page type and Varnish enabled, public scope, ttl is not set' => [
228  true,
229  true,
230  false,
231  null,
232  'test output html',
233  ],
234  'full_page type enabled, Varnish disabled, public scope, ttl is set' => [
235  true,
236  false,
237  false,
238  360,
239  'test output html',
240  ],
241  'full_page type enabled, Varnish disabled, public scope, ttl is not set' => [
242  true,
243  false,
244  false,
245  null,
246  'test output html',
247  ],
248  'full_page type enabled, Varnish disabled, private scope, ttl is not set' => [
249  true,
250  false,
251  true,
252  null,
253  '<!-- BLOCK testBlockName -->test output html<!-- /BLOCK testBlockName -->',
254  ],
255  'full_page type is disabled, Varnish enabled' => [false, true, false, null, 'test output html']
256  ];
257  }
258 }
testExecute( $cacheState, $varnishIsEnabled, $scopeIsPrivate, $blockTtl, $expectedOutput)