Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 39 and 311]

   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  /**
  18   * Fixture for mocking callbacks used in \core_course_category
  19   *
  20   * @package    core_course
  21   * @category   test
  22   * @copyright  2020 Ruslan Kabalin
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace core_course\test {
  27  
  28      /**
  29       * Class mock_hooks
  30       *
  31       * @package    core_course
  32       * @category   test
  33       * @copyright  2020 Ruslan Kabalin
  34       * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35       */
  36      class mock_hooks {
  37          /** @var bool $cancoursecategorydelete */
  38          private static $cancoursecategorydelete = true;
  39  
  40          /** @var bool $cancoursecategorydeletemove */
  41          private static $cancoursecategorydeletemove = true;
  42  
  43          /** @var string $getcoursecategorycontents */
  44          private static $getcoursecategorycontents = '';
  45  
  46          /** @var array $callingarguments */
  47          private static $callingarguments = [];
  48  
  49          /**
  50           * Set calling arguments.
  51           *
  52           * This is supposed to be used in the callbacks to store arguments passed to callback.
  53           *
  54           * @param array $callingarguments
  55           */
  56          public static function set_calling_arguments($callingarguments) {
  57              self::$callingarguments = $callingarguments;
  58          }
  59  
  60          /**
  61           * Get calling arguments.
  62           *
  63           * This is supposed to be used in the test to verify arguments passed to callback.
  64           * This method also reset stored calling arguments.
  65           *
  66           * @return array $callingarguments
  67           */
  68          public static function get_calling_arguments() {
  69              $callingarguments = self::$callingarguments;
  70              self::$callingarguments = [];
  71              return $callingarguments;
  72          }
  73  
  74          /**
  75           * Get can_course_category_delete callback return.
  76           *
  77           * @return bool
  78           */
  79          public static function get_can_course_category_delete_return() : bool {
  80              return self::$cancoursecategorydelete;
  81          }
  82  
  83          /**
  84           * Sets can_course_category_delete callback return.
  85           *
  86           * @param bool $return
  87           */
  88          public static function set_can_course_category_delete_return(bool $return) {
  89              self::$cancoursecategorydelete = $return;
  90          }
  91  
  92          /**
  93           * Get can_course_category_delete_move callback return.
  94           *
  95           * @return bool
  96           */
  97          public static function get_can_course_category_delete_move_return() : bool {
  98              return self::$cancoursecategorydeletemove;
  99          }
 100  
 101          /**
 102           * Sets can_course_category_delete_move callback return.
 103           *
 104           * @param bool $return
 105           */
 106          public static function set_can_course_category_delete_move_return(bool $return) {
 107              self::$cancoursecategorydeletemove = $return;
 108          }
 109  
 110          /**
 111           * Get get_course_category_contents callback return.
 112           *
 113           * @return string
 114           */
 115          public static function get_get_course_category_contents_return() : string {
 116              return self::$getcoursecategorycontents;
 117          }
 118  
 119          /**
 120           * Sets get_course_category_contents callback return.
 121           *
 122           * @param string $return
 123           */
 124          public static function set_get_course_category_contents_return(string $return) {
 125              self::$getcoursecategorycontents = $return;
 126          }
 127      }
 128  }
 129  
 130  namespace {
 131  
 132      use core_course\test\mock_hooks;
 133  
 134      /**
 135       * Test pre_course_category_delete callback.
 136       *
 137       * @param object $category
 138       */
 139      function tool_unittest_pre_course_category_delete(object $category) {
 140          mock_hooks::set_calling_arguments(func_get_args());
 141      }
 142  
 143      /**
 144       * Test pre_course_category_delete_move callback.
 145       *
 146       * @param core_course_category $category
 147       * @param core_course_category $newcategory
 148       */
 149      function tool_unittest_pre_course_category_delete_move(core_course_category $category, core_course_category $newcategory) {
 150          mock_hooks::set_calling_arguments(func_get_args());
 151      }
 152  
 153      /**
 154       * Test can_course_category_delete callback.
 155       *
 156       * @param core_course_category $category
 157       * @return bool
 158       */
 159      function tool_unittest_can_course_category_delete(core_course_category $category) {
 160          mock_hooks::set_calling_arguments(func_get_args());
 161          return mock_hooks::get_can_course_category_delete_return();
 162      }
 163  
 164      /**
 165       * Test can_course_category_delete_move callback.
 166       *
 167       * @param core_course_category $category
 168       * @param core_course_category $newcategory
 169       * @return bool
 170       */
 171      function tool_unittest_can_course_category_delete_move(core_course_category $category, core_course_category $newcategory) {
 172          mock_hooks::set_calling_arguments(func_get_args());
 173          return mock_hooks::get_can_course_category_delete_move_return();
 174      }
 175  
 176      /**
 177       * Test get_course_category_contents callback.
 178       *
 179       * @param core_course_category $category
 180       * @return string
 181       */
 182      function tool_unittest_get_course_category_contents(core_course_category $category) {
 183          mock_hooks::set_calling_arguments(func_get_args());
 184          return mock_hooks::get_get_course_category_contents_return();
 185      }
 186  }