MachineNameTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace Drupal\FunctionalJavascriptTests\Core;
  3. use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
  4. /**
  5. * Tests for the machine name field.
  6. *
  7. * @group field
  8. */
  9. class MachineNameTest extends WebDriverTestBase {
  10. /**
  11. * Required modules.
  12. *
  13. * Node is required because the machine name callback checks for
  14. * access_content.
  15. *
  16. * @var array
  17. */
  18. public static $modules = ['node', 'form_test'];
  19. /**
  20. * {@inheritdoc}
  21. */
  22. protected $defaultTheme = 'stark';
  23. /**
  24. * {@inheritdoc}
  25. */
  26. protected function setUp() {
  27. parent::setUp();
  28. $account = $this->drupalCreateUser([
  29. 'access content',
  30. ]);
  31. $this->drupalLogin($account);
  32. }
  33. /**
  34. * Tests that machine name field functions.
  35. *
  36. * Makes sure that the machine name field automatically provides a valid
  37. * machine name and that the manual editing mode functions.
  38. */
  39. public function testMachineName() {
  40. // Visit the machine name test page which contains two machine name fields.
  41. $this->drupalGet('form-test/machine-name');
  42. // Test values for conversion.
  43. $test_values = [
  44. [
  45. 'input' => 'Test value !0-9@',
  46. 'message' => 'A title that should be transliterated must be equal to the php generated machine name',
  47. 'expected' => 'test_value_0_9_',
  48. ],
  49. [
  50. 'input' => 'Test value',
  51. 'message' => 'A title that should not be transliterated must be equal to the php generated machine name',
  52. 'expected' => 'test_value',
  53. ],
  54. ];
  55. // Get page and session.
  56. $page = $this->getSession()->getPage();
  57. // Get elements from the page.
  58. $title_1 = $page->findField('machine_name_1_label');
  59. $machine_name_1_field = $page->findField('machine_name_1');
  60. $machine_name_2_field = $page->findField('machine_name_2');
  61. $machine_name_1_wrapper = $machine_name_1_field->getParent();
  62. $machine_name_2_wrapper = $machine_name_2_field->getParent();
  63. $machine_name_1_value = $page->find('css', '#edit-machine-name-1-label-machine-name-suffix .machine-name-value');
  64. $machine_name_2_value = $page->find('css', '#edit-machine-name-2-label-machine-name-suffix .machine-name-value');
  65. $machine_name_3_value = $page->find('css', '#edit-machine-name-3-label-machine-name-suffix .machine-name-value');
  66. $button_1 = $page->find('css', '#edit-machine-name-1-label-machine-name-suffix button.link');
  67. // Assert all fields are initialized correctly.
  68. $this->assertNotEmpty($machine_name_1_value, 'Machine name field 1 must be initialized');
  69. $this->assertNotEmpty($machine_name_2_value, 'Machine name field 2 must be initialized');
  70. $this->assertNotEmpty($machine_name_3_value, 'Machine name field 3 must be initialized');
  71. // Assert that a machine name based on a default value is initialized.
  72. $this->assertJsCondition('jQuery("#edit-machine-name-3-label-machine-name-suffix .machine-name-value").html() == "yet_another_machine_name"');
  73. // Field must be present for the rest of the test to work.
  74. if (empty($machine_name_1_value)) {
  75. $this->fail('Cannot finish test, missing machine name field');
  76. }
  77. // Test each value for conversion to a machine name.
  78. foreach ($test_values as $test_info) {
  79. // Set the value for the field, triggering the machine name update.
  80. $title_1->setValue($test_info['input']);
  81. // Wait the set timeout for fetching the machine name.
  82. $this->assertJsCondition('jQuery("#edit-machine-name-1-label-machine-name-suffix .machine-name-value").html() == "' . $test_info['expected'] . '"');
  83. // Validate the generated machine name.
  84. $this->assertEquals($test_info['expected'], $machine_name_1_value->getHtml(), $test_info['message']);
  85. // Validate the second machine name field is empty.
  86. $this->assertEmpty($machine_name_2_value->getHtml(), 'The second machine name field should still be empty');
  87. }
  88. // Validate the machine name field is hidden. Elements are visually hidden
  89. // using positioning, isVisible() will therefore not work.
  90. $this->assertEquals(TRUE, $machine_name_1_wrapper->hasClass('visually-hidden'), 'The ID field must not be visible');
  91. $this->assertEquals(TRUE, $machine_name_2_wrapper->hasClass('visually-hidden'), 'The ID field must not be visible');
  92. // Test switching back to the manual editing mode by clicking the edit link.
  93. $button_1->click();
  94. // Validate the visibility of the machine name field.
  95. $this->assertEquals(FALSE, $machine_name_1_wrapper->hasClass('visually-hidden'), 'The ID field must now be visible');
  96. // Validate the visibility of the second machine name field.
  97. $this->assertEquals(TRUE, $machine_name_2_wrapper->hasClass('visually-hidden'), 'The ID field must not be visible');
  98. // Validate if the element contains the correct value.
  99. $this->assertEquals($test_values[1]['expected'], $machine_name_1_field->getValue(), 'The ID field value must be equal to the php generated machine name');
  100. $assert = $this->assertSession();
  101. $this->drupalGet('/form-test/form-test-machine-name-validation');
  102. // Test errors after with no AJAX.
  103. $assert->buttonExists('Save')->press();
  104. $assert->pageTextContains('Machine-readable name field is required.');
  105. // Ensure only the first machine name field has an error.
  106. $this->assertTrue($assert->fieldExists('id')->hasClass('error'));
  107. $this->assertFalse($assert->fieldExists('id2')->hasClass('error'));
  108. // Test a successful submit after using AJAX.
  109. $assert->fieldExists('Name')->setValue('test 1');
  110. $assert->fieldExists('id')->setValue('test_1');
  111. $assert->selectExists('snack')->selectOption('apple');
  112. $assert->assertWaitOnAjaxRequest();
  113. $assert->buttonExists('Save')->press();
  114. $assert->pageTextContains('The form_test_machine_name_validation_form form has been submitted successfully.');
  115. // Test errors after using AJAX.
  116. $assert->fieldExists('Name')->setValue('duplicate');
  117. $this->assertJsCondition('document.forms[0].id.value === "duplicate"');
  118. $assert->fieldExists('id2')->setValue('duplicate2');
  119. $assert->selectExists('snack')->selectOption('potato');
  120. $assert->assertWaitOnAjaxRequest();
  121. $assert->buttonExists('Save')->press();
  122. $assert->pageTextContains('The machine-readable name is already in use. It must be unique.');
  123. // Ensure both machine name fields both have errors.
  124. $this->assertTrue($assert->fieldExists('id')->hasClass('error'));
  125. $this->assertTrue($assert->fieldExists('id2')->hasClass('error'));
  126. }
  127. }