Commit 90bf3dc2 authored by alexpott's avatar alexpott

Issue #2863262 by Lendude, vaplas, Jo Fitzgerald, dawehner, alexpott:...

Issue #2863262 by Lendude, vaplas, Jo Fitzgerald, dawehner, alexpott: Bootstrap: Convert system functional tests to phpunit
parent c6cdc439
<?php <?php
namespace Drupal\system\Tests\Bootstrap; namespace Drupal\FunctionalTests\Bootstrap;
use Drupal\Core\DependencyInjection\Container; use Drupal\Core\DependencyInjection\Container;
......
<?php <?php
namespace Drupal\system\Tests\Bootstrap; namespace Drupal\FunctionalTests\Bootstrap;
use Drupal\Core\DependencyInjection\Container; use Drupal\Core\DependencyInjection\Container;
......
<?php <?php
namespace Drupal\system\Tests\System; namespace Drupal\FunctionalTests\Bootstrap;
use Drupal\simpletest\WebTestBase; use Drupal\Tests\BrowserTestBase;
/** /**
* Tests kernel panic when things are really messed up. * Tests kernel panic when things are really messed up.
* *
* @group system * @group system
*/ */
class UncaughtExceptionTest extends WebTestBase { class UncaughtExceptionTest extends BrowserTestBase {
/**
* Last cURL response.
*
* @var string
*/
protected $response = '';
/**
* Last cURL info.
*
* @var array
*/
protected $info = [];
/** /**
* Exceptions thrown by site under test that contain this text are ignored. * Exceptions thrown by site under test that contain this text are ignored.
...@@ -34,8 +48,8 @@ protected function setUp() { ...@@ -34,8 +48,8 @@ protected function setUp() {
$settings_filename = $this->siteDirectory . '/settings.php'; $settings_filename = $this->siteDirectory . '/settings.php';
chmod($settings_filename, 0777); chmod($settings_filename, 0777);
$settings_php = file_get_contents($settings_filename); $settings_php = file_get_contents($settings_filename);
$settings_php .= "\ninclude_once 'core/modules/system/src/Tests/Bootstrap/ErrorContainer.php';\n"; $settings_php .= "\ninclude_once 'core/tests/Drupal/FunctionalTests/Bootstrap/ErrorContainer.php';\n";
$settings_php .= "\ninclude_once 'core/modules/system/src/Tests/Bootstrap/ExceptionContainer.php';\n"; $settings_php .= "\ninclude_once 'core/tests/Drupal/FunctionalTests/Bootstrap/ExceptionContainer.php';\n";
file_put_contents($settings_filename, $settings_php); file_put_contents($settings_filename, $settings_php);
$settings = []; $settings = [];
...@@ -144,23 +158,7 @@ public function testMissingDependencyCustomErrorHandler() { ...@@ -144,23 +158,7 @@ public function testMissingDependencyCustomErrorHandler() {
$this->drupalGet('broken-service-class'); $this->drupalGet('broken-service-class');
$this->assertResponse(418); $this->assertResponse(418);
$this->assertRaw('Oh oh, flying teapots'); $this->assertSame('Oh oh, flying teapots', $this->response);
$message = 'Argument 1 passed to Drupal\error_service_test\LonelyMonkeyClass::__construct() must be an instance of Drupal\Core\Database\Connection, non';
$this->assertNoRaw('The website encountered an unexpected error.');
$this->assertNoRaw($message);
$found_exception = FALSE;
foreach ($this->assertions as &$assertion) {
if (strpos($assertion['message'], $message) !== FALSE) {
$found_exception = TRUE;
$this->deleteAssert($assertion['message_id']);
unset($assertion);
}
}
$this->assertTrue($found_exception, 'Ensure that the exception of a missing constructor argument was triggered.');
} }
/** /**
...@@ -169,13 +167,13 @@ public function testMissingDependencyCustomErrorHandler() { ...@@ -169,13 +167,13 @@ public function testMissingDependencyCustomErrorHandler() {
public function testErrorContainer() { public function testErrorContainer() {
$settings = []; $settings = [];
$settings['settings']['container_base_class'] = (object) [ $settings['settings']['container_base_class'] = (object) [
'value' => '\Drupal\system\Tests\Bootstrap\ErrorContainer', 'value' => '\Drupal\FunctionalTests\Bootstrap\ErrorContainer',
'required' => TRUE, 'required' => TRUE,
]; ];
$this->writeSettings($settings); $this->writeSettings($settings);
\Drupal::service('kernel')->invalidateContainer(); \Drupal::service('kernel')->invalidateContainer();
$this->expectedExceptionMessage = 'Argument 1 passed to Drupal\system\Tests\Bootstrap\ErrorContainer::Drupal\system\Tests\Bootstrap\{closur'; $this->expectedExceptionMessage = 'Argument 1 passed to Drupal\FunctionalTests\Bootstrap\ErrorContainer::Drupal\FunctionalTests\Bootstrap\{closur';
$this->drupalGet(''); $this->drupalGet('');
$this->assertResponse(500); $this->assertResponse(500);
...@@ -189,7 +187,7 @@ public function testErrorContainer() { ...@@ -189,7 +187,7 @@ public function testErrorContainer() {
public function testExceptionContainer() { public function testExceptionContainer() {
$settings = []; $settings = [];
$settings['settings']['container_base_class'] = (object) [ $settings['settings']['container_base_class'] = (object) [
'value' => '\Drupal\system\Tests\Bootstrap\ExceptionContainer', 'value' => '\Drupal\FunctionalTests\Bootstrap\ExceptionContainer',
'required' => TRUE, 'required' => TRUE,
]; ];
$this->writeSettings($settings); $this->writeSettings($settings);
...@@ -254,7 +252,7 @@ public function testLoggerException() { ...@@ -254,7 +252,7 @@ public function testLoggerException() {
$this->assertText('The website encountered an unexpected error. Please try again later.'); $this->assertText('The website encountered an unexpected error. Please try again later.');
$this->assertRaw($this->expectedExceptionMessage); $this->assertRaw($this->expectedExceptionMessage);
// Find fatal error logged to the simpletest error.log // Find fatal error logged to the error.log
$errors = file(\Drupal::root() . '/' . $this->siteDirectory . '/error.log'); $errors = file(\Drupal::root() . '/' . $this->siteDirectory . '/error.log');
$this->assertIdentical(count($errors), 8, 'The error + the error that the logging service is broken has been written to the error log.'); $this->assertIdentical(count($errors), 8, 'The error + the error that the logging service is broken has been written to the error log.');
$this->assertTrue(strpos($errors[0], 'Failed to log error') !== FALSE, 'The error handling logs when an error could not be logged to the logger.'); $this->assertTrue(strpos($errors[0], 'Failed to log error') !== FALSE, 'The error handling logs when an error could not be logged to the logger.');
...@@ -270,14 +268,108 @@ public function testLoggerException() { ...@@ -270,14 +268,108 @@ public function testLoggerException() {
} }
/** /**
* {@inheritdoc} * Asserts that a specific error has been logged to the PHP error log.
*
* @param string $error_message
* The expected error message.
*
* @see \Drupal\simpletest\TestBase::prepareEnvironment()
* @see \Drupal\Core\DrupalKernel::bootConfiguration()
*/ */
protected function error($message = '', $group = 'Other', array $caller = NULL) { protected function assertErrorLogged($error_message) {
if (!empty($this->expectedExceptionMessage) && strpos($message, $this->expectedExceptionMessage) !== FALSE) { $error_log_filename = DRUPAL_ROOT . '/' . $this->siteDirectory . '/error.log';
// We're expecting this error. if (!file_exists($error_log_filename)) {
return FALSE; $this->fail('No error logged yet.');
}
$content = file_get_contents($error_log_filename);
$rows = explode(PHP_EOL, $content);
// We iterate over the rows in order to be able to remove the logged error
// afterwards.
$found = FALSE;
foreach ($rows as $row_index => $row) {
if (strpos($content, $error_message) !== FALSE) {
$found = TRUE;
unset($rows[$row_index]);
}
} }
return parent::error($message, $group, $caller);
file_put_contents($error_log_filename, implode("\n", $rows));
$this->assertTrue($found, sprintf('The %s error message was logged.', $error_message));
}
/**
* Asserts that no errors have been logged to the PHP error.log thus far.
*
* @see \Drupal\simpletest\TestBase::prepareEnvironment()
* @see \Drupal\Core\DrupalKernel::bootConfiguration()
*/
protected function assertNoErrorsLogged() {
// Since PHP only creates the error.log file when an actual error is
// triggered, it is sufficient to check whether the file exists.
$this->assertFalse(file_exists(DRUPAL_ROOT . '/' . $this->siteDirectory . '/error.log'), 'PHP error.log is empty.');
}
/**
* Retrieves a Drupal path or an absolute path.
*
* Executes a cURL request for processing errors and exceptions.
*
* @param string|\Drupal\Core\Url $path
* Request path.
* @param array $extra_options
* (optional) Curl options to pass to curl_setopt()
* @param array $headers
* (optional) Not used.
*/
protected function drupalGet($path, array $extra_options = [], array $headers = []) {
$url = $this->buildUrl($path, ['absolute' => TRUE]);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, drupal_generate_test_ua($this->databasePrefix));
$this->response = curl_exec($ch);
$this->info = curl_getinfo($ch);
curl_close($ch);
}
/**
* {@inheritdoc}
*/
protected function assertResponse($code) {
$this->assertSame($code, $this->info['http_code']);
}
/**
* {@inheritdoc}
*/
protected function assertText($text) {
$this->assertContains($text, $this->response);
}
/**
* {@inheritdoc}
*/
protected function assertNoText($text) {
$this->assertNotContains($text, $this->response);
}
/**
* {@inheritdoc}
*/
protected function assertRaw($text) {
$this->assertText($text);
}
/**
* {@inheritdoc}
*/
protected function assertNoRaw($text) {
$this->assertNoText($text);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment