Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[yaffs-website] / web / modules / contrib / layout_plugin / tests / src / Unit / PluginManagerTest.php
1 <?php
2
3 namespace Drupal\Tests\layout_plugin\Unit;
4
5 use Drupal\layout_plugin\Plugin\Layout\LayoutPluginManager;
6 use Drupal\Tests\UnitTestCase;
7
8 /**
9  * Tests the LayoutPluginManager.
10  *
11  * @coversDefaultClass \Drupal\layout_plugin\Plugin\Layout\LayoutPluginManager
12  *
13  * @group LayoutPlugin
14  */
15 class PluginManagerTest extends UnitTestCase {
16
17   /**
18    * Test processDefinition.
19    *
20    * @covers ::processDefinition
21    */
22   public function testProcessDefinition() {
23     $namespaces = new \ArrayObject();
24     $namespaces['Drupal\layout_plugin_test'] = $this->root . '/modules/layout_plugin_test/src';
25
26     $cache_backend = $this->getMock('Drupal\Core\Cache\CacheBackendInterface');
27
28     $module_handler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
29     $module_handler->method('getModuleDirectories')->willReturn(array());
30     $module_handler->method('moduleExists')->willReturn(TRUE);
31     $extension = $this->getMockBuilder('Drupal\Core\Extension\Extension')
32       ->disableOriginalConstructor()
33       ->getMock();
34     $extension->method('getPath')->willReturn('modules/layout_plugin_test');
35     $module_handler->method('getModule')->willReturn($extension);
36
37     $theme_handler = $this->getMock('Drupal\Core\Extension\ThemeHandlerInterface');
38     $theme_handler->method('getThemeDirectories')->willReturn(array());
39
40     $plugin_manager = new LayoutPluginManager($namespaces, $cache_backend, $module_handler, $theme_handler);
41
42     // A simple definition with only the required keys.
43     $definition = [
44       'label' => 'Simple layout',
45       'category' => 'Test layouts',
46       'theme' => 'simple_layout',
47       'provider' => 'layout_plugin_test',
48       'regions' => [
49         'first' => ['label' => 'First region'],
50         'second' => ['label' => 'Second region'],
51       ],
52     ];
53     $plugin_manager->processDefinition($definition, 'simple_layout');
54     $this->assertEquals('modules/layout_plugin_test', $definition['path']);
55     $this->assertEquals([
56       'first' => 'First region',
57       'second' => 'Second region'
58     ], $definition['region_names']);
59
60     // A more complex definition.
61     $definition = [
62       'label' => 'Complex layout',
63       'category' => 'Test layouts',
64       'template' => 'complex-layout',
65       'library' => 'library_module/library_name',
66       'provider' => 'layout_plugin_test',
67       'path' => 'layout/complex',
68       'icon' => 'complex-layout.png',
69       'regions' => [
70         'first' => ['label' => 'First region'],
71         'second' => ['label' => 'Second region'],
72       ],
73     ];
74     $plugin_manager->processDefinition($definition, 'complex_layout');
75     $this->assertEquals('modules/layout_plugin_test/layout/complex', $definition['path']);
76     $this->assertEquals('modules/layout_plugin_test/layout/complex', $definition['template_path']);
77     $this->assertEquals('modules/layout_plugin_test/layout/complex/complex-layout.png', $definition['icon']);
78     $this->assertEquals('complex_layout', $definition['theme']);
79     $this->assertEquals(['module' => ['library_module']], $definition['dependencies']);
80
81     // A layout with a template path.
82     $definition = [
83       'label' => 'Split layout',
84       'category' => 'Test layouts',
85       'template' => 'templates/split-layout',
86       'provider' => 'layout_plugin_test',
87       'path' => 'layouts',
88       'icon' => 'images/split-layout.png',
89       'regions' => [
90         'first' => ['label' => 'First region'],
91         'second' => ['label' => 'Second region'],
92       ],
93     ];
94     $plugin_manager->processDefinition($definition, 'split_layout');
95     $this->assertEquals('modules/layout_plugin_test/layouts', $definition['path']);
96     $this->assertEquals('modules/layout_plugin_test/layouts/templates', $definition['template_path']);
97     $this->assertEquals('modules/layout_plugin_test/layouts/images/split-layout.png', $definition['icon']);
98     $this->assertEquals('split_layout', $definition['theme']);
99
100     // A layout with an auto-registered library.
101     $definition = [
102       'label' => 'Auto library',
103       'category' => 'Test layouts',
104       'theme' => 'auto_library',
105       'provider' => 'layout_plugin_test',
106       'path' => 'layouts/auto_library',
107       'css' => 'css/auto-library.css',
108       'regions' => [
109         'first' => ['label' => 'First region'],
110         'second' => ['label' => 'Second region'],
111       ],
112     ];
113     $plugin_manager->processDefinition($definition, 'auto_library');
114     $this->assertEquals('modules/layout_plugin_test/layouts/auto_library/css/auto-library.css', $definition['css']);
115     $this->assertEquals('layout_plugin/auto_library', $definition['library']);
116   }
117
118   /**
119    * Test getting layout options.
120    *
121    * @covers ::getLayoutOptions
122    */
123   public function testGetLayoutOptions() {
124     /** @var LayoutPluginManager|\PHPUnit_Framework_MockObject_MockBuilder $layout_manager */
125     $layout_manager = $this->getMockBuilder('Drupal\layout_plugin\Plugin\Layout\LayoutPluginManager')
126       ->disableOriginalConstructor()
127       ->setMethods(['getDefinitions'])
128       ->getMock();
129
130     $layout_manager->method('getDefinitions')
131       ->willReturn([
132         'simple_layout' => [
133           'label' => 'Simple layout',
134           'category' => 'Test layouts',
135         ],
136         'complex_layout' => [
137           'label' => 'Complex layout',
138           'category' => 'Test layouts',
139         ],
140       ]);
141
142     $options = $layout_manager->getLayoutOptions();
143     $this->assertEquals([
144       'simple_layout' => 'Simple layout',
145       'complex_layout' => 'Complex layout',
146     ], $options);
147
148     $options = $layout_manager->getLayoutOptions(array('group_by_category' => TRUE));
149     $this->assertEquals([
150       'Test layouts' => [
151         'simple_layout' => 'Simple layout',
152         'complex_layout' => 'Complex layout',
153       ],
154     ], $options);
155   }
156
157   /**
158    * Tests layout theme implementations.
159    *
160    * @covers ::getThemeImplementations
161    */
162   public function testGetThemeImplementations() {
163     /** @var LayoutPluginManager|\PHPUnit_Framework_MockObject_MockBuilder $layout_manager */
164     $layout_manager = $this->getMockBuilder('Drupal\layout_plugin\Plugin\Layout\LayoutPluginManager')
165       ->disableOriginalConstructor()
166       ->setMethods(['getDefinitions'])
167       ->getMock();
168
169     $layout_manager->method('getDefinitions')
170       ->willReturn([
171         // Should get template registered automatically.
172         'simple_layout' => [
173           'path' => 'modules/layout_plugin_test',
174           'template_path' => 'modules/layout_plugin_test/templates',
175           'template' => 'simple-layout',
176           'theme' => 'simple_layout',
177         ],
178         // Shouldn't get registered automatically.
179         'complex_layout' => [
180           'path' => 'modules/layout_plugin_test',
181           'theme' => 'complex_layout',
182         ],
183       ]);
184
185     $theme_registry = $layout_manager->getThemeImplementations();
186     $this->assertEquals([
187       'simple_layout' => [
188         'render element' => 'content',
189         'template' => 'simple-layout',
190         'path' => 'modules/layout_plugin_test/templates',
191       ],
192     ], $theme_registry);
193   }
194
195   /**
196    * Tests layout theme implementations.
197    *
198    * @covers ::alterThemeImplementations
199    */
200   public function testAlterThemeImplementations() {
201     /** @var LayoutPluginManager|\PHPUnit_Framework_MockObject_MockBuilder $layout_manager */
202     $layout_manager = $this->getMockBuilder('Drupal\layout_plugin\Plugin\Layout\LayoutPluginManager')
203       ->disableOriginalConstructor()
204       ->setMethods(['getDefinitions'])
205       ->getMock();
206
207     $layout_manager->method('getDefinitions')
208       ->willReturn([
209         'simple_layout' => [
210           'template' => 'simple-layout',
211           'theme' => 'simple_layout',
212         ],
213         'no_template_preprocess' => [
214           'template' => 'no-template-preprocess',
215           'theme' => 'no_template_preprocess',
216         ],
217         'only_template_preprocess' => [
218           'template' => 'only-template-preprocess',
219           'theme' => 'only_template_preprocess',
220         ],
221         // If the user registered the theme hook themselves, then we don't
222         // want to add our preprocess function (because we're not totally sure
223         // how it'll work).
224         'complex_layout' => [
225           'theme' => 'complex_layout',
226         ],
227       ]);
228
229     $theme_registry = [
230       'other_theme_hook' => [
231         'preprocess functions' => [
232           'template_preprocess_other_theme_hook'
233         ],
234       ],
235       'simple_layout' => [
236         'preprocess functions' => [
237           'template_preprocess',
238           'template_preprocess_simple_layout'
239         ],
240       ],
241       'simple_layout__suggestion_template' => [
242         'base hook' => 'simple_layout',
243         'preprocess functions' => [
244           'template_preprocess',
245           'template_preprocess_simple_layout'
246         ],
247       ],
248       // Make sure our alter still works if there is no 'template_preprocess'.
249       'no_template_preprocess' => [
250         'preprocess functions' => [
251           'template_preprocess_no_template_preprocess'
252         ],
253       ],
254       // Make sure our alter still works if there's only 'template_preprocess'.
255       'only_template_preprocess' => [
256         'preprocess functions' => [
257           'template_preprocess',
258         ],
259       ],
260       'complex_layout' => [
261         'preprocess functions' => [
262           'template_preprocess_complex_layout',
263         ],
264       ],
265     ];
266
267     $layout_manager->alterThemeImplementations($theme_registry);
268     $this->assertEquals([
269       'other_theme_hook' => [
270         'preprocess functions' => [
271           'template_preprocess_other_theme_hook'
272         ],
273       ],
274       'simple_layout' => [
275         'preprocess functions' => [
276           'template_preprocess',
277           '_layout_plugin_preprocess_layout',
278           'template_preprocess_simple_layout'
279         ],
280       ],
281       'simple_layout__suggestion_template' => [
282         'base hook' => 'simple_layout',
283         'preprocess functions' => [
284           'template_preprocess',
285           '_layout_plugin_preprocess_layout',
286           'template_preprocess_simple_layout'
287         ],
288       ],
289       'no_template_preprocess' => [
290         'preprocess functions' => [
291           '_layout_plugin_preprocess_layout',
292           'template_preprocess_no_template_preprocess'
293         ],
294       ],
295       'only_template_preprocess' => [
296         'preprocess functions' => [
297           'template_preprocess',
298           '_layout_plugin_preprocess_layout',
299         ],
300       ],
301       'complex_layout' => [
302         'preprocess functions' => [
303           'template_preprocess_complex_layout',
304         ],
305       ],
306     ], $theme_registry);
307   }
308
309   /**
310    * Tests layout plugin library info.
311    *
312    * @covers ::getLibraryInfo
313    */
314   public function testGetLibraryInfo() {
315     /** @var \Drupal\layout_plugin\Plugin\Layout\LayoutPluginManager|\PHPUnit_Framework_MockObject_MockObject $layout_manager */
316     $layout_manager = $this->getMockBuilder('Drupal\layout_plugin\Plugin\Layout\LayoutPluginManager')
317       ->disableOriginalConstructor()
318       ->setMethods(['getDefinitions', 'getProviderVersion'])
319       ->getMock();
320
321     $layout_manager->method('getDefinitions')
322       ->willReturn([
323         // Should get template registered automatically.
324         'simple_layout' => [
325           'css' => 'modules/layout_plugin_test/layouts/simple_layout/simple-layout.css',
326           'library' => 'layout_plugin/simple_layout',
327           'provider_type' => 'module',
328           'provider' => 'layout_plugin_test',
329         ],
330         'theme_layout' => [
331           'css' => 'themes/theme_with_layout/layouts/theme_layout/theme-layout.css',
332           'library' => 'layout_plugin/theme_layout',
333           'provider_type' => 'theme',
334           'provider' => 'theme_with_layout',
335         ],
336         'complex_layout' => [
337           'library' => 'layout_plugin_test/complex_layout',
338         ],
339       ]);
340
341     $layout_manager->method('getProviderVersion')
342       ->willReturnMap([
343         ['module', 'layout_plugin_test', '1.2.3'],
344         ['theme', 'theme_with_layout', '2.3.4'],
345       ]);
346
347     $library_info = $layout_manager->getLibraryInfo();
348     $this->assertEquals([
349       'simple_layout' => [
350         'version' => '1.2.3',
351         'css' => [
352           'theme' => [
353             '/modules/layout_plugin_test/layouts/simple_layout/simple-layout.css' => [],
354           ],
355         ],
356       ],
357       'theme_layout' => [
358         'version' => '2.3.4',
359         'css' => [
360           'theme' => [
361             '/themes/theme_with_layout/layouts/theme_layout/theme-layout.css' => [],
362           ],
363         ],
364       ],
365     ], $library_info);
366   }
367
368 }