Chris@0: drupalCreateUser([ Chris@0: 'access content', Chris@0: ]); Chris@0: $this->drupalLogin($account); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests that machine name field functions. Chris@0: * Chris@0: * Makes sure that the machine name field automatically provides a valid Chris@0: * machine name and that the manual editing mode functions. Chris@0: */ Chris@0: public function testMachineName() { Chris@0: // Visit the machine name test page which contains two machine name fields. Chris@0: $this->drupalGet('form-test/machine-name'); Chris@0: Chris@0: // Test values for conversion. Chris@0: $test_values = [ Chris@0: [ Chris@0: 'input' => 'Test value !0-9@', Chris@0: 'message' => 'A title that should be transliterated must be equal to the php generated machine name', Chris@0: 'expected' => 'test_value_0_9_', Chris@0: ], Chris@0: [ Chris@0: 'input' => 'Test value', Chris@0: 'message' => 'A title that should not be transliterated must be equal to the php generated machine name', Chris@0: 'expected' => 'test_value', Chris@0: ], Chris@0: ]; Chris@0: Chris@0: // Get page and session. Chris@0: $page = $this->getSession()->getPage(); Chris@0: Chris@0: // Get elements from the page. Chris@0: $title_1 = $page->findField('machine_name_1_label'); Chris@0: $machine_name_1_field = $page->findField('machine_name_1'); Chris@0: $machine_name_2_field = $page->findField('machine_name_2'); Chris@0: $machine_name_1_wrapper = $machine_name_1_field->getParent(); Chris@0: $machine_name_2_wrapper = $machine_name_2_field->getParent(); Chris@0: $machine_name_1_value = $page->find('css', '#edit-machine-name-1-label-machine-name-suffix .machine-name-value'); Chris@0: $machine_name_2_value = $page->find('css', '#edit-machine-name-2-label-machine-name-suffix .machine-name-value'); Chris@0: $button_1 = $page->find('css', '#edit-machine-name-1-label-machine-name-suffix button.link'); Chris@0: Chris@0: // Assert both fields are initialized correctly. Chris@0: $this->assertNotEmpty($machine_name_1_value, 'Machine name field 1 must be initialized'); Chris@0: $this->assertNotEmpty($machine_name_2_value, 'Machine name field 2 must be initialized'); Chris@0: Chris@0: // Field must be present for the rest of the test to work. Chris@0: if (empty($machine_name_1_value)) { Chris@0: $this->fail('Cannot finish test, missing machine name field'); Chris@0: } Chris@0: Chris@0: // Test each value for conversion to a machine name. Chris@0: foreach ($test_values as $test_info) { Chris@0: // Set the value for the field, triggering the machine name update. Chris@0: $title_1->setValue($test_info['input']); Chris@0: Chris@0: // Wait the set timeout for fetching the machine name. Chris@0: $this->assertJsCondition('jQuery("#edit-machine-name-1-label-machine-name-suffix .machine-name-value").html() == "' . $test_info['expected'] . '"'); Chris@0: Chris@0: // Validate the generated machine name. Chris@0: $this->assertEquals($test_info['expected'], $machine_name_1_value->getHtml(), $test_info['message']); Chris@0: Chris@0: // Validate the second machine name field is empty. Chris@0: $this->assertEmpty($machine_name_2_value->getHtml(), 'The second machine name field should still be empty'); Chris@0: } Chris@0: Chris@0: // Validate the machine name field is hidden. Elements are visually hidden Chris@0: // using positioning, isVisible() will therefore not work. Chris@0: $this->assertEquals(TRUE, $machine_name_1_wrapper->hasClass('visually-hidden'), 'The ID field must not be visible'); Chris@0: $this->assertEquals(TRUE, $machine_name_2_wrapper->hasClass('visually-hidden'), 'The ID field must not be visible'); Chris@0: Chris@0: // Test switching back to the manual editing mode by clicking the edit link. Chris@0: $button_1->click(); Chris@0: Chris@0: // Validate the visibility of the machine name field. Chris@0: $this->assertEquals(FALSE, $machine_name_1_wrapper->hasClass('visually-hidden'), 'The ID field must now be visible'); Chris@0: Chris@0: // Validate the visibility of the second machine name field. Chris@0: $this->assertEquals(TRUE, $machine_name_2_wrapper->hasClass('visually-hidden'), 'The ID field must not be visible'); Chris@0: Chris@0: // Validate if the element contains the correct value. Chris@0: $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: Chris@17: $assert = $this->assertSession(); Chris@17: $this->drupalGet('/form-test/form-test-machine-name-validation'); Chris@17: Chris@17: // Test errors after with no AJAX. Chris@17: $assert->buttonExists('Save')->press(); Chris@17: $assert->pageTextContains('Machine-readable name field is required.'); Chris@17: // Ensure only the first machine name field has an error. Chris@17: $this->assertTrue($assert->fieldExists('id')->hasClass('error')); Chris@17: $this->assertFalse($assert->fieldExists('id2')->hasClass('error')); Chris@17: Chris@17: // Test a successful submit after using AJAX. Chris@17: $assert->fieldExists('Name')->setValue('test 1'); Chris@17: $assert->fieldExists('id')->setValue('test_1'); Chris@17: $assert->selectExists('snack')->selectOption('apple'); Chris@17: $assert->assertWaitOnAjaxRequest(); Chris@17: $assert->buttonExists('Save')->press(); Chris@17: $assert->pageTextContains('The form_test_machine_name_validation_form form has been submitted successfully.'); Chris@17: Chris@17: // Test errors after using AJAX. Chris@17: $assert->fieldExists('Name')->setValue('duplicate'); Chris@17: $this->assertJsCondition('document.forms[0].id.value === "duplicate"'); Chris@17: $assert->fieldExists('id2')->setValue('duplicate2'); Chris@17: $assert->selectExists('snack')->selectOption('potato'); Chris@17: $assert->assertWaitOnAjaxRequest(); Chris@17: $assert->buttonExists('Save')->press(); Chris@17: $assert->pageTextContains('The machine-readable name is already in use. It must be unique.'); Chris@17: // Ensure both machine name fields both have errors. Chris@17: $this->assertTrue($assert->fieldExists('id')->hasClass('error')); Chris@17: $this->assertTrue($assert->fieldExists('id2')->hasClass('error')); Chris@0: } Chris@0: Chris@0: }