3 namespace Drupal\Tests\simpletest\Unit;
5 use Drupal\Tests\UnitTestCase;
8 * @requires extension curl
9 * @coversDefaultClass \Drupal\simpletest\WebTestBase
12 class WebTestBaseTest extends UnitTestCase {
15 * Provides data for testing the assertFieldByName() helper.
18 * An array of values passed to the test method.
20 public function providerAssertFieldByName() {
22 $data[] = ['select_2nd_selected', 'test', '1', FALSE];
23 $data[] = ['select_2nd_selected', 'test', '2', TRUE];
24 $data[] = ['select_none_selected', 'test', '', FALSE];
25 $data[] = ['select_none_selected', 'test', '1', TRUE];
26 $data[] = ['select_none_selected', 'test', NULL, TRUE];
32 * Tests the assertFieldByName() helper.
34 * @param string $filename
35 * Name of file containing the output to test.
37 * Name of field to assert.
38 * @param string $value
39 * Value of the field to assert.
40 * @param bool $expected
41 * The expected result of the assert.
43 * @see \Drupal\simpletest\WebTestBase::assertFieldByName()
45 * @dataProvider providerAssertFieldByName
46 * @covers ::assertFieldByName
48 public function testAssertFieldByName($filename, $name, $value, $expected) {
49 $content = file_get_contents(__DIR__ . '/../../fixtures/' . $filename . '.html');
51 $web_test = $this->getMockBuilder('Drupal\simpletest\WebTestBase')
52 ->disableOriginalConstructor()
53 ->setMethods(['getRawContent', 'assertTrue', 'pass'])
56 $web_test->expects($this->any())
57 ->method('getRawContent')
58 ->will($this->returnValue($content));
60 $web_test->expects($this->once())
61 ->method('assertTrue')
62 ->with($this->identicalTo($expected),
63 $this->identicalTo('message'),
64 $this->identicalTo('Browser'));
66 $test_method = new \ReflectionMethod('Drupal\simpletest\WebTestBase', 'assertFieldByName');
67 $test_method->setAccessible(TRUE);
68 $test_method->invokeArgs($web_test, [$name, $value, 'message']);
72 * Data provider for testClickLink().
74 * In the test method, we mock drupalGet() to return a known string:
75 * 'This Text Returned By drupalGet()'. Since clickLink() can only return
76 * either the value of drupalGet() or FALSE, our expected return value is the
77 * same as this mocked return value when we expect a link to be found.
79 * @see https://www.drupal.org/node/1452896
82 * Array of arrays of test data. Test data is structured as follows:
83 * - Expected return value of clickLink().
84 * - Parameter $label to clickLink().
85 * - Parameter $index to clickLink().
86 * - Test data to be returned by mocked xpath(). Return an empty array here
87 * to mock no link found on the page.
89 public function providerTestClickLink() {
91 // Test for a non-existent label.
98 // Test for an existing label.
100 'This Text Returned By drupalGet()',
103 [0 => ['href' => 'this_is_a_url']],
105 // Test for an existing label that isn't the first one.
107 'This Text Returned By drupalGet()',
111 0 => ['href' => 'this_is_a_url'],
112 1 => ['href' => 'this_is_another_url'],
119 * Test WebTestBase::clickLink().
121 * @param mixed $expected
122 * Expected return value of clickLink().
123 * @param string $label
124 * Parameter $label to clickLink().
126 * Parameter $index to clickLink().
127 * @param array $xpath_data
128 * Test data to be returned by mocked xpath().
130 * @dataProvider providerTestClickLink
131 * @covers ::clickLink
133 public function testClickLink($expected, $label, $index, $xpath_data) {
134 // Mock a WebTestBase object and some of its methods.
135 $web_test = $this->getMockBuilder('Drupal\simpletest\WebTestBase')
136 ->disableOriginalConstructor()
147 // Mocked getUrl() is only used for reporting so we just return a string.
148 $web_test->expects($this->any())
150 ->will($this->returnValue('url_before'));
152 // Mocked xpath() should return our test data.
153 $web_test->expects($this->any())
155 ->will($this->returnValue($xpath_data));
157 if ($expected === FALSE) {
158 // If link does not exist clickLink() will not try to do a drupalGet() or
159 // a getAbsoluteUrl()
160 $web_test->expects($this->never())
161 ->method('drupalGet');
162 $web_test->expects($this->never())
163 ->method('getAbsoluteUrl');
164 // The test should fail and not pass.
165 $web_test->expects($this->never())
167 $web_test->expects($this->once())
169 ->will($this->returnValue(NULL));
172 // Mocked getAbsoluteUrl() should return whatever comes in.
173 $web_test->expects($this->once())
174 ->method('getAbsoluteUrl')
175 ->with($xpath_data[$index]['href'])
176 ->will($this->returnArgument(0));
177 // We're only testing clickLink(), so drupalGet() always returns a string.
178 $web_test->expects($this->once())
179 ->method('drupalGet')
180 ->with($xpath_data[$index]['href'])
181 ->will($this->returnValue('This Text Returned By drupalGet()'));
182 // The test should pass and not fail.
183 $web_test->expects($this->never())
185 $web_test->expects($this->once())
187 ->will($this->returnValue(NULL));
190 // Set the clickLink() method to public so we can test it.
191 $clicklink_method = new \ReflectionMethod($web_test, 'clickLink');
192 $clicklink_method->setAccessible(TRUE);
194 $this->assertSame($expected, $clicklink_method->invoke($web_test, $label, $index));
198 * @dataProvider providerTestGetAbsoluteUrl
200 public function testGetAbsoluteUrl($href, $expected_absolute_path) {
201 $web_test = $this->getMockBuilder('Drupal\simpletest\WebTestBase')
202 ->disableOriginalConstructor()
203 ->setMethods(['getUrl'])
206 $web_test->expects($this->any())
208 ->willReturn('http://example.com/drupal/current-path?foo=baz');
210 $GLOBALS['base_url'] = 'http://example.com';
211 $GLOBALS['base_path'] = 'drupal';
213 $get_absolute_url_method = new \ReflectionMethod($web_test, 'getAbsoluteUrl');
214 $get_absolute_url_method->setAccessible(TRUE);
216 $this->assertSame($expected_absolute_path, $get_absolute_url_method->invoke($web_test, $href));
217 unset($GLOBALS['base_url'], $GLOBALS['base_path']);
221 * Provides test data for testGetAbsoluteUrl.
225 public function providerTestGetAbsoluteUrl() {
227 $data['host'] = ['http://example.com/drupal/test-example', 'http://example.com/drupal/test-example'];
228 $data['path'] = ['/drupal/test-example', 'http://example.com/drupal/test-example'];
229 $data['path-with-query'] = ['/drupal/test-example?foo=bar', 'http://example.com/drupal/test-example?foo=bar'];
230 $data['just-query'] = ['?foo=bar', 'http://example.com/drupal/current-path?foo=bar'];