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

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
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\Component\Utility\Crypt;
Chris@0 6 use Drupal\Core\Test\TestDatabase;
Chris@0 7 use Drupal\simpletest\WebTestBase;
Chris@0 8
Chris@0 9 /**
Chris@0 10 * Tests SimpleTest's web interface: check that the intended tests were run and
Chris@0 11 * ensure that test reports display the intended results. Also test SimpleTest's
Chris@0 12 * internal browser and APIs implicitly.
Chris@0 13 *
Chris@0 14 * @group simpletest
Chris@18 15 * @group WebTestBase
Chris@0 16 */
Chris@0 17 class SimpleTestTest extends WebTestBase {
Chris@0 18
Chris@0 19 /**
Chris@0 20 * Modules to enable.
Chris@0 21 *
Chris@0 22 * @var array
Chris@0 23 */
Chris@0 24 public static $modules = ['simpletest'];
Chris@0 25
Chris@0 26 /**
Chris@0 27 * The results array that has been parsed by getTestResults().
Chris@0 28 *
Chris@0 29 * @var array
Chris@0 30 */
Chris@0 31 protected $childTestResults;
Chris@0 32
Chris@0 33 /**
Chris@0 34 * Stores the test ID from each test run for comparison.
Chris@0 35 *
Chris@0 36 * Used to ensure they are incrementing.
Chris@0 37 */
Chris@0 38 protected $testIds = [];
Chris@0 39
Chris@0 40 /**
Chris@0 41 * Translated fail message.
Chris@0 42 *
Chris@0 43 * @var string
Chris@0 44 */
Chris@0 45 private $failMessage = '';
Chris@0 46
Chris@0 47 /**
Chris@0 48 * Translated pass message.
Chris@0 49 * @var string
Chris@0 50 */
Chris@0 51 private $passMessage = '';
Chris@0 52
Chris@0 53 /**
Chris@0 54 * A valid and recognized permission.
Chris@0 55 *
Chris@0 56 * @var string
Chris@0 57 */
Chris@0 58 protected $validPermission;
Chris@0 59
Chris@0 60 /**
Chris@0 61 * An invalid or unrecognized permission.
Chris@0 62 *
Chris@0 63 * @var string
Chris@0 64 */
Chris@0 65 protected $invalidPermission;
Chris@0 66
Chris@0 67 protected function setUp() {
Chris@0 68 if (!$this->isInChildSite()) {
Chris@0 69 $php = <<<'EOD'
Chris@0 70 <?php
Chris@0 71
Chris@0 72 # Make sure that the $test_class variable is defined when this file is included.
Chris@0 73 if ($test_class) {
Chris@0 74 }
Chris@0 75
Chris@0 76 # Define a function to be able to check that this file was loaded with
Chris@0 77 # function_exists().
Chris@0 78 if (!function_exists('simpletest_test_stub_settings_function')) {
Chris@0 79 function simpletest_test_stub_settings_function() {}
Chris@0 80 }
Chris@0 81 EOD;
Chris@0 82
Chris@0 83 file_put_contents($this->siteDirectory . '/' . 'settings.testing.php', $php);
Chris@0 84 // @see \Drupal\system\Tests\DrupalKernel\DrupalKernelSiteTest
Chris@0 85 $class = __CLASS__;
Chris@0 86 $yaml = <<<EOD
Chris@0 87 services:
Chris@0 88 # Add a new service.
Chris@0 89 site.service.yml:
Chris@0 90 class: $class
Chris@0 91 # Swap out a core service.
Chris@0 92 cache.backend.database:
Chris@0 93 class: Drupal\Core\Cache\MemoryBackendFactory
Chris@0 94 EOD;
Chris@0 95 file_put_contents($this->siteDirectory . '/testing.services.yml', $yaml);
Chris@0 96
Chris@0 97 $original_container = $this->originalContainer;
Chris@0 98 parent::setUp();
Chris@0 99 $this->assertNotIdentical(\Drupal::getContainer(), $original_container, 'WebTestBase test creates a new container.');
Chris@0 100 // Create and log in an admin user.
Chris@0 101 $this->drupalLogin($this->drupalCreateUser(['administer unit tests']));
Chris@0 102 }
Chris@0 103 else {
Chris@0 104 // This causes three of the five fails that are asserted in
Chris@0 105 // confirmStubResults().
Chris@0 106 self::$modules = ['non_existent_module'];
Chris@0 107 parent::setUp();
Chris@0 108 }
Chris@0 109 }
Chris@0 110
Chris@0 111 /**
Chris@0 112 * Ensures the tests selected through the web interface are run and displayed.
Chris@0 113 */
Chris@0 114 public function testWebTestRunner() {
Chris@0 115 $this->passMessage = t('SimpleTest pass.');
Chris@0 116 $this->failMessage = t('SimpleTest fail.');
Chris@0 117 $this->validPermission = 'access administration pages';
Chris@0 118 $this->invalidPermission = 'invalid permission';
Chris@0 119
Chris@0 120 if ($this->isInChildSite()) {
Chris@0 121 // Only run following code if this test is running itself through a CURL
Chris@0 122 // request.
Chris@0 123 $this->stubTest();
Chris@0 124 }
Chris@0 125 else {
Chris@0 126 // Run twice so test_ids can be accumulated.
Chris@0 127 for ($i = 0; $i < 2; $i++) {
Chris@0 128 // Run this test from web interface.
Chris@0 129 $this->drupalGet('admin/config/development/testing');
Chris@0 130
Chris@0 131 $edit = [];
Chris@0 132 $edit['tests[Drupal\simpletest\Tests\SimpleTestTest]'] = TRUE;
Chris@0 133 $this->drupalPostForm(NULL, $edit, t('Run tests'));
Chris@0 134
Chris@0 135 // Parse results and confirm that they are correct.
Chris@0 136 $this->getTestResults();
Chris@0 137 $this->confirmStubTestResults();
Chris@0 138 }
Chris@0 139
Chris@0 140 // Regression test for #290316.
Chris@0 141 // Check that test_id is incrementing.
Chris@0 142 $this->assertTrue($this->testIds[0] != $this->testIds[1], 'Test ID is incrementing.');
Chris@0 143 }
Chris@0 144 }
Chris@0 145
Chris@0 146 /**
Chris@0 147 * Test to be run and the results confirmed.
Chris@0 148 *
Chris@0 149 * Here we force test results which must match the expected results from
Chris@0 150 * confirmStubResults().
Chris@0 151 */
Chris@0 152 public function stubTest() {
Chris@0 153 // Ensure the .htkey file exists since this is only created just before a
Chris@0 154 // request. This allows the stub test to make requests. The event does not
Chris@0 155 // fire here and drupal_generate_test_ua() can not generate a key for a
Chris@0 156 // test in a test since the prefix has changed.
Chris@0 157 // @see \Drupal\Core\Test\HttpClientMiddleware\TestHttpClientMiddleware::onBeforeSendRequest()
Chris@0 158 // @see drupal_generate_test_ua();
Chris@0 159 $test_db = new TestDatabase($this->databasePrefix);
Chris@0 160 $key_file = DRUPAL_ROOT . '/' . $test_db->getTestSitePath() . '/.htkey';
Chris@0 161 $private_key = Crypt::randomBytesBase64(55);
Chris@0 162 $site_path = $this->container->get('site.path');
Chris@0 163 file_put_contents($key_file, $private_key);
Chris@0 164
Chris@0 165 // Check to see if runtime assertions are indeed on, if successful this
Chris@0 166 // will be the first of sixteen passes asserted in confirmStubResults()
Chris@0 167 try {
Chris@0 168 // Test with minimum possible arguments to make sure no notice for
Chris@0 169 // missing argument is thrown.
Chris@0 170 assert(FALSE);
Chris@0 171 $this->fail('Runtime assertions are not working.');
Chris@0 172 }
Chris@0 173 catch (\AssertionError $e) {
Chris@0 174 try {
Chris@0 175 // Now test with an error message to ensure it is correctly passed
Chris@0 176 // along by the rethrow.
Chris@0 177 assert(FALSE, 'Lorem Ipsum');
Chris@0 178 }
Chris@0 179 catch (\AssertionError $e) {
Chris@0 180 $this->assertEqual($e->getMessage(), 'Lorem Ipsum', 'Runtime assertions Enabled and running.');
Chris@0 181 }
Chris@0 182 }
Chris@0 183 // This causes the second of the sixteen passes asserted in
Chris@0 184 // confirmStubResults().
Chris@0 185 $this->pass($this->passMessage);
Chris@0 186
Chris@0 187 // The first three fails are caused by enabling a non-existent module in
Chris@0 188 // setUp().
Chris@0 189
Chris@0 190 // This causes the fourth of the five fails asserted in
Chris@0 191 // confirmStubResults().
Chris@0 192 $this->fail($this->failMessage);
Chris@0 193
Chris@0 194 // This causes the third to fifth of the sixteen passes asserted in
Chris@0 195 // confirmStubResults().
Chris@0 196 $user = $this->drupalCreateUser([$this->validPermission], 'SimpleTestTest');
Chris@0 197
Chris@0 198 // This causes the fifth of the five fails asserted in confirmStubResults().
Chris@0 199 $this->drupalCreateUser([$this->invalidPermission]);
Chris@0 200
Chris@0 201 // Test logging in as a user.
Chris@0 202 // This causes the sixth to tenth of the sixteen passes asserted in
Chris@0 203 // confirmStubResults().
Chris@0 204 $this->drupalLogin($user);
Chris@0 205
Chris@0 206 // This causes the eleventh of the sixteen passes asserted in
Chris@0 207 // confirmStubResults().
Chris@0 208 $this->pass(t('Test ID is @id.', ['@id' => $this->testId]));
Chris@0 209
Chris@0 210 // These cause the twelfth to fifteenth of the sixteen passes asserted in
Chris@0 211 // confirmStubResults().
Chris@0 212 $this->assertTrue(file_exists($site_path . '/settings.testing.php'));
Chris@0 213 // Check the settings.testing.php file got included.
Chris@0 214 $this->assertTrue(function_exists('simpletest_test_stub_settings_function'));
Chris@0 215 // Check that the test-specific service file got loaded.
Chris@0 216 $this->assertTrue($this->container->has('site.service.yml'));
Chris@0 217 $this->assertIdentical(get_class($this->container->get('cache.backend.database')), 'Drupal\Core\Cache\MemoryBackendFactory');
Chris@0 218
Chris@0 219 // These cause the two exceptions asserted in confirmStubResults().
Chris@0 220 // Call trigger_error() without the required argument to trigger an E_WARNING.
Chris@0 221 trigger_error();
Chris@0 222 // Generates a warning inside a PHP function.
Chris@0 223 array_key_exists(NULL, NULL);
Chris@0 224
Chris@0 225 // This causes the sixteenth of the sixteen passes asserted in
Chris@0 226 // confirmStubResults().
Chris@0 227 $this->assertNothing();
Chris@0 228
Chris@0 229 // This causes the debug message asserted in confirmStubResults().
Chris@0 230 debug('Foo', 'Debug', FALSE);
Chris@0 231 }
Chris@0 232
Chris@0 233 /**
Chris@0 234 * Assert nothing.
Chris@0 235 */
Chris@0 236 public function assertNothing() {
Chris@0 237 $this->pass("This is nothing.");
Chris@0 238 }
Chris@0 239
Chris@0 240 /**
Chris@0 241 * Confirm that the stub test produced the desired results.
Chris@0 242 */
Chris@0 243 public function confirmStubTestResults() {
Chris@0 244 $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()');
Chris@0 245
Chris@0 246 $this->assertAssertion($this->passMessage, 'Other', 'Pass', 'SimpleTestTest.php', 'Drupal\simpletest\Tests\SimpleTestTest->stubTest()');
Chris@0 247 $this->assertAssertion($this->failMessage, 'Other', 'Fail', 'SimpleTestTest.php', 'Drupal\simpletest\Tests\SimpleTestTest->stubTest()');
Chris@0 248
Chris@0 249 $this->assertAssertion(t('Created permissions: @perms', ['@perms' => $this->validPermission]), 'Role', 'Pass', 'SimpleTestTest.php', 'Drupal\simpletest\Tests\SimpleTestTest->stubTest()');
Chris@0 250 $this->assertAssertion(t('Invalid permission %permission.', ['%permission' => $this->invalidPermission]), 'Role', 'Fail', 'SimpleTestTest.php', 'Drupal\simpletest\Tests\SimpleTestTest->stubTest()');
Chris@0 251
Chris@0 252 // Check that the user was logged in successfully.
Chris@0 253 $this->assertAssertion('User SimpleTestTest successfully logged in.', 'User login', 'Pass', 'SimpleTestTest.php', 'Drupal\simpletest\Tests\SimpleTestTest->stubTest()');
Chris@0 254
Chris@0 255 // Check that a warning is caught by simpletest. The exact error message
Chris@0 256 // differs between PHP versions so only the function name is checked.
Chris@0 257 $this->assertAssertion('trigger_error()', 'Warning', 'Fail', 'SimpleTestTest.php', 'Drupal\simpletest\Tests\SimpleTestTest->stubTest()');
Chris@0 258
Chris@0 259 // Check that the backtracing code works for specific assert function.
Chris@0 260 $this->assertAssertion('This is nothing.', 'Other', 'Pass', 'SimpleTestTest.php', 'Drupal\simpletest\Tests\SimpleTestTest->stubTest()');
Chris@0 261
Chris@0 262 // Check that errors that occur inside PHP internal functions are correctly
Chris@0 263 // reported. The exact error message differs between PHP versions so we
Chris@0 264 // check only the function name 'array_key_exists'.
Chris@0 265 $this->assertAssertion('array_key_exists', 'Warning', 'Fail', 'SimpleTestTest.php', 'Drupal\simpletest\Tests\SimpleTestTest->stubTest()');
Chris@0 266
Chris@0 267 $this->assertAssertion("Debug: 'Foo'", 'Debug', 'Fail', 'SimpleTestTest.php', 'Drupal\simpletest\Tests\SimpleTestTest->stubTest()');
Chris@0 268
Chris@0 269 $this->assertEqual('16 passes, 3 fails, 2 exceptions, 3 debug messages', $this->childTestResults['summary']);
Chris@0 270
Chris@0 271 $this->testIds[] = $test_id = $this->getTestIdFromResults();
Chris@0 272 $this->assertTrue($test_id, 'Found test ID in results.');
Chris@0 273 }
Chris@0 274
Chris@0 275 /**
Chris@0 276 * Fetch the test id from the test results.
Chris@0 277 */
Chris@0 278 public function getTestIdFromResults() {
Chris@0 279 foreach ($this->childTestResults['assertions'] as $assertion) {
Chris@0 280 if (preg_match('@^Test ID is ([0-9]*)\.$@', $assertion['message'], $matches)) {
Chris@0 281 return $matches[1];
Chris@0 282 }
Chris@0 283 }
Chris@0 284 return NULL;
Chris@0 285 }
Chris@0 286
Chris@0 287 /**
Chris@0 288 * Asserts that an assertion with specified values is displayed in results.
Chris@0 289 *
Chris@0 290 * @param string $message
Chris@0 291 * Assertion message.
Chris@0 292 * @param string $type
Chris@0 293 * Assertion type.
Chris@0 294 * @param string $status
Chris@0 295 * Assertion status.
Chris@0 296 * @param string $file
Chris@0 297 * File where the assertion originated.
Chris@0 298 * @param string $function
Chris@0 299 * Function where the assertion originated.
Chris@0 300 *
Chris@0 301 * @return Assertion result.
Chris@0 302 */
Chris@0 303 public function assertAssertion($message, $type, $status, $file, $function) {
Chris@0 304 $message = trim(strip_tags($message));
Chris@0 305 $found = FALSE;
Chris@0 306 foreach ($this->childTestResults['assertions'] as $assertion) {
Chris@0 307 if ((strpos($assertion['message'], $message) !== FALSE) &&
Chris@0 308 $assertion['type'] == $type &&
Chris@0 309 $assertion['status'] == $status &&
Chris@0 310 $assertion['file'] == $file &&
Chris@0 311 $assertion['function'] == $function) {
Chris@0 312 $found = TRUE;
Chris@0 313 break;
Chris@0 314 }
Chris@0 315 }
Chris@0 316 return $this->assertTrue($found, format_string('Found assertion {"@message", "@type", "@status", "@file", "@function"}.', ['@message' => $message, '@type' => $type, '@status' => $status, "@file" => $file, "@function" => $function]));
Chris@0 317 }
Chris@0 318
Chris@0 319 /**
Chris@0 320 * Get the results from a test and store them in the class array $results.
Chris@0 321 */
Chris@0 322 public function getTestResults() {
Chris@0 323 $results = [];
Chris@0 324 if ($this->parse()) {
Chris@0 325 if ($details = $this->getResultFieldSet()) {
Chris@0 326 // Code assumes this is the only test in group.
Chris@0 327 $results['summary'] = $this->asText($details->div->div[1]);
Chris@0 328 $results['name'] = $this->asText($details->summary);
Chris@0 329
Chris@0 330 $results['assertions'] = [];
Chris@0 331 $tbody = $details->div->table->tbody;
Chris@0 332 foreach ($tbody->tr as $row) {
Chris@0 333 $assertion = [];
Chris@0 334 $assertion['message'] = $this->asText($row->td[0]);
Chris@0 335 $assertion['type'] = $this->asText($row->td[1]);
Chris@0 336 $assertion['file'] = $this->asText($row->td[2]);
Chris@0 337 $assertion['line'] = $this->asText($row->td[3]);
Chris@0 338 $assertion['function'] = $this->asText($row->td[4]);
Chris@0 339 $ok_url = file_url_transform_relative(file_create_url('core/misc/icons/73b355/check.svg'));
Chris@0 340 $assertion['status'] = ($row->td[5]->img['src'] == $ok_url) ? 'Pass' : 'Fail';
Chris@0 341 $results['assertions'][] = $assertion;
Chris@0 342 }
Chris@0 343 }
Chris@0 344 }
Chris@0 345 $this->childTestResults = $results;
Chris@0 346 }
Chris@0 347
Chris@0 348 /**
Chris@0 349 * Get the details containing the results for group this test is in.
Chris@0 350 */
Chris@0 351 public function getResultFieldSet() {
Chris@0 352 $all_details = $this->xpath('//details');
Chris@0 353 foreach ($all_details as $details) {
Chris@0 354 if ($this->asText($details->summary) == __CLASS__) {
Chris@0 355 return $details;
Chris@0 356 }
Chris@0 357 }
Chris@0 358 return FALSE;
Chris@0 359 }
Chris@0 360
Chris@0 361 /**
Chris@0 362 * Extract the text contained by the element.
Chris@0 363 *
Chris@0 364 * @param $element
Chris@0 365 * Element to extract text from.
Chris@0 366 *
Chris@0 367 * @return
Chris@0 368 * Extracted text.
Chris@0 369 */
Chris@0 370 public function asText(\SimpleXMLElement $element) {
Chris@0 371 if (!is_object($element)) {
Chris@0 372 return $this->fail('The element is not an element.');
Chris@0 373 }
Chris@0 374 return trim(html_entity_decode(strip_tags($element->asXML())));
Chris@0 375 }
Chris@0 376
Chris@0 377 }