Skip to content
Snippets Groups Projects
Commit 5bb2889d authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2536560 by Aki Tendo, stefan.r, damiankloip: Runtime Assertion unit and functional testing

parent f29f1e21
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
......@@ -1004,7 +1004,7 @@
* verified with standard control structures at all times, not just checked in
* development environments with assert() statements on.
*
* When runtime assertions fail in PHP 7 an \AssertionException is thrown.
* When runtime assertions fail in PHP 7 an \AssertionError is thrown.
* Drupal uses an assertion callback to do the same in PHP 5.x so that unit
* tests involving runtime assertions will work uniformly across both versions.
*
......
<?php
/**
* @file
* Contains \Drupal\Component\Assertion\Handle.
*
* For PHP 5 this contains \AssertionError as well.
*/
namespace {
if (!class_exists('AssertionError', FALSE)) {
/**
* Emulates PHP 7 AssertionError as closely as possible.
*
* We force this class to exist at the root namespace for PHP 5.
* This class exists natively in PHP 7. Note that in PHP 7 it extends from
* Error, not Exception, but that isn't possible for PHP 5 - all exceptions
* must extend from exception.
*/
class AssertionError extends Exception {
/**
* {@inheritdoc}
*/
public function __construct($message = '', $code = 0, Exception $previous = NULL, $file = '', $line = 0) {
parent::__construct($message, $code, $previous);
// Preserve the filename and line number of the assertion failure.
$this->file = $file;
$this->line = $line;
}
}
}
}
namespace Drupal\Component\Assertion {
/**
* Handler for runtime assertion failures.
*
* This class allows PHP 5.x to throw exceptions on runtime assertion fails
* in the same manner as PHP 7, and sets the ASSERT_EXCEPTION flag to TRUE
* for the PHP 7 runtime.
*
* @ingroup php_assert
*/
class Handle {
/**
* Registers uniform assertion handling.
*/
public static function register() {
// Since we're using exceptions, turn error warnings off.
assert_options(ASSERT_WARNING, FALSE);
if (version_compare(PHP_VERSION, '7.0.0-dev') < 0) {
// PHP 5 - create a handler to throw the exception directly.
assert_options(ASSERT_CALLBACK, function($file, $line, $code, $message) {
if (empty($message)) {
$message = $code;
}
throw new \AssertionError($message, 0, NULL, $file, $line);
});
}
else {
// PHP 7 - just turn exception throwing on.
assert_options(ASSERT_EXCEPTION, TRUE);
}
}
}
}
......@@ -923,6 +923,12 @@ public static function bootEnvironment() {
// Simpletest's internal browser.
define('DRUPAL_TEST_IN_CHILD_SITE', TRUE);
// Web tests are to be conducted with runtime assertions active.
assert_options(ASSERT_ACTIVE, TRUE);
// Now synchronize PHP 5 and 7's handling of assertions as much as
// possible.
\Drupal\Component\Assertion\Handle::register();
// Log fatal errors to the test site directory.
ini_set('log_errors', 1);
ini_set('error_log', DRUPAL_ROOT . '/sites/simpletest/' . substr($test_prefix, 10) . '/error.log');
......
......@@ -164,7 +164,16 @@ function stubTest() {
$site_path = $this->container->get('site.path');
file_put_contents($key_file, $private_key);
// This causes the first of the fifteen passes asserted in
// Check to see if runtime assertions are indeed on, if successful this
// will be the first of sixteen passes asserted in confirmStubResults()
try {
assert(FALSE, 'Lorem Ipsum');
$this->fail('Runtime assertions are not working.');
}
catch (\AssertionError $e) {
$this->assertEqual($e->getMessage(), 'Lorem Ipsum', 'Runtime assertions Enabled and running.');
}
// This causes the second of the sixteen passes asserted in
// confirmStubResults().
$this->pass($this->passMessage);
......@@ -175,7 +184,7 @@ function stubTest() {
// confirmStubResults().
$this->fail($this->failMessage);
// This causes the second to fourth of the fifteen passes asserted in
// This causes the third to fifth of the sixteen passes asserted in
// confirmStubResults().
$user = $this->drupalCreateUser(array($this->validPermission), 'SimpleTestTest');
......@@ -183,15 +192,15 @@ function stubTest() {
$this->drupalCreateUser(array($this->invalidPermission));
// Test logging in as a user.
// This causes the fifth to ninth of the fifteen passes asserted in
// This causes the sixth to tenth of the sixteen passes asserted in
// confirmStubResults().
$this->drupalLogin($user);
// This causes the tenth of the fifteen passes asserted in
// This causes the eleventh of the sixteen passes asserted in
// confirmStubResults().
$this->pass(t('Test ID is @id.', array('@id' => $this->testId)));
// These cause the eleventh to fourteenth of the fifteen passes asserted in
// These cause the twelfth to fifteenth of the sixteen passes asserted in
// confirmStubResults().
$this->assertTrue(file_exists($site_path . '/settings.testing.php'));
// Check the settings.testing.php file got included.
......@@ -206,7 +215,7 @@ function stubTest() {
// Generates a warning inside a PHP function.
array_key_exists(NULL, NULL);
// This causes the fifteenth of the fifteen passes asserted in
// This causes the sixteenth of the sixteen passes asserted in
// confirmStubResults().
$this->assertNothing();
......@@ -250,7 +259,7 @@ function confirmStubTestResults() {
$this->assertAssertion("Debug: 'Foo'", 'Debug', 'Fail', 'SimpleTestTest.php', 'Drupal\simpletest\Tests\SimpleTestTest->stubTest()');
$this->assertEqual('15 passes, 3 fails, 2 exceptions, 3 debug messages', $this->childTestResults['summary']);
$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.');
......
......@@ -108,3 +108,10 @@ function drupal_phpunit_get_extension_namespaces($dirs) {
// and DST). This choice is made to prevent timezone related regressions and
// reduce the fragility of the testing system in general.
date_default_timezone_set('Australia/Sydney');
// Runtime assertions. PHPUnit follows the php.ini assert.active setting for
// runtime assertions. By default this setting is on. Here we make a call to
// make PHP 5 and 7 handle assertion failures the same way, but this call does
// not turn runtime assertions on if they weren't on already.
\Drupal\Component\Assertion\Handle::register();
......@@ -27,7 +27,8 @@
*
* @see https://wiki.php.net/rfc/expectations
*/
assert_options(ASSERT_ACTIVE, 1);
assert_options(ASSERT_ACTIVE, TRUE);
\Drupal\Component\Assertion\Handle::register();
/**
* Enable local development services.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment