function WebTestBaseTest::testClickLink

Test WebTestBase::clickLink().

@dataProvider providerTestClickLink @covers ::clickLink

Parameters

mixed $expected: Expected return value of clickLink().

string $label: Parameter $label to clickLink().

int $index: Parameter $index to clickLink().

array $xpath_data: Test data to be returned by mocked xpath().

File

core/modules/simpletest/tests/src/Unit/WebTestBaseTest.php, line 134

Class

WebTestBaseTest
@requires extension curl @coversDefaultClass \Drupal\simpletest\WebTestBase @group simpletest @group WebTestBase

Namespace

Drupal\Tests\simpletest\Unit

Code

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([
        '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));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.