Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

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