/[drupal]/drupal/modules/simpletest/simpletest.test
ViewVC logotype

Contents of /drupal/modules/simpletest/simpletest.test

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.36 - (show annotations) (download) (as text)
Fri Oct 16 03:01:54 2009 UTC (5 weeks, 6 days ago) by dries
Branch: MAIN
CVS Tags: DRUPAL-7-0-UNSTABLE-10, HEAD
Changes since 1.35: +4 -4 lines
File MIME type: text/x-php
- Patch #356074 by chx, Damien Tournoud: provide a sequences API.
1 <?php
2 // $Id: simpletest.test,v 1.35 2009/10/09 07:48:06 webchick Exp $
3
4 class SimpleTestFunctionalTest extends DrupalWebTestCase {
5 /**
6 * The results array that has been parsed by getTestResults().
7 */
8 protected $childTestResults;
9
10 /**
11 * Store the test ID from each test run for comparison, to ensure they are
12 * incrementing.
13 */
14 protected $test_ids = array();
15
16 public static function getInfo() {
17 return array(
18 'name' => 'SimpleTest functionality',
19 'description' => 'Test SimpleTest\'s web interface: check that the intended tests were
20 run and ensure that test reports display the intended results. Also
21 test SimpleTest\'s internal browser and API\'s both explicitly and
22 implicitly.',
23 'group' => 'SimpleTest'
24 );
25 }
26
27 function setUp() {
28 if (!$this->inCURL()) {
29 parent::setUp('simpletest');
30
31 // Create and login user
32 $admin_user = $this->drupalCreateUser(array('administer unit tests'));
33 $this->drupalLogin($admin_user);
34 }
35 else {
36 parent::setUp();
37 }
38 }
39
40 /**
41 * Test the internal browsers functionality.
42 */
43 function testInternalBrowser() {
44 global $conf;
45 if (!$this->inCURL()) {
46 $this->drupalGet('node');
47 $this->assertTrue($this->drupalGetHeader('Date'), t('An HTTP header was received.'));
48 $this->assertTitle(t('Welcome to @site-name | @site-name', array('@site-name' => variable_get('site_name', 'Drupal'))), t('Site title matches.'));
49 $this->assertNoTitle('Foo', t('Site title does not match.'));
50 // Make sure that we are locked out of the installer when prefixing
51 // using the user-agent header. This is an important security check.
52 global $base_url;
53
54 $this->drupalGet($base_url . '/install.php', array('external' => TRUE));
55 $this->assertResponse(403, 'Cannot access install.php with a "simpletest" user-agent header.');
56
57 $this->drupalLogin($this->drupalCreateUser());
58 $headers = $this->drupalGetHeaders(TRUE);
59 $this->assertEqual(count($headers), 2, t('There was one intermediate request.'));
60 $this->assertTrue(strpos($headers[0][':status'], '302') !== FALSE, t('Intermediate response code was 302.'));
61 $this->assertFalse(empty($headers[0]['location']), t('Intermediate request contained a Location header.'));
62 $this->assertEqual($this->getUrl(), $headers[0]['location'], t('HTTP redirect was followed'));
63 $this->assertFalse($this->drupalGetHeader('Location'), t('Headers from intermediate request were reset.'));
64 $this->assertResponse(200, t('Response code from intermediate request was reset.'));
65 }
66 }
67
68 /**
69 * Make sure that tests selected through the web interface are run and
70 * that the results are displayed correctly.
71 */
72 function testWebTestRunner() {
73 $this->pass = t('SimpleTest pass.');
74 $this->fail = t('SimpleTest fail.');
75 $this->valid_permission = 'access content';
76 $this->invalid_permission = 'invalid permission';
77
78 if ($this->inCURL()) {
79 // Only run following code if this test is running itself through a CURL request.
80 $this->stubTest();
81 }
82 else {
83
84 // Run twice so test_ids can be accumulated.
85 for ($i = 0; $i < 2; $i++) {
86 // Run this test from web interface.
87 $this->drupalGet('admin/config/development/testing');
88
89 $edit = array();
90 $edit['SimpleTestFunctionalTest'] = TRUE;
91 $this->drupalPost(NULL, $edit, t('Run tests'));
92
93 // Parse results and confirm that they are correct.
94 $this->getTestResults();
95 $this->confirmStubTestResults();
96 }
97
98 // Regression test for #290316.
99 // Check that test_id is incrementing.
100 $this->assertTrue($this->test_ids[0] != $this->test_ids[1], t('Test ID is incrementing.'));
101 }
102 }
103
104 /**
105 * Test to be run and the results confirmed.
106 */
107 function stubTest() {
108 $this->pass($this->pass);
109 $this->fail($this->fail);
110
111 $this->drupalCreateUser(array($this->valid_permission));
112 $this->drupalCreateUser(array($this->invalid_permission));
113
114 $this->pass(t('Test ID is @id.', array('@id' => $this->testId)));
115
116 // Generates a warning.
117 $i = 1 / 0;
118
119 // Call an assert function specific to that class.
120 $this->assertNothing();
121
122 // Generates a warning inside a PHP function.
123 array_key_exists(NULL, NULL);
124
125 debug('Foo', 'Debug');
126 }
127
128 /**
129 * Assert nothing.
130 */
131 function assertNothing() {
132 $this->pass("This is nothing.");
133 }
134
135 /**
136 * Confirm that the stub test produced the desired results.
137 */
138 function confirmStubTestResults() {
139 $this->assertAssertion($this->pass, 'Other', 'Pass', 'simpletest.test', 'SimpleTestFunctionalTest->stubTest()');
140 $this->assertAssertion($this->fail, 'Other', 'Fail', 'simpletest.test', 'SimpleTestFunctionalTest->stubTest()');
141
142 $this->assertAssertion(t('Created permissions: @perms', array('@perms' => $this->valid_permission)), 'Role', 'Pass', 'simpletest.test', 'SimpleTestFunctionalTest->stubTest()');
143 $this->assertAssertion(t('Invalid permission %permission.', array('%permission' => $this->invalid_permission)), 'Role', 'Fail', 'simpletest.test', 'SimpleTestFunctionalTest->stubTest()');
144
145 // Check that a warning is catched by simpletest.
146 $this->assertAssertion('Division by zero', 'Warning', 'Fail', 'simpletest.test', 'SimpleTestFunctionalTest->stubTest()');
147
148 // Check that the backtracing code works for specific assert function.
149 $this->assertAssertion('This is nothing.', 'Other', 'Pass', 'simpletest.test', 'SimpleTestFunctionalTest->stubTest()');
150
151 // Check that errors that occur inside PHP internal functions are correctly reported.
152 // The exact error message differs between PHP versions so we check only
153 // the function name 'array_key_exists'.
154 $this->assertAssertion('array_key_exists', 'Warning', 'Fail', 'simpletest.test', 'SimpleTestFunctionalTest->stubTest()');
155
156 $this->assertAssertion("Debug: 'Foo'", 'Debug', 'Fail', 'simpletest.test', 'SimpleTestFunctionalTest->stubTest()');
157
158 $this->assertEqual('6 passes, 2 fails, 2 exceptions, and 1 debug message', $this->childTestResults['summary'], 'Stub test summary is correct');
159
160 $this->test_ids[] = $test_id = $this->getTestIdFromResults();
161 $this->assertTrue($test_id, t('Found test ID in results.'));
162 }
163
164 /**
165 * Fetch the test id from the test results.
166 */
167 function getTestIdFromResults() {
168 foreach ($this->childTestResults['assertions'] as $assertion) {
169 if (preg_match('@^Test ID is ([0-9]*)\.$@', $assertion['message'], $matches)) {
170 return $matches[1];
171 }
172 }
173 return NULL;
174 }
175
176 /**
177 * Assert that an assertion with the specified values is displayed
178 * in the test results.
179 *
180 * @param string $message Assertion message.
181 * @param string $type Assertion type.
182 * @param string $status Assertion status.
183 * @param string $file File where the assertion originated.
184 * @param string $functuion Function where the assertion originated.
185 * @return Assertion result.
186 */
187 function assertAssertion($message, $type, $status, $file, $function) {
188 $message = trim(strip_tags($message));
189 $found = FALSE;
190 foreach ($this->childTestResults['assertions'] as $assertion) {
191 if ((strpos($assertion['message'], $message) !== FALSE) &&
192 $assertion['type'] == $type &&
193 $assertion['status'] == $status &&
194 $assertion['file'] == $file &&
195 $assertion['function'] == $function) {
196 $found = TRUE;
197 break;
198 }
199 }
200 return $this->assertTrue($found, t('Found assertion {"@message", "@type", "@status", "@file", "@function"}.', array('@message' => $message, '@type' => $type, '@status' => $status, "@file" => $file, "@function" => $function)));
201 }
202
203 /**
204 * Get the results from a test and store them in the class array $results.
205 */
206 function getTestResults() {
207 $results = array();
208 if ($this->parse()) {
209 if ($fieldset = $this->getResultFieldSet()) {
210 // Code assumes this is the only test in group.
211 $results['summary'] = $this->asText($fieldset->div[1]);
212 $results['name'] = $this->asText($fieldset->legend);
213
214 $results['assertions'] = array();
215 $tbody = $fieldset->table->tbody;
216 foreach ($tbody->tr as $row) {
217 $assertion = array();
218 $assertion['message'] = $this->asText($row->td[0]);
219 $assertion['type'] = $this->asText($row->td[1]);
220 $assertion['file'] = $this->asText($row->td[2]);
221 $assertion['line'] = $this->asText($row->td[3]);
222 $assertion['function'] = $this->asText($row->td[4]);
223 $ok_url = file_create_url('misc/watchdog-ok.png');
224 $assertion['status'] = ($row->td[5]->img['src'] == $ok_url) ? 'Pass' : 'Fail';
225 $results['assertions'][] = $assertion;
226 }
227 }
228 }
229 $this->childTestResults = $results;
230 }
231
232 /**
233 * Get the fieldset containing the results for group this test is in.
234 *
235 * @return fieldset containing the results for group this test is in.
236 */
237 function getResultFieldSet() {
238 $fieldsets = $this->xpath('//fieldset');
239 $info = $this->getInfo();
240 foreach ($fieldsets as $fieldset) {
241 if ($this->asText($fieldset->legend) == $info['name']) {
242 return $fieldset;
243 }
244 }
245 return FALSE;
246 }
247
248 /**
249 * Extract the text contained by the element.
250 *
251 * @param $element
252 * Element to extract text from.
253 * @return
254 * Extracted text.
255 */
256 function asText(SimpleXMLElement $element) {
257 if (!is_object($element)) {
258 return $this->fail('The element is not an element.');
259 }
260 return trim(html_entity_decode(strip_tags($element->asXML())));
261 }
262
263 /**
264 * Check if the test is being run from inside a CURL request.
265 *
266 * @return The test is being run from inside a CURL request.
267 */
268 function inCURL() {
269 return isset($_SERVER['HTTP_USER_AGENT']) && preg_match("/^simpletest\d+/", $_SERVER['HTTP_USER_AGENT']);
270 }
271 }
272
273 /**
274 * Test internal testing framework URL handling.
275 */
276 class SimpleTestURLTestCase extends DrupalWebTestCase {
277 public static function getInfo() {
278 return array(
279 'name' => 'SimpleTest URL handling',
280 'description' => 'Test the URL handling in the testing framework.',
281 'group' => 'SimpleTest',
282 );
283 }
284
285 /**
286 * Test DrupalWebTestCase::getAbsoluteUrl().
287 */
288 function testGetAbsoluteUrl() {
289 // Testbed runs with Clean URLs disabled, so disable it here.
290 variable_set('clean_url', 0);
291 $url = 'user/login';
292
293 $this->drupalGet($url);
294 $absolute = url($url, array('absolute' => TRUE));
295 $this->assertEqual($absolute, $this->url, t('Passed and requested URL are equal.'));
296 $this->assertEqual($this->url, $this->getAbsoluteUrl($this->url), t('Requested and returned absolute URL are equal.'));
297
298 $this->drupalPost(NULL, array(), t('Log in'));
299 $this->assertEqual($absolute, $this->url, t('Passed and requested URL are equal.'));
300 $this->assertEqual($this->url, $this->getAbsoluteUrl($this->url), t('Requested and returned absolute URL are equal.'));
301
302 $this->clickLink('Create new account');
303 $url = 'user/register';
304 $absolute = url($url, array('absolute' => TRUE));
305 $this->assertEqual($absolute, $this->url, t('Passed and requested URL are equal.'));
306 $this->assertEqual($this->url, $this->getAbsoluteUrl($this->url), t('Requested and returned absolute URL are equal.'));
307 }
308 }
309
310 class SimpleTestMailCaptureTestCase extends DrupalWebTestCase {
311 /**
312 * Implement getInfo().
313 */
314 public static function getInfo() {
315 return array(
316 'name' => 'SimpleTest e-mail capturing',
317 'description' => 'Test the SimpleTest e-mail capturing logic, the assertMail assertion and the drupalGetMails function.',
318 'group' => 'SimpleTest',
319 );
320 }
321
322 /**
323 * Test to see if the wrapper function is executed correctly.
324 */
325 function testMailSend() {
326 // Create an e-mail.
327 $subject = $this->randomString(64);
328 $body = $this->randomString(128);
329 $message = array(
330 'id' => 'drupal_mail_test',
331 'headers' => array('Content-type'=> 'text/html'),
332 'subject' => $subject,
333 'to' => 'foobar@example.com',
334 'body' => $body,
335 );
336
337 // Before we send the e-mail, drupalGetMails should return an empty array.
338 $captured_emails = $this->drupalGetMails();
339 $this->assertEqual(count($captured_emails), 0, t('The captured e-mails queue is empty.'), t('E-mail'));
340
341 // Send the e-mail.
342 $response = drupal_mail_system('simpletest', 'drupal_mail_test')->mail($message);
343
344 // Ensure that there is one e-mail in the captured e-mails array.
345 $captured_emails = $this->drupalGetMails();
346 $this->assertEqual(count($captured_emails), 1, t('One e-mail was captured.'), t('E-mail'));
347
348 // Assert that the e-mail was sent by iterating over the message properties
349 // and ensuring that they are captured intact.
350 foreach($message as $field => $value) {
351 $this->assertMail($field, $value, t('The e-mail was sent and the value for property @field is intact.', array('@field' => $field)), t('E-mail'));
352 }
353
354 // Send additional e-mails so more than one e-mail is captured.
355 for ($index = 0; $index < 5; $index++) {
356 $message = array(
357 'id' => 'drupal_mail_test_' . $index,
358 'headers' => array('Content-type'=> 'text/html'),
359 'subject' => $this->randomString(64),
360 'to' => $this->randomName(32) . '@example.com',
361 'body' => $this->randomString(512),
362 );
363 drupal_mail_system('drupal_mail_test', $index)->mail($message);
364 }
365
366 // There should now be 6 e-mails captured.
367 $captured_emails = $this->drupalGetMails();
368 $this->assertEqual(count($captured_emails), 6, t('All e-mails were captured.'), t('E-mail'));
369
370 // Test different ways of getting filtered e-mails via drupalGetMails().
371 $captured_emails = $this->drupalGetMails(array('id' => 'drupal_mail_test'));
372 $this->assertEqual(count($captured_emails), 1, t('Only one e-mail is returned when filtering by id.'), t('E-mail'));
373 $captured_emails = $this->drupalGetMails(array('id' => 'drupal_mail_test', 'subject' => $subject));
374 $this->assertEqual(count($captured_emails), 1, t('Only one e-mail is returned when filtering by id and subject.'), t('E-mail'));
375 $captured_emails = $this->drupalGetMails(array('id' => 'drupal_mail_test', 'subject' => $subject, 'from' => 'this_was_not_used@example.com'));
376 $this->assertEqual(count($captured_emails), 0, t('No e-mails are returned when querying with an unused from address.'), t('E-mail'));
377
378 // Send the last e-mail again, so we can confirm that the drupalGetMails-filter
379 // correctly returns all e-mails with a given property/value.
380 drupal_mail_system('drupal_mail_test', $index)->mail($message);
381 $captured_emails = $this->drupalGetMails(array('id' => 'drupal_mail_test_4'));
382 $this->assertEqual(count($captured_emails), 2, t('All e-mails with the same id are returned when filtering by id.'), t('E-mail'));
383 }
384 }
385
386 /**
387 * Test required modules for tests.
388 */
389 class SimpleTestMissingDependentModuleUnitTest extends DrupalUnitTestCase {
390 public static function getInfo() {
391 return array(
392 'title' => 'Testing dependent module test',
393 'description' => 'This test should not load since it requires a module that is not found.',
394 'group' => 'SimpleTest',
395 'dependencies' => array('simpletest_missing_module'),
396 );
397 }
398
399 /**
400 * Ensure that this test will not be loaded despite its dependency.
401 */
402 function testFail() {
403 $this->fail(t('Running test with missing required module.'));
404 }
405 }
406

  ViewVC Help
Powered by ViewVC 1.1.2