MachineNameTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace Drupal\FunctionalJavascriptTests\Core;
  3. use Drupal\FunctionalJavascriptTests\JavascriptTestBase;
  4. /**
  5. * Tests for the machine name field.
  6. *
  7. * @group field
  8. */
  9. class MachineNameTest extends JavascriptTestBase {
  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. $assert_session = $this->assertSession();
  54. // Get elements from the page.
  55. $title_1 = $page->findField('machine_name_1_label');
  56. $machine_name_1_field = $page->findField('machine_name_1');
  57. $machine_name_2_field = $page->findField('machine_name_2');
  58. $machine_name_1_wrapper = $machine_name_1_field->getParent();
  59. $machine_name_2_wrapper = $machine_name_2_field->getParent();
  60. $machine_name_1_value = $page->find('css', '#edit-machine-name-1-label-machine-name-suffix .machine-name-value');
  61. $machine_name_2_value = $page->find('css', '#edit-machine-name-2-label-machine-name-suffix .machine-name-value');
  62. $button_1 = $page->find('css', '#edit-machine-name-1-label-machine-name-suffix button.link');
  63. // Assert both fields are initialized correctly.
  64. $this->assertNotEmpty($machine_name_1_value, 'Machine name field 1 must be initialized');
  65. $this->assertNotEmpty($machine_name_2_value, 'Machine name field 2 must be initialized');
  66. // Field must be present for the rest of the test to work.
  67. if (empty($machine_name_1_value)) {
  68. $this->fail('Cannot finish test, missing machine name field');
  69. }
  70. // Test each value for conversion to a machine name.
  71. foreach ($test_values as $test_info) {
  72. // Set the value for the field, triggering the machine name update.
  73. $title_1->setValue($test_info['input']);
  74. // Wait the set timeout for fetching the machine name.
  75. $this->assertJsCondition('jQuery("#edit-machine-name-1-label-machine-name-suffix .machine-name-value").html() == "' . $test_info['expected'] . '"');
  76. // Validate the generated machine name.
  77. $this->assertEquals($test_info['expected'], $machine_name_1_value->getHtml(), $test_info['message']);
  78. // Validate the second machine name field is empty.
  79. $this->assertEmpty($machine_name_2_value->getHtml(), 'The second machine name field should still be empty');
  80. }
  81. // Validate the machine name field is hidden. Elements are visually hidden
  82. // using positioning, isVisible() will therefore not work.
  83. $this->assertEquals(TRUE, $machine_name_1_wrapper->hasClass('visually-hidden'), 'The ID field must not be visible');
  84. $this->assertEquals(TRUE, $machine_name_2_wrapper->hasClass('visually-hidden'), 'The ID field must not be visible');
  85. // Test switching back to the manual editing mode by clicking the edit link.
  86. $button_1->click();
  87. // Validate the visibility of the machine name field.
  88. $this->assertEquals(FALSE, $machine_name_1_wrapper->hasClass('visually-hidden'), 'The ID field must now be visible');
  89. // Validate the visibility of the second machine name field.
  90. $this->assertEquals(TRUE, $machine_name_2_wrapper->hasClass('visually-hidden'), 'The ID field must not be visible');
  91. // Validate if the element contains the correct value.
  92. $this->assertEquals($test_values[1]['expected'], $machine_name_1_field->getValue(), 'The ID field value must be equal to the php generated machine name');
  93. }
  94. }