ElementInfoManagerTest.php 4.7 KB

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