ProcessLayoutRenderElementTest.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\PageCache\Test\Unit\Observer;
  8. use Magento\Framework\View\EntitySpecificHandlesList;
  9. class ProcessLayoutRenderElementTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /** @var \Magento\PageCache\Observer\ProcessLayoutRenderElement */
  12. private $_model;
  13. /** @var \PHPUnit_Framework_MockObject_MockObject|EntitySpecificHandlesList */
  14. private $entitySpecificHandlesListMock;
  15. /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\PageCache\Model\Config */
  16. private $_configMock;
  17. /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\View\Element\AbstractBlock */
  18. private $_blockMock;
  19. /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\View\Layout */
  20. private $_layoutMock;
  21. /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Event\Observer */
  22. private $_observerMock;
  23. /** @var \Magento\Framework\DataObject */
  24. private $_transport;
  25. /**
  26. * Set up all mocks and data for test
  27. */
  28. protected function setUp()
  29. {
  30. $this->_configMock = $this->createPartialMock(\Magento\PageCache\Model\Config::class, ['getType', 'isEnabled']);
  31. $this->entitySpecificHandlesListMock = $this->createMock(EntitySpecificHandlesList::class);
  32. $this->_model = new \Magento\PageCache\Observer\ProcessLayoutRenderElement(
  33. $this->_configMock,
  34. $this->entitySpecificHandlesListMock,
  35. new \Magento\Framework\Serialize\Serializer\Json(),
  36. new \Magento\Framework\Serialize\Serializer\Base64Json()
  37. );
  38. $this->_observerMock = $this->createPartialMock(\Magento\Framework\Event\Observer::class, ['getEvent']);
  39. $this->_layoutMock = $this->createPartialMock(
  40. \Magento\Framework\View\Layout::class,
  41. ['isCacheable', 'getBlock', 'getUpdate', 'getHandles']
  42. );
  43. $this->_blockMock = $this->getMockForAbstractClass(
  44. \Magento\Framework\View\Element\AbstractBlock::class,
  45. [],
  46. '',
  47. false,
  48. true,
  49. true,
  50. ['getData', 'isScopePrivate', 'getNameInLayout', 'getUrl']
  51. );
  52. $this->_transport = new \Magento\Framework\DataObject(['output' => 'test output html']);
  53. }
  54. /**
  55. * @param bool $cacheState
  56. * @param bool $varnishIsEnabled
  57. * @param bool $scopeIsPrivate
  58. * @param int|null $blockTtl
  59. * @param string $expectedOutput
  60. * @dataProvider processLayoutRenderDataProvider
  61. */
  62. public function testExecute(
  63. $cacheState,
  64. $varnishIsEnabled,
  65. $scopeIsPrivate,
  66. $blockTtl,
  67. $expectedOutput
  68. ) {
  69. $eventMock = $this->createPartialMock(
  70. \Magento\Framework\Event::class,
  71. ['getLayout', 'getElementName', 'getTransport']
  72. );
  73. $this->_observerMock->expects($this->once())->method('getEvent')->will($this->returnValue($eventMock));
  74. $eventMock->expects($this->once())->method('getLayout')->will($this->returnValue($this->_layoutMock));
  75. $this->_configMock->expects($this->any())->method('isEnabled')->will($this->returnValue($cacheState));
  76. if ($cacheState) {
  77. $eventMock->expects($this->once())
  78. ->method('getElementName')
  79. ->will($this->returnValue('blockName'));
  80. $eventMock->expects($this->once())
  81. ->method('getTransport')
  82. ->will($this->returnValue($this->_transport));
  83. $this->_layoutMock->expects($this->once())
  84. ->method('isCacheable')
  85. ->will($this->returnValue(true));
  86. $this->_layoutMock->expects($this->any())
  87. ->method('getUpdate')
  88. ->will($this->returnSelf());
  89. $this->_layoutMock->expects($this->any())
  90. ->method('getHandles')
  91. ->will($this->returnValue(['default', 'catalog_product_view', 'catalog_product_view_id_1']));
  92. $this->entitySpecificHandlesListMock->expects($this->any())
  93. ->method('getHandles')
  94. ->will($this->returnValue(['catalog_product_view_id_1']));
  95. $this->_layoutMock->expects($this->once())
  96. ->method('getBlock')
  97. ->will($this->returnValue($this->_blockMock));
  98. if ($varnishIsEnabled) {
  99. $this->_blockMock->expects($this->once())
  100. ->method('getData')
  101. ->with('ttl')
  102. ->will($this->returnValue($blockTtl));
  103. $this->_blockMock->expects($this->any())
  104. ->method('getUrl')
  105. ->with(
  106. 'page_cache/block/esi',
  107. ['blocks' => '[null]',
  108. 'handles' => 'WyJkZWZhdWx0IiwiY2F0YWxvZ19wcm9kdWN0X3ZpZXciXQ==']
  109. )
  110. ->will(
  111. $this->returnValue(
  112. 'page_cache/block/wrapesi/with/handles/WyJkZWZhdWx0IiwiY2F0YWxvZ19wcm9kdWN0X3ZpZXciXQ=='
  113. )
  114. );
  115. }
  116. if ($scopeIsPrivate) {
  117. $this->_blockMock->expects($this->once())
  118. ->method('getNameInLayout')
  119. ->will($this->returnValue('testBlockName'));
  120. $this->_blockMock->expects($this->once())
  121. ->method('isScopePrivate')
  122. ->will($this->returnValue($scopeIsPrivate));
  123. }
  124. $this->_configMock->expects($this->any())->method('getType')->will($this->returnValue($varnishIsEnabled));
  125. }
  126. $this->_model->execute($this->_observerMock);
  127. $this->assertEquals($expectedOutput, $this->_transport['output']);
  128. }
  129. public function testExecuteWithBase64Encode()
  130. {
  131. $expectedOutput = '<esi:include src="page_cache/block/wrapesi/with/handles/YW5kL290aGVyL3N0dWZm" />';
  132. $eventMock = $this->createPartialMock(
  133. \Magento\Framework\Event::class,
  134. ['getLayout', 'getElementName', 'getTransport']
  135. );
  136. $expectedUrl = 'page_cache/block/wrapesi/with/handles/' . base64_encode('and/other/stuff');
  137. $this->_observerMock->expects($this->once())->method('getEvent')->will($this->returnValue($eventMock));
  138. $eventMock->expects($this->once())->method('getLayout')->will($this->returnValue($this->_layoutMock));
  139. $this->_configMock->expects($this->any())->method('isEnabled')->will($this->returnValue(true));
  140. $eventMock->expects($this->once())
  141. ->method('getElementName')
  142. ->will($this->returnValue('blockName'));
  143. $eventMock->expects($this->once())
  144. ->method('getTransport')
  145. ->will($this->returnValue($this->_transport));
  146. $this->_layoutMock->expects($this->once())
  147. ->method('isCacheable')
  148. ->will($this->returnValue(true));
  149. $this->_layoutMock->expects($this->any())
  150. ->method('getUpdate')
  151. ->will($this->returnSelf());
  152. $this->_layoutMock->expects($this->any())
  153. ->method('getHandles')
  154. ->will($this->returnValue([]));
  155. $this->_layoutMock->expects($this->once())
  156. ->method('getBlock')
  157. ->will($this->returnValue($this->_blockMock));
  158. $this->entitySpecificHandlesListMock->expects($this->any())
  159. ->method('getHandles')
  160. ->will($this->returnValue(['catalog_product_view_id_1']));
  161. $this->_blockMock->expects($this->once())
  162. ->method('getData')
  163. ->with('ttl')
  164. ->will($this->returnValue(100));
  165. $this->_blockMock->expects($this->any())
  166. ->method('getUrl')
  167. ->will($this->returnValue($expectedUrl));
  168. $this->_blockMock->expects($this->once())
  169. ->method('getNameInLayout')
  170. ->will($this->returnValue('testBlockName'));
  171. $this->_configMock->expects($this->any())->method('getType')->will($this->returnValue(true));
  172. $this->_model->execute($this->_observerMock);
  173. $this->assertEquals($expectedOutput, $this->_transport['output']);
  174. }
  175. /**
  176. * Data provider for testProcessLayoutRenderElement
  177. *
  178. * @return array
  179. */
  180. public function processLayoutRenderDataProvider()
  181. {
  182. return [
  183. 'full_page type and Varnish enabled, public scope, ttl is set' => [
  184. true,
  185. true,
  186. false,
  187. 360,
  188. '<esi:include src="page_cache/block/wrapesi/with/handles/'
  189. . 'WyJkZWZhdWx0IiwiY2F0YWxvZ19wcm9kdWN0X3ZpZXciXQ==" />',
  190. ],
  191. 'full_page type and Varnish enabled, public scope, ttl is not set' => [
  192. true,
  193. true,
  194. false,
  195. null,
  196. 'test output html',
  197. ],
  198. 'full_page type enabled, Varnish disabled, public scope, ttl is set' => [
  199. true,
  200. false,
  201. false,
  202. 360,
  203. 'test output html',
  204. ],
  205. 'full_page type enabled, Varnish disabled, public scope, ttl is not set' => [
  206. true,
  207. false,
  208. false,
  209. null,
  210. 'test output html',
  211. ],
  212. 'full_page type enabled, Varnish disabled, private scope, ttl is not set' => [
  213. true,
  214. false,
  215. true,
  216. null,
  217. '<!-- BLOCK testBlockName -->test output html<!-- /BLOCK testBlockName -->',
  218. ],
  219. 'full_page type is disabled, Varnish enabled' => [false, true, false, null, 'test output html']
  220. ];
  221. }
  222. }