From d66a5316aeb3682f0dde1e93e34f85eb157a103b Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Thu, 10 Feb 2022 11:33:33 +0100 Subject: [PATCH] MDL-73826 phpunit: Allow curl mock responses to handle empty strings Before this commit, is_empty() was being applied before returning the mock response. But we want to be able to mock the empty response for some tests, hence moving the condition to null/isset, that is the value that array_pop() returns where there aren't more elements in the array. With that change performed, we can test lti_load_cartridge() with empty responses, hence adding a new test for that. --- lib/filelib.php | 3 ++- mod/lti/tests/locallib_test.php | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/filelib.php b/lib/filelib.php index a5294a9d81e..ca8506424d2 100644 --- a/lib/filelib.php +++ b/lib/filelib.php @@ -3712,7 +3712,8 @@ class curl { $this->reset_request_state_vars(); if ((defined('PHPUNIT_TEST') && PHPUNIT_TEST)) { - if ($mockresponse = array_pop(self::$mockresponses)) { + $mockresponse = array_pop(self::$mockresponses); + if ($mockresponse !== null) { $this->info = [ 'http_code' => 200 ]; return $mockresponse; } diff --git a/mod/lti/tests/locallib_test.php b/mod/lti/tests/locallib_test.php index bd639b17090..a9697fe9fb1 100644 --- a/mod/lti/tests/locallib_test.php +++ b/mod/lti/tests/locallib_test.php @@ -1945,6 +1945,20 @@ MwIDAQAB $this->assertEquals(16, $countwithproxyid); // 1 type, 15 proxies. } + /** + * Verify that empty curl responses lead to the proper moodle_exception, not to XML ValueError. + * + * @covers ::lti_load_cartridge() + */ + public function test_empty_reponse_lti_load_cartridge() { + // Mock the curl response to empty string, this is hardly + // reproducible in real life (only Windows + GHA). + \curl::mock_response(''); + + $this->expectException(\moodle_exception::class); + lti_load_cartridge('http://example.com/mocked/empty/response', []); + } + /** * Create an LTI Tool. *