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