Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / simpletest / src / Tests / BrokenSetUpTest.php
1 <?php
2
3 namespace Drupal\simpletest\Tests;
4
5 use Drupal\simpletest\WebTestBase;
6
7 /**
8  * Tests a test case that does not call parent::setUp().
9  *
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.
14  *
15  * @group simpletest
16  * @see \Drupal\simpletest\WebTestBase
17  */
18 class BrokenSetUpTest extends WebTestBase {
19
20   /**
21    * Modules to enable.
22    *
23    * @var array
24    */
25   public static $modules = ['simpletest'];
26
27   /**
28    * The path to the shared trigger file.
29    *
30    * @var string
31    */
32   protected $sharedTriggerFile;
33
34   protected function setUp() {
35     // If the test is being run from the main site, set up normally.
36     if (!$this->isInChildSite()) {
37       parent::setUp();
38
39       $this->sharedTriggerFile = $this->publicFilesDirectory . '/trigger';
40
41       // Create and log in user.
42       $admin_user = $this->drupalCreateUser(['administer unit tests']);
43       $this->drupalLogin($admin_user);
44     }
45     // If the test is being run from within simpletest, set up the broken test.
46     else {
47       $this->sharedTriggerFile = $this->originalFileDirectory . '/trigger';
48
49       if (file_get_contents($this->sharedTriggerFile) === 'setup') {
50         throw new \Exception('Broken setup');
51       }
52       $this->pass('The setUp() method has run.');
53     }
54   }
55
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);
60       parent::tearDown();
61     }
62     // If the test is being run from within simpletest, output a message.
63     else {
64       if (file_get_contents($this->sharedTriggerFile) === 'teardown') {
65         throw new \Exception('Broken teardown');
66       }
67       $this->pass('The tearDown() method has run.');
68     }
69   }
70
71   /**
72    * Runs this test case from within the simpletest child site.
73    */
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.');
88
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.');
99
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.');
110     }
111     // If the test is being run from within simpletest, output a message.
112     else {
113       if (file_get_contents($this->sharedTriggerFile) === 'test') {
114         throw new \Exception('Broken test');
115       }
116       $this->pass('The test method has run.');
117     }
118   }
119
120 }