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 delete_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  
  37  /**
  38   * External function test for delete_entry.
  39   *
  40   * @package    mod_glossary
  41   * @copyright  2020 Juan Leyva <juan@moodle.com>
  42   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  43   */
  44  class delete_entry_testcase extends externallib_advanced_testcase {
  45  
  46      /**
  47       * Test the behaviour of delete_entry().
  48       */
  49      public function test_delete_entry() {
  50          global $DB;
  51          $this->resetAfterTest();
  52  
  53          // Create required data.
  54          $course = $this->getDataGenerator()->create_course();
  55          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
  56          $anotherstudent = $this->getDataGenerator()->create_and_enrol($course, 'student');
  57          $glossary = $this->getDataGenerator()->create_module('glossary', ['course' => $course->id]);
  58          $gg = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
  59  
  60          $this->setUser($student);
  61          $entry = $gg->create_content($glossary);
  62  
  63          // Test entry creator can delete.
  64          $result = delete_entry::execute($entry->id);
  65          $result = external_api::clean_returnvalue(delete_entry::execute_returns(), $result);
  66          $this->assertTrue($result['result']);
  67          $this->assertEquals(0, $DB->count_records('glossary_entries', ['id' => $entry->id]));
  68  
  69          // Test admin can delete.
  70          $this->setAdminUser();
  71          $entry = $gg->create_content($glossary);
  72          $result = delete_entry::execute($entry->id);
  73          $result = external_api::clean_returnvalue(delete_entry::execute_returns(), $result);
  74          $this->assertTrue($result['result']);
  75          $this->assertEquals(0, $DB->count_records('glossary_entries', ['id' => $entry->id]));
  76  
  77          $entry = $gg->create_content($glossary);
  78          // Test a different student is not able to delete.
  79          $this->setUser($anotherstudent);
  80          $this->expectExceptionMessage(get_string('nopermissiontodelentry', 'error'));
  81          delete_entry::execute($entry->id);
  82      }
  83  }