Skip to content

Commit 3e937e7

Browse files
Merge pull request #844 from magento-falcons/MAGETWO-64548
Fixed issue: - MAGETWO-64548: Fix backward incompatible changes for Scope/Validator class
2 parents d28b5d0 + 5c50a2a commit 3e937e7

File tree

5 files changed

+254
-16
lines changed

5 files changed

+254
-16
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Framework\App\Config\Scope;
7+
8+
use InvalidArgumentException;
9+
use Magento\Framework\App\Config\ScopeConfigInterface;
10+
use Magento\Framework\App\Scope\ValidatorInterface;
11+
use Magento\Framework\App\ScopeResolverPool;
12+
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\Framework\Exception\NoSuchEntityException;
14+
use Magento\Framework\Phrase;
15+
16+
/**
17+
* @deprecated Added in order to avoid backward incompatibility because class was moved to another directory.
18+
* @see \Magento\Framework\App\Scope\Validator
19+
*/
20+
class Validator implements ValidatorInterface
21+
{
22+
/**
23+
* @var ScopeResolverPool
24+
*/
25+
private $scopeResolverPool;
26+
27+
/**
28+
* @param ScopeResolverPool $scopeResolverPool
29+
*/
30+
public function __construct(ScopeResolverPool $scopeResolverPool)
31+
{
32+
$this->scopeResolverPool = $scopeResolverPool;
33+
}
34+
35+
/**
36+
* {@inheritdoc}
37+
*/
38+
public function isValid($scope, $scopeCode = null)
39+
{
40+
if ($scope === ScopeConfigInterface::SCOPE_TYPE_DEFAULT && empty($scopeCode)) {
41+
return true;
42+
}
43+
44+
if ($scope === ScopeConfigInterface::SCOPE_TYPE_DEFAULT && !empty($scopeCode)) {
45+
throw new LocalizedException(new Phrase(
46+
'The "%1" scope can\'t include a scope code. Try again without entering a scope code.',
47+
[ScopeConfigInterface::SCOPE_TYPE_DEFAULT]
48+
));
49+
}
50+
51+
if (empty($scope)) {
52+
throw new LocalizedException(new Phrase('Enter a scope before proceeding.'));
53+
}
54+
55+
$this->validateScopeCode($scopeCode);
56+
57+
try {
58+
$scopeResolver = $this->scopeResolverPool->get($scope);
59+
$scopeResolver->getScope($scopeCode)->getId();
60+
} catch (InvalidArgumentException $e) {
61+
throw new LocalizedException(new Phrase('The "%1" value doesn\'t exist. Enter another value.', [$scope]));
62+
} catch (NoSuchEntityException $e) {
63+
throw new LocalizedException(
64+
new Phrase('The "%1" value doesn\'t exist. Enter another value.', [$scopeCode])
65+
);
66+
}
67+
68+
return true;
69+
}
70+
71+
/**
72+
* Validate scope code
73+
* Throw exception if not valid.
74+
*
75+
* @param string $scopeCode
76+
* @return void
77+
* @throws LocalizedException if scope code is empty or has a wrong format
78+
*/
79+
private function validateScopeCode($scopeCode)
80+
{
81+
if (empty($scopeCode)) {
82+
throw new LocalizedException(new Phrase('Enter a scope code before proceeding.'));
83+
}
84+
85+
if (!preg_match('/^[a-z]+[a-z0-9_]*$/', $scopeCode)) {
86+
throw new LocalizedException(new Phrase(
87+
'The scope code can include only lowercase letters (a-z), numbers (0-9) and underscores (_). '
88+
. 'Also, the first character must be a letter.'
89+
));
90+
}
91+
}
92+
}

lib/internal/Magento/Framework/App/Scope/Validator.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Magento\Framework\App\Config\ScopeConfigInterface;
1010
use Magento\Framework\App\ScopeResolverPool;
1111
use Magento\Framework\Exception\NoSuchEntityException;
12-
use Magento\Framework\Exception\ValidatorException;
12+
use Magento\Framework\Exception\LocalizedException;
1313
use Magento\Framework\Phrase;
1414

1515
/**
@@ -40,14 +40,14 @@ public function isValid($scope, $scopeCode = null)
4040
}
4141

4242
if ($scope === ScopeConfigInterface::SCOPE_TYPE_DEFAULT && !empty($scopeCode)) {
43-
throw new ValidatorException(new Phrase(
43+
throw new LocalizedException(new Phrase(
4444
'The "%1" scope can\'t include a scope code. Try again without entering a scope code.',
4545
[ScopeConfigInterface::SCOPE_TYPE_DEFAULT]
4646
));
4747
}
4848

4949
if (empty($scope)) {
50-
throw new ValidatorException(new Phrase('Enter a scope before proceeding.'));
50+
throw new LocalizedException(new Phrase('Enter a scope before proceeding.'));
5151
}
5252

5353
$this->validateScopeCode($scopeCode);
@@ -56,9 +56,9 @@ public function isValid($scope, $scopeCode = null)
5656
$scopeResolver = $this->scopeResolverPool->get($scope);
5757
$scopeResolver->getScope($scopeCode)->getId();
5858
} catch (InvalidArgumentException $e) {
59-
throw new ValidatorException(new Phrase('The "%1" value doesn\'t exist. Enter another value.', [$scope]));
59+
throw new LocalizedException(new Phrase('The "%1" value doesn\'t exist. Enter another value.', [$scope]));
6060
} catch (NoSuchEntityException $e) {
61-
throw new ValidatorException(
61+
throw new LocalizedException(
6262
new Phrase('The "%1" value doesn\'t exist. Enter another value.', [$scopeCode])
6363
);
6464
}
@@ -72,16 +72,16 @@ public function isValid($scope, $scopeCode = null)
7272
*
7373
* @param string $scopeCode
7474
* @return void
75-
* @throws ValidatorException if scope code is empty or has a wrong format
75+
* @throws LocalizedException if scope code is empty or has a wrong format
7676
*/
7777
private function validateScopeCode($scopeCode)
7878
{
7979
if (empty($scopeCode)) {
80-
throw new ValidatorException(new Phrase('Enter a scope code before proceeding.'));
80+
throw new LocalizedException(new Phrase('Enter a scope code before proceeding.'));
8181
}
8282

8383
if (!preg_match('/^[a-z]+[a-z0-9_]*$/', $scopeCode)) {
84-
throw new ValidatorException(new Phrase(
84+
throw new LocalizedException(new Phrase(
8585
'The scope code can include only lowercase letters (a-z), numbers (0-9) and underscores (_). '
8686
. 'Also, the first character must be a letter.'
8787
));

lib/internal/Magento/Framework/App/Scope/ValidatorInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
namespace Magento\Framework\App\Scope;
77

8-
use Magento\Framework\Exception\ValidatorException;
8+
use Magento\Framework\Exception\LocalizedException;
99

1010
/**
1111
* Interface Validator for validating scope and scope code
@@ -19,7 +19,7 @@ interface ValidatorInterface
1919
* @param string $scope
2020
* @param string $scopeCode
2121
* @return boolean
22-
* @throws ValidatorException
22+
* @throws LocalizedException
2323
*/
2424
public function isValid($scope, $scopeCode = null);
2525
}

lib/internal/Magento/Framework/App/Test/Unit/Config/Scope/ValidatorTest.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
use Magento\Framework\App\Scope\Validator;
1414
use PHPUnit_Framework_MockObject_MockObject as MockObject;
1515

16+
/**
17+
* @deprecated As tested model class was moved to another directory,
18+
* unit test was created in the appropriate directory.
19+
* @see \Magento\Framework\App\Test\Unit\Scope\ValidatorTest
20+
*/
1621
class ValidatorTest extends \PHPUnit_Framework_TestCase
1722
{
1823
/**
@@ -66,7 +71,7 @@ public function testIsValidDefault()
6671
}
6772

6873
/**
69-
* @expectedException \Magento\Framework\Exception\ValidatorException
74+
* @expectedException \Magento\Framework\Exception\LocalizedException
7075
* @expectedExceptionMessage The "default" scope can't include a scope code. Try again without entering a scope
7176
*/
7277
public function testNotEmptyScopeCodeForDefaultScope()
@@ -75,7 +80,7 @@ public function testNotEmptyScopeCodeForDefaultScope()
7580
}
7681

7782
/**
78-
* @expectedException \Magento\Framework\Exception\ValidatorException
83+
* @expectedException \Magento\Framework\Exception\LocalizedException
7984
* @expectedExceptionMessage Enter a scope before proceeding.
8085
*/
8186
public function testEmptyScope()
@@ -84,7 +89,7 @@ public function testEmptyScope()
8489
}
8590

8691
/**
87-
* @expectedException \Magento\Framework\Exception\ValidatorException
92+
* @expectedException \Magento\Framework\Exception\LocalizedException
8893
* @expectedExceptionMessage Enter a scope code before proceeding.
8994
*/
9095
public function testEmptyScopeCode()
@@ -93,7 +98,7 @@ public function testEmptyScopeCode()
9398
}
9499

95100
/**
96-
* @expectedException \Magento\Framework\Exception\ValidatorException
101+
* @expectedException \Magento\Framework\Exception\LocalizedException
97102
* @expectedExceptionMessage The scope code can include only lowercase letters (a-z), numbers (0-9) and underscores
98103
*/
99104
public function testWrongScopeCodeFormat()
@@ -102,7 +107,7 @@ public function testWrongScopeCodeFormat()
102107
}
103108

104109
/**
105-
* @expectedException \Magento\Framework\Exception\ValidatorException
110+
* @expectedException \Magento\Framework\Exception\LocalizedException
106111
* @expectedExceptionMessage The "not_default_scope" value doesn't exist. Enter another value.
107112
*/
108113
public function testScopeNotExist()
@@ -117,7 +122,7 @@ public function testScopeNotExist()
117122
}
118123

119124
/**
120-
* @expectedException \Magento\Framework\Exception\ValidatorException
125+
* @expectedException \Magento\Framework\Exception\LocalizedException
121126
* @expectedExceptionMessage The "not_exist_scope_code" value doesn't exist. Enter another value.
122127
*/
123128
public function testScopeCodeNotExist()
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Framework\App\Test\Unit\Scope;
7+
8+
use Magento\Framework\App\Config\ScopeConfigInterface;
9+
use Magento\Framework\App\ScopeInterface;
10+
use Magento\Framework\App\ScopeResolverInterface;
11+
use Magento\Framework\App\ScopeResolverPool;
12+
use Magento\Framework\Exception\NoSuchEntityException;
13+
use Magento\Framework\App\Scope\Validator;
14+
use PHPUnit_Framework_MockObject_MockObject as MockObject;
15+
16+
class ValidatorTest extends \PHPUnit_Framework_TestCase
17+
{
18+
/**
19+
* @var Validator
20+
*/
21+
private $model;
22+
23+
/**
24+
* @var ScopeResolverPool|MockObject
25+
*/
26+
private $scopeResolverPoolMock;
27+
28+
/**
29+
* @inheritdoc
30+
*/
31+
protected function setUp()
32+
{
33+
$this->scopeResolverPoolMock = $this->getMockBuilder(ScopeResolverPool::class)
34+
->disableOriginalConstructor()
35+
->getMock();
36+
37+
$this->model = new Validator(
38+
$this->scopeResolverPoolMock
39+
);
40+
}
41+
42+
public function testIsValid()
43+
{
44+
$scope = 'not_default_scope';
45+
$scopeCode = 'not_exist_scope_code';
46+
47+
$scopeResolver = $this->getMockBuilder(ScopeResolverInterface::class)
48+
->getMockForAbstractClass();
49+
$scopeObject = $this->getMockBuilder(ScopeInterface::class)
50+
->getMockForAbstractClass();
51+
$scopeResolver->expects($this->once())
52+
->method('getScope')
53+
->with($scopeCode)
54+
->willReturn($scopeObject);
55+
$this->scopeResolverPoolMock->expects($this->once())
56+
->method('get')
57+
->with($scope)
58+
->willReturn($scopeResolver);
59+
60+
$this->assertTrue($this->model->isValid($scope, $scopeCode));
61+
}
62+
63+
public function testIsValidDefault()
64+
{
65+
$this->assertTrue($this->model->isValid(ScopeConfigInterface::SCOPE_TYPE_DEFAULT));
66+
}
67+
68+
/**
69+
* @expectedException \Magento\Framework\Exception\LocalizedException
70+
* @expectedExceptionMessage The "default" scope can't include a scope code. Try again without entering a scope
71+
*/
72+
public function testNotEmptyScopeCodeForDefaultScope()
73+
{
74+
$this->model->isValid(ScopeConfigInterface::SCOPE_TYPE_DEFAULT, 'some_code');
75+
}
76+
77+
/**
78+
* @expectedException \Magento\Framework\Exception\LocalizedException
79+
* @expectedExceptionMessage Enter a scope before proceeding.
80+
*/
81+
public function testEmptyScope()
82+
{
83+
$this->model->isValid('', 'some_code');
84+
}
85+
86+
/**
87+
* @expectedException \Magento\Framework\Exception\LocalizedException
88+
* @expectedExceptionMessage Enter a scope code before proceeding.
89+
*/
90+
public function testEmptyScopeCode()
91+
{
92+
$this->model->isValid('not_default_scope', '');
93+
}
94+
95+
/**
96+
* @expectedException \Magento\Framework\Exception\LocalizedException
97+
* @expectedExceptionMessage The scope code can include only lowercase letters (a-z), numbers (0-9) and underscores
98+
*/
99+
public function testWrongScopeCodeFormat()
100+
{
101+
$this->model->isValid('not_default_scope', '123');
102+
}
103+
104+
/**
105+
* @expectedException \Magento\Framework\Exception\LocalizedException
106+
* @expectedExceptionMessage The "not_default_scope" value doesn't exist. Enter another value.
107+
*/
108+
public function testScopeNotExist()
109+
{
110+
$scope = 'not_default_scope';
111+
$this->scopeResolverPoolMock->expects($this->once())
112+
->method('get')
113+
->with($scope)
114+
->willThrowException(new \InvalidArgumentException());
115+
116+
$this->model->isValid($scope, 'scope_code');
117+
}
118+
119+
/**
120+
* @expectedException \Magento\Framework\Exception\LocalizedException
121+
* @expectedExceptionMessage The "not_exist_scope_code" value doesn't exist. Enter another value.
122+
*/
123+
public function testScopeCodeNotExist()
124+
{
125+
$scope = 'not_default_scope';
126+
$scopeCode = 'not_exist_scope_code';
127+
128+
$scopeResolver = $this->getMockBuilder(ScopeResolverInterface::class)
129+
->getMockForAbstractClass();
130+
$scopeResolver->expects($this->once())
131+
->method('getScope')
132+
->with($scopeCode)
133+
->willThrowException(new NoSuchEntityException());
134+
$this->scopeResolverPoolMock->expects($this->once())
135+
->method('get')
136+
->with($scope)
137+
->willReturn($scopeResolver);
138+
139+
$this->model->isValid($scope, $scopeCode);
140+
}
141+
}

0 commit comments

Comments
 (0)