From 4c11b779d92fadec04bafba15fd6de179a278ca7 Mon Sep 17 00:00:00 2001 From: mcdruid Date: Thu, 1 Apr 2021 10:45:36 +0100 Subject: [PATCH] Issue #3206429 by Taran2L, chmez: [PHP 8] test failures in Drupal error handlers --- modules/simpletest/simpletest.test | 19 ++++++++++++------- modules/simpletest/tests/common.test | 4 ++-- modules/simpletest/tests/error.test | 5 ++--- modules/simpletest/tests/error_test.module | 8 ++++++-- 4 files changed, 22 insertions(+), 14 deletions(-) diff --git a/modules/simpletest/simpletest.test b/modules/simpletest/simpletest.test index 5d1c718c1c4..80b841d58fc 100644 --- a/modules/simpletest/simpletest.test +++ b/modules/simpletest/simpletest.test @@ -164,13 +164,16 @@ class SimpleTestFunctionalTest extends DrupalWebTestCase { $this->pass(t('Test ID is @id.', array('@id' => $this->testId))); // Generates a warning. - $i = 1 / 0; + $a = ''; + foreach ($a as $b) { + + } // Call an assert function specific to that class. $this->assertNothing(); - // Generates a warning inside a PHP function. - array_key_exists(NULL, NULL); + // Generates 3 warnings inside a PHP function. + simplexml_load_string(''); debug('Foo', 'Debug'); } @@ -195,19 +198,21 @@ class SimpleTestFunctionalTest extends DrupalWebTestCase { $this->assertAssertion(t('Invalid permission %permission.', array('%permission' => $this->invalid_permission)), 'Role', 'Fail', 'simpletest.test', 'SimpleTestFunctionalTest->stubTest()'); // Check that a warning is caught by simpletest. - $this->assertAssertion('Division by zero', 'Warning', 'Fail', 'simpletest.test', 'SimpleTestFunctionalTest->stubTest()'); + // The exact error message differs between PHP versions so we check only + // the presense of the 'foreach' statement. + $this->assertAssertion('foreach()', 'Warning', 'Fail', 'simpletest.test', 'SimpleTestFunctionalTest->stubTest()'); // Check that the backtracing code works for specific assert function. $this->assertAssertion('This is nothing.', 'Other', 'Pass', 'simpletest.test', 'SimpleTestFunctionalTest->stubTest()'); // Check that errors that occur inside PHP internal functions are correctly reported. // The exact error message differs between PHP versions so we check only - // the function name 'array_key_exists'. - $this->assertAssertion('array_key_exists', 'Warning', 'Fail', 'simpletest.test', 'SimpleTestFunctionalTest->stubTest()'); + // the function name 'simplexml_load_string'. + $this->assertAssertion('simplexml_load_string', 'Warning', 'Fail', 'simpletest.test', 'SimpleTestFunctionalTest->stubTest()'); $this->assertAssertion("Debug: 'Foo'", 'Debug', 'Fail', 'simpletest.test', 'SimpleTestFunctionalTest->stubTest()'); - $this->assertEqual('6 passes, 5 fails, 2 exceptions, and 1 debug message', $this->childTestResults['summary'], 'Stub test summary is correct'); + $this->assertEqual('6 passes, 5 fails, 4 exceptions, and 1 debug message', $this->childTestResults['summary'], 'Stub test summary is correct'); $this->test_ids[] = $test_id = $this->getTestIdFromResults(); $this->assertTrue($test_id, 'Found test ID in results.'); diff --git a/modules/simpletest/tests/common.test b/modules/simpletest/tests/common.test index 6145546f754..ff399676305 100644 --- a/modules/simpletest/tests/common.test +++ b/modules/simpletest/tests/common.test @@ -2633,8 +2633,8 @@ class DrupalErrorCollectionUnitTest extends DrupalWebTestCase { $this->assertEqual(count($this->collectedErrors), 3, 'Three errors were collected'); if (count($this->collectedErrors) == 3) { - $this->assertError($this->collectedErrors[0], 'Notice', 'error_test_generate_warnings()', 'error_test.module', 'Undefined variable: bananas'); - $this->assertError($this->collectedErrors[1], 'Warning', 'error_test_generate_warnings()', 'error_test.module', 'Division by zero'); + $this->assertError($this->collectedErrors[0], 'Notice', 'error_test_generate_warnings()', 'error_test.module', 'Object of class stdClass could not be converted to int'); + $this->assertError($this->collectedErrors[1], 'Warning', 'error_test_generate_warnings()', 'error_test.module', \PHP_VERSION_ID < 80000 ? 'Invalid argument supplied for foreach()' : 'foreach() argument must be of type array|object, string given'); $this->assertError($this->collectedErrors[2], 'User warning', 'error_test_generate_warnings()', 'error_test.module', 'Drupal is awesome'); } else { diff --git a/modules/simpletest/tests/error.test b/modules/simpletest/tests/error.test index f946e82f1d2..5b56d52d9ba 100644 --- a/modules/simpletest/tests/error.test +++ b/modules/simpletest/tests/error.test @@ -22,13 +22,13 @@ class DrupalErrorHandlerTestCase extends DrupalWebTestCase { function testErrorHandler() { $error_notice = array( '%type' => 'Notice', - '!message' => 'Undefined variable: bananas', + '!message' => 'Object of class stdClass could not be converted to int', '%function' => 'error_test_generate_warnings()', '%file' => drupal_realpath('modules/simpletest/tests/error_test.module'), ); $error_warning = array( '%type' => 'Warning', - '!message' => 'Division by zero', + '!message' => \PHP_VERSION_ID < 80000 ? 'Invalid argument supplied for foreach()' : 'foreach() argument must be of type array|object, string given', '%function' => 'error_test_generate_warnings()', '%file' => drupal_realpath('modules/simpletest/tests/error_test.module'), ); @@ -113,4 +113,3 @@ class DrupalErrorHandlerTestCase extends DrupalWebTestCase { $this->assertNoRaw($message, format_string('Did not find error message: !message.', array('!message' => $message))); } } - diff --git a/modules/simpletest/tests/error_test.module b/modules/simpletest/tests/error_test.module index d062cb067c4..43d0d0c6187 100644 --- a/modules/simpletest/tests/error_test.module +++ b/modules/simpletest/tests/error_test.module @@ -40,9 +40,13 @@ function error_test_generate_warnings($collect_errors = FALSE) { // Tell Drupal error reporter to send errors to Simpletest or not. define('SIMPLETEST_COLLECT_ERRORS', $collect_errors); // This will generate a notice. - $monkey_love = $bananas; + $notice = new \stdClass(); + $notice == 1 ? 1 : 0; // This will generate a warning. - $awesomely_big = 1/0; + $a = ''; + foreach ($a as $b) { + + } // This will generate a user error. trigger_error("Drupal is awesome", E_USER_WARNING); return "";