/webservice/xmlrpc/tests/lib_test.php

https://github.com/cmiic/moodle · PHP · 140 lines · 50 code · 18 blank · 72 comment · 4 complexity · 0eac62c3da813b2652fcc40f4b8fc887 MD5 · raw file

  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Unit tests for the XML-RPC web service.
  18. *
  19. * @package webservice_xmlrpc
  20. * @category test
  21. * @copyright 2015 Jun Pataleta <jun@moodle.com>
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. defined('MOODLE_INTERNAL') || die();
  25. global $CFG;
  26. require_once($CFG->dirroot . '/webservice/xmlrpc/lib.php');
  27. /**
  28. * Unit tests for the XML-RPC web service.
  29. *
  30. * @package webservice_xmlrpc
  31. * @category test
  32. * @copyright 2015 Jun Pataleta <jun@moodle.com>
  33. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34. */
  35. class webservice_xmlrpc_test extends advanced_testcase {
  36. /**
  37. * Setup.
  38. */
  39. public function setUp() {
  40. $this->resetAfterTest();
  41. // All tests require xmlrpc. Skip tests, if xmlrpc is not installed.
  42. if (!function_exists('xmlrpc_decode')) {
  43. $this->markTestSkipped('XMLRPC is not installed.');
  44. }
  45. }
  46. /**
  47. * Test for array response.
  48. */
  49. public function test_client_with_array_response() {
  50. global $CFG;
  51. $client = new webservice_xmlrpc_client_mock('/webservice/xmlrpc/server.php', 'anytoken');
  52. $mockresponse = file_get_contents($CFG->dirroot . '/webservice/xmlrpc/tests/fixtures/array_response.xml');
  53. $client->set_mock_response($mockresponse);
  54. $result = $client->call('testfunction');
  55. $this->assertEquals(xmlrpc_decode($mockresponse), $result);
  56. }
  57. /**
  58. * Test for value response.
  59. */
  60. public function test_client_with_value_response() {
  61. global $CFG;
  62. $client = new webservice_xmlrpc_client_mock('/webservice/xmlrpc/server.php', 'anytoken');
  63. $mockresponse = file_get_contents($CFG->dirroot . '/webservice/xmlrpc/tests/fixtures/value_response.xml');
  64. $client->set_mock_response($mockresponse);
  65. $result = $client->call('testfunction');
  66. $this->assertEquals(xmlrpc_decode($mockresponse), $result);
  67. }
  68. /**
  69. * Test for fault response.
  70. */
  71. public function test_client_with_fault_response() {
  72. global $CFG;
  73. $client = new webservice_xmlrpc_client_mock('/webservice/xmlrpc/server.php', 'anytoken');
  74. $mockresponse = file_get_contents($CFG->dirroot . '/webservice/xmlrpc/tests/fixtures/fault_response.xml');
  75. $client->set_mock_response($mockresponse);
  76. $this->expectException('moodle_exception');
  77. $client->call('testfunction');
  78. }
  79. }
  80. /**
  81. * Class webservice_xmlrpc_client_mock.
  82. *
  83. * Mock class that returns the processed XML-RPC response.
  84. *
  85. * @package webservice_xmlrpc
  86. * @category test
  87. * @copyright 2015 Jun Pataleta <jun@moodle.com>
  88. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  89. */
  90. class webservice_xmlrpc_client_mock extends webservice_xmlrpc_client {
  91. /** @var string The mock XML-RPC response string. */
  92. private $mockresponse;
  93. /**
  94. * XML-RPC mock response setter.
  95. *
  96. * @param string $mockresponse
  97. */
  98. public function set_mock_response($mockresponse) {
  99. $this->mockresponse = $mockresponse;
  100. }
  101. /**
  102. * Since the call method uses download_file_content and it is hard to make an actual call to a web service,
  103. * we'll just have to simulate the receipt of the response from the server using the mock response so we
  104. * can test the processing result of this method.
  105. *
  106. * @param string $functionname the function name
  107. * @param array $params the parameters of the function
  108. * @return mixed The decoded XML RPC response.
  109. * @throws moodle_exception
  110. */
  111. public function call($functionname, $params = array()) {
  112. // Get the response.
  113. $response = $this->mockresponse;
  114. // This is the part of the code in webservice_xmlrpc_client::call() what we would like to test.
  115. // Decode the response.
  116. $result = xmlrpc_decode($response);
  117. if (is_array($result) && xmlrpc_is_fault($result)) {
  118. throw new moodle_exception($result['faultString']);
  119. }
  120. return $result;
  121. }
  122. }