Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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