Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 39 and 311]

   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  /**
  18   * Unit tests for the XML-RPC web service server.
  19   *
  20   * @package    webservice_xmlrpc
  21   * @category   test
  22   * @copyright  2016 Andrew Nicols <andrew@nicols.co.uk>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  require_once($CFG->dirroot . '/webservice/xmlrpc/locallib.php');
  30  
  31  /**
  32   * Unit tests for the XML-RPC web service server.
  33   *
  34   * @package    webservice_xmlrpc
  35   * @category   test
  36   * @copyright  2016 Andrew Nicols <andrew@nicols.co.uk>
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class xmlrpc_server_test extends advanced_testcase {
  40  
  41      /**
  42       * Setup.
  43       */
  44      public function setUp(): void {
  45          if (!function_exists('xmlrpc_decode')) {
  46              $this->markTestSkipped('XMLRPC is not installed.');
  47          }
  48      }
  49  
  50      /**
  51       * Test parameter parsing.
  52       *
  53       * @dataProvider parse_request_provider
  54       * @param string $input             The XML-RPC request
  55       * @param string $expectfunction    The expected value for the function after decoding the request
  56       * @param array  $expectparams      The expected value for the params after decoding the request
  57       */
  58      public function test_parse_request($input, $expectfunction, $expectparams) {
  59          $server = $this->getMockBuilder('\webservice_xmlrpc_server')
  60              ->onlyMethods(['fetch_input_content'])
  61              ->disableOriginalConstructor()
  62              ->getMock();
  63  
  64          $server->method('fetch_input_content')
  65              ->willReturn($input);
  66  
  67          $rc = new \ReflectionClass('\webservice_xmlrpc_server');
  68          $rcm = $rc->getMethod('parse_request');
  69          $rcm->setAccessible(true);
  70          $rcm->invoke($server);
  71  
  72          $rcp = $rc->getProperty('functionname');
  73          $rcp->setAccessible(true);
  74          $this->assertEquals($expectfunction, $rcp->getValue($server));
  75  
  76          $rcp = $rc->getProperty('parameters');
  77          $rcp->setAccessible(true);
  78          $this->assertEquals($expectparams, $rcp->getValue($server));
  79      }
  80  
  81      /**
  82       * Data provider for testing parse_request.
  83       *
  84       * @return array
  85       */
  86      public function parse_request_provider() {
  87          $xml = '<?xml version="1.0" encoding="UTF-8"?>';
  88  
  89          // This valid webservice call has one required param ('component'), and one optional param ('lang').
  90          $validmethod = '<methodName>core_get_component_strings</methodName>';
  91          $requiredparams = '<params><param><value><string>moodle</string></value></param></params>';
  92          $allparams = '<params><param><value><string>moodle</string></value></param><param><value><string>en</string></value>'
  93                  . '</param></params>';
  94          $requiredparamsnonlatin = '<params><param><value><string>ᛞᛁᛞᛃᛟᚢᚲᚾᛟᚹᛈᚺᛈᛋᚢᛈᛈᛟᚱᛏᛋᚢᛏᚠ8ᚡᚨᚱᛁᚨᛒᛚᛖᚾᚨᛗᛖᛋ</string></value></param></params>';
  95  
  96          return [
  97                  'Valid method, required params only' => [
  98                      "{$xml}<methodCall>{$validmethod}{$requiredparams}</methodCall>",
  99                      'core_get_component_strings',
 100                      ['component' => 'moodle'],
 101                  ],
 102                  'Valid method, all params' => [
 103                      "{$xml}<methodCall>{$validmethod}{$allparams}</methodCall>",
 104                      'core_get_component_strings',
 105                      ['component' => 'moodle', 'lang' => 'en'],
 106                  ],
 107                  'Valid method required params only (non Latin)' => [
 108                      "{$xml}<methodCall>{$validmethod}{$requiredparamsnonlatin}</methodCall>",
 109                      'core_get_component_strings',
 110                      ['component' => 'ᛞᛁᛞᛃᛟᚢᚲᚾᛟᚹᛈᚺᛈᛋᚢᛈᛈᛟᚱᛏᛋᚢᛏᚠ8ᚡᚨᚱᛁᚨᛒᛚᛖᚾᚨᛗᛖᛋ'],
 111                  ],
 112              ];
 113      }
 114  }