Skip to content

Commit 007a7f1

Browse files
committed
MAGETWO-94469: Wrong session messages behavior
1 parent 4ebfa1d commit 007a7f1

File tree

2 files changed

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

2 files changed

+148
-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: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\PageCache\Test\Unit\Model\System\Config\Backend;
8+
9+
use Magento\PageCache\Model\System\Config\Backend\Ttl;
10+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
11+
use Magento\Framework\App\Config\ScopeConfigInterface;
12+
use Magento\Framework\Escaper;
13+
use Magento\Framework\Exception\LocalizedException;
14+
15+
class TtlTest extends \PHPUnit_Framework_TestCase
16+
{
17+
/**
18+
* @var Ttl
19+
*/
20+
private $ttl;
21+
22+
/*
23+
* @var \Magento\Framework\Escaper|\PHPUnit_Framework_MockObject_MockObject
24+
*/
25+
private $escaperMock;
26+
27+
/**
28+
* @inheritDoc
29+
*/
30+
protected function setUp()
31+
{
32+
$objectManager = new ObjectManager($this);
33+
$configMock = $this->getMockForAbstractClass(ScopeConfigInterface::class);
34+
$configMock->expects($this->any())
35+
->method('getValue')
36+
->with('system/full_page_cache/default')
37+
->willReturn(['ttl' => 86400]);
38+
39+
$this->escaperMock = $this->getMockBuilder(Escaper::class)->disableOriginalConstructor()->getMock();
40+
41+
$this->ttl = $objectManager->getObject(
42+
Ttl::class,
43+
[
44+
'config' => $configMock,
45+
'data' => ['field' => 'ttl'],
46+
'escaper' => $this->escaperMock,
47+
]
48+
);
49+
}
50+
51+
/**
52+
* @return array
53+
*/
54+
public function getValidValues()
55+
{
56+
return [
57+
['3600', '3600'],
58+
['10000', '10000'],
59+
['100000', '100000'],
60+
['1000000', '1000000'],
61+
];
62+
}
63+
64+
/**
65+
* @param string $value
66+
* @param string $expectedValue
67+
* @return void
68+
* @dataProvider getValidValues
69+
*/
70+
public function testBeforeSave($value, $expectedValue)
71+
{
72+
$this->ttl->setValue($value);
73+
$this->ttl->beforeSave();
74+
$this->assertEquals($expectedValue, $this->ttl->getValue());
75+
}
76+
77+
/**
78+
* @return array
79+
*/
80+
public function getInvalidValues()
81+
{
82+
return [
83+
['<script>alert(1)</script>'],
84+
['apple'],
85+
['123 street'],
86+
['-123'],
87+
];
88+
}
89+
90+
/**
91+
* @param string $value
92+
* @return void
93+
* @dataProvider getInvalidValues
94+
*/
95+
public function testBeforeSaveInvalid($value)
96+
{
97+
$this->ttl->setValue($value);
98+
$this->escaperMock->expects($this->any())->method('escapeHtml')->with($value)->willReturn($value);
99+
$expMessage = sprintf(
100+
'Ttl value "%s" is not valid. Please use only numbers equal or greater than zero.',
101+
$value
102+
);
103+
$this->setExpectedException(LocalizedException::class, $expMessage);
104+
$this->ttl->beforeSave();
105+
}
106+
}

0 commit comments

Comments
 (0)