MachineNameTest.php 5.9 KB

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