Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
LayoutPluginTest.php
Go to the documentation of this file.
1 <?php
7 
8 class LayoutPluginTest extends \PHPUnit\Framework\TestCase
9 {
13  protected $model;
14 
18  protected $responseMock;
19 
23  protected $layoutMock;
24 
28  protected $configMock;
29 
30  protected function setUp()
31  {
32  $this->layoutMock = $this->getMockForAbstractClass(
33  \Magento\Framework\View\Layout::class,
34  [],
35  '',
36  false,
37  true,
38  true,
39  ['isCacheable', 'getAllBlocks']
40  );
41  $this->responseMock = $this->createMock(\Magento\Framework\App\Response\Http::class);
42  $this->configMock = $this->createMock(\Magento\PageCache\Model\Config::class);
43 
44  $this->model = new \Magento\PageCache\Model\Layout\LayoutPlugin(
45  $this->responseMock,
46  $this->configMock
47  );
48  }
49 
55  public function testAfterGenerateXml($cacheState, $layoutIsCacheable)
56  {
57  $maxAge = 180;
58  $result = 'test';
59 
60  $this->layoutMock->expects($this->once())->method('isCacheable')->will($this->returnValue($layoutIsCacheable));
61  $this->configMock->expects($this->any())->method('isEnabled')->will($this->returnValue($cacheState));
62 
63  if ($layoutIsCacheable && $cacheState) {
64  $this->configMock->expects($this->once())->method('getTtl')->will($this->returnValue($maxAge));
65  $this->responseMock->expects($this->once())->method('setPublicHeaders')->with($maxAge);
66  } else {
67  $this->responseMock->expects($this->never())->method('setPublicHeaders');
68  }
69  $output = $this->model->afterGenerateXml($this->layoutMock, $result);
70  $this->assertSame($result, $output);
71  }
72 
76  public function afterGenerateXmlDataProvider()
77  {
78  return [
79  'Full_cache state is true, Layout is cache-able' => [true, true],
80  'Full_cache state is true, Layout is not cache-able' => [true, false],
81  'Full_cache state is false, Layout is not cache-able' => [false, false],
82  'Full_cache state is false, Layout is cache-able' => [false, true]
83  ];
84  }
85 
94  public function testAfterGetOutput($cacheState, $layoutIsCacheable, $expectedTags, $configCacheType, $ttl)
95  {
96  $html = 'html';
97  $this->configMock->expects($this->any())->method('isEnabled')->will($this->returnValue($cacheState));
98  $blockStub = $this->createPartialMock(
99  \Magento\PageCache\Test\Unit\Block\Controller\StubBlock::class,
100  ['getIdentities']
101  );
102  $blockStub->setTtl($ttl);
103  $blockStub->expects($this->any())->method('getIdentities')->willReturn(['identity1', 'identity2']);
104  $this->layoutMock->expects($this->once())->method('isCacheable')->will($this->returnValue($layoutIsCacheable));
105  $this->layoutMock->expects($this->any())->method('getAllBlocks')->will($this->returnValue([$blockStub]));
106 
107  $this->configMock->expects($this->any())->method('getType')->will($this->returnValue($configCacheType));
108 
109  if ($layoutIsCacheable && $cacheState) {
110  $this->responseMock->expects($this->once())->method('setHeader')->with('X-Magento-Tags', $expectedTags);
111  } else {
112  $this->responseMock->expects($this->never())->method('setHeader');
113  }
114  $output = $this->model->afterGetOutput($this->layoutMock, $html);
115  $this->assertSame($output, $html);
116  }
117 
121  public function afterGetOutputDataProvider()
122  {
123  $tags = 'identity1,identity2';
124  return [
125  'Cacheable layout, Full_cache state is true' => [true, true, $tags, null, 0],
126  'Non-cacheable layout' => [true, false, null, null, 0],
127  'Cacheable layout with Varnish' => [true, true, $tags, \Magento\PageCache\Model\Config::VARNISH, 0],
128  'Cacheable layout with Varnish, Full_cache state is false' => [
129  false,
130  true,
131  $tags,
133  0,
134  ],
135  'Cacheable layout with Varnish and esi' => [
136  true,
137  true,
138  null,
140  100,
141  ],
142  'Cacheable layout with Builtin' => [true, true, $tags, \Magento\PageCache\Model\Config::BUILT_IN, 0],
143  'Cacheable layout with Builtin, Full_cache state is false' => [
144  false,
145  true,
146  $tags,
148  0,
149  ],
150  'Cacheable layout with Builtin and esi' => [
151  true,
152  false,
153  $tags,
155  100,
156  ]
157  ];
158  }
159 }
testAfterGetOutput($cacheState, $layoutIsCacheable, $expectedTags, $configCacheType, $ttl)