annotate core/modules/simpletest/src/Tests/BrokenSetUpTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\simpletest\Tests;
Chris@0 4
Chris@0 5 use Drupal\simpletest\WebTestBase;
Chris@0 6
Chris@0 7 /**
Chris@0 8 * Tests a test case that does not call parent::setUp().
Chris@0 9 *
Chris@0 10 * If a test case does not call parent::setUp(), running
Chris@0 11 * \Drupal\simpletest\WebTestBase::tearDown() would destroy the main site's
Chris@0 12 * database tables. Therefore, we ensure that tests which are not set up
Chris@0 13 * properly are skipped.
Chris@0 14 *
Chris@17 15 * @group WebTestBase
Chris@0 16 * @see \Drupal\simpletest\WebTestBase
Chris@0 17 */
Chris@0 18 class BrokenSetUpTest extends WebTestBase {
Chris@0 19
Chris@0 20 /**
Chris@0 21 * Modules to enable.
Chris@0 22 *
Chris@0 23 * @var array
Chris@0 24 */
Chris@0 25 public static $modules = ['simpletest'];
Chris@0 26
Chris@0 27 /**
Chris@0 28 * The path to the shared trigger file.
Chris@0 29 *
Chris@0 30 * @var string
Chris@0 31 */
Chris@0 32 protected $sharedTriggerFile;
Chris@0 33
Chris@0 34 protected function setUp() {
Chris@0 35 // If the test is being run from the main site, set up normally.
Chris@0 36 if (!$this->isInChildSite()) {
Chris@0 37 parent::setUp();
Chris@0 38
Chris@0 39 $this->sharedTriggerFile = $this->publicFilesDirectory . '/trigger';
Chris@0 40
Chris@0 41 // Create and log in user.
Chris@0 42 $admin_user = $this->drupalCreateUser(['administer unit tests']);
Chris@0 43 $this->drupalLogin($admin_user);
Chris@0 44 }
Chris@0 45 // If the test is being run from within simpletest, set up the broken test.
Chris@0 46 else {
Chris@0 47 $this->sharedTriggerFile = $this->originalFileDirectory . '/trigger';
Chris@0 48
Chris@0 49 if (file_get_contents($this->sharedTriggerFile) === 'setup') {
Chris@0 50 throw new \Exception('Broken setup');
Chris@0 51 }
Chris@0 52 $this->pass('The setUp() method has run.');
Chris@0 53 }
Chris@0 54 }
Chris@0 55
Chris@0 56 protected function tearDown() {
Chris@0 57 // If the test is being run from the main site, tear down normally.
Chris@0 58 if (!$this->isInChildSite()) {
Chris@0 59 unlink($this->sharedTriggerFile);
Chris@0 60 parent::tearDown();
Chris@0 61 }
Chris@0 62 // If the test is being run from within simpletest, output a message.
Chris@0 63 else {
Chris@0 64 if (file_get_contents($this->sharedTriggerFile) === 'teardown') {
Chris@0 65 throw new \Exception('Broken teardown');
Chris@0 66 }
Chris@0 67 $this->pass('The tearDown() method has run.');
Chris@0 68 }
Chris@0 69 }
Chris@0 70
Chris@0 71 /**
Chris@0 72 * Runs this test case from within the simpletest child site.
Chris@0 73 */
Chris@0 74 public function testMethod() {
Chris@0 75 // If the test is being run from the main site, run it again from the web
Chris@0 76 // interface within the simpletest child site.
Chris@0 77 if (!$this->isInChildSite()) {
Chris@0 78 // Verify that a broken setUp() method is caught.
Chris@0 79 file_put_contents($this->sharedTriggerFile, 'setup');
Chris@0 80 $edit['tests[Drupal\simpletest\Tests\BrokenSetUpTest]'] = TRUE;
Chris@0 81 $this->drupalPostForm('admin/config/development/testing', $edit, t('Run tests'));
Chris@0 82 $this->assertRaw('Broken setup');
Chris@0 83 $this->assertNoRaw('The setUp() method has run.');
Chris@0 84 $this->assertNoRaw('Broken test');
Chris@0 85 $this->assertNoRaw('The test method has run.');
Chris@0 86 $this->assertNoRaw('Broken teardown');
Chris@0 87 $this->assertNoRaw('The tearDown() method has run.');
Chris@0 88
Chris@0 89 // Verify that a broken tearDown() method is caught.
Chris@0 90 file_put_contents($this->sharedTriggerFile, 'teardown');
Chris@0 91 $edit['tests[Drupal\simpletest\Tests\BrokenSetUpTest]'] = TRUE;
Chris@0 92 $this->drupalPostForm('admin/config/development/testing', $edit, t('Run tests'));
Chris@0 93 $this->assertNoRaw('Broken setup');
Chris@0 94 $this->assertRaw('The setUp() method has run.');
Chris@0 95 $this->assertNoRaw('Broken test');
Chris@0 96 $this->assertRaw('The test method has run.');
Chris@0 97 $this->assertRaw('Broken teardown');
Chris@0 98 $this->assertNoRaw('The tearDown() method has run.');
Chris@0 99
Chris@0 100 // Verify that a broken test method is caught.
Chris@0 101 file_put_contents($this->sharedTriggerFile, 'test');
Chris@0 102 $edit['tests[Drupal\simpletest\Tests\BrokenSetUpTest]'] = TRUE;
Chris@0 103 $this->drupalPostForm('admin/config/development/testing', $edit, t('Run tests'));
Chris@0 104 $this->assertNoRaw('Broken setup');
Chris@0 105 $this->assertRaw('The setUp() method has run.');
Chris@0 106 $this->assertRaw('Broken test');
Chris@0 107 $this->assertNoRaw('The test method has run.');
Chris@0 108 $this->assertNoRaw('Broken teardown');
Chris@0 109 $this->assertRaw('The tearDown() method has run.');
Chris@0 110 }
Chris@0 111 // If the test is being run from within simpletest, output a message.
Chris@0 112 else {
Chris@0 113 if (file_get_contents($this->sharedTriggerFile) === 'test') {
Chris@0 114 throw new \Exception('Broken test');
Chris@0 115 }
Chris@0 116 $this->pass('The test method has run.');
Chris@0 117 }
Chris@0 118 }
Chris@0 119
Chris@0 120 }