Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
11 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 mod_glossary\external;
18
 
19
defined('MOODLE_INTERNAL') || die();
20
 
21
global $CFG;
22
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
23
 
24
use core_external\external_api;
25
use externallib_advanced_testcase;
26
use mod_glossary_external;
27
use context_module;
28
use context_user;
29
use core_external\util as external_util;
30
 
31
/**
32
 * External function test for update_entry.
33
 *
34
 * @package    mod_glossary
35
 * @category   external
36
 * @covers     \mod_glossary\external\update_entry
37
 * @since      Moodle 3.10
38
 * @copyright  2020 Juan Leyva <juan@moodle.com>
39
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40
 */
41
final class update_entry_test extends externallib_advanced_testcase {
42
 
43
    /**
44
     * test_update_entry_without_optional_settings
45
     */
46
    public function test_update_entry_without_optional_settings() {
47
        global $CFG, $DB;
48
        $this->resetAfterTest(true);
49
 
50
        $course = $this->getDataGenerator()->create_course();
51
        $glossary = $this->getDataGenerator()->create_module('glossary', ['course' => $course->id]);
52
 
53
        $this->setAdminUser();
54
        $concept = 'A concept';
55
        $definition = '<p>A definition</p>';
56
        $return = mod_glossary_external::add_entry($glossary->id, $concept, $definition, FORMAT_HTML);
57
        $return = external_api::clean_returnvalue(mod_glossary_external::add_entry_returns(), $return);
58
        $entryid = $return['entryid'];
59
 
60
        // Updates the entry.
61
        $concept .= ' Updated!';
62
        $definition .= ' <p>Updated!</p>';
63
        $return = update_entry::execute($entryid, $concept, $definition, FORMAT_HTML);
64
        $return = external_api::clean_returnvalue(update_entry::execute_returns(), $return);
65
 
66
        // Get entry from DB.
67
        $entry = $DB->get_record('glossary_entries', ['id' => $entryid]);
68
 
69
        $this->assertEquals($concept, $entry->concept);
70
        $this->assertEquals($definition, $entry->definition);
71
        $this->assertEquals($CFG->glossary_linkentries, $entry->usedynalink);
72
        $this->assertEquals($CFG->glossary_casesensitive, $entry->casesensitive);
73
        $this->assertEquals($CFG->glossary_fullmatch, $entry->fullmatch);
74
        $this->assertEmpty($DB->get_records('glossary_alias', ['entryid' => $entryid]));
75
        $this->assertEmpty($DB->get_records('glossary_entries_categories', ['entryid' => $entryid]));
76
    }
77
 
78
    /**
79
     * test_update_entry_duplicated
80
     */
81
    public function test_update_entry_duplicated() {
82
        global $CFG, $DB;
83
        $this->resetAfterTest(true);
84
 
85
        $course = $this->getDataGenerator()->create_course();
86
        $glossary = $this->getDataGenerator()->create_module('glossary', ['course' => $course->id, 'allowduplicatedentries' => 1]);
87
 
88
        // Create three entries.
89
        $this->setAdminUser();
90
        $concept = 'A concept';
91
        $definition = '<p>A definition</p>';
92
        mod_glossary_external::add_entry($glossary->id, $concept, $definition, FORMAT_HTML);
93
 
94
        $concept = 'B concept';
95
        $definition = '<p>B definition</p>';
96
        mod_glossary_external::add_entry($glossary->id, $concept, $definition, FORMAT_HTML);
97
 
98
        $concept = 'Another concept';
99
        $definition = '<p>Another definition</p>';
100
        $return = mod_glossary_external::add_entry($glossary->id, $concept, $definition, FORMAT_HTML);
101
        $return = external_api::clean_returnvalue(mod_glossary_external::add_entry_returns(), $return);
102
        $entryid = $return['entryid'];
103
 
104
        // Updates the entry using an existing entry name when duplicateds are allowed.
105
        $concept = 'A concept';
106
        update_entry::execute($entryid, $concept, $definition, FORMAT_HTML);
107
 
108
        // Updates the entry using an existing entry name when duplicateds are NOT allowed.
109
        $DB->set_field('glossary', 'allowduplicatedentries', 0, ['id' => $glossary->id]);
110
        $concept = 'B concept';
111
        $this->expectExceptionMessage(get_string('errconceptalreadyexists', 'glossary'));
112
        update_entry::execute($entryid, $concept, $definition, FORMAT_HTML);
113
    }
114
 
115
    /**
116
     * test_update_entry_with_aliases
117
     */
118
    public function test_update_entry_with_aliases() {
119
        global $DB;
120
        $this->resetAfterTest(true);
121
 
122
        $course = $this->getDataGenerator()->create_course();
123
        $glossary = $this->getDataGenerator()->create_module('glossary', ['course' => $course->id]);
124
 
125
        $this->setAdminUser();
126
        $concept = 'A concept';
127
        $definition = 'A definition';
128
        $paramaliases = 'abc, def, gez';
129
        $options = [
130
            [
131
                'name' => 'aliases',
132
                'value' => $paramaliases,
133
            ]
134
        ];
135
        $return = mod_glossary_external::add_entry($glossary->id, $concept, $definition, FORMAT_HTML, $options);
136
        $return = external_api::clean_returnvalue(mod_glossary_external::add_entry_returns(), $return);
137
        $entryid = $return['entryid'];
138
 
139
        // Updates the entry.
140
        $newaliases = 'abz, xyz';
141
        $options[0]['value'] = $newaliases;
142
        $return = update_entry::execute($entryid, $concept, $definition, FORMAT_HTML, $options);
143
        $return = external_api::clean_returnvalue(update_entry::execute_returns(), $return);
144
 
145
        $aliases = $DB->get_records('glossary_alias', ['entryid' => $entryid]);
146
        $this->assertCount(2, $aliases);
147
        foreach ($aliases as $alias) {
148
            $this->assertStringContainsString($alias->alias, $newaliases);
149
        }
150
    }
151
 
152
    /**
153
     * test_update_entry_in_categories
154
     */
155
    public function test_update_entry_in_categories() {
156
        global $DB;
157
        $this->resetAfterTest(true);
158
 
159
        $course = $this->getDataGenerator()->create_course();
160
        $glossary = $this->getDataGenerator()->create_module('glossary', ['course' => $course->id]);
161
        $gg = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
162
        $cat1 = $gg->create_category($glossary);
163
        $cat2 = $gg->create_category($glossary);
164
        $cat3 = $gg->create_category($glossary);
165
 
166
        $this->setAdminUser();
167
        $concept = 'A concept';
168
        $definition = 'A definition';
169
        $paramcategories = "$cat1->id, $cat2->id";
170
        $options = [
171
            [
172
                'name' => 'categories',
173
                'value' => $paramcategories,
174
            ]
175
        ];
176
        $return = mod_glossary_external::add_entry($glossary->id, $concept, $definition, FORMAT_HTML, $options);
177
        $return = external_api::clean_returnvalue(mod_glossary_external::add_entry_returns(), $return);
178
        $entryid = $return['entryid'];
179
 
180
        // Updates the entry.
181
        $newcategories = "$cat1->id, $cat3->id";
182
        $options[0]['value'] = $newcategories;
183
        $return = update_entry::execute($entryid, $concept, $definition, FORMAT_HTML, $options);
184
        $return = external_api::clean_returnvalue(update_entry::execute_returns(), $return);
185
 
186
        $categories = $DB->get_records('glossary_entries_categories', ['entryid' => $entryid]);
187
        $this->assertCount(2, $categories);
188
        foreach ($categories as $category) {
189
            $this->assertStringContainsString($category->categoryid, $newcategories);
190
        }
191
    }
192
 
193
    /**
194
     * test_update_entry_with_attachments
195
     */
196
    public function test_update_entry_with_attachments() {
197
        global $DB, $USER;
198
        $this->resetAfterTest(true);
199
 
200
        $course = $this->getDataGenerator()->create_course();
201
        $glossary = $this->getDataGenerator()->create_module('glossary', ['course' => $course->id]);
202
        $context = context_module::instance($glossary->cmid);
203
 
204
        $this->setAdminUser();
205
        $concept = 'A concept';
206
        $definition = 'A definition';
207
 
208
        // Draft files.
209
        $draftidinlineattach = file_get_unused_draft_itemid();
210
        $draftidattach = file_get_unused_draft_itemid();
211
        $usercontext = context_user::instance($USER->id);
212
        $filerecordinline = [
213
            'contextid' => $usercontext->id,
214
            'component' => 'user',
215
            'filearea'  => 'draft',
216
            'itemid'    => $draftidinlineattach,
217
            'filepath'  => '/',
218
            'filename'  => 'shouldbeanimage.png',
219
        ];
220
        $fs = get_file_storage();
221
 
222
        // Create a file in a draft area for regular attachments.
223
        $filerecordattach = $filerecordinline;
224
        $attachfilename = 'attachment.txt';
225
        $filerecordattach['filename'] = $attachfilename;
226
        $filerecordattach['itemid'] = $draftidattach;
227
        $fs->create_file_from_string($filerecordinline, 'image contents (not really)');
228
        $fs->create_file_from_string($filerecordattach, 'simple text attachment');
229
 
230
        $options = [
231
            [
232
                'name' => 'inlineattachmentsid',
233
                'value' => $draftidinlineattach,
234
            ],
235
            [
236
                'name' => 'attachmentsid',
237
                'value' => $draftidattach,
238
            ]
239
        ];
240
        $return = mod_glossary_external::add_entry($glossary->id, $concept, $definition, FORMAT_HTML, $options);
241
        $return = external_api::clean_returnvalue(mod_glossary_external::add_entry_returns(), $return);
242
        $entryid = $return['entryid'];
243
        $entry = $DB->get_record('glossary_entries', ['id' => $entryid]);
244
 
245
        list($definitionoptions, $attachmentoptions) = glossary_get_editor_and_attachment_options($course, $context, $entry);
246
 
247
        $entry = file_prepare_standard_editor($entry, 'definition', $definitionoptions, $context, 'mod_glossary', 'entry',
248
            $entry->id);
249
        $entry = file_prepare_standard_filemanager($entry, 'attachment', $attachmentoptions, $context, 'mod_glossary', 'attachment',
250
            $entry->id);
251
 
252
        $inlineattachmentsid = $entry->definition_editor['itemid'];
253
        $attachmentsid = $entry->attachment_filemanager;
254
 
255
        // Change the file areas.
256
 
257
        // Delete one inline editor file.
258
        $selectedfile = (object)[
259
            'filename' => $filerecordinline['filename'],
260
            'filepath' => $filerecordinline['filepath'],
261
        ];
262
        $return = repository_delete_selected_files($usercontext, 'user', 'draft', $inlineattachmentsid, [$selectedfile]);
263
 
264
        // Add more files.
265
        $filerecordinline['filename'] = 'newvideo.mp4';
266
        $filerecordinline['itemid'] = $inlineattachmentsid;
267
 
268
        $filerecordattach['filename'] = 'newattach.txt';
269
        $filerecordattach['itemid'] = $attachmentsid;
270
 
271
        $fs->create_file_from_string($filerecordinline, 'image contents (not really)');
272
        $fs->create_file_from_string($filerecordattach, 'simple text attachment');
273
 
274
        // Updates the entry.
275
        $options[0]['value'] = $inlineattachmentsid;
276
        $options[1]['value'] = $attachmentsid;
277
        $return = update_entry::execute($entryid, $concept, $definition, FORMAT_HTML, $options);
278
        $return = external_api::clean_returnvalue(update_entry::execute_returns(), $return);
279
 
280
        $editorfiles = external_util::get_area_files($context->id, 'mod_glossary', 'entry', $entryid);
281
        $attachmentfiles = external_util::get_area_files($context->id, 'mod_glossary', 'attachment', $entryid);
282
 
283
        $this->assertCount(1, $editorfiles);
284
        $this->assertCount(2, $attachmentfiles);
285
 
286
        $this->assertEquals('newvideo.mp4', $editorfiles[0]['filename']);
287
        $this->assertEquals('attachment.txt', $attachmentfiles[0]['filename']);
288
        $this->assertEquals('newattach.txt', $attachmentfiles[1]['filename']);
289
    }
290
}