function DeprecatedElementTest::testBuildInfo

Same name in other branches
  1. 11.x core/tests/Drupal/KernelTests/Core/Render/Element/DeprecatedElementTest.php \Drupal\KernelTests\Core\Render\Element\DeprecatedElementTest::testBuildInfo()

Tests that render elements can trigger deprecations in their constructor.

File

core/tests/Drupal/KernelTests/Core/Render/Element/DeprecatedElementTest.php, line 25

Class

DeprecatedElementTest
@group Render

Namespace

Drupal\KernelTests\Core\Render\Element

Code

public function testBuildInfo() : void {
    $info_manager = $this->container
        ->get('plugin.manager.element_info');
    $element_names = [
        'deprecated',
        'deprecated_extends_render',
    ];
    foreach ($element_names as $element_name) {
        $this->assertSame([
            '#type' => $element_name,
            '#defaults_loaded' => TRUE,
        ], $info_manager->getInfo($element_name));
    }
    $this->assertSame([
        '#input' => TRUE,
        '#value_callback' => [
            DeprecatedExtendsFormElement::class,
            'valueCallback',
        ],
        '#type' => 'deprecated_extends_form',
        '#defaults_loaded' => TRUE,
    ], $info_manager->getInfo('deprecated_extends_form'));
    // Ensure the constructor is triggering a deprecation error.
    $previous_error_handler = set_error_handler(function ($severity, $message, $file, $line) use (&$previous_error_handler) {
        // Convert deprecation error into a catchable exception.
        if ($severity === E_USER_DEPRECATED) {
            throw new \ErrorException($message, 0, $severity, $file, $line);
        }
        if ($previous_error_handler) {
            return $previous_error_handler($severity, $message, $file, $line);
        }
    });
    try {
        $info_manager->createInstance('deprecated');
        $this->fail('No deprecation error triggered.');
    } catch (\ErrorException $e) {
        $this->assertSame('Drupal\\element_info_test\\Element\\Deprecated is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. See https://www.drupal.org/node/3068104', $e->getMessage());
    }
    try {
        $info_manager->createInstance('deprecated_extends_form');
        $this->fail('No deprecation error triggered.');
    } catch (\ErrorException $e) {
        $this->assertSame('\\Drupal\\Core\\Render\\Element\\FormElement is deprecated in drupal:10.3.0 and is removed from drupal:12.0.0. Use \\Drupal\\Core\\Render\\Element\\FormElementBase instead. See https://www.drupal.org/node/3436275', $e->getMessage());
    }
    try {
        $info_manager->createInstance('deprecated_extends_render');
        $this->fail('No deprecation error triggered.');
    } catch (\ErrorException $e) {
        $this->assertSame('\\Drupal\\Core\\Render\\Element\\RenderElement is deprecated in drupal:10.3.0 and is removed from drupal:12.0.0. Use \\Drupal\\Core\\Render\\Element\\RenderElementBase instead. See https://www.drupal.org/node/3436275', $e->getMessage());
    }
    restore_error_handler();
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.