Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [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   * Unit tests for tool_health.
  19   *
  20   * @package    tool_health
  21   * @copyright  2013 Marko Vidberg
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  global $CFG;
  28  require_once($CFG->dirroot . '/' . $CFG->admin . '/tool/health/locallib.php');
  29  
  30  /**
  31   * Health lib testcase.
  32   *
  33   * @package    tool_health
  34   * @copyright  2013 Marko Vidberg
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class healthlib_testcase extends advanced_testcase {
  38  
  39      /**
  40       * Data provider for test_tool_health_category_find_loops.
  41       */
  42      public static function provider_loop_categories() {
  43          return array(
  44              // One item loop including root.
  45              0 => array(
  46                  array(
  47                      '1' => (object) array('id' => 1, 'parent' => 1)
  48                  ),
  49                  array(
  50                      '1' => (object) array('id' => 1, 'parent' => 1)
  51                  ),
  52              ),
  53              // One item loop not including root.
  54              1 => array(
  55                  array(
  56                      '1' => (object) array('id' => 1, 'parent' => 0),
  57                      '2' => (object) array('id' => 2, 'parent' => 2)
  58                  ),
  59                  array(
  60                      '2' => (object) array('id' => 2, 'parent' => 2)
  61                  ),
  62              ),
  63              // Two item loop including root.
  64              2 => array(
  65                  array(
  66                      '1' => (object) array('id' => 1, 'parent' => 2),
  67                      '2' => (object) array('id' => 2, 'parent' => 1)
  68                  ),
  69                  array(
  70                      '2' => (object) array('id' => 2, 'parent' => 1),
  71                      '1' => (object) array('id' => 1, 'parent' => 2),
  72                  )
  73              ),
  74              // Two item loop not including root.
  75              3 => array(
  76                  array(
  77                      '1' => (object) array('id' => 1, 'parent' => 0),
  78                      '2' => (object) array('id' => 2, 'parent' => 3),
  79                      '3' => (object) array('id' => 3, 'parent' => 2),
  80                  ),
  81                  array(
  82                      '3' => (object) array('id' => 3, 'parent' => 2),
  83                      '2' => (object) array('id' => 2, 'parent' => 3),
  84                  )
  85              ),
  86              // Three item loop including root.
  87              4 => array(
  88                  array(
  89                      '1' => (object) array('id' => 1, 'parent' => 2),
  90                      '2' => (object) array('id' => 2, 'parent' => 3),
  91                      '3' => (object) array('id' => 3, 'parent' => 1),
  92                  ),
  93                  array(
  94                      '3' => (object) array('id' => 3, 'parent' => 1),
  95                      '1' => (object) array('id' => 1, 'parent' => 2),
  96                      '2' => (object) array('id' => 2, 'parent' => 3),
  97                  )
  98              ),
  99              // Three item loop not including root.
 100              5 => array(
 101                  array(
 102                      '1' => (object) array('id' => 1, 'parent' => 0),
 103                      '2' => (object) array('id' => 2, 'parent' => 3),
 104                      '3' => (object) array('id' => 3, 'parent' => 4),
 105                      '4' => (object) array('id' => 4, 'parent' => 2)
 106                  ),
 107                  array(
 108                      '4' => (object) array('id' => 4, 'parent' => 2),
 109                      '2' => (object) array('id' => 2, 'parent' => 3),
 110                      '3' => (object) array('id' => 3, 'parent' => 4),
 111                  )
 112              ),
 113              // Multi-loop.
 114              6 => array(
 115                  array(
 116                      '1' => (object) array('id' => 1, 'parent' => 2),
 117                      '2' => (object) array('id' => 2, 'parent' => 1),
 118                      '3' => (object) array('id' => 3, 'parent' => 4),
 119                      '4' => (object) array('id' => 4, 'parent' => 5),
 120                      '5' => (object) array('id' => 5, 'parent' => 3),
 121                      '6' => (object) array('id' => 6, 'parent' => 6),
 122                      '7' => (object) array('id' => 7, 'parent' => 1),
 123                      '8' => (object) array('id' => 8, 'parent' => 7),
 124                  ),
 125                  array(
 126                      '1' => (object) array('id' => 1, 'parent' => 2),
 127                      '2' => (object) array('id' => 2, 'parent' => 1),
 128                      '8' => (object) array('id' => 8, 'parent' => 7),
 129                      '7' => (object) array('id' => 7, 'parent' => 1),
 130                      '6' => (object) array('id' => 6, 'parent' => 6),
 131                      '5' => (object) array('id' => 5, 'parent' => 3),
 132                      '3' => (object) array('id' => 3, 'parent' => 4),
 133                      '4' => (object) array('id' => 4, 'parent' => 5),
 134                  )
 135              ),
 136              // Double-loop
 137              7 => array(
 138                  array(
 139                      '1' => (object) array('id' => 1, 'parent' => 2),
 140                      '2' => (object) array('id' => 2, 'parent' => 1),
 141                      '3' => (object) array('id' => 3, 'parent' => 2),
 142                      '4' => (object) array('id' => 4, 'parent' => 2),
 143                  ),
 144                  array(
 145                      '4' => (object) array('id' => 4, 'parent' => 2),
 146                      '3' => (object) array('id' => 3, 'parent' => 2),
 147                      '2' => (object) array('id' => 2, 'parent' => 1),
 148                      '1' => (object) array('id' => 1, 'parent' => 2),
 149                  )
 150              )
 151          );
 152      }
 153  
 154      /**
 155       * Data provider for test_tool_health_category_find_missing_parents.
 156       */
 157      public static function provider_missing_parent_categories() {
 158          return array(
 159             // Test for two items, both with direct ancestor (parent) missing.
 160              0 => array(
 161                  array(
 162                      '1' => (object) array('id' => 1, 'parent' => 0),
 163                      '2' => (object) array('id' => 2, 'parent' => 3),
 164                      '4' => (object) array('id' => 4, 'parent' => 5),
 165                      '6' => (object) array('id' => 6, 'parent' => 2)
 166                  ),
 167                  array(
 168                      '4' => (object) array('id' => 4, 'parent' => 5),
 169                      '2' => (object) array('id' => 2, 'parent' => 3)
 170                  ),
 171              )
 172          );
 173      }
 174  
 175      /**
 176       * Test finding loops between two items referring to each other.
 177       *
 178       * @param array $categories
 179       * @param array $expected
 180       * @dataProvider provider_loop_categories
 181       */
 182      public function test_tool_health_category_find_loops($categories, $expected) {
 183          $loops = tool_health_category_find_loops($categories);
 184          $this->assertEquals($expected, $loops);
 185      }
 186  
 187      /**
 188       * Test finding missing parent categories.
 189       *
 190       * @param array $categories
 191       * @param array $expected
 192       * @dataProvider provider_missing_parent_categories
 193       */
 194      public function test_tool_health_category_find_missing_parents($categories, $expected) {
 195          $missingparent = tool_health_category_find_missing_parents($categories);
 196          $this->assertEquals($expected, $missingparent);
 197      }
 198  
 199      /**
 200       * Test listing missing parent categories.
 201       */
 202      public function test_tool_health_category_list_missing_parents() {
 203          $missingparent = array((object) array('id' => 2, 'parent' => 3, 'name' => 'test'),
 204                                 (object) array('id' => 4, 'parent' => 5, 'name' => 'test2'));
 205          $result = tool_health_category_list_missing_parents($missingparent);
 206          $this->assertRegExp('/Category 2: test/', $result);
 207          $this->assertRegExp('/Category 4: test2/', $result);
 208      }
 209  
 210      /**
 211       * Test listing loop categories.
 212       */
 213      public function test_tool_health_category_list_loops() {
 214          $loops = array((object) array('id' => 2, 'parent' => 3, 'name' => 'test'));
 215          $result = tool_health_category_list_loops($loops);
 216          $this->assertRegExp('/Category 2: test/', $result);
 217      }
 218  }