Newer
Older
Alex Pott
committed
<?php
/**
* @file
Dries Buytaert
committed
* Contains \Drupal\Tests\simpletest\Unit\WebTestBaseTest.
Alex Pott
committed
*/
Dries Buytaert
committed
namespace Drupal\Tests\simpletest\Unit;
Alex Pott
committed
use Drupal\Tests\UnitTestCase;
/**
Angie Byron
committed
* @coversDefaultClass \Drupal\simpletest\WebTestBase
* @group simpletest
Alex Pott
committed
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
*/
class WebTestBaseTest extends UnitTestCase {
/**
* Provides data for testing the assertFieldByName() helper.
*
* @return array
* An array of values passed to the test method.
*/
public function providerAssertFieldByName() {
$data = array();
$data[] = array('select_2nd_selected', 'test', '1', FALSE);
$data[] = array('select_2nd_selected', 'test', '2', TRUE);
$data[] = array('select_none_selected', 'test', '', FALSE);
$data[] = array('select_none_selected', 'test', '1', TRUE);
$data[] = array('select_none_selected', 'test', NULL, TRUE);
return $data;
}
/**
* Tests the assertFieldByName() helper.
*
* @param string $filename
* Name of file containing the output to test.
* @param string $name
* Name of field to assert.
* @param string $value
* Value of the field to assert.
* @param bool $expected
* The expected result of the assert.
*
* @see \Drupal\simpletest\WebTestBase::assertFieldByName()
*
* @dataProvider providerAssertFieldByName
Angie Byron
committed
* @covers ::assertFieldByName
Alex Pott
committed
*/
public function testAssertFieldByName($filename, $name, $value, $expected) {
Dries Buytaert
committed
$content = file_get_contents(__DIR__ . '/../../fixtures/' . $filename . '.html');
Alex Pott
committed
$web_test = $this->getMockBuilder('Drupal\simpletest\WebTestBase')
->disableOriginalConstructor()
->setMethods(array('getRawContent', 'assertTrue', 'pass'))
Alex Pott
committed
->getMock();
$web_test->expects($this->any())
->method('getRawContent')
Alex Pott
committed
->will($this->returnValue($content));
$web_test->expects($this->once())
->method('assertTrue')
->with($this->identicalTo($expected),
$this->identicalTo('message'),
$this->identicalTo('Browser'));
$test_method = new \ReflectionMethod('Drupal\simpletest\WebTestBase', 'assertFieldByName');
$test_method->setAccessible(TRUE);
$test_method->invokeArgs($web_test, array($name, $value, 'message'));
}
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/**
* 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));
}
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/**
* @dataProvider providerTestGetAbsoluteUrl
*/
public function testGetAbsoluteUrl($href, $expected_absolute_path) {
$web_test = $this->getMockBuilder('Drupal\simpletest\WebTestBase')
->disableOriginalConstructor()
->setMethods(['getUrl'])
->getMock();
$web_test->expects($this->any())
->method('getUrl')
->willReturn('http://example.com/drupal/current-path?foo=baz');
$GLOBALS['base_url'] = 'http://example.com';
$GLOBALS['base_path'] = 'drupal';
$get_absolute_url_method = new \ReflectionMethod($web_test, 'getAbsoluteUrl');
$get_absolute_url_method->setAccessible(TRUE);
$this->assertSame($expected_absolute_path, $get_absolute_url_method->invoke($web_test, $href));
}
/**
* Provides test data for testGetAbsoluteUrl.
*
* @return array
*/
public function providerTestGetAbsoluteUrl() {
$data = [];
$data['host'] = ['http://example.com/drupal/test-example', 'http://example.com/drupal/test-example'];
$data['path'] = ['/drupal/test-example', 'http://example.com/drupal/test-example'];
$data['path-with-query'] = ['/drupal/test-example?foo=bar', 'http://example.com/drupal/test-example?foo=bar'];
$data['just-query'] = ['?foo=bar', 'http://example.com/drupal/current-path?foo=bar'];
return $data;
}