From 635522dc3ce4a176d050b70eb87dfdfd9412a17a Mon Sep 17 00:00:00 2001 From: Simey Lameze Date: Thu, 30 Jan 2025 06:59:46 +0800 Subject: [PATCH] MDL-84343 phpunit: handle warnings on failing tests Signed-off-by: Simey Lameze --- admin/presets/tests/helper_test.php | 3 ++ admin/presets/tests/manager_test.php | 3 ++ .../tests/local/action/import_test.php | 4 +++ lib/phpunit/tests/advanced_test.php | 33 ++++++++++--------- 4 files changed, 27 insertions(+), 16 deletions(-) diff --git a/admin/presets/tests/helper_test.php b/admin/presets/tests/helper_test.php index 630bc468630..3b75bd9ea6c 100644 --- a/admin/presets/tests/helper_test.php +++ b/admin/presets/tests/helper_test.php @@ -290,7 +290,10 @@ final class helper_test extends \advanced_testcase { $generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets'); $generator->create_preset(['name' => 'Preset 1']); + $invokable = self::get_invokable(); + set_error_handler($invokable, E_WARNING); $presetid = helper::change_default_preset($preset); + restore_error_handler(); if (empty($settings) && empty($plugins)) { // The preset hasn't been applied. diff --git a/admin/presets/tests/manager_test.php b/admin/presets/tests/manager_test.php index 60389369c3d..ebe56828190 100644 --- a/admin/presets/tests/manager_test.php +++ b/admin/presets/tests/manager_test.php @@ -438,7 +438,10 @@ final class manager_test extends \advanced_testcase { // Call the method to be tested. $manager = new manager(); try { + $invokable = self::get_invokable(); + set_error_handler($invokable, E_WARNING); list($xml, $preset, $settingsfound, $pluginsfound) = $manager->import_preset($filecontents); + restore_error_handler(); } catch (\exception $e) { if ($expectedexception) { $this->assertInstanceOf($expectedexception, $e); diff --git a/admin/tool/admin_presets/tests/local/action/import_test.php b/admin/tool/admin_presets/tests/local/action/import_test.php index 0e0ea9a5c5e..ea51e833ccb 100644 --- a/admin/tool/admin_presets/tests/local/action/import_test.php +++ b/admin/tool/admin_presets/tests/local/action/import_test.php @@ -81,6 +81,9 @@ final class import_test extends \advanced_testcase { $action = new import(); $sink = $this->redirectEvents(); try { + // Suppress warnings and load XML. + $invokable = self::get_invokable(); + set_error_handler($invokable, E_WARNING); $action->execute(); } catch (\exception $e) { // If import action was successfull, redirect should be called so we will encounter an @@ -90,6 +93,7 @@ final class import_test extends \advanced_testcase { } else { $this->assertInstanceOf(\moodle_exception::class, $e); } + restore_error_handler(); } finally { if ($expecteddebugging) { $this->assertDebuggingCalled(); diff --git a/lib/phpunit/tests/advanced_test.php b/lib/phpunit/tests/advanced_test.php index fecdfe3dbbb..66560d8f607 100644 --- a/lib/phpunit/tests/advanced_test.php +++ b/lib/phpunit/tests/advanced_test.php @@ -627,7 +627,12 @@ final class advanced_test extends \advanced_testcase { $this->assertSame('99', $CFG->timezone); $this->assertSame('Europe/Prague', date_default_timezone_get()); + // Catch warning for invalid 'xxx' timezone. + set_error_handler(function ($errno, $errstr): void { + $this->assertStringContainsString('Unknown or bad timezone', $errstr); + }, E_WARNING); $this->setTimezone('xxx', 'Europe/Prague'); + restore_error_handler(); $this->assertSame('xxx', $CFG->timezone); $this->assertSame('Europe/Prague', date_default_timezone_get()); @@ -635,22 +640,18 @@ final class advanced_test extends \advanced_testcase { $this->assertSame('Australia/Perth', $CFG->timezone); $this->assertSame('Australia/Perth', date_default_timezone_get()); - try { - $this->setTimezone('Pacific/Auckland', ''); - } catch (\Exception $e) { - $this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e); - } - - try { - $this->setTimezone('Pacific/Auckland', 'xxxx'); - } catch (\Exception $e) { - $this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e); - } - - try { - $this->setTimezone('Pacific/Auckland', null); - } catch (\Exception $e) { - $this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e); + // Catch warnings for other invalid cases. + $invalidtimezones = ['', 'xxxx', null]; + foreach ($invalidtimezones as $invalidtz) { + set_error_handler(function ($errno, $errstr): void { + $this->assertStringContainsString('Unknown or bad timezone', $errstr); + }, E_WARNING); + try { + $this->setTimezone('Pacific/Auckland', $invalidtz); + } catch (\Throwable $e) { + $this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e); + } + restore_error_handler(); } }