Skip to content

Commit d66cf04

Browse files
Merge MAGETWO-88597 into 2.3-bugfixes-240918
2 parents 49c17cf + 2597d6c commit d66cf04

File tree

3 files changed

+130
-1
lines changed

3 files changed

+130
-1
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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\Model\System\Config\Backend;
10+
11+
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Framework\Phrase;
13+
14+
/**
15+
* Access List config field.
16+
*/
17+
class AccessList extends Varnish
18+
{
19+
/**
20+
* @inheritDoc
21+
*/
22+
public function beforeSave()
23+
{
24+
parent::beforeSave();
25+
26+
$value = $this->getValue();
27+
if (!is_string($value) || !preg_match('/^[\w\s\.\-\,\:]+$/', $value)) {
28+
throw new LocalizedException(
29+
new Phrase(
30+
'Access List value "%1" is not valid. '
31+
.'Please use only IP addresses and host names.',
32+
[$value]
33+
)
34+
);
35+
}
36+
}
37+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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\AccessList;
12+
use PHPUnit\Framework\TestCase;
13+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
14+
use Magento\Framework\App\Config\ScopeConfigInterface;
15+
16+
class AccessListTest extends TestCase
17+
{
18+
/**
19+
* @var AccessList
20+
*/
21+
private $accessList;
22+
23+
/**
24+
* @inheritDoc
25+
*/
26+
protected function setUp()
27+
{
28+
$objectManager = new ObjectManager($this);
29+
$configMock = $this->getMockForAbstractClass(
30+
ScopeConfigInterface::class
31+
);
32+
$configMock->expects($this->any())
33+
->method('getValue')
34+
->with('system/full_page_cache/default')
35+
->willReturn(['access_list' => 'localhost']);
36+
$this->accessList = $objectManager->getObject(
37+
AccessList::class,
38+
[
39+
'config' => $configMock,
40+
'data' => ['field' => 'access_list']
41+
]
42+
);
43+
}
44+
45+
/**
46+
* @return array
47+
*/
48+
public function getValidValues(): array
49+
{
50+
return [
51+
['localhost', 'localhost'],
52+
[null, 'localhost'],
53+
['127.0.0.1', '127.0.0.1'],
54+
['127.0.0.1, localhost, ::2', '127.0.0.1, localhost, ::2'],
55+
];
56+
}
57+
58+
/**
59+
* @param mixed $value
60+
* @param mixed $expectedValue
61+
* @dataProvider getValidValues
62+
*/
63+
public function testBeforeSave($value, $expectedValue)
64+
{
65+
$this->accessList->setValue($value);
66+
$this->accessList->beforeSave();
67+
$this->assertEquals($expectedValue, $this->accessList->getValue());
68+
}
69+
70+
/**
71+
* @return array
72+
*/
73+
public function getInvalidValues(): array
74+
{
75+
return [
76+
['\\bull val\\'],
77+
['{*I am not an IP*}'],
78+
['{*I am not an IP*}, 127.0.0.1'],
79+
];
80+
}
81+
82+
/**
83+
* @param mixed $value
84+
* @expectedException \Magento\Framework\Exception\LocalizedException
85+
* @dataProvider getInvalidValues
86+
*/
87+
public function testBeforeSaveInvalid($value)
88+
{
89+
$this->accessList->setValue($value);
90+
$this->accessList->beforeSave();
91+
}
92+
}

app/code/Magento/PageCache/etc/adminhtml/system.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<label>Access list</label>
2121
<comment>IPs access list separated with ',' that can purge Varnish configuration for config file generation.
2222
If field is empty default value localhost will be saved.</comment>
23-
<backend_model>Magento\PageCache\Model\System\Config\Backend\Varnish</backend_model>
23+
<backend_model>Magento\PageCache\Model\System\Config\Backend\AccessList</backend_model>
2424
<depends>
2525
<field id="caching_application">1</field>
2626
</depends>

0 commit comments

Comments
 (0)