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.
   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_brickfield;
  18  
  19  /**
  20   * Class area_test_base provides some utility functions that can be used by testing.
  21   *
  22   * @package    tool_brickfield
  23   * @copyright  2020 onward: Brickfield Education Labs, https://www.brickfield.ie
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  abstract class area_test_base extends \advanced_testcase {
  27      /** @var string Message for failed area test. */
  28      protected $areatestmessage = 'Expected %component% area not found';
  29  
  30      /**
  31       * Create and return an array from a recordset. Recordset is destroyed.
  32       * @param \moodle_recordset $rs
  33       * @return array
  34       */
  35      public function array_from_recordset(\moodle_recordset $rs): array {
  36          $records = [];
  37          foreach ($rs as $record) {
  38              $records[] = $record;
  39          }
  40          // Can't rewind a recordset, so might as well close it.
  41          $rs->close();
  42          return $records;
  43      }
  44  
  45      /**
  46       * Test for specified component information present in area recordset. Recordset cannot be reused.
  47       * @param \moodle_recordset $areasrs
  48       * @param string $component
  49       * @param int $contextid
  50       * @param int $itemid
  51       * @param int|null $courseid
  52       * @param int|null $categoryid
  53       * @return void
  54       */
  55      public function assert_area_in_recordset(\moodle_recordset $areasrs, string $component, int $contextid, int $itemid,
  56                                               ?int $courseid, ?int $categoryid): void {
  57          $this->assert_area_in_array(
  58              $this->array_from_recordset($areasrs),
  59              $component,
  60              $contextid,
  61              $itemid,
  62              $courseid,
  63              $categoryid
  64          );
  65      }
  66  
  67      /**
  68       * Test for specified component information present in area array.
  69       * @param array $areas
  70       * @param string $component
  71       * @param int $contextid
  72       * @param int $itemid
  73       * @param int|null $courseid
  74       * @param int|null $categoryid
  75       * @return void
  76       */
  77      public function assert_area_in_array(array $areas, string $component, int $contextid, int $itemid,
  78                                           ?int $courseid, ?int $categoryid): void {
  79          $found = false;
  80          $message = str_replace('%component%', $component, $this->areatestmessage);
  81          foreach ($areas as $area) {
  82              if (($area->component == $component) &&
  83                  ($area->contextid == $contextid) &&
  84                  ((empty($courseid) ? empty($area->courseid) : ($area->courseid == $courseid))) &&
  85                  ((empty($categoryid) ? empty($area->categoryid) : ($area->categoryid == $categoryid))) &&
  86                  ($area->itemid == $itemid)
  87              ) {
  88                  $found = true;
  89                  break;
  90              }
  91          }
  92          $this->assertTrue($found, $message);
  93      }
  94  }