Skip to content

Commit

Permalink
MDL-73826 phpunit: Allow curl mock responses to handle empty strings
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
stronk7 committed Feb 11, 2022
1 parent e6aeab1 commit ad1c072
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/filelib.php
Expand Up @@ -3706,7 +3706,8 @@ protected function request($url, $options = array()) {
$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;
}
Expand Down
14 changes: 14 additions & 0 deletions mod/lti/tests/locallib_test.php
Expand Up @@ -1841,6 +1841,20 @@ public function lti_get_ims_role_provider() {
];
}

/**
* 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.
*
Expand Down

0 comments on commit ad1c072

Please sign in to comment.