Chris@0: drupalCreateUser(); Chris@0: $this->drupalLogin($account); Chris@0: Chris@0: // Visit a Drupal page that requires login. Chris@0: $this->drupalGet('test-page'); Chris@0: $this->assertSession()->statusCodeEquals(200); Chris@0: Chris@0: // Test page contains some text. Chris@0: $this->assertSession()->pageTextContains('Test page text.'); Chris@0: Chris@0: // Check that returned plain text is correct. Chris@0: $text = $this->getTextContent(); Chris@0: $this->assertContains('Test page text.', $text); Chris@0: $this->assertNotContains('', $text); Chris@0: Chris@0: // Response includes cache tags that we can assert. Chris@0: $this->assertSession()->responseHeaderEquals('X-Drupal-Cache-Tags', 'http_response rendered'); Chris@0: Chris@0: // Test that we can read the JS settings. Chris@0: $js_settings = $this->getDrupalSettings(); Chris@0: $this->assertSame('azAZ09();.,\\\/-_{}', $js_settings['test-setting']); Chris@0: Chris@0: // Test drupalGet with a url object. Chris@0: $url = Url::fromRoute('test_page_test.render_title'); Chris@0: $this->drupalGet($url); Chris@0: $this->assertSession()->statusCodeEquals(200); Chris@0: Chris@0: // Test page contains some text. Chris@0: $this->assertSession()->pageTextContains('Hello Drupal'); Chris@0: Chris@0: // Test that setting headers with drupalGet() works. Chris@0: $this->drupalGet('system-test/header', [], [ Chris@0: 'Test-Header' => 'header value', Chris@0: ]); Chris@0: $returned_header = $this->getSession()->getResponseHeader('Test-Header'); Chris@0: $this->assertSame('header value', $returned_header); Chris@0: } Chris@0: Chris@0: /** Chris@16: * Tests drupalGet(). Chris@16: */ Chris@16: public function testDrupalGet() { Chris@16: $this->drupalGet('test-page'); Chris@16: $this->assertSession()->statusCodeEquals(200); Chris@16: $this->assertSession()->addressEquals('test-page'); Chris@16: $this->drupalGet('/test-page'); Chris@16: $this->assertSession()->statusCodeEquals(200); Chris@16: $this->assertSession()->addressEquals('test-page'); Chris@16: $this->drupalGet('/test-page/'); Chris@16: $this->assertSession()->statusCodeEquals(200); Chris@16: $this->assertSession()->addressEquals('/test-page/'); Chris@16: } Chris@16: Chris@16: /** Chris@0: * Tests basic form functionality. Chris@0: */ Chris@0: public function testForm() { Chris@0: // Ensure the proper response code for a _form route. Chris@0: $this->drupalGet('form-test/object-builder'); Chris@0: $this->assertSession()->statusCodeEquals(200); Chris@0: Chris@0: // Ensure the form and text field exist. Chris@0: $this->assertSession()->elementExists('css', 'form#form-test-form-test-object'); Chris@0: $this->assertSession()->fieldExists('bananas'); Chris@0: Chris@0: // Check that the hidden field exists and has a specific value. Chris@0: $this->assertSession()->hiddenFieldExists('strawberry'); Chris@0: $this->assertSession()->hiddenFieldExists('red'); Chris@0: $this->assertSession()->hiddenFieldExists('redstrawberryhiddenfield'); Chris@0: $this->assertSession()->hiddenFieldValueNotEquals('strawberry', 'brown'); Chris@0: $this->assertSession()->hiddenFieldValueEquals('strawberry', 'red'); Chris@0: Chris@0: // Check that a hidden field does not exist. Chris@0: $this->assertSession()->hiddenFieldNotExists('bananas'); Chris@0: $this->assertSession()->hiddenFieldNotExists('pineapple'); Chris@0: Chris@0: $edit = ['bananas' => 'green']; Chris@0: $this->submitForm($edit, 'Save', 'form-test-form-test-object'); Chris@0: Chris@0: $config_factory = $this->container->get('config.factory'); Chris@0: $value = $config_factory->get('form_test.object')->get('bananas'); Chris@0: $this->assertSame('green', $value); Chris@0: Chris@0: // Test drupalPostForm(). Chris@0: $edit = ['bananas' => 'red']; Chris@17: // Submit the form using the button label. Chris@0: $result = $this->drupalPostForm('form-test/object-builder', $edit, 'Save'); Chris@0: $this->assertSame($this->getSession()->getPage()->getContent(), $result); Chris@0: $value = $config_factory->get('form_test.object')->get('bananas'); Chris@0: $this->assertSame('red', $value); Chris@0: Chris@0: $this->drupalPostForm('form-test/object-builder', NULL, 'Save'); Chris@0: $value = $config_factory->get('form_test.object')->get('bananas'); Chris@0: $this->assertSame('', $value); Chris@0: Chris@17: // Submit the form using the button id. Chris@17: $edit = ['bananas' => 'blue']; Chris@17: $result = $this->drupalPostForm('form-test/object-builder', $edit, 'edit-submit'); Chris@17: $this->assertSame($this->getSession()->getPage()->getContent(), $result); Chris@17: $value = $config_factory->get('form_test.object')->get('bananas'); Chris@17: $this->assertSame('blue', $value); Chris@17: Chris@17: // Submit the form using the button name. Chris@17: $edit = ['bananas' => 'purple']; Chris@17: $result = $this->drupalPostForm('form-test/object-builder', $edit, 'op'); Chris@17: $this->assertSame($this->getSession()->getPage()->getContent(), $result); Chris@17: $value = $config_factory->get('form_test.object')->get('bananas'); Chris@17: $this->assertSame('purple', $value); Chris@17: Chris@0: // Test drupalPostForm() with no-html response. Chris@0: $values = Json::decode($this->drupalPostForm('form_test/form-state-values-clean', [], t('Submit'))); Chris@0: $this->assertTrue(1000, $values['beer']); Chris@17: Chris@17: // Test drupalPostForm() with form by HTML id. Chris@17: $this->drupalCreateContentType(['type' => 'page']); Chris@17: $this->drupalLogin($this->drupalCreateUser(['create page content'])); Chris@17: $this->drupalGet('form-test/two-instances-of-same-form'); Chris@17: $this->getSession()->getPage()->fillField('edit-title-0-value', 'form1'); Chris@17: $this->getSession()->getPage()->fillField('edit-title-0-value--2', 'form2'); Chris@17: $this->drupalPostForm(NULL, [], 'Save', [], 'node-page-form--2'); Chris@17: $this->assertSession()->pageTextContains('Page form2 has been created.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests clickLink() functionality. Chris@0: */ Chris@0: public function testClickLink() { Chris@0: $this->drupalGet('test-page'); Chris@0: $this->clickLink('Visually identical test links'); Chris@0: $this->assertContains('user/login', $this->getSession()->getCurrentUrl()); Chris@0: $this->drupalGet('test-page'); Chris@0: $this->clickLink('Visually identical test links', 0); Chris@0: $this->assertContains('user/login', $this->getSession()->getCurrentUrl()); Chris@0: $this->drupalGet('test-page'); Chris@0: $this->clickLink('Visually identical test links', 1); Chris@0: $this->assertContains('user/register', $this->getSession()->getCurrentUrl()); Chris@0: } Chris@0: Chris@0: public function testError() { Chris@0: $this->setExpectedException('\Exception', 'User notice: foo'); Chris@0: $this->drupalGet('test-error'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests linkExists() with pipe character (|) in locator. Chris@0: * Chris@0: * @see \Drupal\Tests\WebAssert::linkExists() Chris@0: */ Chris@0: public function testPipeCharInLocator() { Chris@0: $this->drupalGet('test-pipe-char'); Chris@0: $this->assertSession()->linkExists('foo|bar|baz'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests linkExistsExact() functionality. Chris@0: * Chris@0: * @see \Drupal\Tests\WebAssert::linkExistsExact() Chris@0: */ Chris@0: public function testLinkExistsExact() { Chris@0: $this->drupalGet('test-pipe-char'); Chris@0: $this->assertSession()->linkExistsExact('foo|bar|baz'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests linkExistsExact() functionality fail. Chris@0: * Chris@0: * @see \Drupal\Tests\WebAssert::linkExistsExact() Chris@0: */ Chris@0: public function testInvalidLinkExistsExact() { Chris@0: $this->drupalGet('test-pipe-char'); Chris@0: $this->setExpectedException(ExpectationException::class, 'Link with label foo|bar found'); Chris@0: $this->assertSession()->linkExistsExact('foo|bar'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests linkNotExistsExact() functionality. Chris@0: * Chris@0: * @see \Drupal\Tests\WebAssert::linkNotExistsExact() Chris@0: */ Chris@0: public function testLinkNotExistsExact() { Chris@0: $this->drupalGet('test-pipe-char'); Chris@0: $this->assertSession()->linkNotExistsExact('foo|bar'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests linkNotExistsExact() functionality fail. Chris@0: * Chris@0: * @see \Drupal\Tests\WebAssert::linkNotExistsExact() Chris@0: */ Chris@0: public function testInvalidLinkNotExistsExact() { Chris@0: $this->drupalGet('test-pipe-char'); Chris@0: $this->setExpectedException(ExpectationException::class, 'Link with label foo|bar|baz not found'); Chris@0: $this->assertSession()->linkNotExistsExact('foo|bar|baz'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests legacy text asserts. Chris@0: */ Chris@14: public function testTextAsserts() { Chris@0: $this->drupalGet('test-encoded'); Chris@0: $dangerous = 'Bad html '; Chris@0: $sanitized = Html::escape($dangerous); Chris@0: $this->assertNoText($dangerous); Chris@0: $this->assertText($sanitized); Chris@0: Chris@0: // Test getRawContent(). Chris@17: $this->assertSame($this->getSession()->getPage()->getContent(), $this->getSession()->getPage()->getContent()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests legacy field asserts which use xpath directly. Chris@0: */ Chris@14: public function testXpathAsserts() { Chris@0: $this->drupalGet('test-field-xpath'); Chris@0: $this->assertFieldsByValue($this->xpath("//h1[@class = 'page-title']"), NULL); Chris@0: $this->assertFieldsByValue($this->xpath('//table/tbody/tr[2]/td[1]'), 'one'); Chris@0: $this->assertFieldByXPath('//table/tbody/tr[2]/td[1]', 'one'); Chris@0: Chris@0: $this->assertFieldsByValue($this->xpath("//input[@id = 'edit-name']"), 'Test name'); Chris@0: $this->assertFieldByXPath("//input[@id = 'edit-name']", 'Test name'); Chris@0: $this->assertFieldsByValue($this->xpath("//select[@id = 'edit-options']"), '2'); Chris@0: $this->assertFieldByXPath("//select[@id = 'edit-options']", '2'); Chris@0: Chris@0: $this->assertNoFieldByXPath('//notexisting'); Chris@0: $this->assertNoFieldByXPath("//input[@id = 'edit-name']", 'wrong value'); Chris@0: Chris@0: // Test that the assertion fails correctly. Chris@0: try { Chris@0: $this->assertFieldByXPath("//input[@id = 'notexisting']"); Chris@0: $this->fail('The "notexisting" field was found.'); Chris@0: } Chris@0: catch (\PHPUnit_Framework_ExpectationFailedException $e) { Chris@0: $this->pass('assertFieldByXPath correctly failed. The "notexisting" field was not found.'); Chris@0: } Chris@0: Chris@0: try { Chris@0: $this->assertNoFieldByXPath("//input[@id = 'edit-name']"); Chris@0: $this->fail('The "edit-name" field was not found.'); Chris@0: } Chris@0: catch (ExpectationException $e) { Chris@0: $this->pass('assertNoFieldByXPath correctly failed. The "edit-name" field was found.'); Chris@0: } Chris@0: Chris@0: try { Chris@0: $this->assertFieldsByValue($this->xpath("//input[@id = 'edit-name']"), 'not the value'); Chris@0: $this->fail('The "edit-name" field is found with the value "not the value".'); Chris@0: } Chris@0: catch (\PHPUnit_Framework_ExpectationFailedException $e) { Chris@0: $this->pass('The "edit-name" field is not found with the value "not the value".'); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests legacy field asserts using textfields. Chris@0: */ Chris@14: public function testFieldAssertsForTextfields() { Chris@0: $this->drupalGet('test-field-xpath'); Chris@0: Chris@0: // *** 1. assertNoField(). Chris@0: $this->assertNoField('invalid_name_and_id'); Chris@0: Chris@0: // Test that the assertion fails correctly when searching by name. Chris@0: try { Chris@0: $this->assertNoField('name'); Chris@0: $this->fail('The "name" field was not found based on name.'); Chris@0: } Chris@0: catch (ExpectationException $e) { Chris@0: $this->pass('assertNoField correctly failed. The "name" field was found by name.'); Chris@0: } Chris@0: Chris@0: // Test that the assertion fails correctly when searching by id. Chris@0: try { Chris@0: $this->assertNoField('edit-name'); Chris@0: $this->fail('The "name" field was not found based on id.'); Chris@0: } Chris@0: catch (ExpectationException $e) { Chris@0: $this->pass('assertNoField correctly failed. The "name" field was found by id.'); Chris@0: } Chris@0: Chris@0: // *** 2. assertField(). Chris@0: $this->assertField('name'); Chris@0: $this->assertField('edit-name'); Chris@0: Chris@0: // Test that the assertion fails correctly if the field does not exist. Chris@0: try { Chris@0: $this->assertField('invalid_name_and_id'); Chris@0: $this->fail('The "invalid_name_and_id" field was found.'); Chris@0: } Chris@0: catch (\PHPUnit_Framework_ExpectationFailedException $e) { Chris@0: $this->pass('assertField correctly failed. The "invalid_name_and_id" field was not found.'); Chris@0: } Chris@0: Chris@0: // *** 3. assertNoFieldById(). Chris@0: $this->assertNoFieldById('name'); Chris@0: $this->assertNoFieldById('name', 'not the value'); Chris@0: $this->assertNoFieldById('notexisting'); Chris@0: $this->assertNoFieldById('notexisting', NULL); Chris@0: Chris@0: // Test that the assertion fails correctly if no value is passed in. Chris@0: try { Chris@0: $this->assertNoFieldById('edit-description'); Chris@0: $this->fail('The "description" field, with no value was not found.'); Chris@0: } Chris@0: catch (ExpectationException $e) { Chris@0: $this->pass('The "description" field, with no value was found.'); Chris@0: } Chris@0: Chris@0: // Test that the assertion fails correctly if a NULL value is passed in. Chris@0: try { Chris@0: $this->assertNoFieldById('edit-name', NULL); Chris@0: $this->fail('The "name" field was not found.'); Chris@0: } Chris@0: catch (ExpectationException $e) { Chris@0: $this->pass('The "name" field was found.'); Chris@0: } Chris@0: Chris@0: // *** 4. assertFieldById(). Chris@0: $this->assertFieldById('edit-name', NULL); Chris@0: $this->assertFieldById('edit-name', 'Test name'); Chris@0: $this->assertFieldById('edit-description', NULL); Chris@0: $this->assertFieldById('edit-description'); Chris@0: Chris@0: // Test that the assertion fails correctly if no value is passed in. Chris@0: try { Chris@0: $this->assertFieldById('edit-name'); Chris@0: $this->fail('The "edit-name" field with no value was found.'); Chris@0: } Chris@0: catch (\PHPUnit_Framework_ExpectationFailedException $e) { Chris@0: $this->pass('The "edit-name" field with no value was not found.'); Chris@0: } Chris@0: Chris@0: // Test that the assertion fails correctly if the wrong value is passed in. Chris@0: try { Chris@0: $this->assertFieldById('edit-name', 'not the value'); Chris@0: $this->fail('The "name" field was found, using the wrong value.'); Chris@0: } Chris@0: catch (\PHPUnit_Framework_ExpectationFailedException $e) { Chris@0: $this->pass('The "name" field was not found, using the wrong value.'); Chris@0: } Chris@0: Chris@0: // *** 5. assertNoFieldByName(). Chris@0: $this->assertNoFieldByName('name'); Chris@0: $this->assertNoFieldByName('name', 'not the value'); Chris@0: $this->assertNoFieldByName('notexisting'); Chris@0: $this->assertNoFieldByName('notexisting', NULL); Chris@0: Chris@0: // Test that the assertion fails correctly if no value is passed in. Chris@0: try { Chris@0: $this->assertNoFieldByName('description'); Chris@0: $this->fail('The "description" field, with no value was not found.'); Chris@0: } Chris@0: catch (ExpectationException $e) { Chris@0: $this->pass('The "description" field, with no value was found.'); Chris@0: } Chris@0: Chris@0: // Test that the assertion fails correctly if a NULL value is passed in. Chris@0: try { Chris@0: $this->assertNoFieldByName('name', NULL); Chris@0: $this->fail('The "name" field was not found.'); Chris@0: } Chris@0: catch (ExpectationException $e) { Chris@0: $this->pass('The "name" field was found.'); Chris@0: } Chris@0: Chris@0: // *** 6. assertFieldByName(). Chris@0: $this->assertFieldByName('name'); Chris@0: $this->assertFieldByName('name', NULL); Chris@0: $this->assertFieldByName('name', 'Test name'); Chris@0: $this->assertFieldByName('description'); Chris@0: $this->assertFieldByName('description', ''); Chris@0: $this->assertFieldByName('description', NULL); Chris@0: Chris@0: // Test that the assertion fails correctly if given the wrong name. Chris@0: try { Chris@0: $this->assertFieldByName('non-existing-name'); Chris@0: $this->fail('The "non-existing-name" field was found.'); Chris@0: } Chris@0: catch (\PHPUnit_Framework_ExpectationFailedException $e) { Chris@0: $this->pass('The "non-existing-name" field was not found'); Chris@0: } Chris@0: Chris@0: // Test that the assertion fails correctly if given the wrong value. Chris@0: try { Chris@0: $this->assertFieldByName('name', 'not the value'); Chris@0: $this->fail('The "name" field with incorrect value was found.'); Chris@0: } Chris@0: catch (\PHPUnit_Framework_ExpectationFailedException $e) { Chris@0: $this->pass('assertFieldByName correctly failed. The "name" field with incorrect value was not found.'); Chris@0: } Chris@0: Chris@0: // Test that text areas can contain new lines. Chris@0: $this->assertFieldsByValue($this->xpath("//textarea[@id = 'edit-test-textarea-with-newline']"), "Test text with\nnewline"); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests legacy field asserts for options field type. Chris@0: */ Chris@14: public function testFieldAssertsForOptions() { Chris@0: $this->drupalGet('test-field-xpath'); Chris@0: Chris@0: // Option field type. Chris@0: $this->assertOptionByText('options', 'one'); Chris@0: try { Chris@0: $this->assertOptionByText('options', 'four'); Chris@0: $this->fail('The select option "four" was found.'); Chris@0: } Chris@0: catch (ExpectationException $e) { Chris@0: $this->pass($e->getMessage()); Chris@0: } Chris@0: Chris@0: $this->assertOption('options', 1); Chris@0: try { Chris@0: $this->assertOption('options', 4); Chris@0: $this->fail('The select option "4" was found.'); Chris@0: } Chris@0: catch (ExpectationException $e) { Chris@0: $this->pass($e->getMessage()); Chris@0: } Chris@0: Chris@0: $this->assertNoOption('options', 'non-existing'); Chris@0: try { Chris@0: $this->assertNoOption('options', 'one'); Chris@0: $this->fail('The select option "one" was not found.'); Chris@0: } Chris@0: catch (ExpectationException $e) { Chris@0: $this->pass($e->getMessage()); Chris@0: } Chris@0: Chris@0: $this->assertOptionSelected('options', 2); Chris@0: try { Chris@0: $this->assertOptionSelected('options', 4); Chris@0: $this->fail('The select option "4" was selected.'); Chris@0: } Chris@0: catch (ExpectationException $e) { Chris@0: $this->pass($e->getMessage()); Chris@0: } Chris@0: Chris@0: try { Chris@0: $this->assertOptionSelected('options', 1); Chris@0: $this->fail('The select option "1" was selected.'); Chris@0: } Chris@0: catch (\PHPUnit_Framework_ExpectationFailedException $e) { Chris@0: $this->pass($e->getMessage()); Chris@0: } Chris@0: Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests legacy field asserts for button field type. Chris@0: */ Chris@14: public function testFieldAssertsForButton() { Chris@0: $this->drupalGet('test-field-xpath'); Chris@0: Chris@0: $this->assertFieldById('edit-save', NULL); Chris@0: // Test that the assertion fails correctly if the field value is passed in Chris@0: // rather than the id. Chris@0: try { Chris@0: $this->assertFieldById('Save', NULL); Chris@0: $this->fail('The field with id of "Save" was found.'); Chris@0: } Chris@0: catch (\PHPUnit_Framework_ExpectationFailedException $e) { Chris@0: $this->pass($e->getMessage()); Chris@0: } Chris@0: Chris@0: $this->assertNoFieldById('Save', NULL); Chris@0: // Test that the assertion fails correctly if the id of an actual field is Chris@0: // passed in. Chris@0: try { Chris@0: $this->assertNoFieldById('edit-save', NULL); Chris@0: $this->fail('The field with id of "edit-save" was not found.'); Chris@0: } Chris@0: catch (ExpectationException $e) { Chris@0: $this->pass($e->getMessage()); Chris@0: } Chris@0: Chris@0: // Test that multiple fields with the same name are validated correctly. Chris@0: $this->assertFieldByName('duplicate_button', 'Duplicate button 1'); Chris@0: $this->assertFieldByName('duplicate_button', 'Duplicate button 2'); Chris@0: $this->assertNoFieldByName('duplicate_button', 'Rabbit'); Chris@0: Chris@0: try { Chris@0: $this->assertNoFieldByName('duplicate_button', 'Duplicate button 2'); Chris@0: $this->fail('The "duplicate_button" field with the value Duplicate button 2 was not found.'); Chris@0: } Chris@0: catch (ExpectationException $e) { Chris@0: $this->pass('assertNoFieldByName correctly failed. The "duplicate_button" field with the value Duplicate button 2 was found.'); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests legacy field asserts for checkbox field type. Chris@0: */ Chris@14: public function testFieldAssertsForCheckbox() { Chris@0: $this->drupalGet('test-field-xpath'); Chris@0: Chris@0: // Part 1 - Test by name. Chris@0: // Test that checkboxes are found/not found correctly by name, when using Chris@0: // TRUE or FALSE to match their 'checked' state. Chris@0: $this->assertFieldByName('checkbox_enabled', TRUE); Chris@0: $this->assertFieldByName('checkbox_disabled', FALSE); Chris@0: $this->assertNoFieldByName('checkbox_enabled', FALSE); Chris@0: $this->assertNoFieldByName('checkbox_disabled', TRUE); Chris@0: Chris@0: // Test that checkboxes are found by name when using NULL to ignore the Chris@0: // 'checked' state. Chris@0: $this->assertFieldByName('checkbox_enabled', NULL); Chris@0: $this->assertFieldByName('checkbox_disabled', NULL); Chris@0: Chris@0: // Test that checkboxes are found by name when passing no second parameter. Chris@0: $this->assertFieldByName('checkbox_enabled'); Chris@0: $this->assertFieldByName('checkbox_disabled'); Chris@0: Chris@0: // Test that we have legacy support. Chris@0: $this->assertFieldByName('checkbox_enabled', '1'); Chris@0: $this->assertFieldByName('checkbox_disabled', ''); Chris@0: Chris@0: // Test that the assertion fails correctly when using NULL to ignore state. Chris@0: try { Chris@0: $this->assertNoFieldByName('checkbox_enabled', NULL); Chris@0: $this->fail('The "checkbox_enabled" field was not found by name, using NULL value.'); Chris@0: } Chris@0: catch (ExpectationException $e) { Chris@0: $this->pass('assertNoFieldByName failed correctly. The "checkbox_enabled" field was found using NULL value.'); Chris@0: } Chris@0: Chris@0: // Part 2 - Test by ID. Chris@0: // Test that checkboxes are found/not found correctly by ID, when using Chris@0: // TRUE or FALSE to match their 'checked' state. Chris@0: $this->assertFieldById('edit-checkbox-enabled', TRUE); Chris@0: $this->assertFieldById('edit-checkbox-disabled', FALSE); Chris@0: $this->assertNoFieldById('edit-checkbox-enabled', FALSE); Chris@0: $this->assertNoFieldById('edit-checkbox-disabled', TRUE); Chris@0: Chris@0: // Test that checkboxes are found by ID, when using NULL to ignore the Chris@0: // 'checked' state. Chris@0: $this->assertFieldById('edit-checkbox-enabled', NULL); Chris@0: $this->assertFieldById('edit-checkbox-disabled', NULL); Chris@0: Chris@0: // Test that checkboxes are found by ID when passing no second parameter. Chris@0: $this->assertFieldById('edit-checkbox-enabled'); Chris@0: $this->assertFieldById('edit-checkbox-disabled'); Chris@0: Chris@0: // Test that we have legacy support. Chris@0: $this->assertFieldById('edit-checkbox-enabled', '1'); Chris@0: $this->assertFieldById('edit-checkbox-disabled', ''); Chris@0: Chris@0: // Test that the assertion fails correctly when using NULL to ignore state. Chris@0: try { Chris@0: $this->assertNoFieldById('edit-checkbox-disabled', NULL); Chris@0: $this->fail('The "edit-checkbox-disabled" field was not found by ID, using NULL value.'); Chris@0: } Chris@0: catch (ExpectationException $e) { Chris@0: $this->pass('assertNoFieldById failed correctly. The "edit-checkbox-disabled" field was found by ID using NULL value.'); Chris@0: } Chris@0: Chris@0: // Part 3 - Test the specific 'checked' assertions. Chris@0: $this->assertFieldChecked('edit-checkbox-enabled'); Chris@0: $this->assertNoFieldChecked('edit-checkbox-disabled'); Chris@0: Chris@17: // Test that the assertion fails correctly with non-existent field id. Chris@0: try { Chris@0: $this->assertNoFieldChecked('incorrect_checkbox_id'); Chris@0: $this->fail('The "incorrect_checkbox_id" field was found'); Chris@0: } Chris@0: catch (ExpectationException $e) { Chris@0: $this->pass('assertNoFieldChecked correctly failed. The "incorrect_checkbox_id" field was not found.'); Chris@0: } Chris@0: Chris@0: // Test that the assertion fails correctly for a checkbox that is checked. Chris@0: try { Chris@0: $this->assertNoFieldChecked('edit-checkbox-enabled'); Chris@0: $this->fail('The "edit-checkbox-enabled" field was not found in a checked state.'); Chris@0: } Chris@0: catch (ExpectationException $e) { Chris@0: $this->pass('assertNoFieldChecked correctly failed. The "edit-checkbox-enabled" field was found in a checked state.'); Chris@0: } Chris@0: Chris@0: // Test that the assertion fails correctly for a checkbox that is not Chris@0: // checked. Chris@0: try { Chris@0: $this->assertFieldChecked('edit-checkbox-disabled'); Chris@0: $this->fail('The "edit-checkbox-disabled" field was found and checked.'); Chris@0: } Chris@0: catch (ExpectationException $e) { Chris@0: $this->pass('assertFieldChecked correctly failed. The "edit-checkbox-disabled" field was not found in a checked state.'); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the ::cronRun() method. Chris@0: */ Chris@0: public function testCronRun() { Chris@0: $last_cron_time = \Drupal::state()->get('system.cron_last'); Chris@0: $this->cronRun(); Chris@0: $this->assertSession()->statusCodeEquals(204); Chris@0: $next_cron_time = \Drupal::state()->get('system.cron_last'); Chris@0: Chris@0: $this->assertGreaterThan($last_cron_time, $next_cron_time); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the Drupal install done in \Drupal\Tests\BrowserTestBase::setUp(). Chris@0: */ Chris@0: public function testInstall() { Chris@0: $htaccess_filename = $this->tempFilesDirectory . '/.htaccess'; Chris@0: $this->assertTrue(file_exists($htaccess_filename), "$htaccess_filename exists"); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the assumption that local time is in 'Australia/Sydney'. Chris@0: */ Chris@0: public function testLocalTimeZone() { Chris@18: $expected = 'Australia/Sydney'; Chris@0: // The 'Australia/Sydney' time zone is set in core/tests/bootstrap.php Chris@18: $this->assertEquals($expected, date_default_timezone_get()); Chris@0: Chris@0: // The 'Australia/Sydney' time zone is also set in Chris@0: // FunctionalTestSetupTrait::initConfig(). Chris@0: $config_factory = $this->container->get('config.factory'); Chris@0: $value = $config_factory->get('system.date')->get('timezone.default'); Chris@18: $this->assertEquals($expected, $value); Chris@18: Chris@18: // Test that users have the correct time zone set. Chris@18: $this->assertEquals($expected, $this->rootUser->getTimeZone()); Chris@18: $admin_user = $this->drupalCreateUser(['administer site configuration']); Chris@18: $this->assertEquals($expected, $admin_user->getTimeZone()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the ::checkForMetaRefresh() method. Chris@0: */ Chris@0: public function testCheckForMetaRefresh() { Chris@0: // Disable following redirects in the client. Chris@0: $this->getSession()->getDriver()->getClient()->followRedirects(FALSE); Chris@0: // Set the maximumMetaRefreshCount to zero to make sure the redirect doesn't Chris@0: // happen when doing a drupalGet. Chris@0: $this->maximumMetaRefreshCount = 0; Chris@0: $this->drupalGet('test-meta-refresh'); Chris@0: $this->assertNotEmpty($this->cssSelect('meta[http-equiv="refresh"]')); Chris@0: // Allow one redirect to happen. Chris@0: $this->maximumMetaRefreshCount = 1; Chris@0: $this->checkForMetaRefresh(); Chris@0: // Check that we are now on the test page. Chris@0: $this->assertSession()->pageTextContains('Test page text.'); Chris@0: } Chris@0: Chris@12: public function testGetDefaultDriveInstance() { Chris@12: putenv('MINK_DRIVER_ARGS=' . json_encode([NULL, ['key1' => ['key2' => ['key3' => 3, 'key3.1' => 3.1]]]])); Chris@12: $this->getDefaultDriverInstance(); Chris@12: $this->assertEquals([NULL, ['key1' => ['key2' => ['key3' => 3, 'key3.1' => 3.1]]]], $this->minkDefaultDriverArgs); Chris@12: } Chris@12: Chris@17: /** Chris@17: * Ensures we can't access modules we shouldn't be able to after install. Chris@17: */ Chris@17: public function testProfileModules() { Chris@17: $this->setExpectedException(\InvalidArgumentException::class, 'The module demo_umami_content does not exist.'); Chris@17: $this->assertFileExists('core/profiles/demo_umami/modules/demo_umami_content/demo_umami_content.info.yml'); Chris@17: \Drupal::service('extension.list.module')->getPathname('demo_umami_content'); Chris@17: } Chris@17: Chris@18: /** Chris@18: * Test the protections provided by .htkey. Chris@18: */ Chris@18: public function testHtkey() { Chris@18: // Remove the Simpletest private key file so we can test the protection Chris@18: // against requests that forge a valid testing user agent to gain access Chris@18: // to the installer. Chris@18: // @see drupal_valid_test_ua() Chris@18: // Not using File API; a potential error must trigger a PHP warning. Chris@18: $install_url = Url::fromUri('base:core/install.php', ['external' => TRUE, 'absolute' => TRUE])->toString(); Chris@18: $this->drupalGet($install_url); Chris@18: $this->assertSession()->statusCodeEquals(200); Chris@18: unlink($this->siteDirectory . '/.htkey'); Chris@18: $this->drupalGet($install_url); Chris@18: $this->assertSession()->statusCodeEquals(403); Chris@18: } Chris@18: Chris@0: }