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  declare(strict_types=1);
  18  
  19  namespace core_admin\external;
  20  
  21  defined('MOODLE_INTERNAL') || die();
  22  
  23  global $CFG;
  24  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  25  
  26  /**
  27   * Unit tests to configure the enabled/disabled state of a plugin.
  28   *
  29   * @package     core
  30   * @covers      \core_admin\external\set_plugin_state
  31   * @copyright   2023 Andrew Lyons <andrew@nicols.co.uk>
  32   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class set_plugin_state_test extends \externallib_advanced_testcase {
  35      /**
  36       * Text execute method.
  37       *
  38       * @dataProvider execute_standard_provider
  39       * @param string $plugin The name of the plugin
  40       * @param int|null $initialstate The initial state of the plugin
  41       * @param int $newstate The target state
  42       * @param int $notificationcount The number of notifications expected
  43       */
  44      public function test_execute(
  45          string $plugin,
  46          ?int $initialstate,
  47          int $newstate,
  48          int $notificationcount,
  49      ): void {
  50          $this->resetAfterTest();
  51          $this->setAdminUser();
  52  
  53          if ($initialstate !== null) {
  54              [$plugintype, $pluginname] = \core_component::normalize_component($plugin);
  55              $manager = \core_plugin_manager::resolve_plugininfo_class($plugintype);
  56              $manager::enable_plugin($pluginname, $initialstate);
  57              \core\notification::fetch();
  58          }
  59  
  60          set_plugin_state::execute($plugin, $newstate);
  61          $this->assertCount($notificationcount, \core\notification::fetch());
  62      }
  63  
  64      /**
  65       * Data provider for base tests of the execute method.
  66       *
  67       * @return array
  68       */
  69      public function execute_standard_provider(): array {
  70          $generatetestsfor = function (string $plugin): array {
  71              return [
  72                  [
  73                      'plugin' => $plugin,
  74                      'initialstate' => null,
  75                      'newstate' => 0,
  76                      'notificationcount' => 1,
  77                  ],
  78                  [
  79                      'plugin' => $plugin,
  80                      'initialstate' => 1,
  81                      'newstate' => 0,
  82                      'notificationcount' => 1,
  83                  ],
  84                  [
  85                      'plugin' => $plugin,
  86                      'initialstate' => 1,
  87                      'newstate' => 1,
  88                      'notificationcount' => 0,
  89                  ],
  90                  [
  91                      'plugin' => $plugin,
  92                      'initialstate' => 0,
  93                      'newstate' => 0,
  94                      'notificationcount' => 0,
  95                  ],
  96                  [
  97                      'plugin' => $plugin,
  98                      'initialstate' => 0,
  99                      'newstate' => 1,
 100                      'notificationcount' => 1,
 101                  ],
 102              ];
 103          };
 104  
 105          return array_merge(
 106              $generatetestsfor('mod_assign'),
 107              $generatetestsfor('editor_tiny'),
 108              $generatetestsfor('tiny_h5p'),
 109              $generatetestsfor('block_badges'),
 110          );
 111      }
 112  
 113      /**
 114       * Test execute method with no login.
 115       */
 116      public function test_execute_no_login(): void {
 117          $this->expectException(\require_login_exception::class);
 118          set_plugin_state::execute('mod_assign', 1);
 119      }
 120  
 121      /**
 122       * Test execute method with no login.
 123       */
 124      public function test_execute_no_capability(): void {
 125          $this->resetAfterTest();
 126          $user = $this->getDataGenerator()->create_user();
 127          $this->setUser($user);
 128          $this->expectException(\required_capability_exception::class);
 129          set_plugin_state::execute('mod_assign', 1);
 130      }
 131  }