Upgraded drupal core with security updates
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Render / ElementInfoManagerTest.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Tests\Core\Render\ElementInfoManagerTest.
6  */
7
8 namespace Drupal\Tests\Core\Render;
9
10 use Drupal\Core\Render\ElementInfoManager;
11 use Drupal\Core\Theme\ActiveTheme;
12 use Drupal\Tests\UnitTestCase;
13
14 /**
15  * @coversDefaultClass \Drupal\Core\Render\ElementInfoManager
16  * @group Render
17  */
18 class ElementInfoManagerTest extends UnitTestCase {
19
20   /**
21    * The mocked element_info.
22    *
23    * @var \Drupal\Core\Render\ElementInfoManagerInterface
24    */
25   protected $elementInfo;
26
27   /**
28    * The cache backend to use.
29    *
30    * @var \Drupal\Core\Cache\CacheBackendInterface|\PHPUnit_Framework_MockObject_MockObject
31    */
32   protected $cache;
33
34   /**
35    * The mocked module handler.
36    *
37    * @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
38    */
39   protected $moduleHandler;
40
41   /**
42    * The mocked theme manager.
43    *
44    * @var \Drupal\Core\Theme\ThemeManagerInterface|\PHPUnit_Framework_MockObject_MockObject
45    */
46   protected $themeManager;
47
48   /**
49    * The cache tags invalidator.
50    *
51    * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface|\PHPUnit_Framework_MockObject_MockObject
52    */
53   protected $cacheTagsInvalidator;
54
55   /**
56    * {@inheritdoc}
57    *
58    * @covers ::__construct
59    */
60   protected function setUp() {
61     parent::setUp();
62
63     $this->cache = $this->getMock('Drupal\Core\Cache\CacheBackendInterface');
64     $this->cacheTagsInvalidator = $this->getMock('Drupal\Core\Cache\CacheTagsInvalidatorInterface');
65     $this->moduleHandler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
66     $this->themeManager = $this->getMock('Drupal\Core\Theme\ThemeManagerInterface');
67
68     $this->elementInfo = new ElementInfoManager(new \ArrayObject(), $this->cache, $this->cacheTagsInvalidator, $this->moduleHandler, $this->themeManager);
69   }
70
71   /**
72    * Tests the getInfo() method when render element plugins are used.
73    *
74    * @covers ::getInfo
75    * @covers ::buildInfo
76    *
77    * @dataProvider providerTestGetInfoElementPlugin
78    */
79   public function testGetInfoElementPlugin($plugin_class, $expected_info) {
80     $this->moduleHandler->expects($this->once())
81       ->method('alter')
82       ->with('element_info', $this->anything())
83       ->will($this->returnArgument(0));
84
85     $plugin = $this->getMock($plugin_class);
86     $plugin->expects($this->once())
87       ->method('getInfo')
88       ->willReturn([
89         '#theme' => 'page',
90       ]);
91
92     $element_info = $this->getMockBuilder('Drupal\Core\Render\ElementInfoManager')
93       ->setConstructorArgs([new \ArrayObject(), $this->cache, $this->cacheTagsInvalidator, $this->moduleHandler, $this->themeManager])
94       ->setMethods(['getDefinitions', 'createInstance'])
95       ->getMock();
96
97     $this->themeManager->expects($this->any())
98       ->method('getActiveTheme')
99       ->willReturn(new ActiveTheme(['name' => 'test']));
100
101     $element_info->expects($this->once())
102       ->method('createInstance')
103       ->with('page')
104       ->willReturn($plugin);
105     $element_info->expects($this->once())
106       ->method('getDefinitions')
107       ->willReturn([
108         'page' => ['class' => 'TestElementPlugin'],
109       ]);
110
111     $this->assertEquals($expected_info, $element_info->getInfo('page'));
112   }
113
114   /**
115    * Provides tests data for testGetInfoElementPlugin().
116    *
117    * @return array
118    */
119   public function providerTestGetInfoElementPlugin() {
120     $data = [];
121     $data[] = [
122       'Drupal\Core\Render\Element\ElementInterface',
123       [
124         '#type' => 'page',
125         '#theme' => 'page',
126         '#defaults_loaded' => TRUE,
127       ],
128     ];
129
130     $data[] = [
131       'Drupal\Core\Render\Element\FormElementInterface',
132       [
133         '#type' => 'page',
134         '#theme' => 'page',
135         '#input' => TRUE,
136         '#value_callback' => ['TestElementPlugin', 'valueCallback'],
137         '#defaults_loaded' => TRUE,
138       ],
139     ];
140     return $data;
141   }
142
143   /**
144    * @covers ::getInfoProperty
145    */
146   public function testGetInfoProperty() {
147     $this->themeManager
148       ->method('getActiveTheme')
149       ->willReturn(new ActiveTheme(['name' => 'test']));
150
151     $element_info = new TestElementInfoManager(new \ArrayObject(), $this->cache, $this->cacheTagsInvalidator, $this->moduleHandler, $this->themeManager);
152     $this->assertSame('baz', $element_info->getInfoProperty('foo', '#bar'));
153     $this->assertNull($element_info->getInfoProperty('foo', '#non_existing_property'));
154     $this->assertSame('qux', $element_info->getInfoProperty('foo', '#non_existing_property', 'qux'));
155   }
156
157 }
158
159 /**
160  * Provides a test custom element plugin.
161  */
162 class TestElementInfoManager extends ElementInfoManager {
163
164   /**
165    * {@inheritdoc}
166    */
167   protected $elementInfo = [
168     'test' => [
169       'foo' => [
170         '#bar' => 'baz',
171       ],
172     ],
173   ];
174
175 }