Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
LayoutRuleTest.php
Go to the documentation of this file.
1 <?php
7 
8 class LayoutRuleTest extends \PHPUnit\Framework\TestCase
9 {
11  {
12  $model = new LayoutRule([], [], []);
13  $content = 'any content';
14  $this->assertEmpty($model->getDependencyInfo('any', 'not layout', 'any', $content));
15  }
16 
22  public function testGetDependencyInfo($contents, array $expected)
23  {
24  $model = new LayoutRule([], [], []);
25  $this->assertEquals($expected, $model->getDependencyInfo('Magento\SomeModule', 'layout', 'any', $contents));
26  }
27 
29  {
30  return [
31  [
32  '<element module="Magento\AnotherModule"/>',
33  [
34  [
35  'module' => 'Magento\AnotherModule',
37  'source' => '<element module="Magento\AnotherModule"/>',
38  ]
39  ],
40  ],
41  ['<element module="Magento\SomeModule"/>', []],
42  [
43  '<block class="Magento\AnotherModule\Several\Chunks"/>',
44  [
45  [
46  'module' => 'Magento\AnotherModule',
48  'source' => '<block class="Magento\AnotherModule\Several\Chunks"/>',
49  ]
50  ]
51  ],
52  ['<block class="Magento\SomeModule\Several\Chunks"/>', []],
53  [
54  '<any>
55  <extra></extra><block template="Magento_AnotherModule::template/path.phtml"/>
56  </any>',
57  [
58  [
59  'module' => 'Magento\AnotherModule',
61  'source' => '<block template="Magento_AnotherModule::template/path.phtml"/>',
62  ]
63  ]
64  ],
65  ['<block template="Magento_SomeModule::template/path.phtml"/>', []],
66  [
67  '<block>Magento\AnotherModule\Several\Chunks</block>',
68  [
69  [
70  'module' => 'Magento\AnotherModule',
72  'source' => '<block>Magento\AnotherModule\Several\Chunks</block>',
73  ]
74  ]
75  ],
76  ['<block>Magento\SomeModule\Several\Chunks</block>', []],
77  [
78  '<template>Magento_AnotherModule::template/path.phtml</template>',
79  [
80  [
81  'module' => 'Magento\AnotherModule',
83  'source' => '<template>Magento_AnotherModule::template/path.phtml</template>',
84  ]
85  ]
86  ],
87  ['<template>Magento_SomeModule::template/path.phtml</template>', []],
88  [
89  '<file>Magento_AnotherModule::file/path.txt</file>',
90  [
91  [
92  'module' => 'Magento\AnotherModule',
94  'source' => '<file>Magento_AnotherModule::file/path.txt</file>',
95  ]
96  ]
97  ],
98  ['<file>Magento_SomeModule::file/path.txt</file>', []],
99  [
100  '<any helper="Magento\AnotherModule\Several\Chunks::text"/>',
101  [
102  [
103  'module' => 'Magento\AnotherModule',
105  'source' => '<any helper="Magento\AnotherModule\Several\Chunks::text"/>',
106  ]
107  ]
108  ],
109  ['<any helper="Magento\SomeModule\Several\Chunks::text"/>', []]
110  ];
111  }
112 
119  {
120  $model = new LayoutRule(['router_name' => ['Magento\RouterModule']], [], []);
121  $this->assertEquals([], $model->getDependencyInfo('Magento\RouterModule', 'layout', 'any', $contents));
122  $this->assertEquals(
123  [['module' => 'Magento\RouterModule', 'type' => $type, 'source' => 'router_name_action']],
124  $model->getDependencyInfo('Magento\AnotherModule', 'layout', 'any', $contents)
125  );
126  }
127 
134  public function testLayoutGetDependencyInfo($contents, $type, $isHandle)
135  {
136  // test one module
137  $data = [
138  'frontend' => ['any_handle_name' => ['Magento\AnyHandleModule' => 'Magento\AnyHandleModule']],
139  'default' => ['singlechunk' => ['Magento\DefaultHandleModule' => 'Magento\DefaultHandleModule']],
140  ];
141  $model = $isHandle ? new LayoutRule([], [], $data) : new LayoutRule([], $data, []);
142  $this->assertEquals(
143  [],
144  $model->getDependencyInfo('Magento\AnyHandleModule', 'layout', 'path/frontend/file.txt', $contents)
145  );
146  $this->assertEquals(
147  [],
148  $model->getDependencyInfo('Magento\DefaultHandleModule', 'layout', 'any', $contents)
149  );
150  $this->assertEquals(
151  [['module' => 'Magento\DefaultHandleModule', 'type' => $type, 'source' => 'singlechunk']],
152  $model->getDependencyInfo('any', 'layout', 'any', $contents)
153  );
154  $this->assertEquals(
155  [['module' => 'Magento\AnyHandleModule', 'type' => $type, 'source' => 'any_handle_name']],
156  $model->getDependencyInfo('any', 'layout', 'path/frontend/file.txt', $contents)
157  );
158  // test several modules
159  $data = [
160  'frontend' => [
161  'any_handle_name' => [
162  'Magento\Theme' => 'Magento\Theme',
163  'Magento\HandleModule' => 'Magento\HandleModule',
164  ],
165  ],
166  ];
167  $model = $isHandle ? new LayoutRule([], [], $data) : new LayoutRule([], $data, []);
168  $this->assertEquals(
169  [['module' => 'Magento\Theme', 'type' => $type, 'source' => 'any_handle_name']],
170  $model->getDependencyInfo('any', 'layout', 'path/frontend/file.txt', $contents)
171  );
172  $this->assertEquals(
173  [],
174  $model->getDependencyInfo('Magento\HandleModule', 'layout', 'path/frontend/file.txt', $contents)
175  );
176  }
177 
179  {
180  return [
181  [
182  $this->_getLayoutFileContent('layout_handle.xml'),
184  true,
185  ],
186  [
187  $this->_getLayoutFileContent('layout_handle_parent.xml'),
189  true
190  ],
191  [
192  $this->_getLayoutFileContent('layout_handle_update.xml'),
194  true
195  ]
196  ];
197  }
198 
200  {
201  return array_merge(
203  [
204  [
205  $this->_getLayoutFileContent('layout_reference.xml'),
206  \Magento\Test\Integrity\DependencyTest::TYPE_SOFT,
207  false,
208  ]
209  ]
210  );
211  }
212 
219  protected function _getLayoutFileContent($fileName)
220  {
221  return file_get_contents(str_replace('\\', '/', realpath(__DIR__)) . '/_files/' . $fileName);
222  }
223 }
$contents
Definition: website.php:14
testLayoutGetDependencyInfo($contents, $type, $isHandle)
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
$type
Definition: item.phtml:13
$fileName
Definition: translate.phtml:15
testGetDependencyInfo($contents, array $expected)