LayoutPluginTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\PageCache\Test\Unit\Model\Layout;
  7. class LayoutPluginTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\PageCache\Model\Layout\LayoutPlugin
  11. */
  12. protected $model;
  13. /**
  14. * @var \Magento\Framework\App\ResponseInterface|\PHPUnit_Framework_MockObject_MockObject
  15. */
  16. protected $responseMock;
  17. /**
  18. * @var \Magento\Framework\View\Layout|\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $layoutMock;
  21. /**
  22. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  23. */
  24. protected $configMock;
  25. protected function setUp()
  26. {
  27. $this->layoutMock = $this->getMockForAbstractClass(
  28. \Magento\Framework\View\Layout::class,
  29. [],
  30. '',
  31. false,
  32. true,
  33. true,
  34. ['isCacheable', 'getAllBlocks']
  35. );
  36. $this->responseMock = $this->createMock(\Magento\Framework\App\Response\Http::class);
  37. $this->configMock = $this->createMock(\Magento\PageCache\Model\Config::class);
  38. $this->model = new \Magento\PageCache\Model\Layout\LayoutPlugin(
  39. $this->responseMock,
  40. $this->configMock
  41. );
  42. }
  43. /**
  44. * @param $cacheState
  45. * @param $layoutIsCacheable
  46. * @dataProvider afterGenerateXmlDataProvider
  47. */
  48. public function testAfterGenerateXml($cacheState, $layoutIsCacheable)
  49. {
  50. $maxAge = 180;
  51. $result = 'test';
  52. $this->layoutMock->expects($this->once())->method('isCacheable')->will($this->returnValue($layoutIsCacheable));
  53. $this->configMock->expects($this->any())->method('isEnabled')->will($this->returnValue($cacheState));
  54. if ($layoutIsCacheable && $cacheState) {
  55. $this->configMock->expects($this->once())->method('getTtl')->will($this->returnValue($maxAge));
  56. $this->responseMock->expects($this->once())->method('setPublicHeaders')->with($maxAge);
  57. } else {
  58. $this->responseMock->expects($this->never())->method('setPublicHeaders');
  59. }
  60. $output = $this->model->afterGenerateXml($this->layoutMock, $result);
  61. $this->assertSame($result, $output);
  62. }
  63. /**
  64. * @return array
  65. */
  66. public function afterGenerateXmlDataProvider()
  67. {
  68. return [
  69. 'Full_cache state is true, Layout is cache-able' => [true, true],
  70. 'Full_cache state is true, Layout is not cache-able' => [true, false],
  71. 'Full_cache state is false, Layout is not cache-able' => [false, false],
  72. 'Full_cache state is false, Layout is cache-able' => [false, true]
  73. ];
  74. }
  75. /**
  76. * @param $cacheState
  77. * @param $layoutIsCacheable
  78. * @param $expectedTags
  79. * @param $configCacheType
  80. * @param $ttl
  81. * @dataProvider afterGetOutputDataProvider
  82. */
  83. public function testAfterGetOutput($cacheState, $layoutIsCacheable, $expectedTags, $configCacheType, $ttl)
  84. {
  85. $html = 'html';
  86. $this->configMock->expects($this->any())->method('isEnabled')->will($this->returnValue($cacheState));
  87. $blockStub = $this->createPartialMock(
  88. \Magento\PageCache\Test\Unit\Block\Controller\StubBlock::class,
  89. ['getIdentities']
  90. );
  91. $blockStub->setTtl($ttl);
  92. $blockStub->expects($this->any())->method('getIdentities')->willReturn(['identity1', 'identity2']);
  93. $this->layoutMock->expects($this->once())->method('isCacheable')->will($this->returnValue($layoutIsCacheable));
  94. $this->layoutMock->expects($this->any())->method('getAllBlocks')->will($this->returnValue([$blockStub]));
  95. $this->configMock->expects($this->any())->method('getType')->will($this->returnValue($configCacheType));
  96. if ($layoutIsCacheable && $cacheState) {
  97. $this->responseMock->expects($this->once())->method('setHeader')->with('X-Magento-Tags', $expectedTags);
  98. } else {
  99. $this->responseMock->expects($this->never())->method('setHeader');
  100. }
  101. $output = $this->model->afterGetOutput($this->layoutMock, $html);
  102. $this->assertSame($output, $html);
  103. }
  104. /**
  105. * @return array
  106. */
  107. public function afterGetOutputDataProvider()
  108. {
  109. $tags = 'identity1,identity2';
  110. return [
  111. 'Cacheable layout, Full_cache state is true' => [true, true, $tags, null, 0],
  112. 'Non-cacheable layout' => [true, false, null, null, 0],
  113. 'Cacheable layout with Varnish' => [true, true, $tags, \Magento\PageCache\Model\Config::VARNISH, 0],
  114. 'Cacheable layout with Varnish, Full_cache state is false' => [
  115. false,
  116. true,
  117. $tags,
  118. \Magento\PageCache\Model\Config::VARNISH,
  119. 0,
  120. ],
  121. 'Cacheable layout with Varnish and esi' => [
  122. true,
  123. true,
  124. null,
  125. \Magento\PageCache\Model\Config::VARNISH,
  126. 100,
  127. ],
  128. 'Cacheable layout with Builtin' => [true, true, $tags, \Magento\PageCache\Model\Config::BUILT_IN, 0],
  129. 'Cacheable layout with Builtin, Full_cache state is false' => [
  130. false,
  131. true,
  132. $tags,
  133. \Magento\PageCache\Model\Config::BUILT_IN,
  134. 0,
  135. ],
  136. 'Cacheable layout with Builtin and esi' => [
  137. true,
  138. false,
  139. $tags,
  140. \Magento\PageCache\Model\Config::BUILT_IN,
  141. 100,
  142. ]
  143. ];
  144. }
  145. }