WeightTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace Drupal\KernelTests\Core\Render\Element;
  3. use Drupal\Core\Form\FormState;
  4. use Drupal\Core\Render\Element\Number;
  5. use Drupal\Core\Render\Element\Select;
  6. use Drupal\Core\Render\Element\Weight;
  7. use Drupal\KernelTests\KernelTestBase;
  8. /**
  9. * @coversDefaultClass \Drupal\Core\Render\Element\Weight
  10. * @group Render
  11. */
  12. class WeightTest extends KernelTestBase {
  13. /**
  14. * {@inheritdoc}
  15. */
  16. protected static $modules = ['system', 'element_info_test'];
  17. /**
  18. * {@inheritdoc}
  19. */
  20. protected function setUp() {
  21. parent::setUp();
  22. $this->installConfig(['system']);
  23. }
  24. /**
  25. * Test existing #default_value value in #options list.
  26. *
  27. * @covers ::processWeight
  28. */
  29. public function testProcessWeight() {
  30. $element = [];
  31. $form_state = new FormState();
  32. $complete_form = [];
  33. $element_object = new Weight([], 'weight', []);
  34. $info = $element_object->getInfo();
  35. $element += $info;
  36. $element['#default_value'] = $element['#delta'] + 5;
  37. Weight::processWeight($element, $form_state, $complete_form);
  38. $this->assertTrue(
  39. isset($element['#options'][$element['#default_value']]),
  40. 'Default value exists in the #options list'
  41. );
  42. }
  43. /**
  44. * Test transformation from "select" to "number" for MAX_DELTA + 1.
  45. *
  46. * @throws \Exception
  47. *
  48. * @covers ::processWeight
  49. */
  50. public function testProcessWeightSelectMax() {
  51. $form_state = new FormState();
  52. $definition = [
  53. '#type' => 'weight',
  54. '#delta' => $this->container
  55. ->get('config.factory')
  56. ->get('system.site')
  57. ->get('weight_select_max'),
  58. // Expected by the "doBuildForm()" method of "form_builder" service.
  59. '#parents' => [],
  60. ];
  61. $assert = function ($type, array $element, array $expected) use ($form_state) {
  62. // Pretend we have a form to trigger the "#process" callbacks.
  63. $element = $this->container
  64. ->get('form_builder')
  65. ->doBuildForm(__FUNCTION__, $element, $form_state);
  66. $expected['#type'] = $type;
  67. foreach ($expected as $property => $value) {
  68. static::assertSame($value, $element[$property]);
  69. }
  70. return $element;
  71. };
  72. // When the "#delta" is less or equal to maximum the "weight" must be
  73. // rendered as a "select".
  74. $select = $definition;
  75. $assert('select', $select, [
  76. '#process' => [
  77. [Select::class, 'processSelect'],
  78. [Select::class, 'processAjaxForm'],
  79. ],
  80. '#pre_render' => [
  81. [Select::class, 'preRenderSelect'],
  82. ],
  83. ]);
  84. $number = $definition;
  85. // Increase "#delta" in order to start rendering "number" elements
  86. // instead of "select".
  87. $number['#delta']++;
  88. // The "number" element definition has the "#pre_render" declaration by
  89. // default. The "hook_element_info_alter()" allows to modify the definition
  90. // of an element. We must be sure the standard "#pre_render" callbacks
  91. // are presented (unless explicitly removed) even in a case when the array
  92. // is modified by the alter hook.
  93. $assert('number', $number, [
  94. '#process' => [
  95. [Number::class, 'processAjaxForm'],
  96. ],
  97. '#element_validate' => [
  98. [Number::class, 'validateNumber'],
  99. ],
  100. '#pre_render' => [
  101. [Number::class, 'preRenderNumber'],
  102. // The custom callback is appended.
  103. /* @see element_info_test_element_info_alter() */
  104. 'element_info_test_element_pre_render',
  105. ],
  106. ]);
  107. }
  108. }