Skip to content

Commit 82d7946

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-94468' into 2.2.7-develop-pr45
2 parents 4daa54d + 226aebf commit 82d7946

File tree

2 files changed

+151
-4
lines changed
  • app/code/Magento/PageCache

2 files changed

+151
-4
lines changed

app/code/Magento/PageCache/Model/System/Config/Backend/Ttl.php

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,49 @@
66

77
namespace Magento\PageCache\Model\System\Config\Backend;
88

9+
use Magento\Framework\App\ObjectManager;
10+
use Magento\Framework\Escaper;
11+
use Magento\Framework\App\Config\ScopeConfigInterface;
12+
use Magento\Framework\Exception\LocalizedException;
13+
914
/**
10-
* Backend model for processing Public content cache lifetime settings
15+
* Backend model for processing Public content cache lifetime settings.
1116
*
1217
* Class Ttl
1318
*/
1419
class Ttl extends \Magento\Framework\App\Config\Value
1520
{
1621
/**
17-
* Throw exception if Ttl data is invalid or empty
22+
* @var Escaper
23+
*/
24+
private $escaper;
25+
26+
/**
27+
* @param \Magento\Framework\Model\Context $context
28+
* @param \Magento\Framework\Registry $registry
29+
* @param ScopeConfigInterface $config
30+
* @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
31+
* @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource
32+
* @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection
33+
* @param array $data
34+
* @param Escaper|null $escaper
35+
*/
36+
public function __construct(
37+
\Magento\Framework\Model\Context $context,
38+
\Magento\Framework\Registry $registry,
39+
ScopeConfigInterface $config,
40+
\Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
41+
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
42+
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
43+
array $data = [],
44+
Escaper $escaper = null
45+
) {
46+
parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
47+
$this->escaper = $escaper ?: ObjectManager::getInstance()->create(Escaper::class);
48+
}
49+
50+
/**
51+
* Throw exception if Ttl data is invalid or empty.
1852
*
1953
* @return $this
2054
* @throws \Magento\Framework\Exception\LocalizedException
@@ -23,10 +57,14 @@ public function beforeSave()
2357
{
2458
$value = $this->getValue();
2559
if ($value < 0 || !preg_match('/^[0-9]+$/', $value)) {
26-
throw new \Magento\Framework\Exception\LocalizedException(
27-
__('Ttl value "%1" is not valid. Please use only numbers equal or greater than zero.', $value)
60+
throw new LocalizedException(
61+
__(
62+
'Ttl value "%1" is not valid. Please use only numbers equal or greater than zero.',
63+
$this->escaper->escapeHtml($value)
64+
)
2865
);
2966
}
67+
3068
return $this;
3169
}
3270
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\PageCache\Test\Unit\Model\System\Config\Backend;
10+
11+
use Magento\PageCache\Model\System\Config\Backend\Ttl;
12+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
13+
use Magento\Framework\App\Config\ScopeConfigInterface;
14+
use Magento\Framework\Escaper;
15+
use Magento\Framework\Exception\LocalizedException;
16+
use PHPUnit\Framework\TestCase;
17+
18+
/**
19+
* Class for tesing backend model for processing Public content cache lifetime settings.
20+
*/
21+
class TtlTest extends TestCase
22+
{
23+
/**
24+
* @var Ttl
25+
*/
26+
private $ttl;
27+
28+
/*
29+
* @var \Magento\Framework\Escaper|\PHPUnit_Framework_MockObject_MockObject
30+
*/
31+
private $escaperMock;
32+
33+
/**
34+
* @inheritDoc
35+
*/
36+
protected function setUp()
37+
{
38+
$objectManager = new ObjectManager($this);
39+
$configMock = $this->getMockForAbstractClass(ScopeConfigInterface::class);
40+
$configMock->expects($this->any())
41+
->method('getValue')
42+
->with('system/full_page_cache/default')
43+
->willReturn(['ttl' => 86400]);
44+
45+
$this->escaperMock = $this->getMockBuilder(Escaper::class)->disableOriginalConstructor()->getMock();
46+
47+
$this->ttl = $objectManager->getObject(
48+
Ttl::class,
49+
[
50+
'config' => $configMock,
51+
'data' => ['field' => 'ttl'],
52+
'escaper' => $this->escaperMock,
53+
]
54+
);
55+
}
56+
57+
/**
58+
* @return array
59+
*/
60+
public function getValidValues(): array
61+
{
62+
return [
63+
['3600', '3600'],
64+
['10000', '10000'],
65+
['100000', '100000'],
66+
['1000000', '1000000'],
67+
];
68+
}
69+
70+
/**
71+
* @param string $value
72+
* @param string $expectedValue
73+
* @return void
74+
* @dataProvider getValidValues
75+
*/
76+
public function testBeforeSave(string $value, string $expectedValue)
77+
{
78+
$this->ttl->setValue($value);
79+
$this->ttl->beforeSave();
80+
$this->assertEquals($expectedValue, $this->ttl->getValue());
81+
}
82+
83+
/**
84+
* @return array
85+
*/
86+
public function getInvalidValues(): array
87+
{
88+
return [
89+
['<script>alert(1)</script>'],
90+
['apple'],
91+
['123 street'],
92+
['-123'],
93+
];
94+
}
95+
96+
/**
97+
* @param string $value
98+
* @return void
99+
* @expectedException \Magento\Framework\Exception\LocalizedException
100+
* @expectedExceptionMessageRegExp /Ttl value ".+" is not valid. Please .+ only numbers equal or greater than zero./
101+
* @dataProvider getInvalidValues
102+
*/
103+
public function testBeforeSaveInvalid(string $value)
104+
{
105+
$this->ttl->setValue($value);
106+
$this->escaperMock->expects($this->any())->method('escapeHtml')->with($value)->willReturn($value);
107+
$this->ttl->beforeSave();
108+
}
109+
}

0 commit comments

Comments
 (0)