Skip to content

Commit 79ef4a4

Browse files
author
Oleksandr Gorkun
committed
MAGETWO-88597: Varnish Config Access List
1 parent a7893f9 commit 79ef4a4

File tree

3 files changed

+128
-1
lines changed

3 files changed

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

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)