Skip to content

Commit bd4407c

Browse files
author
Kopylova,Olga(okopylova)
committed
Merge pull request #113 from magento-extensibility/develop
[Extensibility] bug-mania
2 parents 0d08a72 + 7dbdf6c commit bd4407c

File tree

3 files changed

+88
-7
lines changed

3 files changed

+88
-7
lines changed

app/code/Magento/PageCache/Model/App/PageCachePlugin.php

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,17 @@
66

77
namespace Magento\PageCache\Model\App;
88

9+
use Magento\Framework\Exception\LocalizedException;
10+
911
class PageCachePlugin
1012
{
1113
/**
12-
* Attach FPC tag to all saved entries to enable cache type management
14+
* Label for compressed cache entries
15+
*/
16+
const COMPRESSION_PREFIX = 'COMPRESSED_CACHE_';
17+
18+
/**
19+
* Enable type management by adding type tag, and enable cache compression
1320
*
1421
* @param \Magento\Framework\App\PageCache\Cache $subject
1522
* @param string $data
@@ -27,7 +34,44 @@ public function beforeSave(
2734
$tags = [],
2835
$lifeTime = null
2936
) {
37+
$data = $this->handleCompression($data);
3038
$tags[] = \Magento\PageCache\Model\Cache\Type::CACHE_TAG;
3139
return [$data, $identifier, $tags, $lifeTime];
3240
}
41+
42+
/**
43+
* Enable cache de-compression
44+
*
45+
* @param \Magento\Framework\App\PageCache\Cache $subject
46+
* @param string $result
47+
* @return string|bool
48+
* @throws \Magento\Framework\Exception\LocalizedException
49+
*
50+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
51+
*/
52+
public function afterLoad(
53+
\Magento\Framework\App\PageCache\Cache $subject,
54+
$result
55+
) {
56+
if ($result && strpos($result, self::COMPRESSION_PREFIX) === 0) {
57+
$result = function_exists('gzuncompress')
58+
? gzuncompress(substr($result, strlen(self::COMPRESSION_PREFIX)))
59+
: false;
60+
}
61+
return $result;
62+
}
63+
64+
/**
65+
* Label compressed entries and check if gzcompress exists
66+
*
67+
* @param string $data
68+
* @return string
69+
*/
70+
private function handleCompression($data)
71+
{
72+
if (function_exists('gzcompress')) {
73+
$data = self::COMPRESSION_PREFIX . gzcompress($data);
74+
}
75+
return $data;
76+
}
3377
}

app/code/Magento/PageCache/Test/Unit/Model/App/PageCachePluginTest.php

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,61 @@
77
namespace Magento\PageCache\Test\Unit\Model\App;
88

99
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
10+
use Magento\PageCache\Model\App\PageCachePlugin;
1011
use Magento\PageCache\Model\Cache\Type;
1112

1213
class PageCachePluginTest extends \PHPUnit_Framework_TestCase
1314
{
14-
public function testBeforeSave()
15+
/** @var PageCachePlugin */
16+
private $plugin;
17+
18+
/** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\App\PageCache\Cache*/
19+
private $subjectMock;
20+
21+
public function setUp()
1522
{
16-
/** @var \Magento\PageCache\Model\App\PageCachePlugin $plugin */
17-
$plugin = (new ObjectManager($this))->getObject('\Magento\PageCache\Model\App\PageCachePlugin');
18-
$subjectMock = $this->getMockBuilder('\Magento\Framework\App\PageCache\Cache')
23+
$this->plugin = (new ObjectManager($this))->getObject('\Magento\PageCache\Model\App\PageCachePlugin');
24+
$this->subjectMock = $this->getMockBuilder('\Magento\Framework\App\PageCache\Cache')
1925
->disableOriginalConstructor()
2026
->getMock();
27+
}
28+
29+
public function testBeforeSaveAddTag()
30+
{
2131
$initTags = ['tag', 'otherTag'];
22-
$result = $plugin->beforeSave($subjectMock, 'data', 'identifier', $initTags);
32+
$result = $this->plugin->beforeSave($this->subjectMock, 'data', 'identifier', $initTags);
2333
$tags = isset($result[2]) ? $result[2] : null;
2434
$expectedTags = array_merge($initTags, [Type::CACHE_TAG]);
2535
$this->assertNotNull($tags);
2636
foreach ($expectedTags as $expected) {
2737
$this->assertContains($expected, $tags);
2838
}
2939
}
40+
41+
public function testBeforeSaveCompression()
42+
{
43+
$data = 'raw-data';
44+
$expected = PageCachePlugin::COMPRESSION_PREFIX . gzcompress($data);
45+
$result = $this->plugin->beforeSave($this->subjectMock, $data, 'id');
46+
$resultData = $result[0];
47+
$this->assertSame($resultData, $expected);
48+
}
49+
50+
/**
51+
* @dataProvider afterSaveDataProvider
52+
* @param string $dataw
53+
* @param string $initResult
54+
*/
55+
public function testAfterSaveDecompression($data, $initResult)
56+
{
57+
$this->assertSame($data, $this->plugin->afterLoad($this->subjectMock, $initResult));
58+
}
59+
60+
public function afterSaveDataProvider()
61+
{
62+
return [
63+
'Compressed cache' => ['raw-data', PageCachePlugin::COMPRESSION_PREFIX . gzcompress('raw-data')],
64+
'Non-compressed cache' => ['raw-data', 'raw-data']
65+
];
66+
}
3067
}

app/code/Magento/PageCache/etc/di.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111
type="Magento\PageCache\Model\App\CacheIdentifierPlugin" sortOrder="10"/>
1212
</type>
1313
<type name="Magento\Framework\App\PageCache\Cache">
14-
<plugin name="fpc-tag-addition-plugin" type="Magento\PageCache\Model\App\PageCachePlugin"/>
14+
<plugin name="fpc-type-plugin" type="Magento\PageCache\Model\App\PageCachePlugin"/>
1515
</type>
1616
</config>

0 commit comments

Comments
 (0)