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.
   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 tool_admin_presets\local\action;
  18  
  19  /**
  20   * Tests for the load class.
  21   *
  22   * @package    tool_admin_presets
  23   * @category   test
  24   * @copyright  2021 Sara Arjona (sara@moodle.com)
  25   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   * @coversDefaultClass \tool_admin_presets\local\action\load
  27   */
  28  class load_test extends \advanced_testcase {
  29  
  30      /**
  31       * Test the behaviour of show() method when the preset id doesn't exist.
  32       *
  33       * @covers ::show
  34       */
  35      public function test_load_show_unexisting_preset(): void {
  36  
  37          $this->resetAfterTest();
  38          $this->setAdminUser();
  39  
  40          // Create some presets.
  41          $generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets');
  42          $presetid = $generator->create_preset();
  43  
  44          // Initialise the parameters and create the load class.
  45          $_POST['action'] = 'load';
  46          $_POST['mode'] = 'view';
  47          $_POST['id'] = $presetid * 2; // Unexisting preset identifier.
  48  
  49          $action = new load();
  50          $this->expectException(\moodle_exception::class);
  51          $action->show();
  52      }
  53  
  54  
  55      /**
  56       * Test the behaviour of preview() method when the preset id doesn't exist.
  57       *
  58       * @covers ::preview
  59       */
  60      public function test_load_preview_unexisting_preset(): void {
  61  
  62          $this->resetAfterTest();
  63          $this->setAdminUser();
  64  
  65          // Create some presets.
  66          $generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets');
  67          $presetid = $generator->create_preset();
  68  
  69          // Initialise the parameters and create the load class.
  70          $_POST['action'] = 'load';
  71          $_POST['mode'] = 'preview';
  72          $_POST['id'] = $presetid * 2; // Unexisting preset identifier.
  73  
  74          $action = new load();
  75          $action->preview();
  76          $outputs = $generator->access_protected($action, 'outputs');
  77          // In that case, no exception should be raised and the text of no preset found should be displayed.
  78          $this->assertEquals(get_string('errornopreset', 'core_adminpresets'), $outputs);
  79      }
  80  
  81      /**
  82       * Test the behaviour of execute() method.
  83       *
  84       * @covers ::execute
  85       */
  86      public function test_load_execute(): void {
  87          global $DB;
  88  
  89          $this->resetAfterTest();
  90          $this->setAdminUser();
  91  
  92          // Create a preset.
  93          $generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets');
  94          $presetid = $generator->create_preset();
  95  
  96          $currentpresets = $DB->count_records('adminpresets');
  97          $currentitems = $DB->count_records('adminpresets_it');
  98          $currentadvitems = $DB->count_records('adminpresets_it_a');
  99          $currentplugins = $DB->count_records('adminpresets_plug');
 100          $currentapppresets = $DB->count_records('adminpresets_app');
 101          $currentappitems = $DB->count_records('adminpresets_app_it');
 102          $currentappadvitems = $DB->count_records('adminpresets_app_it_a');
 103          $currentappplugins = $DB->count_records('adminpresets_app_plug');
 104  
 105          // Set the config values (to confirm they change after applying the preset).
 106          set_config('enablebadges', 1);
 107          set_config('allowemojipicker', 1);
 108          set_config('mediawidth', '640', 'mod_lesson');
 109          set_config('maxanswers', '5', 'mod_lesson');
 110          set_config('maxanswers_adv', '1', 'mod_lesson');
 111          set_config('enablecompletion', 1);
 112          set_config('usecomments', 0);
 113  
 114          // Get the data we are submitting for the form and mock submitting it.
 115          $formdata = [
 116              'id' => $presetid,
 117              'admin_presets_submit' => 'Load selected settings',
 118          ];
 119          \tool_admin_presets\form\load_form::mock_submit($formdata);
 120  
 121          // Initialise the parameters.
 122          $_POST['action'] = 'load';
 123          $_POST['mode'] = 'execute';
 124          $_POST['id'] = $presetid;
 125          $_POST['sesskey'] = sesskey();
 126  
 127          // Create the load class and execute it.
 128          $action = new load();
 129          $action->execute();
 130  
 131          // Check the preset applied has been added to database.
 132          $this->assertCount($currentapppresets + 1, $DB->get_records('adminpresets_app'));
 133          // Applied items: enablebadges@none, mediawitdh@mod_lesson and maxanswers@@mod_lesson.
 134          $this->assertCount($currentappitems + 3, $DB->get_records('adminpresets_app_it'));
 135          // Applied advanced items: maxanswers_adv@mod_lesson.
 136          $this->assertCount($currentappadvitems + 1, $DB->get_records('adminpresets_app_it_a'));
 137          // Applied plugins: enrol_guest and mod_glossary.
 138          $this->assertCount($currentappplugins + 2, $DB->get_records('adminpresets_app_plug'));
 139          // Check no new preset has been created.
 140          $this->assertCount($currentpresets, $DB->get_records('adminpresets'));
 141          $this->assertCount($currentitems, $DB->get_records('adminpresets_it'));
 142          $this->assertCount($currentadvitems, $DB->get_records('adminpresets_it_a'));
 143          $this->assertCount($currentplugins, $DB->get_records('adminpresets_plug'));
 144  
 145          // Check the setting values have changed accordingly with the ones defined in the preset.
 146          $this->assertEquals(0, get_config('core', 'enablebadges'));
 147          $this->assertEquals(900, get_config('mod_lesson', 'mediawidth'));
 148          $this->assertEquals(2, get_config('mod_lesson', 'maxanswers'));
 149          $this->assertEquals(0, get_config('mod_lesson', 'maxanswers_adv'));
 150  
 151          // These settings will never change.
 152          $this->assertEquals(1, get_config('core', 'allowemojipicker'));
 153          $this->assertEquals(1, get_config('core', 'enablecompletion'));
 154          $this->assertEquals(0, get_config('core', 'usecomments'));
 155  
 156          // Check the plugins visibility have changed accordingly with the ones defined in the preset.
 157          $enabledplugins = \core\plugininfo\enrol::get_enabled_plugins();
 158          $this->assertArrayNotHasKey('guest', $enabledplugins);
 159          $this->assertArrayHasKey('manual', $enabledplugins);
 160          $enabledplugins = \core\plugininfo\mod::get_enabled_plugins();
 161          $this->assertArrayNotHasKey('glossary', $enabledplugins);
 162          $this->assertArrayHasKey('assign', $enabledplugins);
 163          $enabledplugins = \core\plugininfo\qtype::get_enabled_plugins();
 164          $this->assertArrayHasKey('truefalse', $enabledplugins);
 165      }
 166  }