3 namespace Drupal\simpletest\Tests;
5 use Drupal\Component\Utility\Crypt;
6 use Drupal\Core\Test\TestDatabase;
7 use Drupal\simpletest\WebTestBase;
10 * Tests SimpleTest's web interface: check that the intended tests were run and
11 * ensure that test reports display the intended results. Also test SimpleTest's
12 * internal browser and APIs implicitly.
16 class SimpleTestTest extends WebTestBase {
23 public static $modules = ['simpletest'];
26 * The results array that has been parsed by getTestResults().
30 protected $childTestResults;
33 * Stores the test ID from each test run for comparison.
35 * Used to ensure they are incrementing.
37 protected $testIds = [];
40 * Translated fail message.
44 private $failMessage = '';
47 * Translated pass message.
50 private $passMessage = '';
53 * A valid and recognized permission.
57 protected $validPermission;
60 * An invalid or unrecognized permission.
64 protected $invalidPermission;
66 protected function setUp() {
67 if (!$this->isInChildSite()) {
71 # Make sure that the $test_class variable is defined when this file is included.
75 # Define a function to be able to check that this file was loaded with
77 if (!function_exists('simpletest_test_stub_settings_function')) {
78 function simpletest_test_stub_settings_function() {}
82 file_put_contents($this->siteDirectory . '/' . 'settings.testing.php', $php);
83 // @see \Drupal\system\Tests\DrupalKernel\DrupalKernelSiteTest
90 # Swap out a core service.
91 cache.backend.database:
92 class: Drupal\Core\Cache\MemoryBackendFactory
94 file_put_contents($this->siteDirectory . '/testing.services.yml', $yaml);
96 $original_container = $this->originalContainer;
98 $this->assertNotIdentical(\Drupal::getContainer(), $original_container, 'WebTestBase test creates a new container.');
99 // Create and log in an admin user.
100 $this->drupalLogin($this->drupalCreateUser(['administer unit tests']));
103 // This causes three of the five fails that are asserted in
104 // confirmStubResults().
105 self::$modules = ['non_existent_module'];
111 * Ensures the tests selected through the web interface are run and displayed.
113 public function testWebTestRunner() {
114 $this->passMessage = t('SimpleTest pass.');
115 $this->failMessage = t('SimpleTest fail.');
116 $this->validPermission = 'access administration pages';
117 $this->invalidPermission = 'invalid permission';
119 if ($this->isInChildSite()) {
120 // Only run following code if this test is running itself through a CURL
125 // Run twice so test_ids can be accumulated.
126 for ($i = 0; $i < 2; $i++) {
127 // Run this test from web interface.
128 $this->drupalGet('admin/config/development/testing');
131 $edit['tests[Drupal\simpletest\Tests\SimpleTestTest]'] = TRUE;
132 $this->drupalPostForm(NULL, $edit, t('Run tests'));
134 // Parse results and confirm that they are correct.
135 $this->getTestResults();
136 $this->confirmStubTestResults();
139 // Regression test for #290316.
140 // Check that test_id is incrementing.
141 $this->assertTrue($this->testIds[0] != $this->testIds[1], 'Test ID is incrementing.');
146 * Test to be run and the results confirmed.
148 * Here we force test results which must match the expected results from
149 * confirmStubResults().
151 public function stubTest() {
152 // Ensure the .htkey file exists since this is only created just before a
153 // request. This allows the stub test to make requests. The event does not
154 // fire here and drupal_generate_test_ua() can not generate a key for a
155 // test in a test since the prefix has changed.
156 // @see \Drupal\Core\Test\HttpClientMiddleware\TestHttpClientMiddleware::onBeforeSendRequest()
157 // @see drupal_generate_test_ua();
158 $test_db = new TestDatabase($this->databasePrefix);
159 $key_file = DRUPAL_ROOT . '/' . $test_db->getTestSitePath() . '/.htkey';
160 $private_key = Crypt::randomBytesBase64(55);
161 $site_path = $this->container->get('site.path');
162 file_put_contents($key_file, $private_key);
164 // Check to see if runtime assertions are indeed on, if successful this
165 // will be the first of sixteen passes asserted in confirmStubResults()
167 // Test with minimum possible arguments to make sure no notice for
168 // missing argument is thrown.
170 $this->fail('Runtime assertions are not working.');
172 catch (\AssertionError $e) {
174 // Now test with an error message to ensure it is correctly passed
175 // along by the rethrow.
176 assert(FALSE, 'Lorem Ipsum');
178 catch ( \AssertionError $e ) {
179 $this->assertEqual($e->getMessage(), 'Lorem Ipsum', 'Runtime assertions Enabled and running.');
182 // This causes the second of the sixteen passes asserted in
183 // confirmStubResults().
184 $this->pass($this->passMessage);
186 // The first three fails are caused by enabling a non-existent module in
189 // This causes the fourth of the five fails asserted in
190 // confirmStubResults().
191 $this->fail($this->failMessage);
193 // This causes the third to fifth of the sixteen passes asserted in
194 // confirmStubResults().
195 $user = $this->drupalCreateUser([$this->validPermission], 'SimpleTestTest');
197 // This causes the fifth of the five fails asserted in confirmStubResults().
198 $this->drupalCreateUser([$this->invalidPermission]);
200 // Test logging in as a user.
201 // This causes the sixth to tenth of the sixteen passes asserted in
202 // confirmStubResults().
203 $this->drupalLogin($user);
205 // This causes the eleventh of the sixteen passes asserted in
206 // confirmStubResults().
207 $this->pass(t('Test ID is @id.', ['@id' => $this->testId]));
209 // These cause the twelfth to fifteenth of the sixteen passes asserted in
210 // confirmStubResults().
211 $this->assertTrue(file_exists($site_path . '/settings.testing.php'));
212 // Check the settings.testing.php file got included.
213 $this->assertTrue(function_exists('simpletest_test_stub_settings_function'));
214 // Check that the test-specific service file got loaded.
215 $this->assertTrue($this->container->has('site.service.yml'));
216 $this->assertIdentical(get_class($this->container->get('cache.backend.database')), 'Drupal\Core\Cache\MemoryBackendFactory');
218 // These cause the two exceptions asserted in confirmStubResults().
219 // Call trigger_error() without the required argument to trigger an E_WARNING.
221 // Generates a warning inside a PHP function.
222 array_key_exists(NULL, NULL);
224 // This causes the sixteenth of the sixteen passes asserted in
225 // confirmStubResults().
226 $this->assertNothing();
228 // This causes the debug message asserted in confirmStubResults().
229 debug('Foo', 'Debug', FALSE);
235 public function assertNothing() {
236 $this->pass("This is nothing.");
240 * Confirm that the stub test produced the desired results.
242 public function confirmStubTestResults() {
243 $this->assertAssertion(t('Unable to install modules %modules due to missing modules %missing.', ['%modules' => 'non_existent_module', '%missing' => 'non_existent_module']), 'Other', 'Fail', 'SimpleTestTest.php', 'Drupal\simpletest\Tests\SimpleTestTest->setUp()');
245 $this->assertAssertion($this->passMessage, 'Other', 'Pass', 'SimpleTestTest.php', 'Drupal\simpletest\Tests\SimpleTestTest->stubTest()');
246 $this->assertAssertion($this->failMessage, 'Other', 'Fail', 'SimpleTestTest.php', 'Drupal\simpletest\Tests\SimpleTestTest->stubTest()');
248 $this->assertAssertion(t('Created permissions: @perms', ['@perms' => $this->validPermission]), 'Role', 'Pass', 'SimpleTestTest.php', 'Drupal\simpletest\Tests\SimpleTestTest->stubTest()');
249 $this->assertAssertion(t('Invalid permission %permission.', ['%permission' => $this->invalidPermission]), 'Role', 'Fail', 'SimpleTestTest.php', 'Drupal\simpletest\Tests\SimpleTestTest->stubTest()');
251 // Check that the user was logged in successfully.
252 $this->assertAssertion('User SimpleTestTest successfully logged in.', 'User login', 'Pass', 'SimpleTestTest.php', 'Drupal\simpletest\Tests\SimpleTestTest->stubTest()');
254 // Check that a warning is caught by simpletest. The exact error message
255 // differs between PHP versions so only the function name is checked.
256 $this->assertAssertion('trigger_error()', 'Warning', 'Fail', 'SimpleTestTest.php', 'Drupal\simpletest\Tests\SimpleTestTest->stubTest()');
258 // Check that the backtracing code works for specific assert function.
259 $this->assertAssertion('This is nothing.', 'Other', 'Pass', 'SimpleTestTest.php', 'Drupal\simpletest\Tests\SimpleTestTest->stubTest()');
261 // Check that errors that occur inside PHP internal functions are correctly
262 // reported. The exact error message differs between PHP versions so we
263 // check only the function name 'array_key_exists'.
264 $this->assertAssertion('array_key_exists', 'Warning', 'Fail', 'SimpleTestTest.php', 'Drupal\simpletest\Tests\SimpleTestTest->stubTest()');
266 $this->assertAssertion("Debug: 'Foo'", 'Debug', 'Fail', 'SimpleTestTest.php', 'Drupal\simpletest\Tests\SimpleTestTest->stubTest()');
268 $this->assertEqual('16 passes, 3 fails, 2 exceptions, 3 debug messages', $this->childTestResults['summary']);
270 $this->testIds[] = $test_id = $this->getTestIdFromResults();
271 $this->assertTrue($test_id, 'Found test ID in results.');
275 * Fetch the test id from the test results.
277 public function getTestIdFromResults() {
278 foreach ($this->childTestResults['assertions'] as $assertion) {
279 if (preg_match('@^Test ID is ([0-9]*)\.$@', $assertion['message'], $matches)) {
287 * Asserts that an assertion with specified values is displayed in results.
289 * @param string $message
291 * @param string $type
293 * @param string $status
295 * @param string $file
296 * File where the assertion originated.
297 * @param string $function
298 * Function where the assertion originated.
300 * @return Assertion result.
302 public function assertAssertion($message, $type, $status, $file, $function) {
303 $message = trim(strip_tags($message));
305 foreach ($this->childTestResults['assertions'] as $assertion) {
306 if ((strpos($assertion['message'], $message) !== FALSE) &&
307 $assertion['type'] == $type &&
308 $assertion['status'] == $status &&
309 $assertion['file'] == $file &&
310 $assertion['function'] == $function) {
315 return $this->assertTrue($found, format_string('Found assertion {"@message", "@type", "@status", "@file", "@function"}.', ['@message' => $message, '@type' => $type, '@status' => $status, "@file" => $file, "@function" => $function]));
319 * Get the results from a test and store them in the class array $results.
321 public function getTestResults() {
323 if ($this->parse()) {
324 if ($details = $this->getResultFieldSet()) {
325 // Code assumes this is the only test in group.
326 $results['summary'] = $this->asText($details->div->div[1]);
327 $results['name'] = $this->asText($details->summary);
329 $results['assertions'] = [];
330 $tbody = $details->div->table->tbody;
331 foreach ($tbody->tr as $row) {
333 $assertion['message'] = $this->asText($row->td[0]);
334 $assertion['type'] = $this->asText($row->td[1]);
335 $assertion['file'] = $this->asText($row->td[2]);
336 $assertion['line'] = $this->asText($row->td[3]);
337 $assertion['function'] = $this->asText($row->td[4]);
338 $ok_url = file_url_transform_relative(file_create_url('core/misc/icons/73b355/check.svg'));
339 $assertion['status'] = ($row->td[5]->img['src'] == $ok_url) ? 'Pass' : 'Fail';
340 $results['assertions'][] = $assertion;
344 $this->childTestResults = $results;
348 * Get the details containing the results for group this test is in.
350 public function getResultFieldSet() {
351 $all_details = $this->xpath('//details');
352 foreach ($all_details as $details) {
353 if ($this->asText($details->summary) == __CLASS__) {
361 * Extract the text contained by the element.
364 * Element to extract text from.
369 public function asText(\SimpleXMLElement $element) {
370 if (!is_object($element)) {
371 return $this->fail('The element is not an element.');
373 return trim(html_entity_decode(strip_tags($element->asXML())));