View source
<?php
namespace Drupal\simpletest\Tests;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Utility\Crypt;
use Drupal\Core\Test\TestDatabase;
use Drupal\simpletest\WebTestBase;
class SimpleTestTest extends WebTestBase {
public static $modules = [
'simpletest',
];
protected $childTestResults;
protected $testIds = [];
private $failMessage = '';
private $passMessage = '';
protected $validPermission;
protected $invalidPermission;
protected function setUp() {
if (!$this
->isInChildSite()) {
$php = <<<'EOD'
<?php
# Make sure that the $test_class variable is defined when this file is included.
if ($test_class) {
}
# Define a function to be able to check that this file was loaded with
# function_exists().
if (!function_exists('simpletest_test_stub_settings_function')) {
function simpletest_test_stub_settings_function() {}
}
EOD;
file_put_contents($this->siteDirectory . '/' . 'settings.testing.php', $php);
$class = __CLASS__;
$yaml = <<<EOD
services:
# Add a new service.
site.service.yml:
class: {<span class="php-variable">$class</span>}
# Swap out a core service.
cache.backend.database:
class: Drupal\\Core\\Cache\\MemoryBackendFactory
EOD;
file_put_contents($this->siteDirectory . '/testing.services.yml', $yaml);
$original_container = $this->originalContainer;
parent::setUp();
$this
->assertNotIdentical(\Drupal::getContainer(), $original_container, 'WebTestBase test creates a new container.');
$this
->drupalLogin($this
->drupalCreateUser([
'administer unit tests',
]));
}
else {
self::$modules = [
'non_existent_module',
];
parent::setUp();
}
}
public function testWebTestRunner() {
$this->passMessage = t('SimpleTest pass.');
$this->failMessage = t('SimpleTest fail.');
$this->validPermission = 'access administration pages';
$this->invalidPermission = 'invalid permission';
if ($this
->isInChildSite()) {
$this
->stubTest();
}
else {
for ($i = 0; $i < 2; $i++) {
$this
->drupalGet('admin/config/development/testing');
$edit = [];
$edit['tests[Drupal\\simpletest\\Tests\\SimpleTestTest]'] = TRUE;
$this
->drupalPostForm(NULL, $edit, t('Run tests'));
$this
->getTestResults();
$this
->confirmStubTestResults();
}
$this
->assertTrue($this->testIds[0] != $this->testIds[1], 'Test ID is incrementing.');
}
}
public function stubTest() {
$test_db = new TestDatabase($this->databasePrefix);
$key_file = DRUPAL_ROOT . '/' . $test_db
->getTestSitePath() . '/.htkey';
$private_key = Crypt::randomBytesBase64(55);
$site_path = $this->container
->get('site.path');
file_put_contents($key_file, $private_key);
try {
assert(FALSE);
$this
->fail('Runtime assertions are not working.');
} catch (\AssertionError $e) {
try {
assert(FALSE, 'Lorem Ipsum');
} catch (\AssertionError $e) {
$this
->assertEqual($e
->getMessage(), 'Lorem Ipsum', 'Runtime assertions Enabled and running.');
}
}
$this
->pass($this->passMessage);
$this
->fail($this->failMessage);
$user = $this
->drupalCreateUser([
$this->validPermission,
], 'SimpleTestTest');
$this
->drupalCreateUser([
$this->invalidPermission,
]);
$this
->drupalLogin($user);
$this
->pass('Test ID is ' . $this->testId . '.');
$this
->assertTrue(file_exists($site_path . '/settings.testing.php'));
$this
->assertTrue(function_exists('simpletest_test_stub_settings_function'));
$this
->assertTrue($this->container
->has('site.service.yml'));
$this
->assertIdentical(get_class($this->container
->get('cache.backend.database')), 'Drupal\\Core\\Cache\\MemoryBackendFactory');
trigger_error();
array_key_exists(NULL, NULL);
$this
->assertNothing();
debug('Foo', 'Debug', FALSE);
}
public function assertNothing() {
$this
->pass('This is nothing.');
}
public function confirmStubTestResults() {
$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()');
$this
->assertAssertion($this->passMessage, 'Other', 'Pass', 'SimpleTestTest.php', 'Drupal\\simpletest\\Tests\\SimpleTestTest->stubTest()');
$this
->assertAssertion($this->failMessage, 'Other', 'Fail', 'SimpleTestTest.php', 'Drupal\\simpletest\\Tests\\SimpleTestTest->stubTest()');
$this
->assertAssertion(t('Created permissions: @perms', [
'@perms' => $this->validPermission,
]), 'Role', 'Pass', 'SimpleTestTest.php', 'Drupal\\simpletest\\Tests\\SimpleTestTest->stubTest()');
$this
->assertAssertion(t('Invalid permission %permission.', [
'%permission' => $this->invalidPermission,
]), 'Role', 'Fail', 'SimpleTestTest.php', 'Drupal\\simpletest\\Tests\\SimpleTestTest->stubTest()');
$this
->assertAssertion('User SimpleTestTest successfully logged in.', 'User login', 'Pass', 'SimpleTestTest.php', 'Drupal\\simpletest\\Tests\\SimpleTestTest->stubTest()');
$this
->assertAssertion('trigger_error()', 'Warning', 'Fail', 'SimpleTestTest.php', 'Drupal\\simpletest\\Tests\\SimpleTestTest->stubTest()');
$this
->assertAssertion('This is nothing.', 'Other', 'Pass', 'SimpleTestTest.php', 'Drupal\\simpletest\\Tests\\SimpleTestTest->stubTest()');
$this
->assertAssertion('array_key_exists', 'Warning', 'Fail', 'SimpleTestTest.php', 'Drupal\\simpletest\\Tests\\SimpleTestTest->stubTest()');
$this
->assertAssertion("Debug: 'Foo'", 'Debug', 'Fail', 'SimpleTestTest.php', 'Drupal\\simpletest\\Tests\\SimpleTestTest->stubTest()');
$this
->assertEqual('16 passes, 3 fails, 2 exceptions, 3 debug messages', $this->childTestResults['summary']);
$this->testIds[] = $test_id = $this
->getTestIdFromResults();
$this
->assertTrue($test_id, 'Found test ID in results.');
}
public function getTestIdFromResults() {
foreach ($this->childTestResults['assertions'] as $assertion) {
if (preg_match('@^Test ID is ([0-9]*)\\.$@', $assertion['message'], $matches)) {
return $matches[1];
}
}
return NULL;
}
public function assertAssertion($message, $type, $status, $file, $function) {
$message = trim(strip_tags($message));
$found = FALSE;
foreach ($this->childTestResults['assertions'] as $assertion) {
if (strpos($assertion['message'], $message) !== FALSE && $assertion['type'] == $type && $assertion['status'] == $status && $assertion['file'] == $file && $assertion['function'] == $function) {
$found = TRUE;
break;
}
}
return $this
->assertTrue($found, new FormattableMarkup('Found assertion {"@message", "@type", "@status", "@file", "@function"}.', [
'@message' => $message,
'@type' => $type,
'@status' => $status,
"@file" => $file,
"@function" => $function,
]));
}
public function getTestResults() {
$results = [];
if ($this
->parse()) {
if ($details = $this
->getResultFieldSet()) {
$results['summary'] = $this
->asText($details->div->div[1]);
$results['name'] = $this
->asText($details->summary);
$results['assertions'] = [];
$tbody = $details->div->table->tbody;
foreach ($tbody->tr as $row) {
$assertion = [];
$assertion['message'] = $this
->asText($row->td[0]);
$assertion['type'] = $this
->asText($row->td[1]);
$assertion['file'] = $this
->asText($row->td[2]);
$assertion['line'] = $this
->asText($row->td[3]);
$assertion['function'] = $this
->asText($row->td[4]);
$ok_url = file_url_transform_relative(file_create_url('core/misc/icons/73b355/check.svg'));
$assertion['status'] = $row->td[5]->img['src'] == $ok_url ? 'Pass' : 'Fail';
$results['assertions'][] = $assertion;
}
}
}
$this->childTestResults = $results;
}
public function getResultFieldSet() {
$all_details = $this
->xpath('//details');
foreach ($all_details as $details) {
if ($this
->asText($details->summary) == __CLASS__) {
return $details;
}
}
return FALSE;
}
public function asText(\SimpleXMLElement $element) {
if (!is_object($element)) {
return $this
->fail('The element is not an element.');
}
return trim(html_entity_decode(strip_tags($element
->asXML())));
}
}