namespace Drupal\FunctionalTests;
use Behat\Mink\Exception\ExpectationException;
+use Drupal\Component\Serialization\Json;
use Drupal\Component\Utility\Html;
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
// Test drupalPostForm().
$edit = ['bananas' => 'red'];
- $this->drupalPostForm('form-test/object-builder', $edit, 'Save');
+ $result = $this->drupalPostForm('form-test/object-builder', $edit, 'Save');
+ $this->assertSame($this->getSession()->getPage()->getContent(), $result);
$value = $config_factory->get('form_test.object')->get('bananas');
$this->assertSame('red', $value);
$this->drupalPostForm('form-test/object-builder', NULL, 'Save');
$value = $config_factory->get('form_test.object')->get('bananas');
$this->assertSame('', $value);
+
+ // Test drupalPostForm() with no-html response.
+ $values = Json::decode($this->drupalPostForm('form_test/form-state-values-clean', [], t('Submit')));
+ $this->assertTrue(1000, $values['beer']);
}
/**
$this->assertSession()->linkExists('foo|bar|baz');
}
+ /**
+ * Tests linkExistsExact() functionality.
+ *
+ * @see \Drupal\Tests\WebAssert::linkExistsExact()
+ */
+ public function testLinkExistsExact() {
+ $this->drupalGet('test-pipe-char');
+ $this->assertSession()->linkExistsExact('foo|bar|baz');
+ }
+
+ /**
+ * Tests linkExistsExact() functionality fail.
+ *
+ * @see \Drupal\Tests\WebAssert::linkExistsExact()
+ */
+ public function testInvalidLinkExistsExact() {
+ $this->drupalGet('test-pipe-char');
+ $this->setExpectedException(ExpectationException::class, 'Link with label foo|bar found');
+ $this->assertSession()->linkExistsExact('foo|bar');
+ }
+
+ /**
+ * Tests linkNotExistsExact() functionality.
+ *
+ * @see \Drupal\Tests\WebAssert::linkNotExistsExact()
+ */
+ public function testLinkNotExistsExact() {
+ $this->drupalGet('test-pipe-char');
+ $this->assertSession()->linkNotExistsExact('foo|bar');
+ }
+
+ /**
+ * Tests linkNotExistsExact() functionality fail.
+ *
+ * @see \Drupal\Tests\WebAssert::linkNotExistsExact()
+ */
+ public function testInvalidLinkNotExistsExact() {
+ $this->drupalGet('test-pipe-char');
+ $this->setExpectedException(ExpectationException::class, 'Link with label foo|bar|baz not found');
+ $this->assertSession()->linkNotExistsExact('foo|bar|baz');
+ }
+
/**
* Tests legacy text asserts.
*/
$this->assertNoFieldByXPath("//input[@id = 'edit-name']");
$this->fail('The "edit-name" field was not found.');
}
- catch (\PHPUnit_Framework_ExpectationFailedException $e) {
+ catch (ExpectationException $e) {
$this->pass('assertNoFieldByXPath correctly failed. The "edit-name" field was found.');
}
/**
* Tests legacy field asserts using textfields.
*/
- public function testLegacyFieldAssertsWithTextfields() {
+ public function testLegacyFieldAssertsForTextfields() {
$this->drupalGet('test-field-xpath');
// *** 1. assertNoField().
$this->assertField('invalid_name_and_id');
$this->fail('The "invalid_name_and_id" field was found.');
}
- catch (ExpectationException $e) {
+ catch (\PHPUnit_Framework_ExpectationFailedException $e) {
$this->pass('assertField correctly failed. The "invalid_name_and_id" field was not found.');
}
$this->assertFieldByName('non-existing-name');
$this->fail('The "non-existing-name" field was found.');
}
- catch (ExpectationException $e) {
+ catch (\PHPUnit_Framework_ExpectationFailedException $e) {
$this->pass('The "non-existing-name" field was not found');
}
$this->assertFieldByName('name', 'not the value');
$this->fail('The "name" field with incorrect value was found.');
}
- catch (ExpectationException $e) {
+ catch (\PHPUnit_Framework_ExpectationFailedException $e) {
$this->pass('assertFieldByName correctly failed. The "name" field with incorrect value was not found.');
}
+
+ // Test that text areas can contain new lines.
+ $this->assertFieldsByValue($this->xpath("//textarea[@id = 'edit-test-textarea-with-newline']"), "Test text with\nnewline");
}
/**
- * Tests legacy field asserts on other types of field.
+ * Tests legacy field asserts for options field type.
*/
- public function testLegacyFieldAssertsWithNonTextfields() {
+ public function testLegacyFieldAssertsForOptions() {
$this->drupalGet('test-field-xpath');
// Option field type.
$this->pass($e->getMessage());
}
- // Button field type.
+ // Test \Drupal\FunctionalTests\AssertLegacyTrait::getAllOptions.
+ $this->drupalGet('/form-test/select');
+ $this->assertCount(6, $this->getAllOptions($this->cssSelect('select[name="opt_groups"]')[0]));
+ }
+
+ /**
+ * Tests legacy field asserts for button field type.
+ */
+ public function testLegacyFieldAssertsForButton() {
+ $this->drupalGet('test-field-xpath');
+
$this->assertFieldById('edit-save', NULL);
// Test that the assertion fails correctly if the field value is passed in
// rather than the id.
$this->assertFieldById('Save', NULL);
$this->fail('The field with id of "Save" was found.');
}
- catch (ExpectationException $e) {
+ catch (\PHPUnit_Framework_ExpectationFailedException $e) {
$this->pass($e->getMessage());
}
$this->pass($e->getMessage());
}
- // Checkbox field type.
+ // Test that multiple fields with the same name are validated correctly.
+ $this->assertFieldByName('duplicate_button', 'Duplicate button 1');
+ $this->assertFieldByName('duplicate_button', 'Duplicate button 2');
+ $this->assertNoFieldByName('duplicate_button', 'Rabbit');
+
+ try {
+ $this->assertNoFieldByName('duplicate_button', 'Duplicate button 2');
+ $this->fail('The "duplicate_button" field with the value Duplicate button 2 was not found.');
+ }
+ catch (ExpectationException $e) {
+ $this->pass('assertNoFieldByName correctly failed. The "duplicate_button" field with the value Duplicate button 2 was found.');
+ }
+ }
+
+ /**
+ * Tests legacy field asserts for checkbox field type.
+ */
+ public function testLegacyFieldAssertsForCheckbox() {
+ $this->drupalGet('test-field-xpath');
+
+ // Part 1 - Test by name.
// Test that checkboxes are found/not found correctly by name, when using
// TRUE or FALSE to match their 'checked' state.
$this->assertFieldByName('checkbox_enabled', TRUE);
$this->assertFieldByName('checkbox_enabled', NULL);
$this->assertFieldByName('checkbox_disabled', NULL);
+ // Test that checkboxes are found by name when passing no second parameter.
+ $this->assertFieldByName('checkbox_enabled');
+ $this->assertFieldByName('checkbox_disabled');
+
+ // Test that we have legacy support.
+ $this->assertFieldByName('checkbox_enabled', '1');
+ $this->assertFieldByName('checkbox_disabled', '');
+
+ // Test that the assertion fails correctly when using NULL to ignore state.
+ try {
+ $this->assertNoFieldByName('checkbox_enabled', NULL);
+ $this->fail('The "checkbox_enabled" field was not found by name, using NULL value.');
+ }
+ catch (ExpectationException $e) {
+ $this->pass('assertNoFieldByName failed correctly. The "checkbox_enabled" field was found using NULL value.');
+ }
+
+ // Part 2 - Test by ID.
// Test that checkboxes are found/not found correctly by ID, when using
// TRUE or FALSE to match their 'checked' state.
$this->assertFieldById('edit-checkbox-enabled', TRUE);
$this->assertNoFieldById('edit-checkbox-enabled', FALSE);
$this->assertNoFieldById('edit-checkbox-disabled', TRUE);
- // Test that checkboxes are found by by ID, when using NULL to ignore the
+ // Test that checkboxes are found by ID, when using NULL to ignore the
// 'checked' state.
$this->assertFieldById('edit-checkbox-enabled', NULL);
$this->assertFieldById('edit-checkbox-disabled', NULL);
- // Test that the assertion fails correctly when using NULL to ignore state.
- try {
- $this->assertNoFieldByName('checkbox_enabled', NULL);
- $this->fail('The "checkbox_enabled" field was not found by name, using NULL value.');
- }
- catch (ExpectationException $e) {
- $this->pass('assertNoFieldByName failed correctly. The "checkbox_enabled" field was found using NULL value.');
- }
+ // Test that checkboxes are found by ID when passing no second parameter.
+ $this->assertFieldById('edit-checkbox-enabled');
+ $this->assertFieldById('edit-checkbox-disabled');
+
+ // Test that we have legacy support.
+ $this->assertFieldById('edit-checkbox-enabled', '1');
+ $this->assertFieldById('edit-checkbox-disabled', '');
// Test that the assertion fails correctly when using NULL to ignore state.
try {
$this->pass('assertNoFieldById failed correctly. The "edit-checkbox-disabled" field was found by ID using NULL value.');
}
- // Test the specific 'checked' assertions.
+ // Part 3 - Test the specific 'checked' assertions.
$this->assertFieldChecked('edit-checkbox-enabled');
$this->assertNoFieldChecked('edit-checkbox-disabled');
$this->assertEquals('Australia/Sydney', $value);
}
+ /**
+ * Tests the ::checkForMetaRefresh() method.
+ */
+ public function testCheckForMetaRefresh() {
+ // Disable following redirects in the client.
+ $this->getSession()->getDriver()->getClient()->followRedirects(FALSE);
+ // Set the maximumMetaRefreshCount to zero to make sure the redirect doesn't
+ // happen when doing a drupalGet.
+ $this->maximumMetaRefreshCount = 0;
+ $this->drupalGet('test-meta-refresh');
+ $this->assertNotEmpty($this->cssSelect('meta[http-equiv="refresh"]'));
+ // Allow one redirect to happen.
+ $this->maximumMetaRefreshCount = 1;
+ $this->checkForMetaRefresh();
+ // Check that we are now on the test page.
+ $this->assertSession()->pageTextContains('Test page text.');
+ }
+
+ public function testGetDefaultDriveInstance() {
+ putenv('MINK_DRIVER_ARGS=' . json_encode([NULL, ['key1' => ['key2' => ['key3' => 3, 'key3.1' => 3.1]]]]));
+ $this->getDefaultDriverInstance();
+ $this->assertEquals([NULL, ['key1' => ['key2' => ['key3' => 3, 'key3.1' => 3.1]]]], $this->minkDefaultDriverArgs);
+ }
+
}