5 * Contains \Drupal\Tests\Core\Render\ElementInfoManagerTest.
8 namespace Drupal\Tests\Core\Render;
10 use Drupal\Core\Render\ElementInfoManager;
11 use Drupal\Core\Theme\ActiveTheme;
12 use Drupal\Tests\UnitTestCase;
15 * @coversDefaultClass \Drupal\Core\Render\ElementInfoManager
18 class ElementInfoManagerTest extends UnitTestCase {
21 * The mocked element_info.
23 * @var \Drupal\Core\Render\ElementInfoManagerInterface
25 protected $elementInfo;
28 * The cache backend to use.
30 * @var \Drupal\Core\Cache\CacheBackendInterface|\PHPUnit_Framework_MockObject_MockObject
35 * The mocked module handler.
37 * @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
39 protected $moduleHandler;
42 * The mocked theme manager.
44 * @var \Drupal\Core\Theme\ThemeManagerInterface|\PHPUnit_Framework_MockObject_MockObject
46 protected $themeManager;
49 * The cache tags invalidator.
51 * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface|\PHPUnit_Framework_MockObject_MockObject
53 protected $cacheTagsInvalidator;
58 * @covers ::__construct
60 protected function setUp() {
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');
68 $this->elementInfo = new ElementInfoManager(new \ArrayObject(), $this->cache, $this->cacheTagsInvalidator, $this->moduleHandler, $this->themeManager);
72 * Tests the getInfo() method when render element plugins are used.
77 * @dataProvider providerTestGetInfoElementPlugin
79 public function testGetInfoElementPlugin($plugin_class, $expected_info) {
80 $this->moduleHandler->expects($this->once())
82 ->with('element_info', $this->anything())
83 ->will($this->returnArgument(0));
85 $plugin = $this->getMock($plugin_class);
86 $plugin->expects($this->once())
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'])
97 $this->themeManager->expects($this->any())
98 ->method('getActiveTheme')
99 ->willReturn(new ActiveTheme(['name' => 'test']));
101 $element_info->expects($this->once())
102 ->method('createInstance')
104 ->willReturn($plugin);
105 $element_info->expects($this->once())
106 ->method('getDefinitions')
108 'page' => ['class' => 'TestElementPlugin'],
111 $this->assertEquals($expected_info, $element_info->getInfo('page'));
115 * Provides tests data for testGetInfoElementPlugin().
119 public function providerTestGetInfoElementPlugin() {
122 'Drupal\Core\Render\Element\ElementInterface',
126 '#defaults_loaded' => TRUE,
131 'Drupal\Core\Render\Element\FormElementInterface',
136 '#value_callback' => ['TestElementPlugin', 'valueCallback'],
137 '#defaults_loaded' => TRUE,
144 * @covers ::getInfoProperty
146 public function testGetInfoProperty() {
148 ->method('getActiveTheme')
149 ->willReturn(new ActiveTheme(['name' => 'test']));
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'));
160 * Provides a test custom element plugin.
162 class TestElementInfoManager extends ElementInfoManager {
167 protected $elementInfo = [