From c482e6bd512251bfa60845bd50a9f91c76edc579 Mon Sep 17 00:00:00 2001 From: Jun Pataleta Date: Fri, 30 Nov 2018 14:55:54 +0800 Subject: [PATCH 1/1] MDL-64285 core: Fix environment check test * Looping the environment results and skipping some items mean that if a test gets skipped, the rest of the environment results won't be tested. It's better to use a data provider for this in order to individually test each environment result. --- lib/tests/environment_test.php | 65 +++++++++++++++++++++++----------- 1 file changed, 45 insertions(+), 20 deletions(-) diff --git a/lib/tests/environment_test.php b/lib/tests/environment_test.php index a660edb0a62..63498cad81e 100644 --- a/lib/tests/environment_test.php +++ b/lib/tests/environment_test.php @@ -32,36 +32,61 @@ defined('MOODLE_INTERNAL') || die(); class core_environment_testcase extends advanced_testcase { /** - * Test the environment. + * Test the environment check status. */ - public function test_environment() { + public function test_environment_check_status() { global $CFG; + require_once($CFG->libdir.'/environmentlib.php'); + + $results = check_moodle_environment(normalize_version($CFG->release), ENV_SELECT_RELEASE); + + // The first element of the results array contains the environment check status. + $status = reset($results); + $this->assertTrue($status); + } + /** + * Data provider for Moodle environment check tests. + * + * @return array + */ + public function environment_provider() { + global $CFG; require_once($CFG->libdir.'/environmentlib.php'); - list($envstatus, $environment_results) = check_moodle_environment(normalize_version($CFG->release), ENV_SELECT_RELEASE); + $results = check_moodle_environment(normalize_version($CFG->release), ENV_SELECT_RELEASE); + // The second element of the results array contains the list of environment results. + $environmentresults = end($results); + return array_map(function($result) { + return [$result]; + }, $environmentresults); + } + + /** + * Test the environment. + * + * @dataProvider environment_provider + * @param environment_results $result + */ + public function test_environment($result) { $sslmessages = ['ssl/tls configuration not supported', 'invalid ssl/tls configuration']; - $this->assertNotEmpty($envstatus); - foreach ($environment_results as $environment_result) { - if ($environment_result->part === 'php_setting' - and $environment_result->info === 'opcache.enable' - and $environment_result->getLevel() === 'optional' - and $environment_result->getStatus() === false - ) { - $this->markTestSkipped('OPCache extension is not necessary for unit testing.'); - continue; - } - if ($environment_result->part === 'custom_check' - and in_array($environment_result->info, $sslmessages) - and $environment_result->getLevel() === 'optional' - and $environment_result->getStatus() === false - ) { + if ($result->part === 'php_setting' + && $result->info === 'opcache.enable' + && $result->getLevel() === 'optional' + && $result->getStatus() === false) { + $this->markTestSkipped('OPCache extension is not necessary for unit testing.'); + } + + if ($result->part === 'custom_check' + && $result->getLevel() === 'optional' + && $result->getStatus() === false) { + if (in_array($result->info, $sslmessages)) { $this->markTestSkipped('Up-to-date TLS libraries are not necessary for unit testing.'); - continue; } - $this->assertTrue($environment_result->getStatus(), "Problem detected in environment ($environment_result->part:$environment_result->info), fix all warnings and errors!"); } + $info = "{$result->part}:{$result->info}"; + $this->assertTrue($result->getStatus(), "Problem detected in environment ($info), fix all warnings and errors!"); } /** -- 2.43.0