diff --git a/core/modules/simpletest/src/WebTestBase.php b/core/modules/simpletest/src/WebTestBase.php index f0c5257..e5f833f 100644 --- a/core/modules/simpletest/src/WebTestBase.php +++ b/core/modules/simpletest/src/WebTestBase.php @@ -2514,7 +2514,7 @@ protected function clickLink($label, $index = 0) { $url_target = $this->getAbsoluteUrl($urls[$index]['href']); } - $this->assertTrue(isset($urls[$index]), String::format('Clicked link %label (@url_target) from @url_before', array('%label' => $label, '@url_target' => $url_target, '@url_before' => $url_before)), 'Browser'); + $this->assertTrue(isset($urls[$index]), String::format('Clicked link %label (@url_target) from @url_before', array('%label' => $label, '@url_target' => isset($url_target) ? $url_target : '', '@url_before' => $url_before)), 'Browser'); if (isset($url_target)) { return $this->drupalGet($url_target); diff --git a/core/modules/simpletest/tests/src/WebTestBaseTest.php b/core/modules/simpletest/tests/src/WebTestBaseTest.php index 8bce846..b8ac5d3 100644 --- a/core/modules/simpletest/tests/src/WebTestBaseTest.php +++ b/core/modules/simpletest/tests/src/WebTestBaseTest.php @@ -86,4 +86,106 @@ public function testAssertFieldByName($filename, $name, $value, $expected) { $test_method->invokeArgs($web_test, array($name, $value, 'message')); } + /** + * Data provider for testClickLink(). + * + * @return array + * Array of arrays of test data. Test data is structured as follows: + * - Expected return value of clickLink(). + * - Parameter $label to clickLink(). + * - Parameter $index to clickLink(). + * - Test data to be returned by mocked xpath(). Return an empty array here + * to mock no link found on the page. + */ + public function providerTestClickLink() { + return array( + // Test for a non-existent label. + array( + FALSE, + 'does_not_exist', + 0, + array(), + ), + // Test for an existing label. + array( + 'This Text Returned By drupalGet()', + 'exists', + 0, + array(0 => array('href' => 'this_is_a_url')), + ), + // Test for an existing label that isn't the first one. + array( + 'This Text Returned By drupalGet()', + 'exists', + 1, + array( + 0 => array('href' => 'this_is_a_url'), + 1 => array('href' => 'this_is_another_url'), + ), + ), + ); + } + + /** + * Test WebTestBase::clickLink(). + * + * @dataProvider providerTestClickLink + * + * @param mixed $expected + * Expected return value of clickLink(). + * @param string $label + * Parameter $label to clickLink(). + * @param int $index + * Parameter $index to clickLink(). + * @param array $xpath_data + * Test data to be returned by mocked xpath(). + */ + public function testClickLink($expected, $label, $index, $xpath_data) { + // Mock a WebTestBase object and some of its methods. + $web_test = $this->getMockBuilder('Drupal\simpletest\WebTestBase') + ->disableOriginalConstructor() + ->setMethods(array( + 'assertTrue', + 'getUrl', + 'xpath', + 'drupalGet', + 'getAbsoluteUrl', + )) + ->getMock(); + + // Mocked getUrl() is only used for reporting so we just return a string. + $web_test->expects($this->any()) + ->method('getUrl') + ->will($this->returnValue('url_before')); + + // Mocked xpath() should return our test data. + $web_test->expects($this->any()) + ->method('xpath') + ->will($this->returnValue($xpath_data)); + + // Mocked getAbsoluteUrl() should return whatever comes in. + $web_test->expects($this->any()) + ->method('getAbsoluteUrl') + ->will($this->returnArgument(0)); + + // Mocked assertTrue() does nothing. + $web_test->expects($this->any()) + ->method('assertTrue') + ->will($this->returnValue(NULL)); + + // We're only testing clickLink(), so drupalGet() always returns a string. + $web_test->expects($this->any()) + ->method('drupalGet') + ->will($this->returnValue('This Text Returned By drupalGet()')); + + // Set the clickLink() method to public so we can test it. + $clicklink_method = new \ReflectionMethod($web_test, 'clickLink'); + $clicklink_method->setAccessible(TRUE); + + // The test itself. + $this->assertSame( + $expected, $clicklink_method->invoke($web_test, $label, $index), 'clickLink() valid.' + ); + } + }