function WeightTest::testProcessWeightSelectMax

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Render/Element/WeightTest.php \Drupal\KernelTests\Core\Render\Element\WeightTest::testProcessWeightSelectMax()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Render/Element/WeightTest.php \Drupal\KernelTests\Core\Render\Element\WeightTest::testProcessWeightSelectMax()
  3. 10 core/tests/Drupal/KernelTests/Core/Render/Element/WeightTest.php \Drupal\KernelTests\Core\Render\Element\WeightTest::testProcessWeightSelectMax()

Tests transformation from "select" to "number" for MAX_DELTA + 1.

@covers ::processWeight

Throws

\Exception

File

core/tests/Drupal/KernelTests/Core/Render/Element/WeightTest.php, line 64

Class

WeightTest
@coversDefaultClass \Drupal\Core\Render\Element\Weight @group Render

Namespace

Drupal\KernelTests\Core\Render\Element

Code

public function testProcessWeightSelectMax() : void {
    $form_state = new FormState();
    $definition = [
        '#type' => 'weight',
        '#delta' => $this->container
            ->get('config.factory')
            ->get('system.site')
            ->get('weight_select_max'),
        // Expected by the "doBuildForm()" method of "form_builder" service.
'#parents' => [],
    ];
    $assert = function ($type, array $element, array $expected) use ($form_state) {
        // Pretend we have a form to trigger the "#process" callbacks.
        $element = $this->container
            ->get('form_builder')
            ->doBuildForm(__FUNCTION__, $element, $form_state);
        $expected['#type'] = $type;
        foreach ($expected as $property => $value) {
            static::assertSame($value, $element[$property]);
        }
        return $element;
    };
    // When the "#delta" is less or equal to maximum the "weight" must be
    // rendered as a "select".
    $select = $definition;
    $assert('select', $select, [
        '#process' => [
            [
                Select::class,
                'processSelect',
            ],
            [
                Select::class,
                'processAjaxForm',
            ],
        ],
        '#pre_render' => [
            [
                Select::class,
                'preRenderSelect',
            ],
        ],
    ]);
    $number = $definition;
    // Increase "#delta" in order to start rendering "number" elements
    // instead of "select".
    $number['#delta']++;
    // The "number" element definition has the "#pre_render" declaration by
    // default. The "hook_element_info_alter()" allows to modify the definition
    // of an element. We must be sure the standard "#pre_render" callbacks
    // are presented (unless explicitly removed) even in a case when the array
    // is modified by the alter hook.
    $assert('number', $number, [
        '#process' => [
            [
                Number::class,
                'processAjaxForm',
            ],
        ],
        '#element_validate' => [
            [
                Number::class,
                'validateNumber',
            ],
        ],
        '#pre_render' => [
            [
                Number::class,
                'preRenderNumber',
            ],
            // The custom callback is appended.
            
            /* @see \Drupal\element_info_test\ElementInfoTestNumberBuilder::preRender */
[
                ElementInfoTestNumberBuilder::class,
                'preRender',
            ],
        ],
    ]);
}

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