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 helper class.
  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 helper
  27   */
  28  class helper_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 int|null $iscore Preset iscore field.
  39       * @param int|null $iscoreresult Expected iscore value for the result preset.
  40       */
  41      public function test_create_preset(?string $name = null, ?string $comments = null, ?int $iscore = null,
  42             ?int $iscoreresult = null): void {
  43          global $CFG, $DB, $USER;
  44  
  45          $this->resetAfterTest();
  46  
  47          $data = [];
  48          if (isset($name)) {
  49              $data['name'] = $name;
  50          }
  51          if (isset($comments)) {
  52              $data['comments'] = $comments;
  53          }
  54          if (isset($iscore)) {
  55              $data['iscore'] = $iscore;
  56          }
  57          if (!isset($iscoreresult)) {
  58              $iscoreresult = manager::NONCORE_PRESET;
  59          }
  60  
  61          // Create a preset.
  62          $presetid = helper::create_preset($data);
  63  
  64          // Check the preset data.
  65          $preset = $DB->get_record('adminpresets', ['id' => $presetid]);
  66  
  67          $this->assertEquals($name, $preset->name);
  68          $this->assertEquals($comments, $preset->comments);
  69          $this->assertEquals(fullname($USER), $preset->author);
  70          $this->assertEquals($iscoreresult, $preset->iscore);
  71          $this->assertEquals($CFG->version, $preset->moodleversion);
  72          $this->assertEquals($CFG->release, $preset->moodlerelease);
  73          $this->assertEquals($CFG->wwwroot, $preset->site);
  74  
  75          // Check the preset is empty and hasn't settings or plugins.
  76          $settings = $DB->get_records('adminpresets_it', ['adminpresetid' => $presetid]);
  77          $this->assertCount(0, $settings);
  78          $plugins = $DB->get_records('adminpresets_plug', ['adminpresetid' => $presetid]);
  79          $this->assertCount(0, $plugins);
  80      }
  81  
  82      /**
  83       * Data provider for test_create_preset().
  84       *
  85       * @return array
  86       */
  87      public function create_preset_provider(): array {
  88          return [
  89              'Default values' => [
  90              ],
  91              'Name not empty' => [
  92                  'name' => 'Preset xaxi name',
  93              ],
  94              'Comments not empty' => [
  95                  'name' => null,
  96                  'comments' => 'This is a different comment',
  97              ],
  98              'Name and comments not empty' => [
  99                  'name' => 'Preset with a super-nice name',
 100                  'comments' => 'This is a comment different from the previous one',
 101              ],
 102              'Starter preset' => [
 103                  'name' => 'Starter',
 104                  'comments' => null,
 105                  'iscore' => manager::STARTER_PRESET,
 106                  'iscoreresult' => manager::STARTER_PRESET,
 107              ],
 108              'Full preset' => [
 109                  'name' => 'Full',
 110                  'comments' => null,
 111                  'iscore' => manager::FULL_PRESET,
 112                  'iscoreresult' => manager::FULL_PRESET,
 113              ],
 114              'Invalid iscore' => [
 115                  'name' => 'Invalid iscore value',
 116                  'comments' => null,
 117                  'iscore' => -1,
 118                  'iscoreresult' => manager::NONCORE_PRESET,
 119              ],
 120          ];
 121      }
 122  
 123      /**
 124       * Test the behaviour of add_item() method.
 125       *
 126       * @covers ::add_item
 127       * @dataProvider add_item_provider
 128       *
 129       * @param string $name Item name.
 130       * @param string $value Item value.
 131       * @param string|null $plugin Item plugin.
 132       * @param string|null $advname If the item is an advanced setting, the name of the advanced setting should be specified here.
 133       * @param string|null $advvalue If the item is an advanced setting, the value of the advanced setting should be specified here.
 134       */
 135      public function test_add_item(string $name, string $value, ?string $plugin = 'none', ?string $advname = null,
 136              ?string $advvalue = null): void {
 137          global $DB;
 138  
 139          $this->resetAfterTest();
 140  
 141          // Create a preset.
 142          $presetid = helper::create_preset([]);
 143          $this->assertEquals(1, $DB->count_records('adminpresets', ['id' => $presetid]));
 144  
 145          // Add items.
 146          $itemid = helper::add_item($presetid, $name, $value, $plugin, $advname, $advvalue);
 147  
 148          // Check settings have been created.
 149          $settings = $DB->get_records('adminpresets_it', ['adminpresetid' => $presetid]);
 150          $this->assertCount(1, $settings);
 151  
 152          $setting = reset($settings);
 153          $this->assertEquals($itemid, $setting->id);
 154          $this->assertEquals($name, $setting->name);
 155          $this->assertEquals($value, $setting->value);
 156          $this->assertEquals($plugin, $setting->plugin);
 157  
 158          if ($advname) {
 159              // Check settings have been created.
 160              $advsettings = $DB->get_records('adminpresets_it_a', ['itemid' => $itemid]);
 161              $this->assertCount(1, $advsettings);
 162  
 163              $advsetting = reset($advsettings);
 164              $this->assertEquals($advname, $advsetting->name);
 165              $this->assertEquals($advvalue, $advsetting->value);
 166          } else {
 167              // Check no advanced items have been created.
 168              $this->assertEquals(0, $DB->count_records('adminpresets_it_a', ['itemid' => $itemid]));
 169          }
 170  
 171          // Check no plugins have been created.
 172          $this->assertEquals(0, $DB->count_records('adminpresets_plug', ['adminpresetid' => $presetid]));
 173      }
 174  
 175      /**
 176       * Data provider for test_add_item().
 177       *
 178       * @return array
 179       */
 180      public function add_item_provider(): array {
 181          return [
 182              'Setting without plugin' => [
 183                  'name' => 'settingname',
 184                  'value' => 'thisisthevalue',
 185              ],
 186              'Setting with plugin' => [
 187                  'name' => 'settingname',
 188                  'value' => 'thisisthevalue',
 189                  'plugin' => 'pluginname',
 190              ],
 191              'Setting with advanced item' => [
 192                  'name' => 'settingname',
 193                  'value' => 'thevalue',
 194                  'plugin' => 'pluginname',
 195                  'advname' => 'advsettingname',
 196                  'advvalue' => 'advsettingvalue',
 197              ],
 198          ];
 199      }
 200  
 201      /**
 202       * Test the behaviour of add_plugin() method.
 203       *
 204       * @covers ::add_plugin
 205       * @dataProvider add_plugin_provider
 206       *
 207       * @param string $type Plugin type.
 208       * @param string $name Plugin name.
 209       * @param mixed $enabled Whether the plugin will be enabled or not.
 210       */
 211      public function test_add_plugin(string $type, string $name, $enabled = 0): void {
 212          global $DB;
 213  
 214          $this->resetAfterTest();
 215  
 216          // Create a preset.
 217          $presetid = helper::create_preset([]);
 218          $this->assertEquals(1, $DB->count_records('adminpresets', ['id' => $presetid]));
 219  
 220          // Add plugin.
 221          $pluginid = helper::add_plugin($presetid, $type, $name, $enabled);
 222  
 223          // Check plugin has been created.
 224          $pluggins = $DB->get_records('adminpresets_plug', ['adminpresetid' => $presetid]);
 225          $this->assertCount(1, $pluggins);
 226  
 227          $plugin = reset($pluggins);
 228          $this->assertEquals($pluginid, $plugin->id);
 229          $this->assertEquals($type, $plugin->plugin);
 230          $this->assertEquals($name, $plugin->name);
 231          $this->assertEquals((int) $enabled, $plugin->enabled);
 232  
 233          // Check no settings have been created.
 234          $this->assertEquals(0, $DB->count_records('adminpresets_it', ['adminpresetid' => $presetid]));
 235      }
 236  
 237      /**
 238       * Data provider for test_add_plugin().
 239       *
 240       * @return array
 241       */
 242      public function add_plugin_provider(): array {
 243          return [
 244              'Plugin: enabled (using int)' => [
 245                  'type' => 'plugintype',
 246                  'name' => 'pluginname',
 247                  'enabled' => 1,
 248              ],
 249              'Plugin: enabled (using bool)' => [
 250                  'type' => 'plugintype',
 251                  'name' => 'pluginname',
 252                  'enabled' => true,
 253              ],
 254              'Plugin: disabled (using int)' => [
 255                  'type' => 'plugintype',
 256                  'name' => 'pluginname',
 257                  'enabled' => 0,
 258              ],
 259              'Plugin: disabled (using bool)' => [
 260                  'type' => 'plugintype',
 261                  'name' => 'pluginname',
 262                  'enabled' => false,
 263              ],
 264              'Plugin: negative int value' => [
 265                  'type' => 'plugintype',
 266                  'name' => 'pluginname',
 267                  'enabled' => -9999,
 268              ],
 269          ];
 270      }
 271  
 272      /**
 273       * Test the behaviour of change_default_preset() method.
 274       *
 275       * @covers ::change_default_preset
 276       * @dataProvider change_default_preset_provider
 277       *
 278       * @param string $preset The preset name to apply or the path to the XML to be imported and applied.
 279       * @param array|null $settings A few settings to check (with their expected values).
 280       * @param array|null $plugins A few module plugins to check (with their expected values for the visibility).
 281       */
 282      public function test_change_default_preset(string $preset, ?array $settings = null, ?array $plugins = null): void {
 283          $this->resetAfterTest();
 284          $this->setAdminUser();
 285  
 286          // We need to change some of the default values; otherwise, the full preset won't be applied, because all the settings
 287          // and plugins are the same.
 288          set_config('enableanalytics', '0');
 289  
 290          $generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets');
 291          $generator->create_preset(['name' => 'Preset 1']);
 292  
 293          $presetid = helper::change_default_preset($preset);
 294  
 295          if (empty($settings) && empty($plugins)) {
 296              // The preset hasn't been applied.
 297              $this->assertNull($presetid);
 298          } else {
 299              // The preset has been applied. Check the settings and plugins are the expected.
 300              $this->assertNotEmpty($presetid);
 301  
 302              // Check the setting values have changed accordingly with the ones defined in the preset.
 303              foreach ($settings as $settingname => $settingvalue) {
 304                  $this->assertEquals($settingvalue, get_config('core', $settingname));
 305              }
 306  
 307              // Check the plugins visibility have changed accordingly with the ones defined in the preset.
 308              $enabledplugins = \core\plugininfo\mod::get_enabled_plugins();
 309              foreach ($plugins as $pluginname => $pluginvalue) {
 310                  if ($pluginvalue) {
 311                      $this->assertArrayHasKey($pluginname, $enabledplugins);
 312                  } else {
 313                      $this->assertArrayNotHasKey($pluginname, $enabledplugins);
 314                  }
 315              }
 316          }
 317      }
 318  
 319      /**
 320       * Data provider for test_change_default_preset().
 321       *
 322       * @return array
 323       */
 324      public function change_default_preset_provider(): array {
 325          return [
 326              'Starter preset' => [
 327                  'preset' => 'starter',
 328                  'settings' => [
 329                      'enablebadges' => 0,
 330                      'enableportfolios' => 0,
 331                  ],
 332                  'plugins' => [
 333                      'assign' => 1,
 334                      'chat' => 0,
 335                      'data' => 0,
 336                      'lesson' => 0,
 337                  ],
 338              ],
 339              'Full preset' => [
 340                  'preset' => 'full',
 341                  'settings' => [
 342                      'enablebadges' => 1,
 343                      'enableportfolios' => 0,
 344                  ],
 345                  'plugins' => [
 346                      'assign' => 1,
 347                      'chat' => 1,
 348                      'data' => 1,
 349                      'lesson' => 1,
 350                  ],
 351              ],
 352              'Preset 1, created manually' => [
 353                  'preset' => 'Preset 1',
 354                  'settings' => [
 355                      'enablebadges' => 0,
 356                      'allowemojipicker' => 1,
 357                  ],
 358                  'plugins' => [
 359                      'assign' => 1,
 360                      'glossary' => 0,
 361                  ],
 362              ],
 363              'Unexisting preset name' => [
 364                  'preset' => 'unexisting',
 365              ],
 366              'Valid XML file' => [
 367                  'preset' => __DIR__ . '/fixtures/import_settings_plugins.xml',
 368                  'settings' => [
 369                      'allowemojipicker' => 1,
 370                      'enableportfolios' => 1,
 371                  ],
 372                  'plugins' => [
 373                      'assign' => 1,
 374                      'chat' => 0,
 375                      'data' => 0,
 376                      'lesson' => 1,
 377                  ],
 378              ],
 379              'Invalid XML file' => [
 380                  'preset' => __DIR__ . '/fixtures/invalid_xml_file.xml',
 381              ],
 382              'Unexisting XML file' => [
 383                  'preset' => __DIR__ . '/fixtures/unexisting.xml',
 384              ],
 385          ];
 386      }
 387  }