Chris@0: getMockBuilder('Drupal\simpletest\WebTestBase') Chris@0: ->disableOriginalConstructor() Chris@0: ->setMethods(['getRawContent', 'assertTrue', 'pass']) Chris@0: ->getMock(); Chris@0: Chris@0: $web_test->expects($this->any()) Chris@0: ->method('getRawContent') Chris@0: ->will($this->returnValue($content)); Chris@0: Chris@0: $web_test->expects($this->once()) Chris@0: ->method('assertTrue') Chris@0: ->with($this->identicalTo($expected), Chris@0: $this->identicalTo('message'), Chris@0: $this->identicalTo('Browser')); Chris@0: Chris@0: $test_method = new \ReflectionMethod('Drupal\simpletest\WebTestBase', 'assertFieldByName'); Chris@0: $test_method->setAccessible(TRUE); Chris@0: $test_method->invokeArgs($web_test, [$name, $value, 'message']); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Data provider for testClickLink(). Chris@0: * Chris@0: * In the test method, we mock drupalGet() to return a known string: Chris@0: * 'This Text Returned By drupalGet()'. Since clickLink() can only return Chris@0: * either the value of drupalGet() or FALSE, our expected return value is the Chris@0: * same as this mocked return value when we expect a link to be found. Chris@0: * Chris@0: * @see https://www.drupal.org/node/1452896 Chris@0: * Chris@0: * @return array Chris@0: * Array of arrays of test data. Test data is structured as follows: Chris@0: * - Expected return value of clickLink(). Chris@0: * - Parameter $label to clickLink(). Chris@0: * - Parameter $index to clickLink(). Chris@0: * - Test data to be returned by mocked xpath(). Return an empty array here Chris@0: * to mock no link found on the page. Chris@0: */ Chris@0: public function providerTestClickLink() { Chris@0: return [ Chris@0: // Test for a non-existent label. Chris@0: [ Chris@0: FALSE, Chris@0: 'does_not_exist', Chris@0: 0, Chris@0: [], Chris@0: ], Chris@0: // Test for an existing label. Chris@0: [ Chris@0: 'This Text Returned By drupalGet()', Chris@0: 'exists', Chris@0: 0, Chris@0: [0 => ['href' => 'this_is_a_url']], Chris@0: ], Chris@0: // Test for an existing label that isn't the first one. Chris@0: [ Chris@0: 'This Text Returned By drupalGet()', Chris@0: 'exists', Chris@0: 1, Chris@0: [ Chris@0: 0 => ['href' => 'this_is_a_url'], Chris@0: 1 => ['href' => 'this_is_another_url'], Chris@0: ], Chris@0: ], Chris@0: ]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test WebTestBase::clickLink(). Chris@0: * Chris@0: * @param mixed $expected Chris@0: * Expected return value of clickLink(). Chris@0: * @param string $label Chris@0: * Parameter $label to clickLink(). Chris@0: * @param int $index Chris@0: * Parameter $index to clickLink(). Chris@0: * @param array $xpath_data Chris@0: * Test data to be returned by mocked xpath(). Chris@0: * Chris@0: * @dataProvider providerTestClickLink Chris@0: * @covers ::clickLink Chris@0: */ Chris@0: public function testClickLink($expected, $label, $index, $xpath_data) { Chris@0: // Mock a WebTestBase object and some of its methods. Chris@0: $web_test = $this->getMockBuilder('Drupal\simpletest\WebTestBase') Chris@0: ->disableOriginalConstructor() Chris@0: ->setMethods([ Chris@0: 'pass', Chris@0: 'fail', Chris@0: 'getUrl', Chris@0: 'xpath', Chris@0: 'drupalGet', Chris@0: 'getAbsoluteUrl', Chris@0: ]) Chris@0: ->getMock(); Chris@0: Chris@0: // Mocked getUrl() is only used for reporting so we just return a string. Chris@0: $web_test->expects($this->any()) Chris@0: ->method('getUrl') Chris@0: ->will($this->returnValue('url_before')); Chris@0: Chris@0: // Mocked xpath() should return our test data. Chris@0: $web_test->expects($this->any()) Chris@0: ->method('xpath') Chris@0: ->will($this->returnValue($xpath_data)); Chris@0: Chris@0: if ($expected === FALSE) { Chris@0: // If link does not exist clickLink() will not try to do a drupalGet() or Chris@0: // a getAbsoluteUrl() Chris@0: $web_test->expects($this->never()) Chris@0: ->method('drupalGet'); Chris@0: $web_test->expects($this->never()) Chris@0: ->method('getAbsoluteUrl'); Chris@0: // The test should fail and not pass. Chris@0: $web_test->expects($this->never()) Chris@0: ->method('pass'); Chris@0: $web_test->expects($this->once()) Chris@0: ->method('fail') Chris@0: ->will($this->returnValue(NULL)); Chris@0: } Chris@0: else { Chris@0: // Mocked getAbsoluteUrl() should return whatever comes in. Chris@0: $web_test->expects($this->once()) Chris@0: ->method('getAbsoluteUrl') Chris@0: ->with($xpath_data[$index]['href']) Chris@0: ->will($this->returnArgument(0)); Chris@0: // We're only testing clickLink(), so drupalGet() always returns a string. Chris@0: $web_test->expects($this->once()) Chris@0: ->method('drupalGet') Chris@0: ->with($xpath_data[$index]['href']) Chris@0: ->will($this->returnValue('This Text Returned By drupalGet()')); Chris@0: // The test should pass and not fail. Chris@0: $web_test->expects($this->never()) Chris@0: ->method('fail'); Chris@0: $web_test->expects($this->once()) Chris@0: ->method('pass') Chris@0: ->will($this->returnValue(NULL)); Chris@0: } Chris@0: Chris@0: // Set the clickLink() method to public so we can test it. Chris@0: $clicklink_method = new \ReflectionMethod($web_test, 'clickLink'); Chris@0: $clicklink_method->setAccessible(TRUE); Chris@0: Chris@0: $this->assertSame($expected, $clicklink_method->invoke($web_test, $label, $index)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @dataProvider providerTestGetAbsoluteUrl Chris@0: */ Chris@0: public function testGetAbsoluteUrl($href, $expected_absolute_path) { Chris@0: $web_test = $this->getMockBuilder('Drupal\simpletest\WebTestBase') Chris@0: ->disableOriginalConstructor() Chris@0: ->setMethods(['getUrl']) Chris@0: ->getMock(); Chris@0: Chris@0: $web_test->expects($this->any()) Chris@0: ->method('getUrl') Chris@0: ->willReturn('http://example.com/drupal/current-path?foo=baz'); Chris@0: Chris@0: $GLOBALS['base_url'] = 'http://example.com'; Chris@0: $GLOBALS['base_path'] = 'drupal'; Chris@0: Chris@0: $get_absolute_url_method = new \ReflectionMethod($web_test, 'getAbsoluteUrl'); Chris@0: $get_absolute_url_method->setAccessible(TRUE); Chris@0: Chris@0: $this->assertSame($expected_absolute_path, $get_absolute_url_method->invoke($web_test, $href)); Chris@0: unset($GLOBALS['base_url'], $GLOBALS['base_path']); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Provides test data for testGetAbsoluteUrl. Chris@0: * Chris@0: * @return array Chris@0: */ Chris@0: public function providerTestGetAbsoluteUrl() { Chris@0: $data = []; Chris@0: $data['host'] = ['http://example.com/drupal/test-example', 'http://example.com/drupal/test-example']; Chris@0: $data['path'] = ['/drupal/test-example', 'http://example.com/drupal/test-example']; Chris@0: $data['path-with-query'] = ['/drupal/test-example?foo=bar', 'http://example.com/drupal/test-example?foo=bar']; Chris@0: $data['just-query'] = ['?foo=bar', 'http://example.com/drupal/current-path?foo=bar']; Chris@0: Chris@0: return $data; Chris@0: } Chris@0: Chris@0: }