Projet

Général

Profil

Paste
Télécharger (27,8 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / simpletest / simpletest.test @ db2d93dd

1
<?php
2

    
3
/**
4
 * @file
5
 * Tests for simpletest.module.
6
 */
7

    
8
class SimpleTestFunctionalTest extends DrupalWebTestCase {
9
  /**
10
   * The results array that has been parsed by getTestResults().
11
   */
12
  protected $childTestResults;
13

    
14
  /**
15
   * Store the test ID from each test run for comparison, to ensure they are
16
   * incrementing.
17
   */
18
  protected $test_ids = array();
19

    
20
  public static function getInfo() {
21
    return array(
22
      'name' => 'SimpleTest functionality',
23
      'description' => "Test SimpleTest's web interface: check that the intended tests were run and ensure that test reports display the intended results. Also test SimpleTest's internal browser and API's both explicitly and implicitly.",
24
      'group' => 'SimpleTest'
25
    );
26
  }
27

    
28
  function setUp() {
29
    if (!$this->inCURL()) {
30
      parent::setUp('simpletest');
31

    
32
      // Create and login user
33
      $admin_user = $this->drupalCreateUser(array('administer unit tests'));
34
      $this->drupalLogin($admin_user);
35
    }
36
    else {
37
      parent::setUp('non_existent_module');
38
    }
39
  }
40

    
41
  /**
42
   * Test the internal browsers functionality.
43
   */
44
  function testInternalBrowser() {
45
    global $conf;
46
    if (!$this->inCURL()) {
47
      $this->drupalGet('node');
48
      $this->assertTrue($this->drupalGetHeader('Date'), 'An HTTP header was received.');
49
      $this->assertTitle(t('Welcome to @site-name | @site-name', array('@site-name' => variable_get('site_name', 'Drupal'))), 'Site title matches.');
50
      $this->assertNoTitle('Foo', 'Site title does not match.');
51
      // Make sure that we are locked out of the installer when prefixing
52
      // using the user-agent header. This is an important security check.
53
      global $base_url;
54

    
55
      $this->drupalGet($base_url . '/install.php', array('external' => TRUE));
56
      $this->assertResponse(403, 'Cannot access install.php with a "simpletest" user-agent header.');
57

    
58
      $user = $this->drupalCreateUser();
59
      $this->drupalLogin($user);
60
      $headers = $this->drupalGetHeaders(TRUE);
61
      $this->assertEqual(count($headers), 2, 'There was one intermediate request.');
62
      $this->assertTrue(strpos($headers[0][':status'], '302') !== FALSE, 'Intermediate response code was 302.');
63
      $this->assertFalse(empty($headers[0]['location']), 'Intermediate request contained a Location header.');
64
      $this->assertEqual($this->getUrl(), $headers[0]['location'], 'HTTP redirect was followed');
65
      $this->assertFalse($this->drupalGetHeader('Location'), 'Headers from intermediate request were reset.');
66
      $this->assertResponse(200, 'Response code from intermediate request was reset.');
67

    
68
      // Test the maximum redirection option.
69
      $this->drupalLogout();
70
      $edit = array(
71
        'name' => $user->name,
72
        'pass' => $user->pass_raw
73
      );
74
      variable_set('simpletest_maximum_redirects', 1);
75
      $this->drupalPost('user?destination=user/logout', $edit, t('Log in'));
76
      $headers = $this->drupalGetHeaders(TRUE);
77
      $this->assertEqual(count($headers), 2, 'Simpletest stopped following redirects after the first one.');
78
    }
79
  }
80

    
81
  /**
82
   * Test validation of the User-Agent header we use to perform test requests.
83
   */
84
  function testUserAgentValidation() {
85
    if (!$this->inCURL()) {
86
      global $base_url;
87
      $simpletest_path = $base_url . '/' . drupal_get_path('module', 'simpletest');
88
      $HTTP_path = $simpletest_path .'/tests/http.php?q=node';
89
      $https_path = $simpletest_path .'/tests/https.php?q=node';
90
      // Generate a valid simpletest User-Agent to pass validation.
91
      $this->assertTrue(preg_match('/simpletest\d+/', $this->databasePrefix, $matches), 'Database prefix contains simpletest prefix.');
92
      $test_ua = drupal_generate_test_ua($matches[0]);
93
      $this->additionalCurlOptions = array(CURLOPT_USERAGENT => $test_ua);
94

    
95
      // Test pages only available for testing.
96
      $this->drupalGet($HTTP_path);
97
      $this->assertResponse(200, 'Requesting http.php with a legitimate simpletest User-Agent returns OK.');
98
      $this->drupalGet($https_path);
99
      $this->assertResponse(200, 'Requesting https.php with a legitimate simpletest User-Agent returns OK.');
100

    
101
      // Now slightly modify the HMAC on the header, which should not validate.
102
      $this->additionalCurlOptions = array(CURLOPT_USERAGENT => $test_ua . 'X');
103
      $this->drupalGet($HTTP_path);
104
      $this->assertResponse(403, 'Requesting http.php with a bad simpletest User-Agent fails.');
105
      $this->drupalGet($https_path);
106
      $this->assertResponse(403, 'Requesting https.php with a bad simpletest User-Agent fails.');
107

    
108
      // Use a real User-Agent and verify that the special files http.php and
109
      // https.php can't be accessed.
110
      $this->additionalCurlOptions = array(CURLOPT_USERAGENT => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12');
111
      $this->drupalGet($HTTP_path);
112
      $this->assertResponse(403, 'Requesting http.php with a normal User-Agent fails.');
113
      $this->drupalGet($https_path);
114
      $this->assertResponse(403, 'Requesting https.php with a normal User-Agent fails.');
115
    }
116
  }
117

    
118
  /**
119
   * Make sure that tests selected through the web interface are run and
120
   * that the results are displayed correctly.
121
   */
122
  function testWebTestRunner() {
123
    $this->pass = t('SimpleTest pass.');
124
    $this->fail = t('SimpleTest fail.');
125
    $this->valid_permission = 'access content';
126
    $this->invalid_permission = 'invalid permission';
127

    
128
    if ($this->inCURL()) {
129
      // Only run following code if this test is running itself through a CURL request.
130
      $this->stubTest();
131
    }
132
    else {
133

    
134
      // Run twice so test_ids can be accumulated.
135
      for ($i = 0; $i < 2; $i++) {
136
        // Run this test from web interface.
137
        $this->drupalGet('admin/config/development/testing');
138

    
139
        $edit = array();
140
        $edit['SimpleTestFunctionalTest'] = TRUE;
141
        $this->drupalPost(NULL, $edit, t('Run tests'));
142

    
143
        // Parse results and confirm that they are correct.
144
        $this->getTestResults();
145
        $this->confirmStubTestResults();
146
      }
147

    
148
      // Regression test for #290316.
149
      // Check that test_id is incrementing.
150
      $this->assertTrue($this->test_ids[0] != $this->test_ids[1], 'Test ID is incrementing.');
151
    }
152
  }
153

    
154
  /**
155
   * Test to be run and the results confirmed.
156
   */
157
  function stubTest() {
158
    $this->pass($this->pass);
159
    $this->fail($this->fail);
160

    
161
    $this->drupalCreateUser(array($this->valid_permission));
162
    $this->drupalCreateUser(array($this->invalid_permission));
163

    
164
    $this->pass(t('Test ID is @id.', array('@id' => $this->testId)));
165

    
166
    // Generates a warning.
167
    $i = 1 / 0;
168

    
169
    // Call an assert function specific to that class.
170
    $this->assertNothing();
171

    
172
    // Generates a warning inside a PHP function.
173
    array_key_exists(NULL, NULL);
174

    
175
    debug('Foo', 'Debug');
176
  }
177

    
178
  /**
179
   * Assert nothing.
180
   */
181
  function assertNothing() {
182
    $this->pass("This is nothing.");
183
  }
184

    
185
  /**
186
   * Confirm that the stub test produced the desired results.
187
   */
188
  function confirmStubTestResults() {
189
    $this->assertAssertion(t('Enabled modules: %modules', array('%modules' => 'non_existent_module')), 'Other', 'Fail', 'simpletest.test', 'SimpleTestFunctionalTest->setUp()');
190

    
191
    $this->assertAssertion($this->pass, 'Other', 'Pass', 'simpletest.test', 'SimpleTestFunctionalTest->stubTest()');
192
    $this->assertAssertion($this->fail, 'Other', 'Fail', 'simpletest.test', 'SimpleTestFunctionalTest->stubTest()');
193

    
194
    $this->assertAssertion(t('Created permissions: @perms', array('@perms' => $this->valid_permission)), 'Role', 'Pass', 'simpletest.test', 'SimpleTestFunctionalTest->stubTest()');
195
    $this->assertAssertion(t('Invalid permission %permission.', array('%permission' => $this->invalid_permission)), 'Role', 'Fail', 'simpletest.test', 'SimpleTestFunctionalTest->stubTest()');
196

    
197
    // Check that a warning is caught by simpletest.
198
    $this->assertAssertion('Division by zero', 'Warning', 'Fail', 'simpletest.test', 'SimpleTestFunctionalTest->stubTest()');
199

    
200
    // Check that the backtracing code works for specific assert function.
201
    $this->assertAssertion('This is nothing.', 'Other', 'Pass', 'simpletest.test', 'SimpleTestFunctionalTest->stubTest()');
202

    
203
    // Check that errors that occur inside PHP internal functions are correctly reported.
204
    // The exact error message differs between PHP versions so we check only
205
    // the function name 'array_key_exists'.
206
    $this->assertAssertion('array_key_exists', 'Warning', 'Fail', 'simpletest.test', 'SimpleTestFunctionalTest->stubTest()');
207

    
208
    $this->assertAssertion("Debug: 'Foo'", 'Debug', 'Fail', 'simpletest.test', 'SimpleTestFunctionalTest->stubTest()');
209

    
210
    $this->assertEqual('6 passes, 5 fails, 2 exceptions, and 1 debug message', $this->childTestResults['summary'], 'Stub test summary is correct');
211

    
212
    $this->test_ids[] = $test_id = $this->getTestIdFromResults();
213
    $this->assertTrue($test_id, 'Found test ID in results.');
214
  }
215

    
216
  /**
217
   * Fetch the test id from the test results.
218
   */
219
  function getTestIdFromResults() {
220
    foreach ($this->childTestResults['assertions'] as $assertion) {
221
      if (preg_match('@^Test ID is ([0-9]*)\.$@', $assertion['message'], $matches)) {
222
        return $matches[1];
223
      }
224
    }
225
    return NULL;
226
  }
227

    
228
  /**
229
   * Assert that an assertion with the specified values is displayed
230
   * in the test results.
231
   *
232
   * @param string $message Assertion message.
233
   * @param string $type Assertion type.
234
   * @param string $status Assertion status.
235
   * @param string $file File where the assertion originated.
236
   * @param string $functuion Function where the assertion originated.
237
   * @return Assertion result.
238
   */
239
  function assertAssertion($message, $type, $status, $file, $function) {
240
    $message = trim(strip_tags($message));
241
    $found = FALSE;
242
    foreach ($this->childTestResults['assertions'] as $assertion) {
243
      if ((strpos($assertion['message'], $message) !== FALSE) &&
244
          $assertion['type'] == $type &&
245
          $assertion['status'] == $status &&
246
          $assertion['file'] == $file &&
247
          $assertion['function'] == $function) {
248
        $found = TRUE;
249
        break;
250
      }
251
    }
252
    return $this->assertTrue($found, format_string('Found assertion {"@message", "@type", "@status", "@file", "@function"}.', array('@message' => $message, '@type' => $type, '@status' => $status, "@file" => $file, "@function" => $function)));
253
  }
254

    
255
  /**
256
   * Get the results from a test and store them in the class array $results.
257
   */
258
  function getTestResults() {
259
    $results = array();
260
    if ($this->parse()) {
261
      if ($fieldset = $this->getResultFieldSet()) {
262
        // Code assumes this is the only test in group.
263
        $results['summary'] = $this->asText($fieldset->div->div[1]);
264
        $results['name'] = $this->asText($fieldset->legend);
265

    
266
        $results['assertions'] = array();
267
        $tbody = $fieldset->div->table->tbody;
268
        foreach ($tbody->tr as $row) {
269
          $assertion = array();
270
          $assertion['message'] = $this->asText($row->td[0]);
271
          $assertion['type'] = $this->asText($row->td[1]);
272
          $assertion['file'] = $this->asText($row->td[2]);
273
          $assertion['line'] = $this->asText($row->td[3]);
274
          $assertion['function'] = $this->asText($row->td[4]);
275
          $ok_url = file_create_url('misc/watchdog-ok.png');
276
          $assertion['status'] = ($row->td[5]->img['src'] == $ok_url) ? 'Pass' : 'Fail';
277
          $results['assertions'][] = $assertion;
278
        }
279
      }
280
    }
281
    $this->childTestResults = $results;
282
  }
283

    
284
  /**
285
   * Get the fieldset containing the results for group this test is in.
286
   */
287
  function getResultFieldSet() {
288
    $fieldsets = $this->xpath('//fieldset');
289
    $info = $this->getInfo();
290
    foreach ($fieldsets as $fieldset) {
291
      if ($this->asText($fieldset->legend) == $info['name']) {
292
        return $fieldset;
293
      }
294
    }
295
    return FALSE;
296
  }
297

    
298
  /**
299
   * Extract the text contained by the element.
300
   *
301
   * @param $element
302
   *   Element to extract text from.
303
   * @return
304
   *   Extracted text.
305
   */
306
  function asText(SimpleXMLElement $element) {
307
    if (!is_object($element)) {
308
      return $this->fail('The element is not an element.');
309
    }
310
    return trim(html_entity_decode(strip_tags($element->asXML())));
311
  }
312

    
313
  /**
314
   * Check if the test is being run from inside a CURL request.
315
   */
316
  function inCURL() {
317
    return (bool) drupal_valid_test_ua();
318
  }
319
}
320

    
321
/**
322
 * Test internal testing framework browser.
323
 */
324
class SimpleTestBrowserTestCase extends DrupalWebTestCase {
325
  public static function getInfo() {
326
    return array(
327
      'name' => 'SimpleTest browser',
328
      'description' => 'Test the internal browser of the testing framework.',
329
      'group' => 'SimpleTest',
330
    );
331
  }
332

    
333
  function setUp() {
334
    parent::setUp();
335
    variable_set('user_register', USER_REGISTER_VISITORS);
336
  }
337

    
338
  /**
339
   * Test DrupalWebTestCase::getAbsoluteUrl().
340
   */
341
  function testGetAbsoluteUrl() {
342
    // Testbed runs with Clean URLs disabled, so disable it here.
343
    variable_set('clean_url', 0);
344
    $url = 'user/login';
345

    
346
    $this->drupalGet($url);
347
    $absolute = url($url, array('absolute' => TRUE));
348
    $this->assertEqual($absolute, $this->url, 'Passed and requested URL are equal.');
349
    $this->assertEqual($this->url, $this->getAbsoluteUrl($this->url), 'Requested and returned absolute URL are equal.');
350

    
351
    $this->drupalPost(NULL, array(), t('Log in'));
352
    $this->assertEqual($absolute, $this->url, 'Passed and requested URL are equal.');
353
    $this->assertEqual($this->url, $this->getAbsoluteUrl($this->url), 'Requested and returned absolute URL are equal.');
354

    
355
    $this->clickLink('Create new account');
356
    $url = 'user/register';
357
    $absolute = url($url, array('absolute' => TRUE));
358
    $this->assertEqual($absolute, $this->url, 'Passed and requested URL are equal.');
359
    $this->assertEqual($this->url, $this->getAbsoluteUrl($this->url), 'Requested and returned absolute URL are equal.');
360
  }
361

    
362
  /**
363
   * Tests XPath escaping.
364
   */
365
  function testXPathEscaping() {
366
    $testpage = <<< EOF
367
<html>
368
<body>
369
<a href="link1">A "weird" link, just to bother the dumb "XPath 1.0"</a>
370
<a href="link2">A second "even more weird" link, in memory of George O'Malley</a>
371
</body>
372
</html>
373
EOF;
374
    $this->drupalSetContent($testpage);
375

    
376
    // Matches the first link.
377
    $urls = $this->xpath('//a[text()=:text]', array(':text' => 'A "weird" link, just to bother the dumb "XPath 1.0"'));
378
    $this->assertEqual($urls[0]['href'], 'link1', 'Match with quotes.');
379

    
380
    $urls = $this->xpath('//a[text()=:text]', array(':text' => 'A second "even more weird" link, in memory of George O\'Malley'));
381
    $this->assertEqual($urls[0]['href'], 'link2', 'Match with mixed single and double quotes.');
382
  }
383
}
384

    
385
class SimpleTestMailCaptureTestCase extends DrupalWebTestCase {
386
  /**
387
   * Implement getInfo().
388
   */
389
  public static function getInfo() {
390
    return array(
391
      'name' => 'SimpleTest e-mail capturing',
392
      'description' => 'Test the SimpleTest e-mail capturing logic, the assertMail assertion and the drupalGetMails function.',
393
      'group' => 'SimpleTest',
394
    );
395
  }
396

    
397
  /**
398
   * Test to see if the wrapper function is executed correctly.
399
   */
400
  function testMailSend() {
401
    // Create an e-mail.
402
    $subject = $this->randomString(64);
403
    $body = $this->randomString(128);
404
    $message = array(
405
      'id' => 'drupal_mail_test',
406
      'headers' => array('Content-type'=> 'text/html'),
407
      'subject' => $subject,
408
      'to' => 'foobar@example.com',
409
      'body' => $body,
410
    );
411

    
412
    // Before we send the e-mail, drupalGetMails should return an empty array.
413
    $captured_emails = $this->drupalGetMails();
414
    $this->assertEqual(count($captured_emails), 0, 'The captured e-mails queue is empty.', 'E-mail');
415

    
416
    // Send the e-mail.
417
    $response = drupal_mail_system('simpletest', 'drupal_mail_test')->mail($message);
418

    
419
    // Ensure that there is one e-mail in the captured e-mails array.
420
    $captured_emails = $this->drupalGetMails();
421
    $this->assertEqual(count($captured_emails), 1, 'One e-mail was captured.', 'E-mail');
422

    
423
    // Assert that the e-mail was sent by iterating over the message properties
424
    // and ensuring that they are captured intact.
425
    foreach ($message as $field => $value) {
426
      $this->assertMail($field, $value, format_string('The e-mail was sent and the value for property @field is intact.', array('@field' => $field)), 'E-mail');
427
    }
428

    
429
    // Send additional e-mails so more than one e-mail is captured.
430
    for ($index = 0; $index < 5; $index++) {
431
      $message = array(
432
        'id' => 'drupal_mail_test_' . $index,
433
        'headers' => array('Content-type'=> 'text/html'),
434
        'subject' => $this->randomString(64),
435
        'to' => $this->randomName(32) . '@example.com',
436
        'body' => $this->randomString(512),
437
      );
438
      drupal_mail_system('drupal_mail_test', $index)->mail($message);
439
    }
440

    
441
    // There should now be 6 e-mails captured.
442
    $captured_emails = $this->drupalGetMails();
443
    $this->assertEqual(count($captured_emails), 6, 'All e-mails were captured.', 'E-mail');
444

    
445
    // Test different ways of getting filtered e-mails via drupalGetMails().
446
    $captured_emails = $this->drupalGetMails(array('id' => 'drupal_mail_test'));
447
    $this->assertEqual(count($captured_emails), 1, 'Only one e-mail is returned when filtering by id.', 'E-mail');
448
    $captured_emails = $this->drupalGetMails(array('id' => 'drupal_mail_test', 'subject' => $subject));
449
    $this->assertEqual(count($captured_emails), 1, 'Only one e-mail is returned when filtering by id and subject.', 'E-mail');
450
    $captured_emails = $this->drupalGetMails(array('id' => 'drupal_mail_test', 'subject' => $subject, 'from' => 'this_was_not_used@example.com'));
451
    $this->assertEqual(count($captured_emails), 0, 'No e-mails are returned when querying with an unused from address.', 'E-mail');
452

    
453
    // Send the last e-mail again, so we can confirm that the drupalGetMails-filter
454
    // correctly returns all e-mails with a given property/value.
455
    drupal_mail_system('drupal_mail_test', $index)->mail($message);
456
    $captured_emails = $this->drupalGetMails(array('id' => 'drupal_mail_test_4'));
457
    $this->assertEqual(count($captured_emails), 2, 'All e-mails with the same id are returned when filtering by id.', 'E-mail');
458
  }
459
}
460

    
461
/**
462
 * Test Folder creation
463
 */
464
class SimpleTestFolderTestCase extends DrupalWebTestCase {
465
  public static function getInfo() {
466
    return array(
467
      'name' => 'Testing SimpleTest setUp',
468
      'description' => "This test will check SimpleTest's treatment of hook_install during setUp.  Image module is used for test.",
469
      'group' => 'SimpleTest',
470
    );
471
  }
472

    
473
  function setUp() {
474
    return parent::setUp('image');
475
  }
476

    
477
  function testFolderSetup() {
478
    $directory = file_default_scheme() . '://styles';
479
    $this->assertTrue(file_prepare_directory($directory, FALSE), 'Directory created.');
480
  }
481
}
482

    
483
/**
484
 * Test required modules for tests.
485
 */
486
class SimpleTestMissingDependentModuleUnitTest extends DrupalUnitTestCase {
487
  public static function getInfo() {
488
    return array(
489
      'name' => 'Testing dependent module test',
490
      'description' => 'This test should not load since it requires a module that is not found.',
491
      'group' => 'SimpleTest',
492
      'dependencies' => array('simpletest_missing_module'),
493
    );
494
  }
495

    
496
  /**
497
   * Ensure that this test will not be loaded despite its dependency.
498
   */
499
  function testFail() {
500
    $this->fail(t('Running test with missing required module.'));
501
  }
502
}
503

    
504
/**
505
 * Tests a test case that does not run parent::setUp() in its setUp() method.
506
 *
507
 * If a test case does not call parent::setUp(), running
508
 * DrupalTestCase::tearDown() would destroy the main site's database tables.
509
 * Therefore, we ensure that tests which are not set up properly are skipped.
510
 *
511
 * @see DrupalTestCase
512
 */
513
class SimpleTestBrokenSetUp extends DrupalWebTestCase {
514
  public static function getInfo() {
515
    return array(
516
      'name' => 'Broken SimpleTest method',
517
      'description' => 'Tests a test case that does not call parent::setUp().',
518
      'group' => 'SimpleTest'
519
    );
520
  }
521

    
522
  function setUp() {
523
    // If the test is being run from the main site, set up normally.
524
    if (!drupal_valid_test_ua()) {
525
      parent::setUp('simpletest');
526
      // Create and log in user.
527
      $admin_user = $this->drupalCreateUser(array('administer unit tests'));
528
      $this->drupalLogin($admin_user);
529
    }
530
    // If the test is being run from within simpletest, set up the broken test.
531
    else {
532
      $this->pass(t('The test setUp() method has been run.'));
533
      // Don't call parent::setUp(). This should trigger an error message.
534
    }
535
  }
536

    
537
  function tearDown() {
538
    // If the test is being run from the main site, tear down normally.
539
    if (!drupal_valid_test_ua()) {
540
      parent::tearDown();
541
    }
542
    else {
543
      // If the test is being run from within simpletest, output a message.
544
      $this->pass(t('The tearDown() method has run.'));
545
    }
546
  }
547

    
548
  /**
549
   * Runs this test case from within the simpletest child site.
550
   */
551
  function testBreakSetUp() {
552
    // If the test is being run from the main site, run it again from the web
553
    // interface within the simpletest child site.
554
    if (!drupal_valid_test_ua()) {
555
      $edit['SimpleTestBrokenSetUp'] = TRUE;
556
      $this->drupalPost('admin/config/development/testing', $edit, t('Run tests'));
557

    
558
      // Verify that the broken test and its tearDown() method are skipped.
559
      $this->assertRaw(t('The test setUp() method has been run.'));
560
      $this->assertRaw(t('The test cannot be executed because it has not been set up properly.'));
561
      $this->assertNoRaw(t('The test method has run.'));
562
      $this->assertNoRaw(t('The tearDown() method has run.'));
563
    }
564
    // If the test is being run from within simpletest, output a message.
565
    else {
566
      $this->pass(t('The test method has run.'));
567
    }
568
  }
569
}
570

    
571
/**
572
 * Verifies that tests bundled with installation profile modules are found.
573
 */
574
class SimpleTestInstallationProfileModuleTestsTestCase extends DrupalWebTestCase {
575
  /**
576
   * Use the Testing profile.
577
   *
578
   * The Testing profile contains drupal_system_listing_compatible_test.test,
579
   * which attempts to:
580
   * - run tests using the Minimal profile (which does not contain the
581
   *   drupal_system_listing_compatible_test.module)
582
   * - but still install the drupal_system_listing_compatible_test.module
583
   *   contained in the Testing profile.
584
   *
585
   * @see DrupalSystemListingCompatibleTestCase
586
   */
587
  protected $profile = 'testing';
588

    
589
  public static function getInfo() {
590
    return array(
591
      'name' => 'Installation profile module tests',
592
      'description' => 'Verifies that tests bundled with installation profile modules are found.',
593
      'group' => 'SimpleTest',
594
    );
595
  }
596

    
597
  function setUp() {
598
    parent::setUp(array('simpletest'));
599

    
600
    $this->admin_user = $this->drupalCreateUser(array('administer unit tests'));
601
    $this->drupalLogin($this->admin_user);
602
  }
603

    
604
  /**
605
   * Tests existence of test case located in an installation profile module.
606
   */
607
  function testInstallationProfileTests() {
608
    $this->drupalGet('admin/config/development/testing');
609
    $this->assertText('Installation profile module tests helper');
610
    $edit = array(
611
      'DrupalSystemListingCompatibleTestCase' => TRUE,
612
    );
613
    $this->drupalPost(NULL, $edit, t('Run tests'));
614
    $this->assertText('DrupalSystemListingCompatibleTestCase test executed.');
615
  }
616
}
617

    
618
/**
619
 * Verifies that tests in other installation profiles are not found.
620
 *
621
 * @see SimpleTestInstallationProfileModuleTestsTestCase
622
 */
623
class SimpleTestOtherInstallationProfileModuleTestsTestCase extends DrupalWebTestCase {
624
  /**
625
   * Use the Minimal profile.
626
   *
627
   * The Testing profile contains drupal_system_listing_compatible_test.test,
628
   * which should not be found.
629
   *
630
   * @see SimpleTestInstallationProfileModuleTestsTestCase
631
   * @see DrupalSystemListingCompatibleTestCase
632
   */
633
  protected $profile = 'minimal';
634

    
635
  public static function getInfo() {
636
    return array(
637
      'name' => 'Other Installation profiles',
638
      'description' => 'Verifies that tests in other installation profiles are not found.',
639
      'group' => 'SimpleTest',
640
    );
641
  }
642

    
643
  function setUp() {
644
    parent::setUp(array('simpletest'));
645

    
646
    $this->admin_user = $this->drupalCreateUser(array('administer unit tests'));
647
    $this->drupalLogin($this->admin_user);
648
  }
649

    
650
  /**
651
   * Tests that tests located in another installation profile do not appear.
652
   */
653
  function testOtherInstallationProfile() {
654
    $this->drupalGet('admin/config/development/testing');
655
    $this->assertNoText('Installation profile module tests helper');
656
  }
657
}
658

    
659
/**
660
 * Verifies that tests in other installation profiles are not found.
661
 *
662
 * @see SimpleTestInstallationProfileModuleTestsTestCase
663
 */
664
class SimpleTestDiscoveryTestCase extends DrupalWebTestCase {
665
  /**
666
   * Use the Testing profile.
667
   *
668
   * The Testing profile contains drupal_system_listing_compatible_test.test,
669
   * which attempts to:
670
   * - run tests using the Minimal profile (which does not contain the
671
   *   drupal_system_listing_compatible_test.module)
672
   * - but still install the drupal_system_listing_compatible_test.module
673
   *   contained in the Testing profile.
674
   *
675
   * @see DrupalSystemListingCompatibleTestCase
676
   */
677
  protected $profile = 'testing';
678

    
679
  public static function getInfo() {
680
    return array(
681
      'name' => 'Discovery of test classes',
682
      'description' => 'Verifies that tests classes are discovered and can be autoloaded (class_exists).',
683
      'group' => 'SimpleTest',
684
    );
685
  }
686

    
687
  function setUp() {
688
    parent::setUp(array('simpletest'));
689

    
690
    $this->admin_user = $this->drupalCreateUser(array('administer unit tests'));
691
    $this->drupalLogin($this->admin_user);
692
  }
693

    
694
  /**
695
   * Test discovery of PSR-0 test classes.
696
   */
697
  function testDiscoveryFunctions() {
698
    if (version_compare(PHP_VERSION, '5.3') < 0) {
699
      // Don't expect PSR-0 tests to be discovered on older PHP versions.
700
      return;
701
    }
702
    // TODO: What if we have cached values? Do we need to force a cache refresh?
703
    $classes_all = simpletest_test_get_all();
704
    foreach (array(
705
      'Drupal\\simpletest\\Tests\\PSR0WebTest',
706
      'Drupal\\simpletest\\Tests\\PSR4WebTest',
707
      'Drupal\\psr_0_test\\Tests\\ExampleTest',
708
      'Drupal\\psr_4_test\\Tests\\ExampleTest',
709
    ) as $class) {
710
      $this->assert(!empty($classes_all['SimpleTest'][$class]), t('Class @class must be discovered by simpletest_test_get_all().', array('@class' => $class)));
711
    }
712
  }
713

    
714
  /**
715
   * Tests existence of test cases.
716
   */
717
  function testDiscovery() {
718
    $this->drupalGet('admin/config/development/testing');
719
    // Tests within enabled modules.
720
    // (without these, this test wouldn't happen in the first place, so this is
721
    // a bit pointless. We still run it for proof-of-concept.)
722
    // This one is defined in system module.
723
    $this->assertText('Drupal error handlers');
724
    // This one is defined in simpletest module.
725
    $this->assertText('Discovery of test classes');
726
    // Tests within disabled modules.
727
    if (version_compare(PHP_VERSION, '5.3') < 0) {
728
      // Don't expect PSR-0 tests to be discovered on older PHP versions.
729
      return;
730
    }
731
    // These are provided by simpletest itself via PSR-0 and PSR-4.
732
    $this->assertText('PSR0 web test');
733
    $this->assertText('PSR4 web test');
734
    $this->assertText('PSR0 example test: PSR-0 in disabled modules.');
735
    $this->assertText('PSR4 example test: PSR-4 in disabled modules.');
736
    $this->assertText('PSR0 example test: PSR-0 in nested subfolders.');
737
    $this->assertText('PSR4 example test: PSR-4 in nested subfolders.');
738

    
739
    // Test each test individually.
740
    foreach (array(
741
      'Drupal\\psr_0_test\\Tests\\ExampleTest',
742
      'Drupal\\psr_0_test\\Tests\\Nested\\NestedExampleTest',
743
      'Drupal\\psr_4_test\\Tests\\ExampleTest',
744
      'Drupal\\psr_4_test\\Tests\\Nested\\NestedExampleTest',
745
    ) as $class) {
746
      $this->drupalGet('admin/config/development/testing');
747
      $edit = array($class => TRUE);
748
      $this->drupalPost(NULL, $edit, t('Run tests'));
749
      $this->assertText('The test run finished', t('Test @class must finish.', array('@class' => $class)));
750
      $this->assertText('1 pass, 0 fails, and 0 exceptions', t('Test @class must pass.', array('@class' => $class)));
751
    }
752
  }
753
}