1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* File containing a test class. |
5
|
|
|
* |
6
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
7
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
namespace eZ\Publish\Core\REST\Server\Tests\Output\ValueObjectVisitor; |
10
|
|
|
|
11
|
|
|
use eZ\Publish\Core\Base\Exceptions\ContentFieldValidationException as CoreContentFieldValidationException; |
12
|
|
|
use eZ\Publish\Core\FieldType\ValidationError; |
13
|
|
|
use eZ\Publish\Core\REST\Server\Output\ValueObjectVisitor; |
14
|
|
|
use eZ\Publish\Core\REST\Server\Exceptions; |
15
|
|
|
use Symfony\Component\Translation\Translator; |
16
|
|
|
|
17
|
|
|
class ContentFieldValidationExceptionTest extends ExceptionTest |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Test the ContentFieldValidationException visitor. |
21
|
|
|
* |
22
|
|
|
* @return string |
23
|
|
|
*/ |
24
|
|
|
public function testVisit() |
25
|
|
|
{ |
26
|
|
|
$visitor = $this->getVisitor(); |
27
|
|
|
$generator = $this->getGenerator(); |
28
|
|
|
|
29
|
|
|
$generator->startDocument(null); |
30
|
|
|
|
31
|
|
|
$exception = $this->getException(); |
32
|
|
|
|
33
|
|
|
$visitor->visit( |
34
|
|
|
$this->getVisitorMock(), |
|
|
|
|
35
|
|
|
$generator, |
36
|
|
|
$exception |
37
|
|
|
); |
38
|
|
|
|
39
|
|
|
$result = $generator->endDocument(null); |
40
|
|
|
|
41
|
|
|
$this->assertNotNull($result); |
42
|
|
|
|
43
|
|
|
return $result; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Test if result contains ErrorMessage element and description. |
48
|
|
|
* |
49
|
|
|
* @param string $result |
50
|
|
|
* |
51
|
|
|
* @depends testVisit |
52
|
|
|
*/ |
53
|
|
|
public function testResultContainsErrorDescription($result) |
54
|
|
|
{ |
55
|
|
|
$this->assertXMLTag( |
56
|
|
|
[ |
57
|
|
|
'tag' => 'errorDescription', |
58
|
|
|
'content' => $this->getExpectedDescription(), |
59
|
|
|
], |
60
|
|
|
$result, |
61
|
|
|
'Missing <errorDescription> element.' |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Test if result contains ErrorMessage element and details. |
67
|
|
|
* |
68
|
|
|
* @param string $result |
69
|
|
|
* |
70
|
|
|
* @depends testVisit |
71
|
|
|
*/ |
72
|
|
|
public function testResultContainsErrorDetails($result) |
73
|
|
|
{ |
74
|
|
|
$this->assertXMLTag( |
75
|
|
|
[ |
76
|
|
|
'tag' => 'errorDetails', |
77
|
|
|
], |
78
|
|
|
$result, |
79
|
|
|
'Missing <errorDetails> element.' |
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
$this->assertXMLTag( |
83
|
|
|
[ |
84
|
|
|
'tag' => 'field', |
85
|
|
|
], |
86
|
|
|
$result, |
87
|
|
|
'Missing <field> element.' |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get expected status code. |
93
|
|
|
* |
94
|
|
|
* @return int |
95
|
|
|
*/ |
96
|
|
|
protected function getExpectedStatusCode() |
97
|
|
|
{ |
98
|
|
|
return 400; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get expected message. |
103
|
|
|
* |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
protected function getExpectedMessage() |
107
|
|
|
{ |
108
|
|
|
return 'Bad Request'; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get expected description. |
113
|
|
|
* |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
|
|
protected function getExpectedDescription() |
117
|
|
|
{ |
118
|
|
|
return 'Content fields did not validate'; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Gets the exception. |
123
|
|
|
* |
124
|
|
|
* @return \Exception |
125
|
|
|
*/ |
126
|
|
|
protected function getException() |
127
|
|
|
{ |
128
|
|
|
return new Exceptions\ContentFieldValidationException( |
129
|
|
|
new CoreContentFieldValidationException([ |
|
|
|
|
130
|
|
|
1 => [ |
131
|
|
|
'eng-GB' => new ValidationError( |
132
|
|
|
"Value for required field definition '%identifier%' with language '%languageCode%' is empty", |
133
|
|
|
null, |
134
|
|
|
['%identifier%' => 'name', '%languageCode%' => 'eng-GB'], |
135
|
|
|
'empty' |
136
|
|
|
), |
137
|
|
|
], |
138
|
|
|
2 => [ |
139
|
|
|
'eng-GB' => new ValidationError( |
140
|
|
|
'The value must be a valid email address.', |
141
|
|
|
null, |
142
|
|
|
[], |
143
|
|
|
'email' |
144
|
|
|
), |
145
|
|
|
], |
146
|
|
|
]) |
147
|
|
|
); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Gets the exception visitor. |
152
|
|
|
* |
153
|
|
|
* @return \eZ\Publish\Core\REST\Server\Output\ValueObjectVisitor\ContentFieldValidationException |
154
|
|
|
*/ |
155
|
|
|
protected function internalGetVisitor() |
156
|
|
|
{ |
157
|
|
|
return new ValueObjectVisitor\ContentFieldValidationException(false, new Translator('eng-GB')); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.