Issue #2157053 by alexpott, twistor, dawehner, sun: Ensure register_shutdown_function() works with php-fpm (blocks testbot php-fpm).

8.0.x
Nathaniel Catchpole 2014-02-07 13:04:25 +00:00
parent 580dd44a5f
commit 1b584005bb
4 changed files with 40 additions and 18 deletions

View File

@ -2585,12 +2585,16 @@ function _drupal_shutdown_function() {
} }
} }
catch (Exception $exception) { catch (Exception $exception) {
// If we are displaying errors, then do so with no possibility of a further // If using PHP-FPM then fastcgi_finish_request() will have been fired
// uncaught exception being thrown. // preventing further output to the browser.
require_once __DIR__ . '/errors.inc'; if (!function_exists('fastcgi_finish_request')) {
if (error_displayable()) { // If we are displaying errors, then do so with no possibility of a
print '<h1>Uncaught exception thrown in shutdown function.</h1>'; // further uncaught exception being thrown.
print '<p>' . Error::renderExceptionSafe($exception) . '</p><hr />'; require_once __DIR__ . '/errors.inc';
if (error_displayable()) {
print '<h1>Uncaught exception thrown in shutdown function.</h1>';
print '<p>' . Error::renderExceptionSafe($exception) . '</p><hr />';
}
} }
error_log($exception); error_log($exception);
} }

View File

@ -36,11 +36,23 @@ class ShutdownFunctionsTest extends WebTestBase {
$arg1 = $this->randomName(); $arg1 = $this->randomName();
$arg2 = $this->randomName(); $arg2 = $this->randomName();
$this->drupalGet('system-test/shutdown-functions/' . $arg1 . '/' . $arg2); $this->drupalGet('system-test/shutdown-functions/' . $arg1 . '/' . $arg2);
$this->assertText(t('First shutdown function, arg1 : @arg1, arg2: @arg2', array('@arg1' => $arg1, '@arg2' => $arg2)));
$this->assertText(t('Second shutdown function, arg1 : @arg1, arg2: @arg2', array('@arg1' => $arg1, '@arg2' => $arg2)));
// Make sure exceptions displayed through // If using PHP-FPM then fastcgi_finish_request() will have been fired
// \Drupal\Core\Utility\Error::renderExceptionSafe() are correctly escaped. // returning the response before shutdown functions have fired.
$this->assertRaw('Drupal is &lt;blink&gt;awesome&lt;/blink&gt;.'); // @see \Drupal\system_test\Controller\SystemTestController::shutdownFunctions()
$server_using_fastcgi = strpos($this->drupalGetContent(), 'The function fastcgi_finish_request exists when serving the request.');
if ($server_using_fastcgi) {
// We need to wait to ensure that the shutdown functions have fired.
sleep(1);
}
$this->assertEqual(\Drupal::state()->get('_system_test_first_shutdown_function'), array($arg1, $arg2));
$this->assertEqual(\Drupal::state()->get('_system_test_second_shutdown_function'), array($arg1, $arg2));
if (!$server_using_fastcgi) {
// Make sure exceptions displayed through
// \Drupal\Core\Utility\Error::renderExceptionSafe() are correctly
// escaped.
$this->assertRaw('Drupal is &lt;blink&gt;awesome&lt;/blink&gt;.');
}
} }
} }

View File

@ -68,6 +68,14 @@ class SystemTestController extends ControllerBase {
*/ */
public function shutdownFunctions($arg1, $arg2) { public function shutdownFunctions($arg1, $arg2) {
system_test_page_shutdown_functions($arg1, $arg2); system_test_page_shutdown_functions($arg1, $arg2);
// If using PHP-FPM then fastcgi_finish_request() will have been fired
// preventing further output to the browser which means that the escaping of
// the exception message can not be tested.
// @see _drupal_shutdown_function()
// @see \Drupal\system\Tests\System\ShutdownFunctionsTest
if (function_exists('fastcgi_finish_request')) {
return 'The function fastcgi_finish_request exists when serving the request.';
}
} }
} }

View File

@ -140,9 +140,8 @@ function system_test_page_shutdown_functions($arg1, $arg2) {
* Dummy shutdown function which registers another shutdown function. * Dummy shutdown function which registers another shutdown function.
*/ */
function _system_test_first_shutdown_function($arg1, $arg2) { function _system_test_first_shutdown_function($arg1, $arg2) {
// Output something, page has already been printed and the session stored // Set something to ensure that this function got called.
// so we can't use drupal_set_message. \Drupal::state()->set('_system_test_first_shutdown_function', array($arg1, $arg2));
print t('First shutdown function, arg1 : @arg1, arg2: @arg2', array('@arg1' => $arg1, '@arg2' => $arg2));
drupal_register_shutdown_function('_system_test_second_shutdown_function', $arg1, $arg2); drupal_register_shutdown_function('_system_test_second_shutdown_function', $arg1, $arg2);
} }
@ -150,14 +149,13 @@ function _system_test_first_shutdown_function($arg1, $arg2) {
* Dummy shutdown function. * Dummy shutdown function.
*/ */
function _system_test_second_shutdown_function($arg1, $arg2) { function _system_test_second_shutdown_function($arg1, $arg2) {
// Output something, page has already been printed and the session stored // Set something to ensure that this function got called.
// so we can't use drupal_set_message. \Drupal::state()->set('_system_test_second_shutdown_function', array($arg1, $arg2));
print t('Second shutdown function, arg1 : @arg1, arg2: @arg2', array('@arg1' => $arg1, '@arg2' => $arg2));
// Throw an exception with an HTML tag. Since this is called in a shutdown // Throw an exception with an HTML tag. Since this is called in a shutdown
// function, it will not bubble up to the default exception handler but will // function, it will not bubble up to the default exception handler but will
// be caught in _drupal_shutdown_function() and be displayed through // be caught in _drupal_shutdown_function() and be displayed through
// \Drupal\Core\Utility\Error::renderExceptionSafe(). // \Drupal\Core\Utility\Error::renderExceptionSafe() if possible.
throw new Exception('Drupal is <blink>awesome</blink>.'); throw new Exception('Drupal is <blink>awesome</blink>.');
} }