Skip to content

Commit aad3f99

Browse files
author
Oleksandr Gorkun
committed
MAGETWO-88599: [Backport for 2.1.x] Varnish Config Access List
1 parent fa0f4cd commit aad3f99

File tree

3 files changed

+125
-1
lines changed

3 files changed

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

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)