diff --git a/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php index a442dd07661..69b8454a41d 100644 --- a/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php +++ b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php @@ -3043,16 +3043,17 @@ abstract class WebTestBase extends TestBase { } elseif (isset($field->option)) { // Select element found. - if ($this->getSelectedItem($field) == $value) { - $found = TRUE; - } - else { + $selected = $this->getSelectedItem($field); + if ($selected === FALSE) { // No item selected so use first item. $items = $this->getAllOptions($field); if (!empty($items) && $items[0]['value'] == $value) { $found = TRUE; } } + elseif ($selected == $value) { + $found = TRUE; + } } elseif ((string) $field == $value) { // Text area with correct text. diff --git a/core/modules/simpletest/tests/Drupal/simpletest/Tests/Fixtures/select_2nd_selected.html b/core/modules/simpletest/tests/Drupal/simpletest/Tests/Fixtures/select_2nd_selected.html new file mode 100644 index 00000000000..ec30bd6eaac --- /dev/null +++ b/core/modules/simpletest/tests/Drupal/simpletest/Tests/Fixtures/select_2nd_selected.html @@ -0,0 +1,4 @@ + diff --git a/core/modules/simpletest/tests/Drupal/simpletest/Tests/Fixtures/select_none_selected.html b/core/modules/simpletest/tests/Drupal/simpletest/Tests/Fixtures/select_none_selected.html new file mode 100644 index 00000000000..a6c87499684 --- /dev/null +++ b/core/modules/simpletest/tests/Drupal/simpletest/Tests/Fixtures/select_none_selected.html @@ -0,0 +1,4 @@ + diff --git a/core/modules/simpletest/tests/Drupal/simpletest/Tests/WebTestBaseTest.php b/core/modules/simpletest/tests/Drupal/simpletest/Tests/WebTestBaseTest.php new file mode 100644 index 00000000000..bbfdadddfa4 --- /dev/null +++ b/core/modules/simpletest/tests/Drupal/simpletest/Tests/WebTestBaseTest.php @@ -0,0 +1,87 @@ + 'WebTestBase helper functions test', + 'description' => 'Test helper functions provided by the WebTestBase abstract class.', + 'group' => 'Simpletest', + ); + } + + /** + * Provides data for testing the assertFieldByName() helper. + * + * @return array + * An array of values passed to the test method. + */ + public function providerAssertFieldByName() { + $data = array(); + $data[] = array('select_2nd_selected', 'test', '1', FALSE); + $data[] = array('select_2nd_selected', 'test', '2', TRUE); + $data[] = array('select_none_selected', 'test', '', FALSE); + $data[] = array('select_none_selected', 'test', '1', TRUE); + $data[] = array('select_none_selected', 'test', NULL, TRUE); + + return $data; + } + + /** + * Tests the assertFieldByName() helper. + * + * @param string $filename + * Name of file containing the output to test. + * @param string $name + * Name of field to assert. + * @param string $value + * Value of the field to assert. + * @param bool $expected + * The expected result of the assert. + * + * @see \Drupal\simpletest\WebTestBase::assertFieldByName() + * + * @dataProvider providerAssertFieldByName + */ + public function testAssertFieldByName($filename, $name, $value, $expected) { + $content = file_get_contents(__DIR__ . '/Fixtures/' . $filename . '.html'); + + $web_test = $this->getMockBuilder('Drupal\simpletest\WebTestBase') + ->disableOriginalConstructor() + ->setMethods(array('assertTrue', 'drupalGetContent', 'pass')) + ->getMock(); + + $web_test->expects($this->any()) + ->method('drupalGetContent') + ->will($this->returnValue($content)); + + $web_test->expects($this->once()) + ->method('assertTrue') + ->with($this->identicalTo($expected), + $this->identicalTo('message'), + $this->identicalTo('Browser')); + + $test_method = new \ReflectionMethod('Drupal\simpletest\WebTestBase', 'assertFieldByName'); + $test_method->setAccessible(TRUE); + $test_method->invokeArgs($web_test, array($name, $value, 'message')); + } + +} diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermFieldTest.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermFieldTest.php index f9f3e8d8dba..be0ad94152c 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermFieldTest.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermFieldTest.php @@ -102,7 +102,7 @@ class TermFieldTest extends TaxonomyTestBase { // Display creation form. $this->drupalGet('entity_test/add'); - $this->assertFieldByName($this->field_name, '', 'Widget is displayed.'); + $this->assertFieldByName($this->field_name, NULL, 'Widget is displayed.'); // Submit with some value. $edit = array(