Pull merge.
[yaffs-website] / web / core / tests / Drupal / FunctionalJavascriptTests / Core / MachineNameTest.php
1 <?php
2
3 namespace Drupal\FunctionalJavascriptTests\Core;
4
5 use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
6
7 /**
8  * Tests for the machine name field.
9  *
10  * @group field
11  */
12 class MachineNameTest extends WebDriverTestBase {
13
14   /**
15    * Required modules.
16    *
17    * Node is required because the machine name callback checks for
18    * access_content.
19    *
20    * @var array
21    */
22   public static $modules = ['node', 'form_test'];
23
24   /**
25    * {@inheritdoc}
26    */
27   protected function setUp() {
28     parent::setUp();
29
30     $account = $this->drupalCreateUser([
31       'access content',
32     ]);
33     $this->drupalLogin($account);
34   }
35
36   /**
37    * Tests that machine name field functions.
38    *
39    * Makes sure that the machine name field automatically provides a valid
40    * machine name and that the manual editing mode functions.
41    */
42   public function testMachineName() {
43     // Visit the machine name test page which contains two machine name fields.
44     $this->drupalGet('form-test/machine-name');
45
46     // Test values for conversion.
47     $test_values = [
48       [
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_',
52       ],
53       [
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',
57       ],
58     ];
59
60     // Get page and session.
61     $page = $this->getSession()->getPage();
62
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');
72
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');
76
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');
80     }
81
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']);
86
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'] . '"');
89
90       // Validate the generated machine name.
91       $this->assertEquals($test_info['expected'], $machine_name_1_value->getHtml(), $test_info['message']);
92
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');
95     }
96
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');
101
102     // Test switching back to the manual editing mode by clicking the edit link.
103     $button_1->click();
104
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');
107
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');
110
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');
113
114     $assert = $this->assertSession();
115     $this->drupalGet('/form-test/form-test-machine-name-validation');
116
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'));
123
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.');
131
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'));
143   }
144
145 }