Skip to content
Snippets Groups Projects
Commit 2d71ca9a authored by Yaroslav Onischenko's avatar Yaroslav Onischenko
Browse files

Merge branch 'MAGETWO-44250' into develop

parents 8ca9edd2 6d2516f9
Branches
No related merge requests found
...@@ -66,8 +66,9 @@ class BuiltinPlugin ...@@ -66,8 +66,9 @@ class BuiltinPlugin
) { ) {
$result = $proceed($response); $result = $proceed($response);
$usePlugin = $this->registry->registry('use_page_cache_plugin'); $usePlugin = $this->registry->registry('use_page_cache_plugin');
if (!$this->config->isEnabled() || $this->config->getType() != \Magento\PageCache\Model\Config::BUILT_IN if (!$usePlugin || !$this->config->isEnabled()
|| !$usePlugin) { || $this->config->getType() != \Magento\PageCache\Model\Config::BUILT_IN
) {
return $result; return $result;
} }
...@@ -76,6 +77,16 @@ class BuiltinPlugin ...@@ -76,6 +77,16 @@ class BuiltinPlugin
$response->setHeader('X-Magento-Cache-Control', $cacheControl); $response->setHeader('X-Magento-Cache-Control', $cacheControl);
$response->setHeader('X-Magento-Cache-Debug', 'MISS', true); $response->setHeader('X-Magento-Cache-Debug', 'MISS', true);
} }
$tagsHeader = $response->getHeader('X-Magento-Tags');
$tags = [];
if ($tagsHeader) {
$tags = explode(',', $tagsHeader->getFieldValue());
$response->clearHeader('X-Magento-Tags');
}
$tags = array_unique(array_merge($tags, [\Magento\PageCache\Model\Cache\Type::CACHE_TAG]));
$response->setHeader('X-Magento-Tags', implode(',', $tags));
$this->kernel->process($response); $this->kernel->process($response);
return $result; return $result;
} }
......
...@@ -11,83 +11,133 @@ namespace Magento\PageCache\Test\Unit\Model\Controller\Result; ...@@ -11,83 +11,133 @@ namespace Magento\PageCache\Test\Unit\Model\Controller\Result;
class BuiltinPluginTest extends \PHPUnit_Framework_TestCase class BuiltinPluginTest extends \PHPUnit_Framework_TestCase
{ {
/** /**
* @param bool $usePlugin * @var \Magento\PageCache\Model\Controller\Result\BuiltinPlugin
* @param \PHPUnit_Framework_MockObject_Matcher_InvokedCount $getHeaderCount
* @param \PHPUnit_Framework_MockObject_Matcher_InvokedCount $setCacheControlHeaderCount
* @param \PHPUnit_Framework_MockObject_Matcher_InvokedCount $setCacheDebugHeaderCount
* @param \PHPUnit_Framework_MockObject_Matcher_InvokedCount $getModeCount
* @param \PHPUnit_Framework_MockObject_Matcher_InvokedCount $processCount
* @dataProvider dataProvider
*/ */
public function testAroundResult( protected $plugin;
$usePlugin, $getHeaderCount, $setCacheControlHeaderCount, $setCacheDebugHeaderCount, $getModeCount,
$processCount /**
) { * @var \Magento\Framework\Controller\ResultInterface
$cacheControl = 'test'; */
protected $subject;
$header = $this->getMockBuilder('Zend\Http\Header\HeaderInterface')
->getMockForAbstractClass(); /**
$header->expects($this->any())->method('getFieldValue') * @var \Closure
->willReturn($cacheControl); */
protected $closure;
$response = $this->getMock('Magento\Framework\App\Response\Http', [], [], '', false);
$response->expects($getHeaderCount) /**
->method('getHeader') * @var \Magento\Framework\App\Response\Http|\PHPUnit_Framework_MockObject_MockObject
->with('Cache-Control') */
->willReturn($header); protected $response;
$response->expects($setCacheControlHeaderCount)->method('setHeader')
->with('X-Magento-Cache-Control', $cacheControl); /**
$response->expects($setCacheDebugHeaderCount)->method('setHeader') * @var \Magento\Framework\Registry|\PHPUnit_Framework_MockObject_MockObject
->with('X-Magento-Cache-Debug', 'MISS', true); */
protected $registry;
/** @var \Magento\Framework\Controller\ResultInterface $result */
/**
* @var \Magento\Framework\App\State|\PHPUnit_Framework_MockObject_MockObject
*/
protected $state;
/**
* @var \Zend\Http\Header\HeaderInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $header;
/**
* @var \Magento\Framework\App\PageCache\Kernel|\PHPUnit_Framework_MockObject_MockObject
*/
protected $kernel;
protected function setUp()
{
$result = $this->getMock('Magento\Framework\Controller\ResultInterface', [], [], '', false); $result = $this->getMock('Magento\Framework\Controller\ResultInterface', [], [], '', false);
$closure = function () use ($result) { $this->closure = function() use ($result) {
return $result; return $result;
}; };
/** @var \Magento\Framework\Registry|\PHPUnit_Framework_MockObject_MockObject $registry */ $this->header = $this->getMock('Zend\Http\Header\HeaderInterface', [], [], '', false);
$registry = $this->getMock('Magento\Framework\Registry', [], [], '', false); $this->subject = $this->getMock('Magento\Framework\Controller\ResultInterface', [], [], '', false);
$registry->expects($this->once())->method('registry')->with('use_page_cache_plugin') $this->response = $this->getMock(
->will($this->returnValue($usePlugin)); 'Magento\Framework\App\Response\Http',
['getHeader', 'clearHeader', 'setHeader'],
/** @var \Magento\PageCache\Model\Config|\PHPUnit_Framework_MockObject_MockObject $config */ [],
$config = $this->getMock('Magento\PageCache\Model\Config', [], [], '', false); '',
$config->expects($this->once())->method('isEnabled')->will($this->returnValue(true)); false
$config->expects($this->once())->method('getType') );
->will($this->returnValue(\Magento\PageCache\Model\Config::BUILT_IN)); $this->response->expects($this->any())->method('getHeader')->willReturnMap(
[
['X-Magento-Tags', $this->header],
['Cache-Control', $this->header]
]
);
/** @var \Magento\Framework\App\State|\PHPUnit_Framework_MockObject_MockObject $state */ $this->registry = $this->getMock('Magento\Framework\Registry', [], [], '', false);
$state = $this->getMock('Magento\Framework\App\State', [], [], '', false);
$state->expects($getModeCount)->method('getMode')
->will($this->returnValue(\Magento\Framework\App\State::MODE_DEVELOPER));
$kernel = $this->getMock('Magento\Framework\App\PageCache\Kernel', [], [], '', false); $config = $this->getMock('Magento\PageCache\Model\Config', ['isEnabled', 'getType'], [], '', false);
$kernel->expects($processCount)->method('process')->with($response); $config->expects($this->any())->method('isEnabled')->willReturn(true);
$config->expects($this->any())->method('getType')->willReturn(\Magento\PageCache\Model\Config::BUILT_IN);
$subject = $this->getMock('Magento\Framework\Controller\ResultInterface', [], [], '', false); $this->kernel = $this->getMock('Magento\Framework\App\PageCache\Kernel', [], [], '', false);
/** @var \Magento\PageCache\Model\Controller\Result\BuiltinPlugin $plugin */ $this->state = $this->getMock('Magento\Framework\App\State', [], [], '', false);
$plugin = (new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this))->getObject( $this->plugin = (new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this))->getObject(
'Magento\PageCache\Model\Controller\Result\BuiltinPlugin', 'Magento\PageCache\Model\Controller\Result\BuiltinPlugin',
[ [
'registry' => $registry, 'registry' => $this->registry,
'config' => $config, 'config' => $config,
'kernel' => $kernel, 'kernel' => $this->kernel,
'state' => $state 'state' => $this->state
] ]
); );
$this->assertSame($result, $plugin->aroundRenderResult($subject, $closure, $response));
} }
/** public function testAroundResultWithoutPlugin()
* @return array
*/
public function dataProvider()
{ {
return [ $this->registry->expects($this->once())->method('registry')->with('use_page_cache_plugin')->willReturn(false);
[true, $this->once(), $this->at(1), $this->at(2), $this->once(), $this->once()], $this->kernel->expects($this->never())->method('process')->with($this->response);
[false, $this->never(), $this->never(), $this->never(), $this->never(), $this->never()] $this->assertSame(
]; call_user_func($this->closure),
$this->plugin->aroundRenderResult($this->subject, $this->closure, $this->response)
);
}
public function testAroundResultWithPlugin()
{
$this->registry->expects($this->once())->method('registry')->with('use_page_cache_plugin')->willReturn(true);
$this->state->expects($this->once())->method('getMode')->willReturn(null);
$this->header->expects($this->any())->method('getFieldValue')->willReturn('tag,tag');
$this->response->expects($this->once())->method('clearHeader')->with('X-Magento-Tags');
$this->response->expects($this->once())->method('setHeader')->with(
'X-Magento-Tags',
'tag,' . \Magento\PageCache\Model\Cache\Type::CACHE_TAG
);
$this->kernel->expects($this->once())->method('process')->with($this->response);
$result = call_user_func($this->closure);
$this->assertSame($result, $this->plugin->aroundRenderResult($this->subject, $this->closure, $this->response));
}
public function testAroundResultWithPluginDeveloperMode()
{
$this->registry->expects($this->once())->method('registry')->with('use_page_cache_plugin')->willReturn(true);
$this->state->expects($this->once())->method('getMode')
->willReturn(\Magento\Framework\App\State::MODE_DEVELOPER);
$this->header->expects($this->any())->method('getFieldValue')->willReturnOnConsecutiveCalls('test', 'tag,tag2');
$this->response->expects($this->any())->method('setHeader')->withConsecutive(
['X-Magento-Cache-Control', 'test'],
['X-Magento-Cache-Debug', 'MISS', true],
['X-Magento-Tags', 'tag,tag2,' . \Magento\PageCache\Model\Cache\Type::CACHE_TAG]
);
$this->response->expects($this->once())->method('clearHeader')->with('X-Magento-Tags');
$this->registry->expects($this->once())->method('registry')->with('use_page_cache_plugin')
->will($this->returnValue(true));
$this->kernel->expects($this->once())->method('process')->with($this->response);
$result = call_user_func($this->closure);
$this->assertSame($result, $this->plugin->aroundRenderResult($this->subject, $this->closure, $this->response));
} }
} }
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment