3 namespace Drupal\simpletest\Tests;
5 use Drupal\simpletest\WebTestBase;
8 * Tests a test case that does not call parent::setUp().
10 * If a test case does not call parent::setUp(), running
11 * \Drupal\simpletest\WebTestBase::tearDown() would destroy the main site's
12 * database tables. Therefore, we ensure that tests which are not set up
13 * properly are skipped.
16 * @see \Drupal\simpletest\WebTestBase
18 class BrokenSetUpTest extends WebTestBase {
25 public static $modules = ['simpletest'];
28 * The path to the shared trigger file.
32 protected $sharedTriggerFile;
34 protected function setUp() {
35 // If the test is being run from the main site, set up normally.
36 if (!$this->isInChildSite()) {
39 $this->sharedTriggerFile = $this->publicFilesDirectory . '/trigger';
41 // Create and log in user.
42 $admin_user = $this->drupalCreateUser(['administer unit tests']);
43 $this->drupalLogin($admin_user);
45 // If the test is being run from within simpletest, set up the broken test.
47 $this->sharedTriggerFile = $this->originalFileDirectory . '/trigger';
49 if (file_get_contents($this->sharedTriggerFile) === 'setup') {
50 throw new \Exception('Broken setup');
52 $this->pass('The setUp() method has run.');
56 protected function tearDown() {
57 // If the test is being run from the main site, tear down normally.
58 if (!$this->isInChildSite()) {
59 unlink($this->sharedTriggerFile);
62 // If the test is being run from within simpletest, output a message.
64 if (file_get_contents($this->sharedTriggerFile) === 'teardown') {
65 throw new \Exception('Broken teardown');
67 $this->pass('The tearDown() method has run.');
72 * Runs this test case from within the simpletest child site.
74 public function testMethod() {
75 // If the test is being run from the main site, run it again from the web
76 // interface within the simpletest child site.
77 if (!$this->isInChildSite()) {
78 // Verify that a broken setUp() method is caught.
79 file_put_contents($this->sharedTriggerFile, 'setup');
80 $edit['tests[Drupal\simpletest\Tests\BrokenSetUpTest]'] = TRUE;
81 $this->drupalPostForm('admin/config/development/testing', $edit, t('Run tests'));
82 $this->assertRaw('Broken setup');
83 $this->assertNoRaw('The setUp() method has run.');
84 $this->assertNoRaw('Broken test');
85 $this->assertNoRaw('The test method has run.');
86 $this->assertNoRaw('Broken teardown');
87 $this->assertNoRaw('The tearDown() method has run.');
89 // Verify that a broken tearDown() method is caught.
90 file_put_contents($this->sharedTriggerFile, 'teardown');
91 $edit['tests[Drupal\simpletest\Tests\BrokenSetUpTest]'] = TRUE;
92 $this->drupalPostForm('admin/config/development/testing', $edit, t('Run tests'));
93 $this->assertNoRaw('Broken setup');
94 $this->assertRaw('The setUp() method has run.');
95 $this->assertNoRaw('Broken test');
96 $this->assertRaw('The test method has run.');
97 $this->assertRaw('Broken teardown');
98 $this->assertNoRaw('The tearDown() method has run.');
100 // Verify that a broken test method is caught.
101 file_put_contents($this->sharedTriggerFile, 'test');
102 $edit['tests[Drupal\simpletest\Tests\BrokenSetUpTest]'] = TRUE;
103 $this->drupalPostForm('admin/config/development/testing', $edit, t('Run tests'));
104 $this->assertNoRaw('Broken setup');
105 $this->assertRaw('The setUp() method has run.');
106 $this->assertRaw('Broken test');
107 $this->assertNoRaw('The test method has run.');
108 $this->assertNoRaw('Broken teardown');
109 $this->assertRaw('The tearDown() method has run.');
111 // If the test is being run from within simpletest, output a message.
113 if (file_get_contents($this->sharedTriggerFile) === 'test') {
114 throw new \Exception('Broken test');
116 $this->pass('The test method has run.');