forked from ezsystems/ezpublish-kernel
/
CustomTagsValidatorTest.php
231 lines (218 loc) · 7.27 KB
/
CustomTagsValidatorTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
/**
* This file is part of the eZ Publish Kernel package.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace eZ\Publish\Core\FieldType\Tests\RichText;
use DOMDocument;
use eZ\Publish\Core\FieldType\RichText\CustomTagsValidator;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Yaml\Yaml;
/**
* Test RichText CustomTagsValidator.
*
* @see \eZ\Publish\Core\FieldType\RichText\CustomTagsValidator
*/
class CustomTagsValidatorTest extends TestCase
{
/**
* @var \eZ\Publish\Core\FieldType\RichText\CustomTagsValidator
*/
private $validator;
public function setUp()
{
// reuse Custom Tags configuration from common test settings
$commonSettings = Yaml::parseFile(__DIR__ . '/../../../settings/tests/common.yml');
$customTagsConfiguration = $commonSettings['parameters']['ezplatform.ezrichtext.custom_tags'];
$this->validator = new CustomTagsValidator($customTagsConfiguration);
}
/**
* Test validating DocBook document containing Custom Tags.
*
* @covers \eZ\Publish\Core\FieldType\RichText\CustomTagsValidator::validateDocument
*
* @dataProvider providerForTestValidateDocument
*
* @param \DOMDocument $document
* @param array $expectedErrors
*/
public function testValidateDocument(DOMDocument $document, array $expectedErrors)
{
self::assertEquals(
$expectedErrors,
$this->validator->validateDocument($document)
);
}
/**
* Data provider for testValidateDocument.
*
* @see testValidateDocument
*
* @return array
*/
public function providerForTestValidateDocument()
{
return [
[
$this->createDocument(
<<<DOCBOOK
<?xml version="1.0" encoding="UTF-8"?>
<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:ezxhtml="http://ez.no/xmlns/ezpublish/docbook/xhtml"
xmlns:ezcustom="http://ez.no/xmlns/ezpublish/docbook/custom"
version="5.0-variant ezpublish-1.0">
<eztemplate name="">
</eztemplate>
</section>
DOCBOOK
),
[
'Missing RichText Custom Tag name',
],
],
[
$this->createDocument(
<<<DOCBOOK
<?xml version="1.0" encoding="UTF-8"?>
<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:ezxhtml="http://ez.no/xmlns/ezpublish/docbook/xhtml"
xmlns:ezcustom="http://ez.no/xmlns/ezpublish/docbook/custom"
version="5.0-variant ezpublish-1.0">
<eztemplate name="video">
<ezcontent>Content</ezcontent>
<ezconfig>
<ezvalue key="">Test</ezvalue>
<ezvalue key="title">Test</ezvalue>
<ezvalue key="width">360</ezvalue>
</ezconfig>
</eztemplate>
</section>
DOCBOOK
),
[
"Missing attribute name for RichText Custom Tag 'video'",
],
],
[
$this->createDocument(
<<<DOCBOOK
<?xml version="1.0" encoding="UTF-8"?>
<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:ezxhtml="http://ez.no/xmlns/ezpublish/docbook/xhtml"
xmlns:ezcustom="http://ez.no/xmlns/ezpublish/docbook/custom"
version="5.0-variant ezpublish-1.0">
<eztemplate name="video">
<ezcontent>Content</ezcontent>
<ezconfig>
<ezvalue key="title">Test</ezvalue>
<ezvalue key="unknown">Test</ezvalue>
<ezvalue key="width">360</ezvalue>
</ezconfig>
</eztemplate>
</section>
DOCBOOK
),
[
"Unknown attribute 'unknown' of RichText Custom Tag 'video'",
],
],
[
$this->createDocument(
<<<DOCBOOK
<?xml version="1.0" encoding="UTF-8"?>
<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:ezxhtml="http://ez.no/xmlns/ezpublish/docbook/xhtml"
xmlns:ezcustom="http://ez.no/xmlns/ezpublish/docbook/custom"
version="5.0-variant ezpublish-1.0">
<eztemplate name="video">
<ezcontent>Content</ezcontent>
<ezconfig>
<ezvalue key="autoplay">false</ezvalue>
</ezconfig>
</eztemplate>
</section>
DOCBOOK
),
[
"The attribute 'title' of RichText Custom Tag 'video' cannot be empty",
"The attribute 'width' of RichText Custom Tag 'video' cannot be empty",
],
],
[
$this->createDocument(
<<<DOCBOOK
<?xml version="1.0" encoding="UTF-8"?>
<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:ezxhtml="http://ez.no/xmlns/ezpublish/docbook/xhtml"
xmlns:ezcustom="http://ez.no/xmlns/ezpublish/docbook/custom"
version="5.0-variant ezpublish-1.0">
<eztemplate name="">
</eztemplate>
<eztemplate name="undefined_tag">
</eztemplate>
<eztemplate name="video">
<ezcontent>Content</ezcontent>
<ezconfig>
<ezvalue key="">Test</ezvalue>
</ezconfig>
</eztemplate>
<eztemplate name="equation">
<ezcontent>Content</ezcontent>
<ezconfig>
<ezvalue key="name">Test</ezvalue>
<ezvalue key="unknown">Test</ezvalue>
<ezvalue key="processor">latex</ezvalue>
</ezconfig>
</eztemplate>
</section>
DOCBOOK
),
[
'Missing RichText Custom Tag name',
"Missing attribute name for RichText Custom Tag 'video'",
"The attribute 'title' of RichText Custom Tag 'video' cannot be empty",
"The attribute 'width' of RichText Custom Tag 'video' cannot be empty",
"Unknown attribute 'unknown' of RichText Custom Tag 'equation'",
],
],
];
}
/**
* Test that defined but not configured yet Custom Tag doesn't cause validation error.
*/
public function testValidateDocumentAcceptsLegacyTags()
{
$document = $this->createDocument(
<<<DOCBOOK
<?xml version="1.0" encoding="UTF-8"?>
<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:ezxhtml="http://ez.no/xmlns/ezpublish/docbook/xhtml"
xmlns:ezcustom="http://ez.no/xmlns/ezpublish/docbook/custom"
version="5.0-variant ezpublish-1.0">
<eztemplate name="undefined_tag">
<ezcontent>Undefined</ezcontent>
<ezconfig>
<ezvalue key="title">Test</ezvalue>
</ezconfig>
</eztemplate>
</section>
DOCBOOK
);
self::assertEmpty($this->validator->validateDocument($document));
}
/**
* @param string $source XML source
*
* @return \DOMDocument
*/
protected function createDocument($source)
{
$document = new DOMDocument();
$document->preserveWhiteSpace = false;
$document->formatOutput = false;
$document->loadXml($source, LIBXML_NOENT);
return $document;
}
}