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