11
11
use Magento \Framework \Validation \ValidationException ;
12
12
use Magento \Framework \Validator \HTML \ConfigurableWYSIWYGValidator ;
13
13
use Magento \Framework \Validator \HTML \AttributeValidatorInterface ;
14
+ use Magento \Framework \Validator \HTML \TagValidatorInterface ;
14
15
use PHPUnit \Framework \TestCase ;
15
16
16
17
class ConfigurableWYSIWYGValidatorTest extends TestCase
@@ -23,25 +24,43 @@ class ConfigurableWYSIWYGValidatorTest extends TestCase
23
24
public function getConfigurations (): array
24
25
{
25
26
return [
26
- 'no-html ' => [['div ' ], [], [], 'just text ' , true , []],
27
- 'allowed-tag ' => [['div ' ], [], [], 'just text and <div>a div</div> ' , true , []],
27
+ 'no-html ' => [['div ' ], [], [], 'just text ' , true , [], [] ],
28
+ 'allowed-tag ' => [['div ' ], [], [], 'just text and <div>a div</div> ' , true , [], [] ],
28
29
'restricted-tag ' => [
29
30
['div ' , 'p ' ],
30
31
[],
31
32
[],
32
33
'text and <p>a p</p>, <div>a div</div>, <tr>a tr</tr> ' ,
33
34
false ,
35
+ [],
36
+ []
37
+ ],
38
+ 'restricted-tag-wtih-attr ' => [
39
+ ['div ' ],
40
+ [],
41
+ [],
42
+ 'just text and <p class="fake-class">a p</p> ' ,
43
+ false ,
44
+ [],
45
+ []
46
+ ],
47
+ 'allowed-tag-with-attr ' => [
48
+ ['div ' ],
49
+ [],
50
+ [],
51
+ 'just text and <div class="fake-class">a div</div> ' ,
52
+ false ,
53
+ [],
34
54
[]
35
55
],
36
- 'restricted-tag-wtih-attr ' => [['div ' ], [], [], 'just text and <p class="fake-class">a p</p> ' , false , []],
37
- 'allowed-tag-with-attr ' => [['div ' ], [], [], 'just text and <div class="fake-class">a div</div> ' , false , []],
38
- 'multiple-tags ' => [['div ' , 'p ' ], [], [], 'just text and <div>a div</div> and <p>a p</p> ' , true , []],
56
+ 'multiple-tags ' => [['div ' , 'p ' ], [], [], 'just text and <div>a div</div> and <p>a p</p> ' , true , [], []],
39
57
'tags-with-attrs ' => [
40
58
['div ' , 'p ' ],
41
59
['class ' , 'style ' ],
42
60
[],
43
61
'text and <div class="fake-class">a div</div> and <p style="color: blue">a p</p> ' ,
44
62
true ,
63
+ [],
45
64
[]
46
65
],
47
66
'tags-with-restricted-attrs ' => [
@@ -50,6 +69,7 @@ public function getConfigurations(): array
50
69
[],
51
70
'text and <div class="fake-class">a div</div> and <p style="color: blue">a p</p> ' ,
52
71
false ,
72
+ [],
53
73
[]
54
74
],
55
75
'tags-with-specific-attrs ' => [
@@ -59,6 +79,7 @@ public function getConfigurations(): array
59
79
'<div class="fake-class" style="color: blue">a div</div>, <a href="/some-path" class="a">an a</a> '
60
80
.', <p class="p-class">a p</p> ' ,
61
81
true ,
82
+ [],
62
83
[]
63
84
],
64
85
'tags-with-specific-restricted-attrs ' => [
@@ -67,6 +88,7 @@ public function getConfigurations(): array
67
88
['a ' => ['href ' ]],
68
89
'text and <div class="fake-class" href="what">a div</div> and <a href="/some-path" class="a">an a</a> ' ,
69
90
false ,
91
+ [],
70
92
[]
71
93
],
72
94
'invalid-tag-with-full-config ' => [
@@ -76,6 +98,7 @@ public function getConfigurations(): array
76
98
'<div class="fake-class" style="color: blue">a div</div>, <a href="/some-path" class="a">an a</a> '
77
99
.', <p class="p-class">a p</p>, <img src="test.jpg" /> ' ,
78
100
false ,
101
+ [],
79
102
[]
80
103
],
81
104
'invalid-html ' => [
@@ -84,6 +107,7 @@ public function getConfigurations(): array
84
107
['a ' => ['href ' ], 'div ' => ['style ' ]],
85
108
'some </,none-> </html> ' ,
86
109
true ,
110
+ [],
87
111
[]
88
112
],
89
113
'invalid-html-with-violations ' => [
@@ -92,6 +116,7 @@ public function getConfigurations(): array
92
116
['a ' => ['href ' ], 'div ' => ['style ' ]],
93
117
'some </,none-> </html> <tr>some trs</tr> ' ,
94
118
false ,
119
+ [],
95
120
[]
96
121
],
97
122
'invalid-html-attributes ' => [
@@ -100,36 +125,58 @@ public function getConfigurations(): array
100
125
[],
101
126
'some <div class="value">DIV</div> ' ,
102
127
false ,
103
- ['class ' => false ]
128
+ ['class ' => false ],
129
+ []
104
130
],
105
131
'ignored-html-attributes ' => [
106
132
['div ' , 'a ' , 'p ' ],
107
133
['class ' , 'src ' ],
108
134
[],
109
135
'some <div class="value">DIV</div> ' ,
110
136
true ,
111
- ['src ' => false , 'class ' => true ]
137
+ ['src ' => false , 'class ' => true ],
138
+ []
112
139
],
113
140
'valid-html-attributes ' => [
114
141
['div ' , 'a ' , 'p ' ],
115
142
['class ' , 'src ' ],
116
143
[],
117
144
'some <div class="value">DIV</div> ' ,
118
145
true ,
119
- ['src ' => true , 'class ' => true ]
146
+ ['src ' => true , 'class ' => true ],
147
+ []
148
+ ],
149
+ 'invalid-allowed-tag ' => [
150
+ ['div ' ],
151
+ ['class ' , 'src ' ],
152
+ [],
153
+ '<div class="some-class" src="some-src">IS A DIV</div> ' ,
154
+ false ,
155
+ [],
156
+ ['div ' => ['class ' => false ]]
157
+ ],
158
+ 'valid-allowed-tag ' => [
159
+ ['div ' ],
160
+ ['class ' , 'src ' ],
161
+ [],
162
+ '<div class="some-class">IS A DIV</div> ' ,
163
+ true ,
164
+ [],
165
+ ['div ' => ['src ' => false ]]
120
166
]
121
167
];
122
168
}
123
169
124
170
/**
125
171
* Test different configurations and content.
126
172
*
127
- * @param array $allowedTags
128
- * @param array $allowedAttr
129
- * @param array $allowedTagAttrs
173
+ * @param string[] $allowedTags
174
+ * @param string[] $allowedAttr
175
+ * @param string[][] $allowedTagAttrs
130
176
* @param string $html
131
177
* @param bool $isValid
132
- * @param array $attributeValidityMap
178
+ * @param bool[] $attributeValidityMap
179
+ * @param bool[][] $tagValidators
133
180
* @return void
134
181
* @dataProvider getConfigurations
135
182
*/
@@ -139,7 +186,8 @@ public function testConfigurations(
139
186
array $ allowedTagAttrs ,
140
187
string $ html ,
141
188
bool $ isValid ,
142
- array $ attributeValidityMap
189
+ array $ attributeValidityMap ,
190
+ array $ tagValidators
143
191
): void {
144
192
$ attributeValidator = $ this ->getMockForAbstractClass (AttributeValidatorInterface::class);
145
193
$ attributeValidator ->method ('validate ' )
@@ -152,13 +200,32 @@ function (string $tag, string $attribute, string $content) use ($attributeValidi
152
200
);
153
201
$ attrValidators = [];
154
202
foreach (array_keys ($ attributeValidityMap ) as $ attr ) {
155
- $ attrValidators [$ attr ] = $ attributeValidator ;
203
+ $ attrValidators [$ attr ] = [$ attributeValidator ];
204
+ }
205
+ $ tagValidatorsMocks = [];
206
+ foreach ($ tagValidators as $ tag => $ allowedAttributes ) {
207
+ $ mock = $ this ->getMockForAbstractClass (TagValidatorInterface::class);
208
+ $ mock ->method ('validate ' )
209
+ ->willReturnCallback (
210
+ function (string $ givenTag , array $ attrs , string $ value ) use ($ tag , $ allowedAttributes ): void {
211
+ if ($ givenTag !== $ tag ) {
212
+ throw new \RuntimeException ();
213
+ }
214
+ foreach (array_keys ($ attrs ) as $ attr ) {
215
+ if (array_key_exists ($ attr , $ allowedAttributes ) && !$ allowedAttributes [$ attr ]) {
216
+ throw new ValidationException (__ ('Invalid tag ' ));
217
+ }
218
+ }
219
+ }
220
+ );
221
+ $ tagValidatorsMocks [$ tag ] = [$ mock ];
156
222
}
157
223
$ validator = new ConfigurableWYSIWYGValidator (
158
224
$ allowedTags ,
159
225
$ allowedAttr ,
160
226
$ allowedTagAttrs ,
161
- $ attrValidators
227
+ $ attrValidators ,
228
+ $ tagValidatorsMocks
162
229
);
163
230
$ valid = true ;
164
231
try {
0 commit comments