class ElementInfoManagerTest
Tests Drupal\Core\Render\ElementInfoManager.
Attributes
#[CoversClass(ElementInfoManager::class)]
#[Group('Render')]
  Hierarchy
- class \Drupal\Tests\UnitTestCase uses \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Drupal\TestTools\Extension\DeprecationBridge\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait extends \PHPUnit\Framework\TestCase
- class \Drupal\Tests\Core\Render\ElementInfoManagerTest extends \Drupal\Tests\UnitTestCase
 
 
Expanded class hierarchy of ElementInfoManagerTest
File
- 
              core/
tests/ Drupal/ Tests/ Core/ Render/ ElementInfoManagerTest.php, line 18  
Namespace
Drupal\Tests\Core\RenderView source
class ElementInfoManagerTest extends UnitTestCase {
  
  /**
   * The mocked element_info.
   *
   * @var \Drupal\Core\Render\ElementInfoManagerInterface
   */
  protected $elementInfo;
  
  /**
   * The cache backend to use.
   *
   * @var \Drupal\Core\Cache\CacheBackendInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $cache;
  
  /**
   * The mocked module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $moduleHandler;
  
  /**
   * The mocked theme manager.
   *
   * @var \Drupal\Core\Theme\ThemeManagerInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $themeManager;
  
  /**
   * The mocked theme handler.
   *
   * @var \Drupal\Core\Extension\ThemeHandlerInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $themeHandler;
  
  /**
   * {@inheritdoc}
   *
   * @legacy-covers ::__construct
   */
  protected function setUp() : void {
    parent::setUp();
    $this->cache = $this->createMock('Drupal\\Core\\Cache\\CacheBackendInterface');
    $this->themeHandler = $this->createMock(ThemeHandlerInterface::class);
    $this->moduleHandler = $this->createMock('Drupal\\Core\\Extension\\ModuleHandlerInterface');
    $this->themeManager = $this->createMock('Drupal\\Core\\Theme\\ThemeManagerInterface');
    $this->elementInfo = new ElementInfoManager(new \ArrayObject(), $this->cache, $this->themeHandler, $this->moduleHandler, $this->themeManager);
  }
  
  /**
   * Tests the getInfo() method when render element plugins are used.
   *
   * @legacy-covers ::getInfo
   * @legacy-covers ::buildInfo
   */
  public function testGetInfoElementPlugin($plugin_class, $expected_info) : void {
    $this->moduleHandler
      ->expects($this->once())
      ->method('alter')
      ->with('element_info', $this->anything())
      ->willReturnArgument(0);
    $plugin = $this->createMock($plugin_class);
    $plugin->expects($this->once())
      ->method('getInfo')
      ->willReturn([
      '#theme' => 'page',
    ]);
    $element_info = $this->getMockBuilder('Drupal\\Core\\Render\\ElementInfoManager')
      ->setConstructorArgs([
      new \ArrayObject(),
      $this->cache,
      $this->themeHandler,
      $this->moduleHandler,
      $this->themeManager,
    ])
      ->onlyMethods([
      'getDefinitions',
      'createInstance',
    ])
      ->getMock();
    $this->themeManager
      ->expects($this->any())
      ->method('getActiveTheme')
      ->willReturn(new ActiveTheme([
      'name' => 'test',
    ]));
    $element_info->expects($this->once())
      ->method('createInstance')
      ->with('page')
      ->willReturn($plugin);
    $element_info->expects($this->once())
      ->method('getDefinitions')
      ->willReturn([
      'page' => [
        'class' => 'TestElementPlugin',
      ],
    ]);
    $this->assertEquals($expected_info, $element_info->getInfo('page'));
  }
  
  /**
   * Provides tests data for testGetInfoElementPlugin().
   *
   * @return array
   *   An array of test data for testGetInfoElementPlugin().
   */
  public static function providerTestGetInfoElementPlugin() {
    $data = [];
    $data[] = [
      'Drupal\\Core\\Render\\Element\\ElementInterface',
      [
        '#type' => 'page',
        '#theme' => 'page',
        '#defaults_loaded' => TRUE,
      ],
    ];
    $data[] = [
      'Drupal\\Core\\Render\\Element\\FormElementInterface',
      [
        '#type' => 'page',
        '#theme' => 'page',
        '#input' => TRUE,
        '#value_callback' => [
          'TestElementPlugin',
          'valueCallback',
        ],
        '#defaults_loaded' => TRUE,
      ],
    ];
    return $data;
  }
  
  /**
   * Tests get info property.
   *
   * @legacy-covers ::getInfoProperty
   */
  public function testGetInfoProperty() : void {
    $this->themeManager
      ->method('getActiveTheme')
      ->willReturn(new ActiveTheme([
      'name' => 'test',
    ]));
    $element_info = new TestElementInfoManager(new \ArrayObject(), $this->cache, $this->themeHandler, $this->moduleHandler, $this->themeManager);
    $this->assertSame('baz', $element_info->getInfoProperty('foo', '#bar'));
    $this->assertNull($element_info->getInfoProperty('foo', '#non_existing_property'));
    $this->assertSame('qux', $element_info->getInfoProperty('foo', '#non_existing_property', 'qux'));
  }
}
Members
| Title Sort descending | Modifiers | Object type | Summary | Overriden Title | 
|---|---|---|---|---|
| ElementInfoManagerTest::$cache | protected | property | The cache backend to use. | |
| ElementInfoManagerTest::$elementInfo | protected | property | The mocked element_info. | |
| ElementInfoManagerTest::$moduleHandler | protected | property | The mocked module handler. | |
| ElementInfoManagerTest::$themeHandler | protected | property | The mocked theme handler. | |
| ElementInfoManagerTest::$themeManager | protected | property | The mocked theme manager. | |
| ElementInfoManagerTest::providerTestGetInfoElementPlugin | public static | function | Provides tests data for testGetInfoElementPlugin(). | |
| ElementInfoManagerTest::setUp | protected | function | @legacy-covers ::__construct | Overrides UnitTestCase::setUp | 
| ElementInfoManagerTest::testGetInfoElementPlugin | public | function | Tests the getInfo() method when render element plugins are used. | |
| ElementInfoManagerTest::testGetInfoProperty | public | function | Tests get info property. | |
| ExpectDeprecationTrait::expectDeprecation | public | function | Adds an expected deprecation. | |
| ExpectDeprecationTrait::setUpErrorHandler | public | function | Sets up the test error handler. | |
| ExpectDeprecationTrait::tearDownErrorHandler | public | function | Tears down the test error handler. | |
| RandomGeneratorTrait::getRandomGenerator | protected | function | Gets the random generator for the utility methods. | |
| RandomGeneratorTrait::randomMachineName | protected | function | Generates a unique random string containing letters and numbers. | |
| RandomGeneratorTrait::randomObject | public | function | Generates a random PHP object. | |
| RandomGeneratorTrait::randomString | public | function | Generates a pseudo-random string of ASCII characters of codes 32 to 126. | |
| UnitTestCase::$root | protected | property | The app root. | |
| UnitTestCase::getClassResolverStub | protected | function | Returns a stub class resolver. | |
| UnitTestCase::getConfigFactoryStub | public | function | Returns a stub config factory that behaves according to the passed array. | |
| UnitTestCase::getContainerWithCacheTagsInvalidator | protected | function | Sets up a container with a cache tags invalidator. | |
| UnitTestCase::getStringTranslationStub | public | function | Returns a stub translation manager that just returns the passed string. | |
| UnitTestCase::setDebugDumpHandler | public static | function | Registers the dumper CLI handler when the DebugDump extension is enabled. | |
| UnitTestCase::setupMockIterator | protected | function | Set up a traversable class mock to return specific items when iterated. | 
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.