Proyectos de Subversion Moodle

Rev

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