3 namespace Drupal\Tests\layout_plugin\Unit;
5 use Drupal\layout_plugin\Plugin\Layout\LayoutPluginManager;
6 use Drupal\Tests\UnitTestCase;
9 * Tests the LayoutPluginManager.
11 * @coversDefaultClass \Drupal\layout_plugin\Plugin\Layout\LayoutPluginManager
15 class PluginManagerTest extends UnitTestCase {
18 * Test processDefinition.
20 * @covers ::processDefinition
22 public function testProcessDefinition() {
23 $namespaces = new \ArrayObject();
24 $namespaces['Drupal\layout_plugin_test'] = $this->root . '/modules/layout_plugin_test/src';
26 $cache_backend = $this->getMock('Drupal\Core\Cache\CacheBackendInterface');
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()
34 $extension->method('getPath')->willReturn('modules/layout_plugin_test');
35 $module_handler->method('getModule')->willReturn($extension);
37 $theme_handler = $this->getMock('Drupal\Core\Extension\ThemeHandlerInterface');
38 $theme_handler->method('getThemeDirectories')->willReturn(array());
40 $plugin_manager = new LayoutPluginManager($namespaces, $cache_backend, $module_handler, $theme_handler);
42 // A simple definition with only the required keys.
44 'label' => 'Simple layout',
45 'category' => 'Test layouts',
46 'theme' => 'simple_layout',
47 'provider' => 'layout_plugin_test',
49 'first' => ['label' => 'First region'],
50 'second' => ['label' => 'Second region'],
53 $plugin_manager->processDefinition($definition, 'simple_layout');
54 $this->assertEquals('modules/layout_plugin_test', $definition['path']);
56 'first' => 'First region',
57 'second' => 'Second region'
58 ], $definition['region_names']);
60 // A more complex 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',
70 'first' => ['label' => 'First region'],
71 'second' => ['label' => 'Second region'],
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']);
81 // A layout with a template path.
83 'label' => 'Split layout',
84 'category' => 'Test layouts',
85 'template' => 'templates/split-layout',
86 'provider' => 'layout_plugin_test',
88 'icon' => 'images/split-layout.png',
90 'first' => ['label' => 'First region'],
91 'second' => ['label' => 'Second region'],
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']);
100 // A layout with an auto-registered library.
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',
109 'first' => ['label' => 'First region'],
110 'second' => ['label' => 'Second region'],
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']);
119 * Test getting layout options.
121 * @covers ::getLayoutOptions
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'])
130 $layout_manager->method('getDefinitions')
133 'label' => 'Simple layout',
134 'category' => 'Test layouts',
136 'complex_layout' => [
137 'label' => 'Complex layout',
138 'category' => 'Test layouts',
142 $options = $layout_manager->getLayoutOptions();
143 $this->assertEquals([
144 'simple_layout' => 'Simple layout',
145 'complex_layout' => 'Complex layout',
148 $options = $layout_manager->getLayoutOptions(array('group_by_category' => TRUE));
149 $this->assertEquals([
151 'simple_layout' => 'Simple layout',
152 'complex_layout' => 'Complex layout',
158 * Tests layout theme implementations.
160 * @covers ::getThemeImplementations
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'])
169 $layout_manager->method('getDefinitions')
171 // Should get template registered automatically.
173 'path' => 'modules/layout_plugin_test',
174 'template_path' => 'modules/layout_plugin_test/templates',
175 'template' => 'simple-layout',
176 'theme' => 'simple_layout',
178 // Shouldn't get registered automatically.
179 'complex_layout' => [
180 'path' => 'modules/layout_plugin_test',
181 'theme' => 'complex_layout',
185 $theme_registry = $layout_manager->getThemeImplementations();
186 $this->assertEquals([
188 'render element' => 'content',
189 'template' => 'simple-layout',
190 'path' => 'modules/layout_plugin_test/templates',
196 * Tests layout theme implementations.
198 * @covers ::alterThemeImplementations
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'])
207 $layout_manager->method('getDefinitions')
210 'template' => 'simple-layout',
211 'theme' => 'simple_layout',
213 'no_template_preprocess' => [
214 'template' => 'no-template-preprocess',
215 'theme' => 'no_template_preprocess',
217 'only_template_preprocess' => [
218 'template' => 'only-template-preprocess',
219 'theme' => 'only_template_preprocess',
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
224 'complex_layout' => [
225 'theme' => 'complex_layout',
230 'other_theme_hook' => [
231 'preprocess functions' => [
232 'template_preprocess_other_theme_hook'
236 'preprocess functions' => [
237 'template_preprocess',
238 'template_preprocess_simple_layout'
241 'simple_layout__suggestion_template' => [
242 'base hook' => 'simple_layout',
243 'preprocess functions' => [
244 'template_preprocess',
245 'template_preprocess_simple_layout'
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'
254 // Make sure our alter still works if there's only 'template_preprocess'.
255 'only_template_preprocess' => [
256 'preprocess functions' => [
257 'template_preprocess',
260 'complex_layout' => [
261 'preprocess functions' => [
262 'template_preprocess_complex_layout',
267 $layout_manager->alterThemeImplementations($theme_registry);
268 $this->assertEquals([
269 'other_theme_hook' => [
270 'preprocess functions' => [
271 'template_preprocess_other_theme_hook'
275 'preprocess functions' => [
276 'template_preprocess',
277 '_layout_plugin_preprocess_layout',
278 'template_preprocess_simple_layout'
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'
289 'no_template_preprocess' => [
290 'preprocess functions' => [
291 '_layout_plugin_preprocess_layout',
292 'template_preprocess_no_template_preprocess'
295 'only_template_preprocess' => [
296 'preprocess functions' => [
297 'template_preprocess',
298 '_layout_plugin_preprocess_layout',
301 'complex_layout' => [
302 'preprocess functions' => [
303 'template_preprocess_complex_layout',
310 * Tests layout plugin library info.
312 * @covers ::getLibraryInfo
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'])
321 $layout_manager->method('getDefinitions')
323 // Should get template registered automatically.
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',
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',
336 'complex_layout' => [
337 'library' => 'layout_plugin_test/complex_layout',
341 $layout_manager->method('getProviderVersion')
343 ['module', 'layout_plugin_test', '1.2.3'],
344 ['theme', 'theme_with_layout', '2.3.4'],
347 $library_info = $layout_manager->getLibraryInfo();
348 $this->assertEquals([
350 'version' => '1.2.3',
353 '/modules/layout_plugin_test/layouts/simple_layout/simple-layout.css' => [],
358 'version' => '2.3.4',
361 '/themes/theme_with_layout/layouts/theme_layout/theme-layout.css' => [],