Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.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  use core_adminpresets\manager;
  20  
  21  /**
  22   * Tests for the export class.
  23   *
  24   * @package    tool_admin_presets
  25   * @category   test
  26   * @copyright  2021 Sara Arjona (sara@moodle.com)
  27   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  28   * @coversDefaultClass \tool_admin_presets\local\action\export
  29   */
  30  class export_test extends \advanced_testcase {
  31  
  32      /**
  33       * Test the behaviour of execute() method.
  34       * @covers ::execute
  35       * @dataProvider export_execute_provider
  36       *
  37       * @param bool $includesensible Whether the sensible settings should be exported too or not.
  38       * @param string $presetname Preset name.
  39       */
  40      public function test_export_execute(bool $includesensible = false, string $presetname = 'Export 1'): void {
  41          global $DB;
  42  
  43          $this->resetAfterTest();
  44          $this->setAdminUser();
  45  
  46          // Get current presets and items.
  47          $currentpresets = $DB->count_records('adminpresets');
  48          $currentadvitems = $DB->count_records('adminpresets_it_a');
  49  
  50          // Initialise some settings (to compare their values have been exported as expected).
  51          set_config('recaptchapublickey', 'abcde');
  52          set_config('enablebadges', '0');
  53          set_config('mediawidth', '900', 'mod_lesson');
  54          set_config('maxanswers', '2', 'mod_lesson');
  55          set_config('maxanswers_adv', '0', 'mod_lesson');
  56          set_config('defaultfeedback', '0', 'mod_lesson');
  57          set_config('defaultfeedback_adv', '1', 'mod_lesson');
  58  
  59          // Get the data we are submitting for the form and mock submitting it.
  60          $formdata = [
  61              'name' => $presetname,
  62              'comments' => ['text' => 'This is a presets for testing export'],
  63              'author' => 'Super-Girl',
  64              'includesensiblesettings' => $includesensible,
  65              'admin_presets_submit' => 'Save changes',
  66          ];
  67          \tool_admin_presets\form\export_form::mock_submit($formdata);
  68  
  69          // Initialise the parameters and create the export class.
  70          $_POST['action'] = 'export';
  71          $_POST['mode'] = 'execute';
  72          $_POST['sesskey'] = sesskey();
  73  
  74          $action = new export();
  75          $sink = $this->redirectEvents();
  76          try {
  77              $action->execute();
  78          } catch (\exception $e) {
  79              // If export action was successfull, redirect should be called so we will encounter an
  80              // 'unsupported redirect error' moodle_exception.
  81              $this->assertInstanceOf(\moodle_exception::class, $e);
  82          } finally {
  83              // Check the preset record has been created.
  84              $presets = $DB->get_records('adminpresets');
  85              $this->assertCount($currentpresets + 1, $presets);
  86              $generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets');
  87              $presetid = $generator->access_protected($action, 'id');
  88              $this->assertArrayHasKey($presetid, $presets);
  89              $preset = $presets[$presetid];
  90              $this->assertEquals($presetname, $preset->name);
  91              $this->assertEquals(manager::NONCORE_PRESET, $preset->iscore);
  92  
  93              // Check the items, advanced attributes and plugins have been created.
  94              $this->assertGreaterThan(0, $DB->count_records('adminpresets_it', ['adminpresetid' => $presetid]));
  95              $this->assertGreaterThan($currentadvitems, $DB->count_records('adminpresets_it_a'));
  96              $this->assertGreaterThan(0, $DB->count_records('adminpresets_plug', ['adminpresetid' => $presetid]));
  97  
  98              // Check settings have been created with the expected values.
  99              $params = ['adminpresetid' => $presetid, 'plugin' => 'none', 'name' => 'enablebadges'];
 100              $setting = $DB->get_record('adminpresets_it', $params);
 101              $this->assertEquals('0', $setting->value);
 102  
 103              $params = ['adminpresetid' => $presetid, 'plugin' => 'mod_lesson', 'name' => 'mediawidth'];
 104              $setting = $DB->get_record('adminpresets_it', $params);
 105              $this->assertEquals('900', $setting->value);
 106  
 107              $params = ['adminpresetid' => $presetid, 'plugin' => 'mod_lesson', 'name' => 'maxanswers'];
 108              $setting = $DB->get_record('adminpresets_it', $params);
 109              $this->assertEquals('2', $setting->value);
 110              $params = ['itemid' => $setting->id, 'name' => 'maxanswers_adv'];
 111              $setting = $DB->get_record('adminpresets_it_a', $params);
 112              $this->assertEquals('0', $setting->value);
 113  
 114              $params = ['adminpresetid' => $presetid, 'plugin' => 'mod_lesson', 'name' => 'defaultfeedback'];
 115              $setting = $DB->get_record('adminpresets_it', $params);
 116              $this->assertEquals('0', $setting->value);
 117              $params = ['itemid' => $setting->id, 'name' => 'defaultfeedback_adv'];
 118              $setting = $DB->get_record('adminpresets_it_a', $params);
 119              $this->assertEquals('1', $setting->value);
 120  
 121              // Check plugins have been created with the expected values.
 122              $manager = \core_plugin_manager::instance();
 123              $plugintype = 'enrol';
 124              $plugins = $manager->get_present_plugins($plugintype);
 125              $enabledplugins = $manager->get_enabled_plugins($plugintype);
 126              foreach ($plugins as $pluginname => $unused) {
 127                  $params = ['adminpresetid' => $presetid, 'plugin' => $plugintype, 'name' => $pluginname];
 128                  $plugin = $DB->get_record('adminpresets_plug', $params);
 129                  $enabled = (!empty($enabledplugins) && array_key_exists($pluginname, $enabledplugins));
 130                  $this->assertEquals($enabled, (bool) $plugin->enabled);
 131              }
 132  
 133              // Check whether sensible settings have been exported or not.
 134              $params = ['adminpresetid' => $presetid, 'plugin' => 'none', 'name' => 'recaptchapublickey'];
 135              $recaptchasetting = $DB->get_record('adminpresets_it', $params);
 136              $params = ['adminpresetid' => $presetid, 'plugin' => 'none', 'name' => 'cronremotepassword'];
 137              $cronsetting = $DB->get_record('adminpresets_it', $params);
 138              if ($includesensible) {
 139                  $this->assertEquals('abcde', $recaptchasetting->value);
 140                  $this->assertNotFalse($cronsetting);
 141              } else {
 142                  $this->assertFalse($recaptchasetting);
 143                  $this->assertFalse($cronsetting);
 144              }
 145  
 146              // Check the export event has been raised.
 147              $events = $sink->get_events();
 148              $sink->close();
 149              $event = reset($events);
 150              $this->assertInstanceOf('\\tool_admin_presets\\event\\preset_exported', $event);
 151          }
 152      }
 153  
 154      /**
 155       * Data provider for test_export_execute().
 156       *
 157       * @return array
 158       */
 159      public function export_execute_provider(): array {
 160          return [
 161              'Export settings and plugins, excluding sensible' => [
 162                  'includesensible' => false,
 163              ],
 164              'Export settings and plugins, including sensible' => [
 165                  'includesensible' => true,
 166              ],
 167              'Export settings and plugins, with Starter name (it should not be marked as core)' => [
 168                  'includesensible' => false,
 169                  'presetname' => 'Starter',
 170              ],
 171              'Export settings and plugins, with Full name (it should not be marked as core)' => [
 172                  'includesensible' => false,
 173                  'presetname' => 'Full',
 174              ],
 175          ];
 176      }
 177  }