Chris@0: isInChildSite()) { Chris@0: $php = <<<'EOD' Chris@0: siteDirectory . '/' . 'settings.testing.php', $php); Chris@0: // @see \Drupal\system\Tests\DrupalKernel\DrupalKernelSiteTest Chris@0: $class = __CLASS__; Chris@0: $yaml = <<siteDirectory . '/testing.services.yml', $yaml); Chris@0: Chris@0: $original_container = $this->originalContainer; Chris@0: parent::setUp(); Chris@0: $this->assertNotIdentical(\Drupal::getContainer(), $original_container, 'WebTestBase test creates a new container.'); Chris@0: // Create and log in an admin user. Chris@0: $this->drupalLogin($this->drupalCreateUser(['administer unit tests'])); Chris@0: } Chris@0: else { Chris@0: // This causes three of the five fails that are asserted in Chris@0: // confirmStubResults(). Chris@0: self::$modules = ['non_existent_module']; Chris@0: parent::setUp(); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Ensures the tests selected through the web interface are run and displayed. Chris@0: */ Chris@0: public function testWebTestRunner() { Chris@0: $this->passMessage = t('SimpleTest pass.'); Chris@0: $this->failMessage = t('SimpleTest fail.'); Chris@0: $this->validPermission = 'access administration pages'; Chris@0: $this->invalidPermission = 'invalid permission'; Chris@0: Chris@0: if ($this->isInChildSite()) { Chris@0: // Only run following code if this test is running itself through a CURL Chris@0: // request. Chris@0: $this->stubTest(); Chris@0: } Chris@0: else { Chris@0: // Run twice so test_ids can be accumulated. Chris@0: for ($i = 0; $i < 2; $i++) { Chris@0: // Run this test from web interface. Chris@0: $this->drupalGet('admin/config/development/testing'); Chris@0: Chris@0: $edit = []; Chris@0: $edit['tests[Drupal\simpletest\Tests\SimpleTestTest]'] = TRUE; Chris@0: $this->drupalPostForm(NULL, $edit, t('Run tests')); Chris@0: Chris@0: // Parse results and confirm that they are correct. Chris@0: $this->getTestResults(); Chris@0: $this->confirmStubTestResults(); Chris@0: } Chris@0: Chris@0: // Regression test for #290316. Chris@0: // Check that test_id is incrementing. Chris@0: $this->assertTrue($this->testIds[0] != $this->testIds[1], 'Test ID is incrementing.'); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test to be run and the results confirmed. Chris@0: * Chris@0: * Here we force test results which must match the expected results from Chris@0: * confirmStubResults(). Chris@0: */ Chris@0: public function stubTest() { Chris@0: // Ensure the .htkey file exists since this is only created just before a Chris@0: // request. This allows the stub test to make requests. The event does not Chris@0: // fire here and drupal_generate_test_ua() can not generate a key for a Chris@0: // test in a test since the prefix has changed. Chris@0: // @see \Drupal\Core\Test\HttpClientMiddleware\TestHttpClientMiddleware::onBeforeSendRequest() Chris@0: // @see drupal_generate_test_ua(); Chris@0: $test_db = new TestDatabase($this->databasePrefix); Chris@0: $key_file = DRUPAL_ROOT . '/' . $test_db->getTestSitePath() . '/.htkey'; Chris@0: $private_key = Crypt::randomBytesBase64(55); Chris@0: $site_path = $this->container->get('site.path'); Chris@0: file_put_contents($key_file, $private_key); Chris@0: Chris@0: // Check to see if runtime assertions are indeed on, if successful this Chris@0: // will be the first of sixteen passes asserted in confirmStubResults() Chris@0: try { Chris@0: // Test with minimum possible arguments to make sure no notice for Chris@0: // missing argument is thrown. Chris@0: assert(FALSE); Chris@0: $this->fail('Runtime assertions are not working.'); Chris@0: } Chris@0: catch (\AssertionError $e) { Chris@0: try { Chris@0: // Now test with an error message to ensure it is correctly passed Chris@0: // along by the rethrow. Chris@0: assert(FALSE, 'Lorem Ipsum'); Chris@0: } Chris@0: catch (\AssertionError $e) { Chris@0: $this->assertEqual($e->getMessage(), 'Lorem Ipsum', 'Runtime assertions Enabled and running.'); Chris@0: } Chris@0: } Chris@0: // This causes the second of the sixteen passes asserted in Chris@0: // confirmStubResults(). Chris@0: $this->pass($this->passMessage); Chris@0: Chris@0: // The first three fails are caused by enabling a non-existent module in Chris@0: // setUp(). Chris@0: Chris@0: // This causes the fourth of the five fails asserted in Chris@0: // confirmStubResults(). Chris@0: $this->fail($this->failMessage); Chris@0: Chris@0: // This causes the third to fifth of the sixteen passes asserted in Chris@0: // confirmStubResults(). Chris@0: $user = $this->drupalCreateUser([$this->validPermission], 'SimpleTestTest'); Chris@0: Chris@0: // This causes the fifth of the five fails asserted in confirmStubResults(). Chris@0: $this->drupalCreateUser([$this->invalidPermission]); Chris@0: Chris@0: // Test logging in as a user. Chris@0: // This causes the sixth to tenth of the sixteen passes asserted in Chris@0: // confirmStubResults(). Chris@0: $this->drupalLogin($user); Chris@0: Chris@0: // This causes the eleventh of the sixteen passes asserted in Chris@0: // confirmStubResults(). Chris@0: $this->pass(t('Test ID is @id.', ['@id' => $this->testId])); Chris@0: Chris@0: // These cause the twelfth to fifteenth of the sixteen passes asserted in Chris@0: // confirmStubResults(). Chris@0: $this->assertTrue(file_exists($site_path . '/settings.testing.php')); Chris@0: // Check the settings.testing.php file got included. Chris@0: $this->assertTrue(function_exists('simpletest_test_stub_settings_function')); Chris@0: // Check that the test-specific service file got loaded. Chris@0: $this->assertTrue($this->container->has('site.service.yml')); Chris@0: $this->assertIdentical(get_class($this->container->get('cache.backend.database')), 'Drupal\Core\Cache\MemoryBackendFactory'); Chris@0: Chris@0: // These cause the two exceptions asserted in confirmStubResults(). Chris@0: // Call trigger_error() without the required argument to trigger an E_WARNING. Chris@0: trigger_error(); Chris@0: // Generates a warning inside a PHP function. Chris@0: array_key_exists(NULL, NULL); Chris@0: Chris@0: // This causes the sixteenth of the sixteen passes asserted in Chris@0: // confirmStubResults(). Chris@0: $this->assertNothing(); Chris@0: Chris@0: // This causes the debug message asserted in confirmStubResults(). Chris@0: debug('Foo', 'Debug', FALSE); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Assert nothing. Chris@0: */ Chris@0: public function assertNothing() { Chris@0: $this->pass("This is nothing."); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Confirm that the stub test produced the desired results. Chris@0: */ Chris@0: public function confirmStubTestResults() { Chris@0: $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: Chris@0: $this->assertAssertion($this->passMessage, 'Other', 'Pass', 'SimpleTestTest.php', 'Drupal\simpletest\Tests\SimpleTestTest->stubTest()'); Chris@0: $this->assertAssertion($this->failMessage, 'Other', 'Fail', 'SimpleTestTest.php', 'Drupal\simpletest\Tests\SimpleTestTest->stubTest()'); Chris@0: Chris@0: $this->assertAssertion(t('Created permissions: @perms', ['@perms' => $this->validPermission]), 'Role', 'Pass', 'SimpleTestTest.php', 'Drupal\simpletest\Tests\SimpleTestTest->stubTest()'); Chris@0: $this->assertAssertion(t('Invalid permission %permission.', ['%permission' => $this->invalidPermission]), 'Role', 'Fail', 'SimpleTestTest.php', 'Drupal\simpletest\Tests\SimpleTestTest->stubTest()'); Chris@0: Chris@0: // Check that the user was logged in successfully. Chris@0: $this->assertAssertion('User SimpleTestTest successfully logged in.', 'User login', 'Pass', 'SimpleTestTest.php', 'Drupal\simpletest\Tests\SimpleTestTest->stubTest()'); Chris@0: Chris@0: // Check that a warning is caught by simpletest. The exact error message Chris@0: // differs between PHP versions so only the function name is checked. Chris@0: $this->assertAssertion('trigger_error()', 'Warning', 'Fail', 'SimpleTestTest.php', 'Drupal\simpletest\Tests\SimpleTestTest->stubTest()'); Chris@0: Chris@0: // Check that the backtracing code works for specific assert function. Chris@0: $this->assertAssertion('This is nothing.', 'Other', 'Pass', 'SimpleTestTest.php', 'Drupal\simpletest\Tests\SimpleTestTest->stubTest()'); Chris@0: Chris@0: // Check that errors that occur inside PHP internal functions are correctly Chris@0: // reported. The exact error message differs between PHP versions so we Chris@0: // check only the function name 'array_key_exists'. Chris@0: $this->assertAssertion('array_key_exists', 'Warning', 'Fail', 'SimpleTestTest.php', 'Drupal\simpletest\Tests\SimpleTestTest->stubTest()'); Chris@0: Chris@0: $this->assertAssertion("Debug: 'Foo'", 'Debug', 'Fail', 'SimpleTestTest.php', 'Drupal\simpletest\Tests\SimpleTestTest->stubTest()'); Chris@0: Chris@0: $this->assertEqual('16 passes, 3 fails, 2 exceptions, 3 debug messages', $this->childTestResults['summary']); Chris@0: Chris@0: $this->testIds[] = $test_id = $this->getTestIdFromResults(); Chris@0: $this->assertTrue($test_id, 'Found test ID in results.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Fetch the test id from the test results. Chris@0: */ Chris@0: public function getTestIdFromResults() { Chris@0: foreach ($this->childTestResults['assertions'] as $assertion) { Chris@0: if (preg_match('@^Test ID is ([0-9]*)\.$@', $assertion['message'], $matches)) { Chris@0: return $matches[1]; Chris@0: } Chris@0: } Chris@0: return NULL; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Asserts that an assertion with specified values is displayed in results. Chris@0: * Chris@0: * @param string $message Chris@0: * Assertion message. Chris@0: * @param string $type Chris@0: * Assertion type. Chris@0: * @param string $status Chris@0: * Assertion status. Chris@0: * @param string $file Chris@0: * File where the assertion originated. Chris@0: * @param string $function Chris@0: * Function where the assertion originated. Chris@0: * Chris@0: * @return Assertion result. Chris@0: */ Chris@0: public function assertAssertion($message, $type, $status, $file, $function) { Chris@0: $message = trim(strip_tags($message)); Chris@0: $found = FALSE; Chris@0: foreach ($this->childTestResults['assertions'] as $assertion) { Chris@0: if ((strpos($assertion['message'], $message) !== FALSE) && Chris@0: $assertion['type'] == $type && Chris@0: $assertion['status'] == $status && Chris@0: $assertion['file'] == $file && Chris@0: $assertion['function'] == $function) { Chris@0: $found = TRUE; Chris@0: break; Chris@0: } Chris@0: } Chris@0: 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: } Chris@0: Chris@0: /** Chris@0: * Get the results from a test and store them in the class array $results. Chris@0: */ Chris@0: public function getTestResults() { Chris@0: $results = []; Chris@0: if ($this->parse()) { Chris@0: if ($details = $this->getResultFieldSet()) { Chris@0: // Code assumes this is the only test in group. Chris@0: $results['summary'] = $this->asText($details->div->div[1]); Chris@0: $results['name'] = $this->asText($details->summary); Chris@0: Chris@0: $results['assertions'] = []; Chris@0: $tbody = $details->div->table->tbody; Chris@0: foreach ($tbody->tr as $row) { Chris@0: $assertion = []; Chris@0: $assertion['message'] = $this->asText($row->td[0]); Chris@0: $assertion['type'] = $this->asText($row->td[1]); Chris@0: $assertion['file'] = $this->asText($row->td[2]); Chris@0: $assertion['line'] = $this->asText($row->td[3]); Chris@0: $assertion['function'] = $this->asText($row->td[4]); Chris@0: $ok_url = file_url_transform_relative(file_create_url('core/misc/icons/73b355/check.svg')); Chris@0: $assertion['status'] = ($row->td[5]->img['src'] == $ok_url) ? 'Pass' : 'Fail'; Chris@0: $results['assertions'][] = $assertion; Chris@0: } Chris@0: } Chris@0: } Chris@0: $this->childTestResults = $results; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Get the details containing the results for group this test is in. Chris@0: */ Chris@0: public function getResultFieldSet() { Chris@0: $all_details = $this->xpath('//details'); Chris@0: foreach ($all_details as $details) { Chris@0: if ($this->asText($details->summary) == __CLASS__) { Chris@0: return $details; Chris@0: } Chris@0: } Chris@0: return FALSE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Extract the text contained by the element. Chris@0: * Chris@0: * @param $element Chris@0: * Element to extract text from. Chris@0: * Chris@0: * @return Chris@0: * Extracted text. Chris@0: */ Chris@0: public function asText(\SimpleXMLElement $element) { Chris@0: if (!is_object($element)) { Chris@0: return $this->fail('The element is not an element.'); Chris@0: } Chris@0: return trim(html_entity_decode(strip_tags($element->asXML()))); Chris@0: } Chris@0: Chris@0: }