comparison core/tests/Drupal/FunctionalJavascriptTests/Core/MachineNameTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 129ea1e6d783
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\FunctionalJavascriptTests\Core;
4
5 use Drupal\FunctionalJavascriptTests\JavascriptTestBase;
6
7 /**
8 * Tests for the machine name field.
9 *
10 * @group field
11 */
12 class MachineNameTest extends JavascriptTestBase {
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 $assert_session = $this->assertSession();
63
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');
73
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');
77
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');
81 }
82
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']);
87
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'] . '"');
90
91 // Validate the generated machine name.
92 $this->assertEquals($test_info['expected'], $machine_name_1_value->getHtml(), $test_info['message']);
93
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');
96 }
97
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');
102
103 // Test switching back to the manual editing mode by clicking the edit link.
104 $button_1->click();
105
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');
108
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');
111
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');
114 }
115
116 }