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
/**
18
 * Core h5p external functions tests.
19
 *
20
 * @package    core_h5p
21
 * @category   external
22
 * @copyright  2019 Carlos Escobedo <carlos@moodle.com>
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 * @since      Moodle 3.8
25
 */
26
 
27
namespace core_h5p\external;
28
 
29
use externallib_advanced_testcase;
30
 
31
defined('MOODLE_INTERNAL') || die();
32
 
33
global $CFG;
34
 
35
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
36
 
37
use core_h5p\external;
38
use core_h5p\local\library\autoloader;
39
 
40
/**
41
 * Core h5p external functions tests
42
 *
43
 * @package    core_h5p
44
 * @category   external
45
 * @copyright  2019 Carlos Escobedo <carlos@moodle.com>
46
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
47
 * @since      Moodle 3.8
48
 */
49
class external_test extends externallib_advanced_testcase {
50
 
51
    protected function setUp(): void {
52
        parent::setUp();
53
        autoloader::register();
54
    }
55
 
56
    /**
57
     * test_get_trusted_h5p_file description
58
     */
11 efrain 59
    public function test_get_trusted_h5p_file(): void {
1 efrain 60
        $this->resetAfterTest(true);
61
        $this->setAdminUser();
62
 
63
        // This is a valid .H5P file.
64
        $filename = 'find-the-words.h5p';
65
        $syscontext = \context_system::instance();
66
 
67
        // Create a fake export H5P file with normal pluginfile call.
68
        $generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
69
        $deployedfile = $generator->create_export_file($filename,
70
            $syscontext->id,
71
            \core_h5p\file_storage::COMPONENT,
72
            \core_h5p\file_storage::EXPORT_FILEAREA,
73
            $generator::PLUGINFILE);
74
 
75
        // Make the URL to pass to the WS.
76
        $url  = \moodle_url::make_pluginfile_url(
77
            $syscontext->id,
78
            \core_h5p\file_storage::COMPONENT,
79
            \core_h5p\file_storage::EXPORT_FILEAREA,
80
            0,
81
            '/',
82
            $filename
83
        );
84
 
85
        // Call the WS.
86
        $result = external::get_trusted_h5p_file($url->out(false), 0, 0, 0, 0);
87
        $result = \core_external\external_api::clean_returnvalue(external::get_trusted_h5p_file_returns(), $result);
88
        // Expected result: Just 1 record on files and none on warnings.
89
        $this->assertCount(1, $result['files']);
90
        $this->assertCount(0, $result['warnings']);
91
 
92
        // Check info export file to compare with the ws's results.
93
        $this->assertEquals($deployedfile['filepath'], $result['files'][0]['filepath']);
94
        $this->assertEquals($deployedfile['mimetype'], $result['files'][0]['mimetype']);
95
        $this->assertEquals($deployedfile['filesize'], $result['files'][0]['filesize']);
96
        $this->assertEquals($deployedfile['timemodified'], $result['files'][0]['timemodified']);
97
        $this->assertStringContainsString($deployedfile['filename'], $result['files'][0]['filename']);
98
        $this->assertStringContainsString($deployedfile['fileurl'], $result['files'][0]['fileurl']);
99
    }
100
 
101
    /**
102
     * test_h5p_invalid_url description
103
     */
11 efrain 104
    public function test_h5p_invalid_url(): void {
1 efrain 105
        $this->resetAfterTest();
106
        $this->setAdminUser();
107
 
108
        // Create an empty url.
109
        $urlempty = '';
110
        $result = external::get_trusted_h5p_file($urlempty, 0, 0, 0, 0);
111
        $result = \core_external\external_api::clean_returnvalue(external::get_trusted_h5p_file_returns(), $result);
112
        // Expected result: Just 1 record on warnings and none on files.
113
        $this->assertCount(0, $result['files']);
114
        $this->assertCount(1, $result['warnings']);
115
        // Check the warnings to be sure that h5pinvalidurl is the message by moodle_exception.
116
        $this->assertEquals($urlempty, $result['warnings'][0]['item']);
117
        $this->assertEquals(get_string('h5pinvalidurl', 'core_h5p'), $result['warnings'][0]['message']);
118
 
119
        // Create a non-local URL.
120
        $urlnonlocal = 'http://www.google.com/pluginfile.php/644/block_html/content/arithmetic-quiz-1-1.h5p';
121
        $result = external::get_trusted_h5p_file($urlnonlocal, 0, 0, 0, 0);
122
        $result = \core_external\external_api::clean_returnvalue(external::get_trusted_h5p_file_returns(), $result);
123
        // Expected result: Just 1 record on warnings and none on files.
124
        $this->assertCount(0, $result['files']);
125
        $this->assertCount(1, $result['warnings']);
126
        // Check the warnings to be sure that h5pinvalidurl is the message by moodle_exception.
127
        $this->assertEquals($urlnonlocal, $result['warnings'][0]['item']);
128
        $this->assertEquals(get_string('h5pinvalidurl', 'core_h5p'), $result['warnings'][0]['message']);
129
    }
130
 
131
    /**
132
     * test_h5p_file_not_found description
133
     */
11 efrain 134
    public function test_h5p_file_not_found(): void {
1 efrain 135
        $this->resetAfterTest();
136
        $this->setAdminUser();
137
 
138
        // Create a valid url with an h5pfile which doesn't exist in DB.
139
        $syscontext = \context_system::instance();
140
        $filenotfoundurl  = \moodle_url::make_pluginfile_url(
141
            $syscontext->id,
142
            \core_h5p\file_storage::COMPONENT,
143
            'unittest',
144
            0,
145
            '/',
146
            'notfound.h5p'
147
        );
148
        // Call the ws.
149
        $result = external::get_trusted_h5p_file($filenotfoundurl->out(), 0, 0, 0, 0);
150
        $result = \core_external\external_api::clean_returnvalue(external::get_trusted_h5p_file_returns(), $result);
151
        // Expected result: Just 1 record on warnings and none on files.
152
        $this->assertCount(0, $result['files']);
153
        $this->assertCount(1, $result['warnings']);
154
        // Check the warnings to be sure that h5pfilenotfound is the message by h5p error.
155
        $this->assertEquals($filenotfoundurl->out(), $result['warnings'][0]['item']);
156
        $this->assertEquals(get_string('h5pfilenotfound', 'core_h5p'), $result['warnings'][0]['message']);
157
    }
158
 
159
    /**
160
     * Test the request to get_trusted_h5p_file
161
     * using webservice/pluginfile.php as url param.
162
     */
11 efrain 163
    public function test_allow_webservice_pluginfile_in_url_param(): void {
1 efrain 164
        $this->resetAfterTest(true);
165
        $this->setAdminUser();
166
 
167
        // This is a valid .H5P file.
168
        $filename = 'find-the-words.h5p';
169
        $syscontext = \context_system::instance();
170
 
171
        // Create a fake export H5P file with webservice call.
172
        $generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
173
        $deployedfile = $generator->create_export_file($filename,
174
            $syscontext->id,
175
            \core_h5p\file_storage::COMPONENT,
176
            \core_h5p\file_storage::EXPORT_FILEAREA);
177
 
178
        // Make the URL to pass to the WS.
179
        $url  = \moodle_url::make_webservice_pluginfile_url(
180
            $syscontext->id,
181
            \core_h5p\file_storage::COMPONENT,
182
            \core_h5p\file_storage::EXPORT_FILEAREA,
183
            0,
184
            '/',
185
            $filename
186
        );
187
 
188
        // Call the WS.
189
        $result = external::get_trusted_h5p_file($url->out(), 0, 0, 0, 0);
190
        $result = \core_external\external_api::clean_returnvalue(external::get_trusted_h5p_file_returns(), $result);
191
 
192
        // Check info export file to compare with the ws's results.
193
        $this->assertEquals($deployedfile['filepath'], $result['files'][0]['filepath']);
194
        $this->assertEquals($deployedfile['mimetype'], $result['files'][0]['mimetype']);
195
        $this->assertEquals($deployedfile['filesize'], $result['files'][0]['filesize']);
196
        $this->assertEquals($deployedfile['timemodified'], $result['files'][0]['timemodified']);
197
        $this->assertStringContainsString($deployedfile['filename'], $result['files'][0]['filename']);
198
        $this->assertStringContainsString($deployedfile['fileurl'], $result['files'][0]['fileurl']);
199
    }
200
 
201
    /**
202
     * Test the request to get_trusted_h5p_file
203
     * using tokenpluginfile.php as url param.
204
     */
11 efrain 205
    public function test_allow_tokenluginfile_in_url_param(): void {
1 efrain 206
        $this->resetAfterTest(true);
207
        $this->setAdminUser();
208
 
209
        // This is a valid .H5P file.
210
        $filename = 'find-the-words.h5p';
211
        $syscontext = \context_system::instance();
212
 
213
        // Create a fake export H5P file with tokenfile call.
214
        $generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
215
        $deployedfile = $generator->create_export_file($filename,
216
            $syscontext->id,
217
            \core_h5p\file_storage::COMPONENT,
218
            \core_h5p\file_storage::EXPORT_FILEAREA,
219
            $generator::TOKENPLUGINFILE);
220
 
221
        // Make the URL to pass to the WS.
222
        $url  = \moodle_url::make_pluginfile_url(
223
            $syscontext->id,
224
            \core_h5p\file_storage::COMPONENT,
225
            \core_h5p\file_storage::EXPORT_FILEAREA,
226
            0,
227
            '/',
228
            $filename,
229
            false,
230
            true
231
        );
232
 
233
        // Call the WS.
234
        $result = external::get_trusted_h5p_file($url->out(false), 0, 0, 0, 0);
235
        $result = \core_external\external_api::clean_returnvalue(external::get_trusted_h5p_file_returns(), $result);
236
        // Expected result: Just 1 record on files and none on warnings.
237
        $this->assertCount(1, $result['files']);
238
        $this->assertCount(0, $result['warnings']);
239
 
240
        // Check info export file to compare with the ws's results.
241
        $this->assertEquals($deployedfile['filepath'], $result['files'][0]['filepath']);
242
        $this->assertEquals($deployedfile['mimetype'], $result['files'][0]['mimetype']);
243
        $this->assertEquals($deployedfile['filesize'], $result['files'][0]['filesize']);
244
        $this->assertEquals($deployedfile['timemodified'], $result['files'][0]['timemodified']);
245
        $this->assertStringContainsString($deployedfile['filename'], $result['files'][0]['filename']);
246
        $this->assertStringContainsString($deployedfile['fileurl'], $result['files'][0]['fileurl']);
247
    }
248
}