11<?php
22/**
3- * Copyright © Magento, Inc. All rights reserved.
4- * See COPYING.txt for license details.
5- */
6-
7- /**
8- * Test for validation rules implemented by XSD schemas for email templates configuration
3+ * Copyright 2013 Adobe
4+ * All Rights Reserved.
95 */
106declare (strict_types=1 );
117
1410use Magento \Framework \Config \Dom ;
1511use Magento \Framework \Config \Dom \UrnResolver ;
1612use Magento \Framework \Config \ValidationStateInterface ;
13+ use PHPUnit \Framework \AssertionFailedError ;
1714use PHPUnit \Framework \TestCase ;
1815
16+ /**
17+ * Test for validation rules implemented by XSD schemas for email templates configuration
18+ */
1919class XsdTest extends TestCase
2020{
2121 /**
@@ -50,114 +50,150 @@ public static function mergedXmlDataProvider()
5050 'empty root node ' => [
5151 '<config/> ' ,
5252 [
53- "Element 'config': Missing child element(s). Expected is ( template ).The xml was: \n" .
54- "0:<?xml version= \"1.0 \"?> \n1:<config/> \n2: \n"
53+ [
54+ "Element 'config': Missing child element(s). Expected is ( template ).The xml was: \n" .
55+ "0:<?xml version= \"1.0 \"?> \n1:<config/> \n2: \n" ,
56+ false ,
57+ ],
5558 ],
5659 ],
5760 'irrelevant root node ' => [
5861 '<template id="test" label="Test" file="test.txt" type="text" module="Module" area="frontend"/> ' ,
5962 [
60- "Element 'template': No matching global declaration available for the validation root. " .
61- "The xml was: \n0:<?xml version= \"1.0 \"?> \n1:<template id= \"test \" label= \"Test \" " .
62- "file= \"test.txt \" type= \"text \" module= \"Module \" area= \"frontend \"/> \n2: \n"
63+ [
64+ "Element 'template': No matching global declaration available for the validation root. " .
65+ "The xml was: \n0:<?xml version= \"1.0 \"?> \n1:<template id= \"test \" label= \"Test \" " .
66+ "file= \"test.txt \" type= \"text \" module= \"Module \" area= \"frontend \"/> \n2: \n" ,
67+ false ,
68+ ],
6369 ],
6470 ],
6571 'invalid node ' => [
6672 '<config><invalid/></config> ' ,
6773 [
68- "Element 'invalid': This element is not expected. Expected is ( template ).The xml was: \n" .
69- "0:<?xml version= \"1.0 \"?> \n1:<config><invalid/></config> \n2: \n"
74+ [
75+ "Element 'invalid': This element is not expected. Expected is ( template ).The xml was: \n" .
76+ "0:<?xml version= \"1.0 \"?> \n1:<config><invalid/></config> \n2: \n" ,
77+ false ,
78+ ],
7079 ],
7180 ],
7281 'node "template" with value ' => [
7382 '<config>
7483 <template id="test" label="Test" file="test.txt" type="text" module="Module" area="frontend">invalid</template>
7584 </config> ' ,
7685 [
77- "Element 'template': Character content is not allowed, because the content type is empty. " .
78- "The xml was: \n0:<?xml version= \"1.0 \"?> \n1:<config> \n2: <template " .
79- "id= \"test \" label= \"Test \" file= \"test.txt \" type= \"text \" module= \"Module \" " .
80- "area= \"frontend \">invalid</template> \n3: </config> \n4: \n"
86+ [
87+ "Element 'template': Character content is not allowed, because the content type is empty. " .
88+ "The xml was: \n0:<?xml version= \"1.0 \"?> \n1:<config> \n2: <template " .
89+ "id= \"test \" label= \"Test \" file= \"test.txt \" type= \"text \" module= \"Module \" " .
90+ "area= \"frontend \">invalid</template> \n3: </config> \n4: \n" ,
91+ false ,
92+ ],
8193 ],
8294 ],
8395 'node "template" with children ' => [
8496 '<config>
8597 <template id="test" label="Test" file="test.txt" type="text" module="Module" area="frontend"><invalid/></template>
8698 </config> ' ,
8799 [
88- "Element 'template': Element content is not allowed, because the content type is empty.The xml " .
89- "was: \n0:<?xml version= \"1.0 \"?> \n1:<config> \n2: <template id= \"test \" " .
90- "label= \"Test \" file= \"test.txt \" type= \"text \" module= \"Module \" area= \"frontend \"><invalid/> " .
91- "</template> \n3: </config> \n4: \n"
100+ [
101+ "Element 'template': Element content is not allowed, because the content type is empty.The xml " .
102+ "was: \n0:<?xml version= \"1.0 \"?> \n1:<config> \n2: <template id= \"test \" " .
103+ "label= \"Test \" file= \"test.txt \" type= \"text \" module= \"Module \" area= \"frontend \"><invalid/> " .
104+ "</template> \n3: </config> \n4: \n" ,
105+ false ,
106+ ],
92107 ],
93108 ],
94109 'node "template" without attribute "id" ' => [
95110 '<config><template label="Test" file="test.txt" type="text" module="Module" area="frontend"/></config> ' ,
96111 [
97- "Element 'template': The attribute 'id' is required but missing.The xml was: \n" .
98- "0:<?xml version= \"1.0 \"?> \n1:<config><template label= \"Test \" file= \"test.txt \" type= \"text \" " .
99- "module= \"Module \" area= \"frontend \"/></config> \n2: \n"
112+ [
113+ "Element 'template': The attribute 'id' is required but missing.The xml was: \n" .
114+ "0:<?xml version= \"1.0 \"?> \n1:<config><template label= \"Test \" file= \"test.txt \" type= \"text \" " .
115+ "module= \"Module \" area= \"frontend \"/></config> \n2: \n" ,
116+ false ,
117+ ],
100118 ],
101119 ],
102120 'node "template" without attribute "label" ' => [
103121 '<config><template id="test" file="test.txt" type="text" module="Module" area="frontend"/></config> ' ,
104122 [
105- "Element 'template': The attribute 'label' is required but missing.The xml was: \n" .
106- "0:<?xml version= \"1.0 \"?> \n1:<config><template id= \"test \" file= \"test.txt \" type= \"text \" " .
107- "module= \"Module \" area= \"frontend \"/></config> \n2: \n"
123+ [
124+ "Element 'template': The attribute 'label' is required but missing.The xml was: \n" .
125+ "0:<?xml version= \"1.0 \"?> \n1:<config><template id= \"test \" file= \"test.txt \" type= \"text \" " .
126+ "module= \"Module \" area= \"frontend \"/></config> \n2: \n" ,
127+ false ,
128+ ],
108129 ],
109130 ],
110131 'node "template" without attribute "file" ' => [
111132 '<config><template id="test" label="Test" type="text" module="Module" area="frontend"/></config> ' ,
112133 [
113- "Element 'template': The attribute 'file' is required but missing.The xml was: \n" .
114- "0:<?xml version= \"1.0 \"?> \n1:<config><template id= \"test \" label= \"Test \" type= \"text \" " .
115- "module= \"Module \" area= \"frontend \"/></config> \n2: \n"
134+ [
135+ "Element 'template': The attribute 'file' is required but missing.The xml was: \n" .
136+ "0:<?xml version= \"1.0 \"?> \n1:<config><template id= \"test \" label= \"Test \" type= \"text \" " .
137+ "module= \"Module \" area= \"frontend \"/></config> \n2: \n" ,
138+ false ,
139+ ],
116140 ],
117141 ],
118142 'node "template" without attribute "type" ' => [
119143 '<config><template id="test" label="Test" file="test.txt" module="Module" area="frontend"/></config> ' ,
120144 [
121- "Element 'template': The attribute 'type' is required but missing.The xml was: \n" .
122- "0:<?xml version= \"1.0 \"?> \n1:<config><template id= \"test \" label= \"Test \" file= \"test.txt \" " .
123- "module= \"Module \" area= \"frontend \"/></config> \n2: \n"
145+ [
146+ "Element 'template': The attribute 'type' is required but missing.The xml was: \n" .
147+ "0:<?xml version= \"1.0 \"?> \n1:<config><template id= \"test \" label= \"Test \" file= \"test.txt \" " .
148+ "module= \"Module \" area= \"frontend \"/></config> \n2: \n" ,
149+ false ,
150+ ],
124151 ],
125152 ],
126153 'node "template" with invalid attribute "type" ' => [
127154 '<config><template id="test" label="Test" file="test.txt" type="invalid" module="Module" area="frontend"/></config> ' ,
128155 [
129- "Element 'template', attribute 'type': [facet 'enumeration'] The value 'invalid' is not an " .
130- "element of the set {'html', 'text'}.The xml was: \n0:<?xml version= \"1.0 \"?> \n" .
131- "1:<config><template id= \"test \" label= \"Test \" file= \"test.txt \" type= \"invalid \" " .
132- "module= \"Module \" area= \"frontend \"/></config> \n2: \n"
156+ [
157+ "Element 'template', attribute 'type': [facet 'enumeration'] The value 'invalid' is not an " .
158+ "element of the set {'html', 'text'}.The xml was: \n0:<?xml version= \"1.0 \"?> \n" .
159+ "1:<config><template id= \"test \" label= \"Test \" file= \"test.txt \" type= \"invalid \" " .
160+ "module= \"Module \" area= \"frontend \"/></config> \n2: \n" ,
161+ false ,
162+ ],
133163 ],
134164 ],
135165 'node "template" without attribute "area" ' => [
136166 '<config><template id="test" label="Test" file="test.txt" type="text" module="Module"/></config> ' ,
137167 [
138- "Element 'template': The attribute 'area' is required but missing.The xml was: \n" .
139- "0:<?xml version= \"1.0 \"?> \n1:<config><template id= \"test \" label= \"Test \" file= \"test.txt \" " .
140- "type= \"text \" module= \"Module \"/></config> \n2: \n"
168+ [
169+ "Element 'template': The attribute 'area' is required but missing.The xml was: \n" .
170+ "0:<?xml version= \"1.0 \"?> \n1:<config><template id= \"test \" label= \"Test \" file= \"test.txt \" " .
171+ "type= \"text \" module= \"Module \"/></config> \n2: \n" ,
172+ false ,
173+ ],
141174 ],
142175 ],
143176 'node "template" with invalid attribute "area" ' => [
144177 '<config><template id="test" label="Test" file="test.txt" type="text" module="Module" area="invalid"/></config> ' ,
145178 [
146- " Element 'template', attribute 'area': 'invalid' is not a valid value of the atomic type " .
147- " 'areaType'.The xml was: \n 0:<?xml version= \" 1.0 \" ?> \n 1:<config><template id= \" test \" " .
148- " label= \" Test \" file= \" test.txt \" type= \" text \" module= \" Module \" area= \" invalid \" /> " .
149- " </config> \n 2: \n" ,
179+ [
180+ " /Element \'template\', attribute \'area\': .*\'invalid\' is not (a valid value|an element of the set).*/ " ,
181+ true ,
182+ ] ,
150183 ],
151184 ],
152185 'node "template" with unknown attribute ' => [
153186 '<config>
154187 <template id="test" label="Test" file="test.txt" type="text" module="Module" area="frontend" unknown="true"/>
155188 </config> ' ,
156189 [
157- "Element 'template', attribute 'unknown': The attribute 'unknown' is not allowed.The xml was: \n" .
158- "0:<?xml version= \"1.0 \"?> \n1:<config> \n2: <template id= \"test \" " .
159- "label= \"Test \" file= \"test.txt \" type= \"text \" module= \"Module \" area= \"frontend \" " .
160- "unknown= \"true \"/> \n3: </config> \n4: \n"
190+ [
191+ "Element 'template', attribute 'unknown': The attribute 'unknown' is not allowed.The xml was: \n" .
192+ "0:<?xml version= \"1.0 \"?> \n1:<config> \n2: <template id= \"test \" " .
193+ "label= \"Test \" file= \"test.txt \" type= \"text \" module= \"Module \" area= \"frontend \" " .
194+ "unknown= \"true \"/> \n3: </config> \n4: \n" ,
195+ false ,
196+ ],
161197 ],
162198 ]
163199 ];
@@ -179,8 +215,22 @@ protected function _testXmlAgainstXsd($fixtureXml, $schemaFile, array $expectedE
179215 $ dom = new Dom ($ fixtureXml , $ validationStateMock , [], null , null , '%message% ' );
180216 $ actualResult = $ dom ->validate ($ schemaFile , $ actualErrors );
181217 $ this ->assertEquals (empty ($ expectedErrors ), $ actualResult );
182- foreach ($ expectedErrors as $ error ) {
183- $ this ->assertContains ($ error , $ actualErrors );
218+ $ this ->assertEquals (empty ($ expectedErrors ), empty ($ actualErrors ));
219+ foreach ($ expectedErrors as [$ error , $ isRegex ]) {
220+ if ($ isRegex ) {
221+ $ matched = false ;
222+ foreach ($ actualErrors as $ actualError ) {
223+ try {
224+ $ this ->assertMatchesRegularExpression ($ error , $ actualError );
225+ $ matched = true ;
226+ break ;
227+ } catch (AssertionFailedError ) {
228+ }
229+ }
230+ $ this ->assertTrue ($ matched , "None of the errors matched: $ error " );
231+ } else {
232+ $ this ->assertContains ($ error , $ actualErrors );
233+ }
184234 }
185235 }
186236}
0 commit comments