You are here

public function WeightTest::testProcessWeightSelectMax in Drupal 8

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()

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 \Drupal\Core\Render\Element\Weight @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',
    ],
  ]);
}