Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.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 core_adminpresets;
  18  
  19  /**
  20   * Tests for the data generator.
  21   *
  22   * @package    core_adminpresets
  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 core_adminpresets_generator
  27   */
  28  class generator_test extends \advanced_testcase {
  29  
  30      /**
  31       * Test the behaviour of create_preset() method.
  32       *
  33       * @covers ::create_preset
  34       * @dataProvider create_preset_provider
  35       *
  36       * @param string|null $name Preset name field.
  37       * @param string|null $comments Preset comments field.
  38       * @param string|null $author Preset author field.
  39       * @param bool $applypreset Whether the preset should be applied or not.
  40       * @param int|null $iscore Whether the preset is a core preset or not.
  41       * @param int|null $iscoreresult Expected iscore value for the result preset.
  42       */
  43      public function test_create_preset(?string $name = null, ?string $comments = null, ?string $author = null,
  44              bool $applypreset = false, ?int $iscore = null, ?int $iscoreresult = null): void {
  45          global $CFG, $DB;
  46  
  47          $this->resetAfterTest();
  48  
  49          $data = [];
  50          if (isset($name)) {
  51              $data['name'] = $name;
  52          } else {
  53              // Set the default value used in the generator.
  54              $name = 'Preset default name';
  55          }
  56          if (isset($comments)) {
  57              // Set the default value used in the generator.
  58              $data['comments'] = $comments;
  59          } else {
  60              // Set the default value used in the generator.
  61              $comments = 'Preset default comment';
  62          }
  63          if (isset($author)) {
  64              $data['author'] = $author;
  65          } else {
  66              $author = 'Default author';
  67          }
  68          if ($applypreset) {
  69              $data['applypreset'] = $applypreset;
  70          }
  71          if (isset($iscore)) {
  72              $data['iscore'] = $iscore;
  73          }
  74          if (!isset($iscoreresult)) {
  75              $iscoreresult = manager::NONCORE_PRESET;
  76          }
  77  
  78          // Create a preset.
  79          $presetid = $this->getDataGenerator()->get_plugin_generator('core_adminpresets')->create_preset($data);
  80  
  81          // Check the preset data.
  82          $preset = $DB->get_record('adminpresets', ['id' => $presetid]);
  83  
  84          $this->assertEquals($name, $preset->name);
  85          $this->assertEquals($comments, $preset->comments);
  86          $this->assertEquals($author, $preset->author);
  87          $this->assertEquals($iscoreresult, $preset->iscore);
  88          $this->assertEquals($CFG->version, $preset->moodleversion);
  89          $this->assertEquals($CFG->release, $preset->moodlerelease);
  90          $this->assertEquals($CFG->wwwroot, $preset->site);
  91  
  92          // Check the settings.
  93          $settings = $DB->get_records('adminpresets_it', ['adminpresetid' => $presetid]);
  94          $this->assertCount(4, $settings);
  95          // These are the settings created in the generator. Check the results match them.
  96          $expectedsettings = [
  97              'enablebadges' => 0,
  98              'allowemojipicker' => 1,
  99              'mediawidth' => 900,
 100              'maxanswers' => 2,
 101          ];
 102          foreach ($settings as $setting) {
 103              $this->assertArrayHasKey($setting->name, $expectedsettings);
 104              $this->assertEquals($expectedsettings[$setting->name], $setting->value);
 105          }
 106  
 107          // Check the advanced settings (should be only one).
 108          $settingsid = array_keys($settings);
 109          list($insql, $inparams) = $DB->get_in_or_equal($settingsid);
 110          $advsettings = $DB->get_records_select('adminpresets_it_a', 'itemid ' . $insql, $inparams);
 111          $this->assertCount(1, $advsettings);
 112          $advsetting = reset($advsettings);
 113          $this->assertEquals('maxanswers_adv', $advsetting->name);
 114          $this->assertEquals(0, $advsetting->value);
 115  
 116          // Check the plugins.
 117          $plugins = $DB->get_records('adminpresets_plug', ['adminpresetid' => $presetid]);
 118          $this->assertCount(3, $plugins);
 119          // These are the plugins created in the generator. Check the results match them.
 120          $expectedplugins = [
 121              'enrol' => [
 122                  'guest' => 0,
 123              ],
 124              'mod' => [
 125                  'glossary' => 0,
 126              ],
 127              'qtype' => [
 128                  'truefalse' => 1,
 129              ],
 130          ];
 131          foreach ($plugins as $plugin) {
 132              $this->assertArrayHasKey($plugin->plugin, $expectedplugins);
 133              $this->assertArrayHasKey($plugin->name, $expectedplugins[$plugin->plugin]);
 134              $this->assertEquals($expectedplugins[$plugin->plugin][$plugin->name], $plugin->enabled);
 135          }
 136  
 137          if ($applypreset) {
 138              // Verify that the preset has been applied.
 139              $apps = $DB->get_records('adminpresets_app', ['adminpresetid' => $presetid]);
 140              $this->assertCount(1, $apps);
 141              $app = reset($apps);
 142  
 143              // Check the applied settings.
 144              $appsettings = $DB->get_records('adminpresets_app_it', ['adminpresetapplyid' => $app->id]);
 145              $this->assertCount(3, $appsettings);
 146              // These are the settings created in the generator (all but the allowemojipicker because it hasn't changed).
 147              $expectedappsettings = $expectedsettings;
 148              unset($expectedappsettings['allowemojipicker']);
 149              // Check the results match the expected settings applied.
 150              foreach ($appsettings as $appsetting) {
 151                  $configlog = $DB->get_record('config_log', ['id' => $appsetting->configlogid]);
 152                  $this->assertArrayHasKey($configlog->name, $expectedappsettings);
 153                  $this->assertEquals($expectedappsettings[$configlog->name], $configlog->value);
 154              }
 155  
 156              $appsettings = $DB->get_records('adminpresets_app_it_a', ['adminpresetapplyid' => $app->id]);
 157              $this->assertCount(1, $appsettings);
 158              $appsetting = reset($appsettings);
 159              $configlog = $DB->get_record('config_log', ['id' => $appsetting->configlogid]);
 160              $this->assertEquals('maxanswers_adv', $configlog->name);
 161              $this->assertEquals(0, $configlog->value);
 162  
 163              // Check the applied plugins.
 164              $appplugins = $DB->get_records('adminpresets_app_plug', ['adminpresetapplyid' => $app->id]);
 165              $this->assertCount(2, $appplugins);
 166              // These are the plugins created in the generator (all but the qtype_truefalse because it hasn't changed).
 167              $expectedappplugins = $expectedplugins;
 168              unset($expectedappplugins['qtype']);
 169              // Check the results match the expected plugins applied.
 170              foreach ($appplugins as $appplugin) {
 171                  $this->assertArrayHasKey($appplugin->plugin, $expectedappplugins);
 172                  $this->assertArrayHasKey($appplugin->name, $expectedappplugins[$appplugin->plugin]);
 173                  $this->assertEquals($expectedappplugins[$appplugin->plugin][$appplugin->name], $appplugin->value);
 174              }
 175          }
 176      }
 177  
 178      /**
 179       * Data provider for test_create_preset().
 180       *
 181       * @return array
 182       */
 183      public function create_preset_provider(): array {
 184          return [
 185              'Default values' => [
 186              ],
 187              'Name not empty' => [
 188                  'name' => 'Preset xaxi name',
 189              ],
 190              'Comment not empty' => [
 191                  'name' => null,
 192                  'comments' => 'This is a different comment',
 193              ],
 194              'Author not empty' => [
 195                  'name' => null,
 196                  'comments' => null,
 197                  'author' => 'Ada Lovelace',
 198              ],
 199              'No default values for all the fields' => [
 200                  'name' => 'Preset with a super-nice name',
 201                  'comments' => 'This is a comment different from the previous one',
 202                  'author' => 'Alejandro Sanz',
 203              ],
 204              'Apply preset' => [
 205                  'name' => null,
 206                  'comments' => null,
 207                  'author' => null,
 208                  'applypreset' => true,
 209              ],
 210              'Starter preset' => [
 211                  'name' => 'Starter',
 212                  'comments' => null,
 213                  'author' => null,
 214                  'applypreset' => false,
 215                  'iscore' => manager::STARTER_PRESET,
 216                  'iscoreresult' => manager::STARTER_PRESET,
 217              ],
 218              'Full preset' => [
 219                  'name' => 'Full',
 220                  'comments' => null,
 221                  'author' => null,
 222                  'applypreset' => false,
 223                  'iscore' => manager::FULL_PRESET,
 224                  'iscoreresult' => manager::FULL_PRESET,
 225              ],
 226              'Invalid iscore' => [
 227                  'name' => 'Invalid iscore value',
 228                  'comments' => null,
 229                  'author' => null,
 230                  'applypreset' => false,
 231                  'iscore' => -1,
 232                  'iscoreresult' => manager::NONCORE_PRESET,
 233              ],
 234          ];
 235      }
 236  }