diff --git a/core/core.api.php b/core/core.api.php index baba9a25346b0919adec565c49068e05be295b35..e59bd2c1757551f805dee7f34671363c830ea427 100644 --- a/core/core.api.php +++ b/core/core.api.php @@ -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. * diff --git a/core/lib/Drupal/Component/Assertion/Handle.php b/core/lib/Drupal/Component/Assertion/Handle.php new file mode 100644 index 0000000000000000000000000000000000000000..3912156fc4e7ad1c3c859a18b8478b02a97eaef3 --- /dev/null +++ b/core/lib/Drupal/Component/Assertion/Handle.php @@ -0,0 +1,75 @@ +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); + } + } + +} + +} diff --git a/core/lib/Drupal/Core/DrupalKernel.php b/core/lib/Drupal/Core/DrupalKernel.php index 8ae5924ce27c0db7ee0302324e918b98c4560794..2ddc3eebfdf6429521f338f90c18e290d3f0629b 100644 --- a/core/lib/Drupal/Core/DrupalKernel.php +++ b/core/lib/Drupal/Core/DrupalKernel.php @@ -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'); diff --git a/core/modules/simpletest/src/Tests/SimpleTestTest.php b/core/modules/simpletest/src/Tests/SimpleTestTest.php index 1f978453c872d531cd71e0fa2ced873d06dd5034..c3758a7f8544b59dc23f433c555f34267f569035 100644 --- a/core/modules/simpletest/src/Tests/SimpleTestTest.php +++ b/core/modules/simpletest/src/Tests/SimpleTestTest.php @@ -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.'); diff --git a/core/tests/bootstrap.php b/core/tests/bootstrap.php index 893452596d5448e12d5bd4a1f2d3b6cce0d3c2a5..31ac4cd050d565e2007b7a9709ba0f60bed712d1 100644 --- a/core/tests/bootstrap.php +++ b/core/tests/bootstrap.php @@ -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(); + diff --git a/sites/example.settings.local.php b/sites/example.settings.local.php index 34b4e191c193c6f614627cc36dfd2b663798496e..23090a4162713e656e9d2f4b04a7b2ab0b7c18c4 100644 --- a/sites/example.settings.local.php +++ b/sites/example.settings.local.php @@ -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.