3 namespace Drupal\FunctionalJavascriptTests\Core;
5 use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
8 * Tests for the machine name field.
12 class MachineNameTest extends WebDriverTestBase {
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();
63 // Get elements from the page.
64 $title_1 = $page->findField('machine_name_1_label');
65 $machine_name_1_field = $page->findField('machine_name_1');
66 $machine_name_2_field = $page->findField('machine_name_2');
67 $machine_name_1_wrapper = $machine_name_1_field->getParent();
68 $machine_name_2_wrapper = $machine_name_2_field->getParent();
69 $machine_name_1_value = $page->find('css', '#edit-machine-name-1-label-machine-name-suffix .machine-name-value');
70 $machine_name_2_value = $page->find('css', '#edit-machine-name-2-label-machine-name-suffix .machine-name-value');
71 $button_1 = $page->find('css', '#edit-machine-name-1-label-machine-name-suffix button.link');
73 // Assert both fields are initialized correctly.
74 $this->assertNotEmpty($machine_name_1_value, 'Machine name field 1 must be initialized');
75 $this->assertNotEmpty($machine_name_2_value, 'Machine name field 2 must be initialized');
77 // Field must be present for the rest of the test to work.
78 if (empty($machine_name_1_value)) {
79 $this->fail('Cannot finish test, missing machine name field');
82 // Test each value for conversion to a machine name.
83 foreach ($test_values as $test_info) {
84 // Set the value for the field, triggering the machine name update.
85 $title_1->setValue($test_info['input']);
87 // Wait the set timeout for fetching the machine name.
88 $this->assertJsCondition('jQuery("#edit-machine-name-1-label-machine-name-suffix .machine-name-value").html() == "' . $test_info['expected'] . '"');
90 // Validate the generated machine name.
91 $this->assertEquals($test_info['expected'], $machine_name_1_value->getHtml(), $test_info['message']);
93 // Validate the second machine name field is empty.
94 $this->assertEmpty($machine_name_2_value->getHtml(), 'The second machine name field should still be empty');
97 // Validate the machine name field is hidden. Elements are visually hidden
98 // using positioning, isVisible() will therefore not work.
99 $this->assertEquals(TRUE, $machine_name_1_wrapper->hasClass('visually-hidden'), 'The ID field must not be visible');
100 $this->assertEquals(TRUE, $machine_name_2_wrapper->hasClass('visually-hidden'), 'The ID field must not be visible');
102 // Test switching back to the manual editing mode by clicking the edit link.
105 // Validate the visibility of the machine name field.
106 $this->assertEquals(FALSE, $machine_name_1_wrapper->hasClass('visually-hidden'), 'The ID field must now be visible');
108 // Validate the visibility of the second machine name field.
109 $this->assertEquals(TRUE, $machine_name_2_wrapper->hasClass('visually-hidden'), 'The ID field must not be visible');
111 // Validate if the element contains the correct value.
112 $this->assertEquals($test_values[1]['expected'], $machine_name_1_field->getValue(), 'The ID field value must be equal to the php generated machine name');
114 $assert = $this->assertSession();
115 $this->drupalGet('/form-test/form-test-machine-name-validation');
117 // Test errors after with no AJAX.
118 $assert->buttonExists('Save')->press();
119 $assert->pageTextContains('Machine-readable name field is required.');
120 // Ensure only the first machine name field has an error.
121 $this->assertTrue($assert->fieldExists('id')->hasClass('error'));
122 $this->assertFalse($assert->fieldExists('id2')->hasClass('error'));
124 // Test a successful submit after using AJAX.
125 $assert->fieldExists('Name')->setValue('test 1');
126 $assert->fieldExists('id')->setValue('test_1');
127 $assert->selectExists('snack')->selectOption('apple');
128 $assert->assertWaitOnAjaxRequest();
129 $assert->buttonExists('Save')->press();
130 $assert->pageTextContains('The form_test_machine_name_validation_form form has been submitted successfully.');
132 // Test errors after using AJAX.
133 $assert->fieldExists('Name')->setValue('duplicate');
134 $this->assertJsCondition('document.forms[0].id.value === "duplicate"');
135 $assert->fieldExists('id2')->setValue('duplicate2');
136 $assert->selectExists('snack')->selectOption('potato');
137 $assert->assertWaitOnAjaxRequest();
138 $assert->buttonExists('Save')->press();
139 $assert->pageTextContains('The machine-readable name is already in use. It must be unique.');
140 // Ensure both machine name fields both have errors.
141 $this->assertTrue($assert->fieldExists('id')->hasClass('error'));
142 $this->assertTrue($assert->fieldExists('id2')->hasClass('error'));