Commit 97ee3b9c authored by catch's avatar catch
Browse files

Issue #1452896 by Mile23, marthinal, Freso, kid_icarus, alexpott,...

Issue #1452896 by Mile23, marthinal, Freso, kid_icarus, alexpott, tim.plunkett, jhodgdon: Fixed PHP notice in clickLink if link does not exist.
parent d34f1b65
......@@ -2496,7 +2496,9 @@ protected function assertNoLinkByHref($href, $message = '', $group = 'Other') {
*
* Will click the first link found with this link text by default, or a later
* one if an index is given. Match is case sensitive with normalized space.
* The label is translated label. There is an assert for successful click.
* The label is translated label.
*
* If the link is discovered and clicked, the test passes. Fail otherwise.
*
* @param $label
* Text between the anchor tags.
......@@ -2504,21 +2506,17 @@ protected function assertNoLinkByHref($href, $message = '', $group = 'Other') {
* Link position counting from zero.
*
* @return
* Page on success, or FALSE on failure.
* Page contents on success, or FALSE on failure.
*/
protected function clickLink($label, $index = 0) {
$url_before = $this->getUrl();
$urls = $this->xpath('//a[normalize-space()=:label]', array(':label' => $label));
if (isset($urls[$index])) {
$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');
if (isset($url_target)) {
$this->pass(String::format('Clicked link %label (@url_target) from @url_before', array('%label' => $label, '@url_target' => $url_target, '@url_before' => $url_before)), 'Browser');
return $this->drupalGet($url_target);
}
$this->fail(String::format('Link $label does not exist on @url_before', array('%label' => $label, '@url_before' => $url_before)), 'Browser');
return FALSE;
}
......
......@@ -86,4 +86,130 @@ public function testAssertFieldByName($filename, $name, $value, $expected) {
$test_method->invokeArgs($web_test, array($name, $value, 'message'));
}
/**
* Data provider for testClickLink().
*
* In the test method, we mock drupalGet() to return a known string:
* 'This Text Returned By drupalGet()'. Since clickLink() can only return
* either the value of drupalGet() or FALSE, our expected return value is the
* same as this mocked return value when we expect a link to be found.
*
* @see https://www.drupal.org/node/1452896
*
* @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().
*
* @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().
*
* @dataProvider providerTestClickLink
* @covers ::clickLink
*/
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(
'pass',
'fail',
'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));
if ($expected === FALSE) {
// If link does not exist clickLink() will not try to do a drupalGet() or
// a getAbsoluteUrl()
$web_test->expects($this->never())
->method('drupalGet');
$web_test->expects($this->never())
->method('getAbsoluteUrl');
// The test should fail and not pass.
$web_test->expects($this->never())
->method('pass');
$web_test->expects($this->once())
->method('fail')
->will($this->returnValue(NULL));
}
else {
// Mocked getAbsoluteUrl() should return whatever comes in.
$web_test->expects($this->once())
->method('getAbsoluteUrl')
->with($xpath_data[$index]['href'])
->will($this->returnArgument(0));
// We're only testing clickLink(), so drupalGet() always returns a string.
$web_test->expects($this->once())
->method('drupalGet')
->with($xpath_data[$index]['href'])
->will($this->returnValue('This Text Returned By drupalGet()'));
// The test should pass and not fail.
$web_test->expects($this->never())
->method('fail');
$web_test->expects($this->once())
->method('pass')
->will($this->returnValue(NULL));
}
// Set the clickLink() method to public so we can test it.
$clicklink_method = new \ReflectionMethod($web_test, 'clickLink');
$clicklink_method->setAccessible(TRUE);
$this->assertSame($expected, $clicklink_method->invoke($web_test, $label, $index));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment