@@ -16,18 +16,24 @@ class TemplatesTest extends \PHPUnit_Framework_TestCase
16
16
*/
17
17
protected static $ templates = [];
18
18
19
+ /**
20
+ * @var array
21
+ */
22
+ protected static $ blockVirtualTypes = [];
23
+
19
24
/**
20
25
* Collect declarations of containers per layout file that have aliases
21
26
*/
22
27
public static function setUpBeforeClass ()
23
28
{
24
29
$ count = 0 ;
30
+ self ::getBlockVirtualTypesWithDifferentModule ();
25
31
foreach (Files::init ()->getLayoutFiles ([], false ) as $ file ) {
26
32
$ xml = simplexml_load_file ($ file );
27
- $ templateElements = $ xml ->xpath ('//block[@template] ' ) ?: [];
33
+ $ blocks = $ xml ->xpath ('//block[@template] ' ) ?: [];
28
34
$ fileTemplates = [];
29
- foreach ($ templateElements as $ node ) {
30
- $ fileTemplates [] = (string )$ node [ ' template ' ];
35
+ foreach ($ blocks as $ block ) {
36
+ $ fileTemplates [] = [ ' class ' => (string )$ block [ ' class ' ], ' file ' => ( string ) $ block [ ' template '] ];
31
37
}
32
38
if (!empty ($ fileTemplates )) {
33
39
self ::$ templates [$ file ] = $ fileTemplates ;
@@ -45,13 +51,18 @@ public static function setUpBeforeClass()
45
51
public function testTemplateFollowsCanonicalName ()
46
52
{
47
53
$ errors = [];
54
+ $ warnings = [];
48
55
foreach (self ::$ templates as $ file => $ templates ) {
49
- foreach ($ templates as $ template ) {
50
- if (!preg_match ('/[A-Za-z0-9]_[A-Za-z0-9]+\:\:[A-Za-z0-9 \\_\.]+/ ' , $ template )) {
56
+ foreach ($ templates as $ templatePair ) {
57
+ if (!preg_match ('/[A-Za-z0-9]_[A-Za-z0-9]+\:\:[A-Za-z0-9 \\_\-\ .]+/ ' , $ templatePair [ ' file ' ] )) {
51
58
if (!isset ($ errors [$ file ])) {
52
59
$ errors [$ file ] = [];
53
60
}
54
- $ errors [$ file ][] = $ template ;
61
+ $ errors [$ file ][] = $ templatePair ['file ' ];
62
+ } else {
63
+ if (isset (self ::$ blockVirtualTypes [$ templatePair ['class ' ]])) {
64
+ $ warnings [$ file ][] = $ templatePair ;
65
+ }
55
66
}
56
67
}
57
68
}
@@ -66,4 +77,32 @@ public function testTemplateFollowsCanonicalName()
66
77
$ this ->fail ($ message );
67
78
}
68
79
}
80
+
81
+ /**
82
+ * Initialize array with the Virtual types for blocks
83
+ *
84
+ * Contains just those occurrences where base type and virtual type are located in different modules
85
+ */
86
+ private static function getBlockVirtualTypesWithDifferentModule ()
87
+ {
88
+ $ virtual = \Magento \Framework \App \Utility \Classes::getVirtualClasses ();
89
+ foreach ($ virtual as $ className => $ resolvedName ) {
90
+ if (strpos ($ resolvedName , 'Block ' ) !== false ) {
91
+ $ matches = [];
92
+ preg_match ('/([A-Za-z0-9]+ \\\\[A-Za-z0-9]+).*/ ' , $ className , $ matches );
93
+ if (count ($ matches ) > 1 ) {
94
+ $ oldModule = $ matches [1 ];
95
+ } else {
96
+ $ oldModule = $ className ;
97
+ }
98
+
99
+ $ matches = [];
100
+ preg_match ('/([A-Za-z0-9]+ \\\\[A-Za-z0-9]+).*/ ' , $ resolvedName , $ matches );
101
+ $ newModule = $ matches [1 ];
102
+ if ($ oldModule != $ newModule ) {
103
+ self ::$ blockVirtualTypes [$ className ] = $ resolvedName ;
104
+ }
105
+ }
106
+ }
107
+ }
69
108
}
0 commit comments