function DeprecatedElementTest::testDeprecatedStaticMethods

Same name and namespace in other branches
  1. 10 core/tests/Drupal/KernelTests/Core/Render/Element/DeprecatedElementTest.php \Drupal\KernelTests\Core\Render\Element\DeprecatedElementTest::testDeprecatedStaticMethods()

Test use of static methods trigger deprecations.

File

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

Class

DeprecatedElementTest
@group Render

Namespace

Drupal\KernelTests\Core\Render\Element

Code

public function testDeprecatedStaticMethods() : void {
    $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);
        }
    });
    $element = [];
    $form_state = new FormState();
    $complete_form = [];
    $static_methods_render = [
        'setAttributes' => [
            $element,
        ],
        'preRenderGroup' => [
            $element,
        ],
        'processAjaxForm' => [
            $element,
            $form_state,
            $complete_form,
        ],
        'preRenderAjaxForm' => [
            $element,
        ],
        'processGroup' => [
            $element,
            $form_state,
            $complete_form,
        ],
    ];
    $static_methods_form = [
        'valueCallback' => [
            $element,
            FALSE,
            $form_state,
        ],
        'processPattern' => [
            $element,
            $form_state,
            $complete_form,
        ],
        'validatePattern' => [
            $element,
            $form_state,
            $complete_form,
        ],
        'processAutocomplete' => [
            $element,
            $form_state,
            $complete_form,
        ],
    ];
    $class_names = [
        DeprecatedExtendsRenderElement::class,
        DeprecatedExtendsFormElement::class,
    ];
    foreach ($class_names as $class_name) {
        foreach ($static_methods_render as $method_name => $arguments) {
            try {
                $class_name::$method_name(...$arguments);
                $this->fail('No deprecation error triggered.');
            } catch (\ErrorException $e) {
                $parent_class = get_parent_class($class_name);
                $this->assertSame("\\{$parent_class}::{$method_name}() is deprecated in drupal:10.3.0 and is removed from drupal:12.0.0. Use \\{$parent_class}Base::{$method_name}() instead. See https://www.drupal.org/node/3436275", $e->getMessage());
            }
        }
    }
    foreach ($static_methods_form as $method_name => $arguments) {
        try {
            DeprecatedExtendsFormElement::$method_name(...$arguments);
            $this->fail('No deprecation error triggered.');
        } catch (\ErrorException $e) {
            $this->assertSame("\\Drupal\\Core\\Render\\Element\\FormElement::{$method_name}() is deprecated in drupal:10.3.0 and is removed from drupal:12.0.0. Use \\Drupal\\Core\\Render\\Element\\FormElementBase::{$method_name}() 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.