. /** * Unit tests for the XML-RPC web service server. * * @package webservice_xmlrpc * @category test * @copyright 2016 Cameron Ball * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ defined('MOODLE_INTERNAL') || die(); global $CFG; require_once($CFG->dirroot . '/webservice/xmlrpc/locallib.php'); /** * Unit tests for the XML-RPC web service server. * * @package webservice_xmlrpc * @category test * @copyright 2016 Cameron Ball * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class webservice_xmlrpc_locallib_testcase extends advanced_testcase { /** * Setup. */ public function setUp() { if (!function_exists('xmlrpc_decode')) { $this->markTestSkipped('XMLRPC is not installed.'); } } /** * Test that the response generated is correct * * There is a bug in PHP that causes the xml_rpc library to * incorrectly escape multibyte characters. See https://bugs.php.net/bug.php?id=41650 * * @dataProvider prepare_response_provider * @param string $returnsdesc Webservice function return description * @param string $returns Webservice function description * @param string $expected The expected XML-RPC response */ public function test_prepare_response($returnsdesc, $returns, $expected) { $server = $this->getMockBuilder('webservice_xmlrpc_server') ->disableOriginalConstructor() ->setMethods(null) ->getMock(); $rc = new \ReflectionClass('webservice_xmlrpc_server'); $rcm = $rc->getMethod('prepare_response'); $rcm->setAccessible(true); $func = $rc->getProperty('function'); $func->setAccessible(true); $func->setValue($server, (object) ['returns_desc' => new external_value(PARAM_RAW, $returnsdesc, VALUE_OPTIONAL)]); $ret = $rc->getProperty('returns'); $ret->setAccessible(true); $ret->setValue($server, $returns); $rcm->invokeArgs($server, []); $response = $rc->getProperty('response'); $response->setAccessible(true); $this->assertEquals($expected, $response->getValue($server)); } /** * Test that the response generated is correct * * There is a bug in PHP that causes the xml_rpc library to * incorrectly escape multibyte characters. See https://bugs.php.net/bug.php?id=41650 * * @dataProvider generate_error_provider * @param Exception $exception An exception to be provided to generate_error * @param string $code An error code to be provided to generate_error * @param string $expected The expected XML-RPC response */ public function test_generate_error($exception, $code, $expected) { $server = $this->getMockBuilder('webservice_xmlrpc_server') ->disableOriginalConstructor() ->setMethods(null) ->getMock(); $rc = new \ReflectionClass('webservice_xmlrpc_server'); $rcm = $rc->getMethod('generate_error'); $rcm->setAccessible(true); if ($code === null) { $result = $rcm->invokeArgs($server, [$exception]); } else { $result = $rcm->invokeArgs($server, [$exception, $code]); } $this->assertEquals($expected, $result); } /** * Data provider for the prepare_response testcase * * @return array of testcases */ public function prepare_response_provider() { return [ 'Description written with Latin script' => [ 'Ennyn Durin, Aran Moria: pedo mellon a minno', 'Mellon!', 'Mellon!' . '' ], 'Description with non-Latin glyphs' => [ 'What biscuits do you have?', // V Unicode 9! V. '😂🤵😂 𝒪𝓃𝓁𝓎 𝓉𝒽𝑒 𝒻𝒾𝓃𝑒𝓈𝓉 𝐼𝓉𝒶𝓁𝒾𝒶𝓃 𝒷𝒾𝓈𝒸𝓊𝒾𝓉𝓈 😂🤵😂', '' . '😂🤵😂 𝒪𝓃𝓁𝓎 𝓉𝒽𝑒 𝒻𝒾𝓃𝑒𝓈𝓉 𝐼𝓉𝒶𝓁𝒾𝒶𝓃 𝒷𝒾𝓈𝒸𝓊𝒾𝓉𝓈 😂🤵😂' ] ]; } /** * Data provider for the generate_error testcase * * @return array of testcases */ public function generate_error_provider() { return [ 'Standard exception with default faultcode' => [ new \Exception(), null, 'faultCode404faultString' ], 'Standard exception with default faultcode and exception content' => [ new \Exception('PC LOAD LETTER'), null, 'faultCode404faultStringPC LOAD LETTER' ], 'Standard exception with really messed up non-Latin glyphs' => [ new \Exception('P̫̬̳̫̓͊̇r̨͎̜ͧa͚̬̙̺͎̙ͬẏ͎̲̦̲e̶̞͎͙̻͐̉r͙̙ͮ̓̈ͧ̔̃ ̠ͨ́ͭ̎̎̇̿n̗̥̞͗o̼̖͛̂̒̿ͮ͘t̷̞͎̘̘̝̥̲͂̌ͭ ͕̹͚̪͖̖̊̆́̒ͫ̓̀fͤͦͭͥ͊ͩo̼̱̻̹͒̿͒u̡͕̞͕̜̠͕ͥͭ̈̄̈́͐ń̘̼̇͜d̸̰̻͎͉̱̰̥̿͒'), null, 'faultCode404faultStringP̫̬̳̫̓͊̇r̨͎̜ͧa͚̬̙̺͎̙ͬẏ͎̲̦̲e̶̞͎͙̻͐̉r͙̙ͮ̓̈ͧ̔̃ ̠ͨ́ͭ̎̎̇̿n̗̥̞͗o̼̖͛̂̒̿ͮ͘t̷̞͎̘̘̝̥̲͂̌ͭ ͕̹͚̪͖̖̊̆́̒ͫ̓̀fͤͦͭͥ͊ͩo̼̱̻̹͒̿͒u̡͕̞͕̜̠͕ͥͭ̈̄̈́͐ń̘̼̇͜d̸̰̻͎͉̱̰̥̿͒' ] ]; } }