comparison core/modules/simpletest/tests/src/Unit/WebTestBaseTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children af1871eacc83
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Tests\simpletest\Unit;
4
5 use Drupal\Tests\UnitTestCase;
6
7 /**
8 * @requires extension curl
9 * @coversDefaultClass \Drupal\simpletest\WebTestBase
10 * @group simpletest
11 */
12 class WebTestBaseTest extends UnitTestCase {
13
14 /**
15 * Provides data for testing the assertFieldByName() helper.
16 *
17 * @return array
18 * An array of values passed to the test method.
19 */
20 public function providerAssertFieldByName() {
21 $data = [];
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];
27
28 return $data;
29 }
30
31 /**
32 * Tests the assertFieldByName() helper.
33 *
34 * @param string $filename
35 * Name of file containing the output to test.
36 * @param string $name
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.
42 *
43 * @see \Drupal\simpletest\WebTestBase::assertFieldByName()
44 *
45 * @dataProvider providerAssertFieldByName
46 * @covers ::assertFieldByName
47 */
48 public function testAssertFieldByName($filename, $name, $value, $expected) {
49 $content = file_get_contents(__DIR__ . '/../../fixtures/' . $filename . '.html');
50
51 $web_test = $this->getMockBuilder('Drupal\simpletest\WebTestBase')
52 ->disableOriginalConstructor()
53 ->setMethods(['getRawContent', 'assertTrue', 'pass'])
54 ->getMock();
55
56 $web_test->expects($this->any())
57 ->method('getRawContent')
58 ->will($this->returnValue($content));
59
60 $web_test->expects($this->once())
61 ->method('assertTrue')
62 ->with($this->identicalTo($expected),
63 $this->identicalTo('message'),
64 $this->identicalTo('Browser'));
65
66 $test_method = new \ReflectionMethod('Drupal\simpletest\WebTestBase', 'assertFieldByName');
67 $test_method->setAccessible(TRUE);
68 $test_method->invokeArgs($web_test, [$name, $value, 'message']);
69 }
70
71 /**
72 * Data provider for testClickLink().
73 *
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.
78 *
79 * @see https://www.drupal.org/node/1452896
80 *
81 * @return array
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.
88 */
89 public function providerTestClickLink() {
90 return [
91 // Test for a non-existent label.
92 [
93 FALSE,
94 'does_not_exist',
95 0,
96 [],
97 ],
98 // Test for an existing label.
99 [
100 'This Text Returned By drupalGet()',
101 'exists',
102 0,
103 [0 => ['href' => 'this_is_a_url']],
104 ],
105 // Test for an existing label that isn't the first one.
106 [
107 'This Text Returned By drupalGet()',
108 'exists',
109 1,
110 [
111 0 => ['href' => 'this_is_a_url'],
112 1 => ['href' => 'this_is_another_url'],
113 ],
114 ],
115 ];
116 }
117
118 /**
119 * Test WebTestBase::clickLink().
120 *
121 * @param mixed $expected
122 * Expected return value of clickLink().
123 * @param string $label
124 * Parameter $label to clickLink().
125 * @param int $index
126 * Parameter $index to clickLink().
127 * @param array $xpath_data
128 * Test data to be returned by mocked xpath().
129 *
130 * @dataProvider providerTestClickLink
131 * @covers ::clickLink
132 */
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()
137 ->setMethods([
138 'pass',
139 'fail',
140 'getUrl',
141 'xpath',
142 'drupalGet',
143 'getAbsoluteUrl',
144 ])
145 ->getMock();
146
147 // Mocked getUrl() is only used for reporting so we just return a string.
148 $web_test->expects($this->any())
149 ->method('getUrl')
150 ->will($this->returnValue('url_before'));
151
152 // Mocked xpath() should return our test data.
153 $web_test->expects($this->any())
154 ->method('xpath')
155 ->will($this->returnValue($xpath_data));
156
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())
166 ->method('pass');
167 $web_test->expects($this->once())
168 ->method('fail')
169 ->will($this->returnValue(NULL));
170 }
171 else {
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())
184 ->method('fail');
185 $web_test->expects($this->once())
186 ->method('pass')
187 ->will($this->returnValue(NULL));
188 }
189
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);
193
194 $this->assertSame($expected, $clicklink_method->invoke($web_test, $label, $index));
195 }
196
197 /**
198 * @dataProvider providerTestGetAbsoluteUrl
199 */
200 public function testGetAbsoluteUrl($href, $expected_absolute_path) {
201 $web_test = $this->getMockBuilder('Drupal\simpletest\WebTestBase')
202 ->disableOriginalConstructor()
203 ->setMethods(['getUrl'])
204 ->getMock();
205
206 $web_test->expects($this->any())
207 ->method('getUrl')
208 ->willReturn('http://example.com/drupal/current-path?foo=baz');
209
210 $GLOBALS['base_url'] = 'http://example.com';
211 $GLOBALS['base_path'] = 'drupal';
212
213 $get_absolute_url_method = new \ReflectionMethod($web_test, 'getAbsoluteUrl');
214 $get_absolute_url_method->setAccessible(TRUE);
215
216 $this->assertSame($expected_absolute_path, $get_absolute_url_method->invoke($web_test, $href));
217 unset($GLOBALS['base_url'], $GLOBALS['base_path']);
218 }
219
220 /**
221 * Provides test data for testGetAbsoluteUrl.
222 *
223 * @return array
224 */
225 public function providerTestGetAbsoluteUrl() {
226 $data = [];
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'];
231
232 return $data;
233 }
234
235 }