function WeightTest::testProcessWeightSelectMax

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

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

@covers ::processWeight

Throws

\Exception

File

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

Class

WeightTest
@coversDefaultClass <a href="/api/drupal/core%21lib%21Drupal%21Core%21Render%21Element%21Weight.php/class/Weight/8.9.x" title="Provides a form element for input of a weight." class="local">\Drupal\Core\Render\Element\Weight</a> @group Render

Namespace

Drupal\KernelTests\Core\Render\Element

Code

public function testProcessWeightSelectMax() {
    $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 element_info_test_element_info_alter() */
'element_info_test_element_pre_render',
        ],
    ]);
}

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