annotate core/tests/Drupal/FunctionalJavascriptTests/Core/MachineNameTest.php @ 16:c2387f117808

Routine composer update
author Chris Cannam
date Tue, 10 Jul 2018 15:07:59 +0100
parents 4c8ae668cc8c
children 129ea1e6d783
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\FunctionalJavascriptTests\Core;
Chris@0 4
Chris@0 5 use Drupal\FunctionalJavascriptTests\JavascriptTestBase;
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@0 12 class MachineNameTest extends JavascriptTestBase {
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 $assert_session = $this->assertSession();
Chris@0 63
Chris@0 64 // Get elements from the page.
Chris@0 65 $title_1 = $page->findField('machine_name_1_label');
Chris@0 66 $machine_name_1_field = $page->findField('machine_name_1');
Chris@0 67 $machine_name_2_field = $page->findField('machine_name_2');
Chris@0 68 $machine_name_1_wrapper = $machine_name_1_field->getParent();
Chris@0 69 $machine_name_2_wrapper = $machine_name_2_field->getParent();
Chris@0 70 $machine_name_1_value = $page->find('css', '#edit-machine-name-1-label-machine-name-suffix .machine-name-value');
Chris@0 71 $machine_name_2_value = $page->find('css', '#edit-machine-name-2-label-machine-name-suffix .machine-name-value');
Chris@0 72 $button_1 = $page->find('css', '#edit-machine-name-1-label-machine-name-suffix button.link');
Chris@0 73
Chris@0 74 // Assert both fields are initialized correctly.
Chris@0 75 $this->assertNotEmpty($machine_name_1_value, 'Machine name field 1 must be initialized');
Chris@0 76 $this->assertNotEmpty($machine_name_2_value, 'Machine name field 2 must be initialized');
Chris@0 77
Chris@0 78 // Field must be present for the rest of the test to work.
Chris@0 79 if (empty($machine_name_1_value)) {
Chris@0 80 $this->fail('Cannot finish test, missing machine name field');
Chris@0 81 }
Chris@0 82
Chris@0 83 // Test each value for conversion to a machine name.
Chris@0 84 foreach ($test_values as $test_info) {
Chris@0 85 // Set the value for the field, triggering the machine name update.
Chris@0 86 $title_1->setValue($test_info['input']);
Chris@0 87
Chris@0 88 // Wait the set timeout for fetching the machine name.
Chris@0 89 $this->assertJsCondition('jQuery("#edit-machine-name-1-label-machine-name-suffix .machine-name-value").html() == "' . $test_info['expected'] . '"');
Chris@0 90
Chris@0 91 // Validate the generated machine name.
Chris@0 92 $this->assertEquals($test_info['expected'], $machine_name_1_value->getHtml(), $test_info['message']);
Chris@0 93
Chris@0 94 // Validate the second machine name field is empty.
Chris@0 95 $this->assertEmpty($machine_name_2_value->getHtml(), 'The second machine name field should still be empty');
Chris@0 96 }
Chris@0 97
Chris@0 98 // Validate the machine name field is hidden. Elements are visually hidden
Chris@0 99 // using positioning, isVisible() will therefore not work.
Chris@0 100 $this->assertEquals(TRUE, $machine_name_1_wrapper->hasClass('visually-hidden'), 'The ID field must not be visible');
Chris@0 101 $this->assertEquals(TRUE, $machine_name_2_wrapper->hasClass('visually-hidden'), 'The ID field must not be visible');
Chris@0 102
Chris@0 103 // Test switching back to the manual editing mode by clicking the edit link.
Chris@0 104 $button_1->click();
Chris@0 105
Chris@0 106 // Validate the visibility of the machine name field.
Chris@0 107 $this->assertEquals(FALSE, $machine_name_1_wrapper->hasClass('visually-hidden'), 'The ID field must now be visible');
Chris@0 108
Chris@0 109 // Validate the visibility of the second machine name field.
Chris@0 110 $this->assertEquals(TRUE, $machine_name_2_wrapper->hasClass('visually-hidden'), 'The ID field must not be visible');
Chris@0 111
Chris@0 112 // Validate if the element contains the correct value.
Chris@0 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');
Chris@0 114 }
Chris@0 115
Chris@0 116 }