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  namespace mod_data\external;
  18  
  19  defined('MOODLE_INTERNAL') || die();
  20  
  21  global $CFG;
  22  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  23  
  24  use externallib_advanced_testcase;
  25  use external_api;
  26  use mod_data\manager;
  27  
  28  /**
  29   * External function test for delete_saved_preset.
  30   *
  31   * @package    mod_data
  32   * @category   external
  33   * @since      Moodle 4.1
  34   * @copyright  2022 Amaia Anabitarte <amaia@moodle.com>
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   * @coversDefaultClass \mod_data\external\delete_saved_preset
  37   */
  38  class delete_saved_preset_test extends externallib_advanced_testcase {
  39  
  40      /**
  41       * Test the behaviour of delete_saved_preset().
  42       *
  43       * @covers ::execute
  44       */
  45      public function test_delete_saved_preset() {
  46          $this->resetAfterTest();
  47  
  48          // Create course, database activity and users.
  49          $course = $this->getDataGenerator()->create_course();
  50          $data = $this->getDataGenerator()->create_module('data', ['course' => $course->id]);
  51          $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
  52          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
  53          $manager = manager::create_from_instance($data);
  54          $initialpresets = $manager->get_available_presets();
  55  
  56          $preset1name = 'Admin preset';
  57          $preset2name = 'Teacher preset';
  58  
  59          // Trying to delete a preset when there is no saved preset created.
  60          $result = delete_saved_preset::execute($data->id, [$preset1name, $preset2name]);
  61          $result = external_api::clean_returnvalue(delete_saved_preset::execute_returns(), $result);
  62          $this->assertFalse($result['result']);
  63          $this->assertCount(2, $result['warnings']);
  64          // Check no preset has been deleted.
  65          $currentpresets = $manager->get_available_presets();
  66          $this->assertEquals(count($initialpresets), count($currentpresets));
  67  
  68          // Create a saved preset.
  69          $plugingenerator = $this->getDataGenerator()->get_plugin_generator('mod_data');
  70          $record = (object)[
  71              'name' => $preset1name,
  72              'description' => 'Testing preset description',
  73          ];
  74          $adminpreset = $plugingenerator->create_preset($data, $record);
  75          // Update initial preset list.
  76          $initialpresets = $manager->get_available_presets();
  77  
  78          // There is a warning for non-existing preset.
  79          $result = delete_saved_preset::execute($data->id, ['Another preset']);
  80          $result = external_api::clean_returnvalue(delete_saved_preset::execute_returns(), $result);
  81          $this->assertFalse($result['result']);
  82          $this->assertCount(1, $result['warnings']);
  83          // Check no preset has been deleted.
  84          $currentpresets = $manager->get_available_presets();
  85          $this->assertEquals(count($initialpresets), count($currentpresets));
  86  
  87          // Create a saved preset by teacher.
  88          $this->setUser($teacher);
  89          $plugingenerator = $this->getDataGenerator()->get_plugin_generator('mod_data');
  90          $record = (object)[
  91              'name' => $preset2name,
  92              'description' => 'Testing preset description',
  93          ];
  94          $teacherpreset = $plugingenerator->create_preset($data, $record);
  95          // Update initial preset list.
  96          $this->setAdminUser();
  97          $initialpresets = $manager->get_available_presets();
  98  
  99          // Student can't delete presets.
 100          $this->setUser($student);
 101          $result = delete_saved_preset::execute($data->id, [$preset1name, $preset2name]);
 102          $result = external_api::clean_returnvalue(delete_saved_preset::execute_returns(), $result);
 103          $this->assertFalse($result['result']);
 104          $this->assertCount(2, $result['warnings']);
 105          // Check no preset has been deleted.
 106          $this->setAdminUser();
 107          $currentpresets = $manager->get_available_presets();
 108          $this->assertEquals(count($initialpresets), count($currentpresets));
 109  
 110          // Teacher can delete their preset.
 111          $this->setUser($teacher);
 112          $result = delete_saved_preset::execute($data->id, [$preset2name]);
 113          $result = external_api::clean_returnvalue(delete_saved_preset::execute_returns(), $result);
 114          $this->assertTrue($result['result']);
 115          $this->assertCount(0, $result['warnings']);
 116          // Check the preset has been deleted.
 117          $this->setAdminUser();
 118          $currentpresets = $manager->get_available_presets();
 119          $this->assertEquals(count($initialpresets) - 1, count($currentpresets));
 120          foreach ($currentpresets as $currentpreset) {
 121              $this->assertNotEquals($currentpreset->name, $preset2name);
 122          }
 123  
 124          // Teacher can't delete other users' preset.
 125          $this->setUser($teacher);
 126          $result = delete_saved_preset::execute($data->id, [$preset1name]);
 127          $result = external_api::clean_returnvalue(delete_saved_preset::execute_returns(), $result);
 128          $this->assertFalse($result['result']);
 129          $this->assertCount(1, $result['warnings']);
 130          // Check no preset has been deleted.
 131          $this->setAdminUser();
 132          $currentpresets = $manager->get_available_presets();
 133          $this->assertEquals(count($initialpresets) - 1, count($currentpresets));
 134      }
 135  }