PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/web/core/modules/field/tests/src/FunctionalJavascript/FormJSAddMoreTest.php

https://gitlab.com/mohamed_hussein/prodt
PHP | 128 lines | 74 code | 25 blank | 29 comment | 0 complexity | fbf0d054d70535ea8e6490cd8f43b6bc MD5 | raw file
  1. <?php
  2. namespace Drupal\Tests\field\FunctionalJavascript;
  3. use Drupal\Core\Entity\Entity\EntityFormDisplay;
  4. use Drupal\Core\Field\FieldStorageDefinitionInterface;
  5. use Drupal\field\Entity\FieldConfig;
  6. use Drupal\field\Entity\FieldStorageConfig;
  7. use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
  8. /**
  9. * Tests add more behavior for a multiple value field.
  10. *
  11. * @group field
  12. */
  13. class FormJSAddMoreTest extends WebDriverTestBase {
  14. /**
  15. * {@inheritdoc}
  16. */
  17. protected static $modules = ['field_test', 'entity_test'];
  18. /**
  19. * {@inheritdoc}
  20. */
  21. protected $defaultTheme = 'stark';
  22. /**
  23. * {@inheritdoc}
  24. */
  25. protected function setUp(): void {
  26. parent::setUp();
  27. $account = $this->drupalCreateUser([
  28. 'view test entity',
  29. 'administer entity_test content',
  30. ]);
  31. $this->drupalLogin($account);
  32. $field = [
  33. 'field_name' => 'field_unlimited',
  34. 'entity_type' => 'entity_test',
  35. 'bundle' => 'entity_test',
  36. 'label' => $this->randomMachineName() . '_label',
  37. 'description' => '[site:name]_description',
  38. 'weight' => mt_rand(0, 127),
  39. 'settings' => [
  40. 'test_field_setting' => $this->randomMachineName(),
  41. ],
  42. ];
  43. FieldStorageConfig::create([
  44. 'field_name' => 'field_unlimited',
  45. 'entity_type' => 'entity_test',
  46. 'type' => 'test_field',
  47. 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
  48. ])->save();
  49. FieldConfig::create($field)->save();
  50. $entity_form_display = EntityFormDisplay::load($field['entity_type'] . '.' . $field['bundle'] . '.default');
  51. $entity_form_display->setComponent($field['field_name'])->save();
  52. }
  53. /**
  54. * Tests the 'Add more' functionality.
  55. */
  56. public function testFieldFormJsAddMore() {
  57. $this->drupalGet('entity_test/add');
  58. $assert_session = $this->assertSession();
  59. $page = $this->getSession()->getPage();
  60. $add_more_button = $page->findButton('field_unlimited_add_more');
  61. // First set a value on the first input field.
  62. $field_0 = $page->findField('field_unlimited[0][value]');
  63. $field_0->setValue('1');
  64. // Add another item.
  65. $add_more_button->click();
  66. $field_1 = $assert_session->waitForField('field_unlimited[1][value]');
  67. $this->assertNotEmpty($field_1, 'Successfully added another item.');
  68. // Validate the value of the first field has not changed.
  69. $this->assertEquals('1', $field_0->getValue(), 'Value for the first item has not changed.');
  70. // Validate the value of the second item is empty.
  71. $this->assertEmpty($field_1->getValue(), 'Value for the second item is currently empty.');
  72. // Add another item.
  73. $add_more_button->click();
  74. $field_2 = $assert_session->waitForField('field_unlimited[2][value]');
  75. $this->assertNotEmpty($field_2, 'Successfully added another item.');
  76. // Set values for the 2nd and 3rd fields to validate dragging.
  77. $field_1->setValue('2');
  78. $field_2->setValue('3');
  79. $field_weight_0 = $page->findField('field_unlimited[0][_weight]');
  80. $field_weight_1 = $page->findField('field_unlimited[1][_weight]');
  81. $field_weight_2 = $page->findField('field_unlimited[2][_weight]');
  82. // Assert starting situation matches expectations.
  83. $this->assertGreaterThan($field_weight_0->getValue(), $field_weight_1->getValue());
  84. $this->assertGreaterThan($field_weight_1->getValue(), $field_weight_2->getValue());
  85. // Drag the first row after the third row.
  86. $dragged = $field_0->find('xpath', 'ancestor::tr[contains(@class, "draggable")]//a[@class="tabledrag-handle"]');
  87. $target = $field_2->find('xpath', 'ancestor::tr[contains(@class, "draggable")]');
  88. $dragged->dragTo($target);
  89. // Assert the order of items is updated correctly after dragging.
  90. $this->assertGreaterThan($field_weight_2->getValue(), $field_weight_0->getValue());
  91. $this->assertGreaterThan($field_weight_1->getValue(), $field_weight_2->getValue());
  92. // Validate the order of items conforms to the last drag action after a
  93. // updating the form via the server.
  94. $add_more_button->click();
  95. $field_3 = $assert_session->waitForField('field_unlimited[3][value]');
  96. $this->assertNotEmpty($field_3);
  97. $this->assertGreaterThan($field_weight_2->getValue(), $field_weight_0->getValue());
  98. $this->assertGreaterThan($field_weight_1->getValue(), $field_weight_2->getValue());
  99. // Validate no extraneous widget is displayed.
  100. $element = $page->findField('field_unlimited[4][value]');
  101. $this->assertEmpty($element);
  102. }
  103. }