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 311 and 402] [Versions 311 and 403] [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  namespace webservice_rest;
  18  
  19  defined('MOODLE_INTERNAL') || die();
  20  global $CFG;
  21  
  22  require_once($CFG->libdir . '/externallib.php');
  23  require_once($CFG->dirroot . '/webservice/rest/locallib.php');
  24  
  25  /**
  26   * Rest server testcase.
  27   *
  28   * @package    webservice_rest
  29   * @copyright  2016 Frédéric Massart - FMCorz.net
  30   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31   */
  32  class server_test extends \advanced_testcase {
  33  
  34      /**
  35       * Data provider for test_xmlize.
  36       * @return array
  37       */
  38      public function xmlize_provider() {
  39          $data = [];
  40          $data[] = [null, null, ''];
  41          $data[] = [new \external_value(PARAM_BOOL), false, "<VALUE>0</VALUE>\n"];
  42          $data[] = [new \external_value(PARAM_BOOL), true, "<VALUE>1</VALUE>\n"];
  43          $data[] = [new \external_value(PARAM_ALPHA), null, "<VALUE null=\"null\"/>\n"];
  44          $data[] = [new \external_value(PARAM_ALPHA), 'a', "<VALUE>a</VALUE>\n"];
  45          $data[] = [new \external_value(PARAM_INT), 123, "<VALUE>123</VALUE>\n"];
  46          $data[] = [
  47              new \external_multiple_structure(new \external_value(PARAM_INT)),
  48              [1, 2, 3],
  49              "<MULTIPLE>\n" .
  50              "<VALUE>1</VALUE>\n" .
  51              "<VALUE>2</VALUE>\n" .
  52              "<VALUE>3</VALUE>\n" .
  53              "</MULTIPLE>\n"
  54          ];
  55          $data[] = [ // Multiple structure with null value.
  56              new \external_multiple_structure(new \external_value(PARAM_ALPHA)),
  57              ['A', null, 'C'],
  58              "<MULTIPLE>\n" .
  59              "<VALUE>A</VALUE>\n" .
  60              "<VALUE null=\"null\"/>\n" .
  61              "<VALUE>C</VALUE>\n" .
  62              "</MULTIPLE>\n"
  63          ];
  64          $data[] = [ // Multiple structure without values.
  65              new \external_multiple_structure(new \external_value(PARAM_ALPHA)),
  66              [],
  67              "<MULTIPLE>\n" .
  68              "</MULTIPLE>\n"
  69          ];
  70          $data[] = [
  71              new \external_single_structure([
  72                  'one' => new \external_value(PARAM_INT),
  73                  'two' => new \external_value(PARAM_INT),
  74                  'three' => new \external_value(PARAM_INT),
  75              ]),
  76              ['one' => 1, 'two' => 2, 'three' => 3],
  77              "<SINGLE>\n" .
  78              "<KEY name=\"one\"><VALUE>1</VALUE>\n</KEY>\n" .
  79              "<KEY name=\"two\"><VALUE>2</VALUE>\n</KEY>\n" .
  80              "<KEY name=\"three\"><VALUE>3</VALUE>\n</KEY>\n" .
  81              "</SINGLE>\n"
  82          ];
  83          $data[] = [ // Single structure with null value.
  84              new \external_single_structure([
  85                  'one' => new \external_value(PARAM_INT),
  86                  'two' => new \external_value(PARAM_INT),
  87                  'three' => new \external_value(PARAM_INT),
  88              ]),
  89              ['one' => 1, 'two' => null, 'three' => 3],
  90              "<SINGLE>\n" .
  91              "<KEY name=\"one\"><VALUE>1</VALUE>\n</KEY>\n" .
  92              "<KEY name=\"two\"><VALUE null=\"null\"/>\n</KEY>\n" .
  93              "<KEY name=\"three\"><VALUE>3</VALUE>\n</KEY>\n" .
  94              "</SINGLE>\n"
  95          ];
  96          $data[] = [ // Single structure missing keys.
  97              new \external_single_structure([
  98                  'one' => new \external_value(PARAM_INT),
  99                  'two' => new \external_value(PARAM_INT),
 100                  'three' => new \external_value(PARAM_INT),
 101              ]),
 102              ['two' => null, 'three' => 3],
 103              "<SINGLE>\n" .
 104              "<KEY name=\"one\"><VALUE null=\"null\"/>\n</KEY>\n" .
 105              "<KEY name=\"two\"><VALUE null=\"null\"/>\n</KEY>\n" .
 106              "<KEY name=\"three\"><VALUE>3</VALUE>\n</KEY>\n" .
 107              "</SINGLE>\n"
 108          ];
 109          $data[] = [ // Nested structure.
 110              new \external_single_structure([
 111                  'one' => new \external_multiple_structure(
 112                      new \external_value(PARAM_INT)
 113                  ),
 114                  'two' => new \external_multiple_structure(
 115                      new \external_single_structure([
 116                          'firstname' => new \external_value(PARAM_RAW),
 117                          'lastname' => new \external_value(PARAM_RAW),
 118                      ])
 119                  ),
 120                  'three' => new \external_single_structure([
 121                      'firstname' => new \external_value(PARAM_RAW),
 122                      'lastname' => new \external_value(PARAM_RAW),
 123                  ]),
 124              ]),
 125              [
 126                  'one' => [2, 3, 4],
 127                  'two' => [
 128                      ['firstname' => 'Louis', 'lastname' => 'Armstrong'],
 129                      ['firstname' => 'Neil', 'lastname' => 'Armstrong'],
 130                  ],
 131                  'three' => ['firstname' => 'Neil', 'lastname' => 'Armstrong'],
 132              ],
 133              "<SINGLE>\n" .
 134              "<KEY name=\"one\"><MULTIPLE>\n".
 135                  "<VALUE>2</VALUE>\n" .
 136                  "<VALUE>3</VALUE>\n" .
 137                  "<VALUE>4</VALUE>\n" .
 138              "</MULTIPLE>\n</KEY>\n" .
 139              "<KEY name=\"two\"><MULTIPLE>\n".
 140                  "<SINGLE>\n" .
 141                      "<KEY name=\"firstname\"><VALUE>Louis</VALUE>\n</KEY>\n" .
 142                      "<KEY name=\"lastname\"><VALUE>Armstrong</VALUE>\n</KEY>\n" .
 143                  "</SINGLE>\n" .
 144                  "<SINGLE>\n" .
 145                      "<KEY name=\"firstname\"><VALUE>Neil</VALUE>\n</KEY>\n" .
 146                      "<KEY name=\"lastname\"><VALUE>Armstrong</VALUE>\n</KEY>\n" .
 147                  "</SINGLE>\n" .
 148              "</MULTIPLE>\n</KEY>\n" .
 149              "<KEY name=\"three\"><SINGLE>\n" .
 150                  "<KEY name=\"firstname\"><VALUE>Neil</VALUE>\n</KEY>\n" .
 151                  "<KEY name=\"lastname\"><VALUE>Armstrong</VALUE>\n</KEY>\n" .
 152              "</SINGLE>\n</KEY>\n" .
 153              "</SINGLE>\n"
 154          ];
 155          $data[] = [ // Nested structure with missing keys.
 156              new \external_single_structure([
 157                  'one' => new \external_multiple_structure(
 158                      new \external_value(PARAM_INT)
 159                  ),
 160                  'two' => new \external_multiple_structure(
 161                      new \external_single_structure([
 162                          'firstname' => new \external_value(PARAM_RAW),
 163                          'lastname' => new \external_value(PARAM_RAW),
 164                      ])
 165                  ),
 166                  'three' => new \external_single_structure([
 167                      'firstname' => new \external_value(PARAM_RAW),
 168                      'lastname' => new \external_value(PARAM_RAW),
 169                  ]),
 170              ]),
 171              [
 172                  'two' => [
 173                      ['firstname' => 'Louis'],
 174                      ['lastname' => 'Armstrong'],
 175                  ],
 176                  'three' => ['lastname' => 'Armstrong'],
 177              ],
 178              "<SINGLE>\n" .
 179              "<KEY name=\"one\"><MULTIPLE>\n</MULTIPLE>\n</KEY>\n" .
 180              "<KEY name=\"two\"><MULTIPLE>\n".
 181                  "<SINGLE>\n" .
 182                      "<KEY name=\"firstname\"><VALUE>Louis</VALUE>\n</KEY>\n" .
 183                      "<KEY name=\"lastname\"><VALUE null=\"null\"/>\n</KEY>\n" .
 184                  "</SINGLE>\n" .
 185                  "<SINGLE>\n" .
 186                      "<KEY name=\"firstname\"><VALUE null=\"null\"/>\n</KEY>\n" .
 187                      "<KEY name=\"lastname\"><VALUE>Armstrong</VALUE>\n</KEY>\n" .
 188                  "</SINGLE>\n" .
 189              "</MULTIPLE>\n</KEY>\n" .
 190              "<KEY name=\"three\"><SINGLE>\n" .
 191                  "<KEY name=\"firstname\"><VALUE null=\"null\"/>\n</KEY>\n" .
 192                  "<KEY name=\"lastname\"><VALUE>Armstrong</VALUE>\n</KEY>\n" .
 193              "</SINGLE>\n</KEY>\n" .
 194              "</SINGLE>\n"
 195          ];
 196          return $data;
 197      }
 198  
 199      /**
 200       * @dataProvider xmlize_provider
 201       * @param external_description $description The data structure.
 202       * @param mixed $value The value to xmlise.
 203       * @param mixed $expected The expected output.
 204       */
 205      public function test_xmlize($description, $value, $expected) {
 206          $method = new \ReflectionMethod('webservice_rest_server', 'xmlize_result');
 207          $method->setAccessible(true);
 208          $this->assertEquals($expected, $method->invoke(null, $value, $description));
 209      }
 210  
 211  }