diff --git a/core/tests/Drupal/FunctionalJavascriptTests/Core/MachineNameTest.php b/core/tests/Drupal/FunctionalJavascriptTests/Core/MachineNameTest.php new file mode 100644 index 0000000..bf4649f --- /dev/null +++ b/core/tests/Drupal/FunctionalJavascriptTests/Core/MachineNameTest.php @@ -0,0 +1,103 @@ +drupalCreateUser(array( + 'access content', + 'administer permissions', + )); + $this->drupalLogin($account); + + // Get the transliteration server for validating the generated machine name. + $this->transliteration = \Drupal::service('transliteration'); + } + + /** + * Tests that machine name field functions. + * + * Makes sure that the machine name field automatically provides a valid + * machine name and that the manual editing mode functions. + */ + public function testMachineName() { + // Visit the add role page which contains a machine name field. + $this->drupalGet('admin/people/roles/add'); + + // Test values for conversion. + $test_values = array( + 'Test value !0-9@' => 'A title that should be transliterated must be equal to the php generated machine name', + 'Test value' => 'A title that should not be transliterated must be equal to the php generated machine name', + ); + + // Find relevant elements on the page for this test. + $page = $this->getSession()->getPage(); + $assert_session = $this->assertSession(); + $element = $page->findField('label'); + $machine_name = $page->find('css', '#edit-label-machine-name-suffix .machine-name-value'); + $button = $page->find('css', '#edit-label-machine-name-suffix button.link'); + $id = $page->findField('id'); + + // Test each value for conversion to a machine name. + foreach ($test_values as $test_value => $message) { + $transliterated = $this->transliteration->transliterate($test_value); + $transliterated = Unicode::strtolower($transliterated); + $transliterated = preg_replace('@' . strtr('[^a-z0-9_]+', [ + '@' => '\@', + chr(0) => '' + ]) . '@', '_', $transliterated); + + // Set the value for the role, triggering the machine name update. + $element->setValue($test_value); + + // Wait the set timeout for fetching the machine name. + $this->getSession()->wait(300); + + // And wait for the autocomplete to finish. + $assert_session->waitOnAutocomplete(); + + // Get the generated machine name. + $generated_value = $machine_name->getHTML(); + + // Validate the generated machine name. + $this->assertEquals($transliterated, $generated_value, $message); + } + + // Test switching back to the manual editing mode by clicking the edit link. + $button->click(); + + // Validate the visibility of the machine name field. + $this->assertTrue($id->isVisible(), 'The ID field must now be visible'); + + // Validate if the element contains the correct value. + $this->assertEquals($transliterated, $id->getValue(), 'The ID field value must be equal to the php generated machine name'); + } + +}