diff --git a/core/core.services.yml b/core/core.services.yml index 68c7d23..15318b0 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -735,5 +735,5 @@ services: info_parser: class: Drupal\Core\Extension\InfoParser element_info: - class: Drupal\Core\Utility\ElementInfo + class: Drupal\Core\Render\ElementInfo arguments: ['@module_handler'] diff --git a/core/includes/install.core.inc b/core/includes/install.core.inc index ddf5835..6dd276b 100644 --- a/core/includes/install.core.inc +++ b/core/includes/install.core.inc @@ -475,7 +475,7 @@ function install_begin_request(&$install_state) { // non-existing file would throw an exception. $container->get('config.factory')->setOverrideState(FALSE); - $container->register('element_info', 'Drupal\Core\Utility\ElementInfo') + $container->register('element_info', 'Drupal\Core\Render\ElementInfo') ->addArgument(new Reference('module_handler')); } diff --git a/core/tests/Drupal/Tests/Core/Render/ElementInfoTest.php b/core/tests/Drupal/Tests/Core/Render/ElementInfoTest.php index 3b5159d..db19d59 100644 --- a/core/tests/Drupal/Tests/Core/Render/ElementInfoTest.php +++ b/core/tests/Drupal/Tests/Core/Render/ElementInfoTest.php @@ -7,6 +7,115 @@ namespace Drupal\Tests\Core\Render; -class ElementInfoTest { +use Drupal\Core\Render\ElementInfo; +use Drupal\Tests\UnitTestCase; + +/** + * Tests the element info. + * + * @coversDefaultClass \Drupal\Core\Render\ElementInfo + */ +class ElementInfoTest extends UnitTestCase { + + /** + * The tested element info. + * + * @var \Drupal\Core\Render\ElementInfo + */ + protected $elementInfo; + + /** + * The mocked module handler. + * + * @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject + */ + protected $moduleHandler; + + + + /** + * {@inheritdoc} + * + * @covers ::__construct + */ + protected function setUp() { + $this->moduleHandler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface'); + + $this->elementInfo = new ElementInfo($this->moduleHandler); + } + + /** + * Tests the getInfo method. + * + * @covers ::getInfo + * @covers ::buildInfo + * + * @dataProvider providerTestGetInfo + */ + public function testGetInfo($type, $expected_info, $module_info, callable $alter_callback = NULL) { + $this->moduleHandler->expects($this->once()) + ->method('invokeAll') + ->with('element_info') + ->will($this->returnValue($module_info)); + $this->moduleHandler->expects($this->once()) + ->method('alter') + ->with('element_info', $this->anything()) + ->will($this->returnCallback($alter_callback ?: function($info) { + return $info; + })); + + $this->assertEquals($expected_info, $this->elementInfo->getInfo($type)); + } + + /** + * Provides tests data for getInfo. + * + * @return array + */ + public function providerTestGetInfo() { + $data = array(); + // Provide an element and expect it is returned. + $data[] = array( + 'page', + array( + '#type' => 'page', + '#show_messages' => TRUE, + '#theme' => 'page', + ), + array('page' => array( + '#show_messages' => TRUE, + '#theme' => 'page', + )), + ); + // Provide an element but request an non existent one. + $data[] = array( + 'form', + array( + ), + array('page' => array( + '#show_messages' => TRUE, + '#theme' => 'page', + )), + ); + // Provide an element and alter it to ensure it is altered. + $data[] = array( + 'page', + array( + '#type' => 'page', + '#show_messages' => TRUE, + '#theme' => 'page', + '#number' => 597219, + ), + array('page' => array( + '#show_messages' => TRUE, + '#theme' => 'page', + )), + function ($alter_name, array &$info) { + $info['page']['#number'] = 597219; + } + ); + return $data; + } + }