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  /**
  20   * Tests for the base 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\base
  27   */
  28  class base_test extends \advanced_testcase {
  29  
  30      /**
  31       * Test the behaviour of log() method.
  32       *
  33       * @covers ::log
  34       * @dataProvider log_provider
  35       *
  36       * @param string $action Action to log.
  37       * @param string $mode Mode to log.
  38       * @param string|null $expectedclassname The expected classname or null if no event is expected.
  39       */
  40      public function test_base_log(string $action, string $mode, ?string $expectedclassname): void {
  41          $this->resetAfterTest();
  42          $this->setAdminUser();
  43  
  44          // Initialise the parameters and create the class.
  45          if (!empty($mode)) {
  46              $_POST['mode'] = $mode;
  47          }
  48          if (!empty($action)) {
  49              $_POST['action'] = $action;
  50          }
  51          $base = new base();
  52  
  53          // Redirect events (to capture them) and call to the log method.
  54          $sink = $this->redirectEvents();
  55          $base->log();
  56          $events = $sink->get_events();
  57          $sink->close();
  58          $event = reset($events);
  59  
  60          // Validate event data.
  61          if (is_null($expectedclassname)) {
  62              $this->assertFalse($event);
  63          } else {
  64              $this->assertInstanceOf('\\tool_admin_presets\\event\\' . $expectedclassname, $event);
  65          }
  66      }
  67  
  68      /**
  69       * Data provider for test_base_log().
  70       *
  71       * @return array
  72       */
  73      public function log_provider(): array {
  74          return [
  75              // Action = base.
  76              'action=base and mode = show' => [
  77                  'action' => 'base',
  78                  'mode' => 'show',
  79                  'expectedclassname' => 'presets_listed',
  80              ],
  81              'action=base and mode = execute' => [
  82                  'action' => 'base',
  83                  'mode' => 'execute',
  84                  'expectedclassname' => 'presets_listed',
  85              ],
  86  
  87              // Action = delete.
  88              'action=delete and mode = show' => [
  89                  'action' => 'delete',
  90                  'mode' => 'show',
  91                  'expectedclassname' => null,
  92              ],
  93              'action=delete and mode = execute' => [
  94                  'action' => 'delete',
  95                  'mode' => 'execute',
  96                  'expectedclassname' => 'preset_deleted',
  97              ],
  98              'mode = delete and action = base' => [
  99                  'action' => 'base',
 100                  'mode' => 'delete',
 101                  'expectedclassname' => 'preset_deleted',
 102              ],
 103  
 104              // Action = export.
 105              'action=export and mode = show' => [
 106                  'action' => 'export',
 107                  'mode' => 'show',
 108                  'expectedclassname' => null,
 109              ],
 110              'action=export and mode = execute' => [
 111                  'action' => 'export',
 112                  'mode' => 'execute',
 113                  'expectedclassname' => 'preset_exported',
 114              ],
 115              'mode = export and action = download_xml' => [
 116                  'action' => 'export',
 117                  'mode' => 'download_xml',
 118                  'expectedclassname' => 'preset_downloaded',
 119              ],
 120  
 121              // Action = load.
 122              'action=load and mode = show' => [
 123                  'action' => 'load',
 124                  'mode' => 'show',
 125                  'expectedclassname' => 'preset_previewed',
 126              ],
 127              'action=load and mode = execute' => [
 128                  'action' => 'load',
 129                  'mode' => 'execute',
 130                  'expectedclassname' => 'preset_loaded',
 131              ],
 132  
 133              // Unexisting action/method.
 134              'Unexisting action' => [
 135                  'action' => 'unexisting',
 136                  'mode' => 'show',
 137                  'expectedclassname' => null,
 138              ],
 139              'Unexisting mode' => [
 140                  'action' => 'delete',
 141                  'mode' => 'unexisting',
 142                  'expectedclassname' => null,
 143              ],
 144          ];
 145      }
 146  }