3 namespace Drupal\FunctionalJavascriptTests\Core;
5 use Drupal\FunctionalJavascriptTests\JavascriptTestBase;
8 * Tests for the machine name field.
12 class MachineNameTest extends JavascriptTestBase {
17 * Node is required because the machine name callback checks for
22 public static $modules = ['node', 'form_test'];
27 protected function setUp() {
30 $account = $this->drupalCreateUser([
33 $this->drupalLogin($account);
37 * Tests that machine name field functions.
39 * Makes sure that the machine name field automatically provides a valid
40 * machine name and that the manual editing mode functions.
42 public function testMachineName() {
43 // Visit the machine name test page which contains two machine name fields.
44 $this->drupalGet('form-test/machine-name');
46 // Test values for conversion.
49 'input' => 'Test value !0-9@',
50 'message' => 'A title that should be transliterated must be equal to the php generated machine name',
51 'expected' => 'test_value_0_9_',
54 'input' => 'Test value',
55 'message' => 'A title that should not be transliterated must be equal to the php generated machine name',
56 'expected' => 'test_value',
60 // Get page and session.
61 $page = $this->getSession()->getPage();
62 $assert_session = $this->assertSession();
64 // Get elements from the page.
65 $title_1 = $page->findField('machine_name_1_label');
66 $machine_name_1_field = $page->findField('machine_name_1');
67 $machine_name_2_field = $page->findField('machine_name_2');
68 $machine_name_1_wrapper = $machine_name_1_field->getParent();
69 $machine_name_2_wrapper = $machine_name_2_field->getParent();
70 $machine_name_1_value = $page->find('css', '#edit-machine-name-1-label-machine-name-suffix .machine-name-value');
71 $machine_name_2_value = $page->find('css', '#edit-machine-name-2-label-machine-name-suffix .machine-name-value');
72 $button_1 = $page->find('css', '#edit-machine-name-1-label-machine-name-suffix button.link');
74 // Assert both fields are initialized correctly.
75 $this->assertNotEmpty($machine_name_1_value, 'Machine name field 1 must be initialized');
76 $this->assertNotEmpty($machine_name_2_value, 'Machine name field 2 must be initialized');
78 // Field must be present for the rest of the test to work.
79 if (empty($machine_name_1_value)) {
80 $this->fail('Cannot finish test, missing machine name field');
83 // Test each value for conversion to a machine name.
84 foreach ($test_values as $test_info) {
85 // Set the value for the field, triggering the machine name update.
86 $title_1->setValue($test_info['input']);
88 // Wait the set timeout for fetching the machine name.
89 $this->assertJsCondition('jQuery("#edit-machine-name-1-label-machine-name-suffix .machine-name-value").html() == "' . $test_info['expected'] . '"');
91 // Validate the generated machine name.
92 $this->assertEquals($test_info['expected'], $machine_name_1_value->getHtml(), $test_info['message']);
94 // Validate the second machine name field is empty.
95 $this->assertEmpty($machine_name_2_value->getHtml(), 'The second machine name field should still be empty');
98 // Validate the machine name field is hidden. Elements are visually hidden
99 // using positioning, isVisible() will therefore not work.
100 $this->assertEquals(TRUE, $machine_name_1_wrapper->hasClass('visually-hidden'), 'The ID field must not be visible');
101 $this->assertEquals(TRUE, $machine_name_2_wrapper->hasClass('visually-hidden'), 'The ID field must not be visible');
103 // Test switching back to the manual editing mode by clicking the edit link.
106 // Validate the visibility of the machine name field.
107 $this->assertEquals(FALSE, $machine_name_1_wrapper->hasClass('visually-hidden'), 'The ID field must now be visible');
109 // Validate the visibility of the second machine name field.
110 $this->assertEquals(TRUE, $machine_name_2_wrapper->hasClass('visually-hidden'), 'The ID field must not be visible');
112 // Validate if the element contains the correct value.
113 $this->assertEquals($test_values[1]['expected'], $machine_name_1_field->getValue(), 'The ID field value must be equal to the php generated machine name');