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] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   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   * Area category unit tests.
  19   *
  20   * @package    core_search
  21   * @copyright  2018 Dmitrii Metelkin <dmitriim@catalyst-au.net>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * Area category unit tests.
  29   *
  30   * @package    core_search
  31   * @copyright  2018 Dmitrii Metelkin <dmitriim@catalyst-au.net>
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class search_area_category_testcase extends advanced_testcase {
  35  
  36      /**
  37       * A helper function to get a mocked search area.
  38       * @param string $areaid
  39       *
  40       * @return \PHPUnit\Framework\MockObject\MockObject
  41       */
  42      protected function get_mocked_area($areaid) {
  43          $builder = $this->getMockBuilder('\core_search\base');
  44          $builder->disableOriginalConstructor();
  45          $builder->setMethods(array('get_area_id'));
  46          $area = $builder->getMockForAbstractClass();
  47          $area->method('get_area_id')->willReturn($areaid);
  48  
  49          return $area;
  50      }
  51  
  52      /**
  53       * A helper function to get a list of search areas.
  54       *
  55       * @return array
  56       */
  57      protected function get_areas() {
  58          $areas = [];
  59          $areas[] = $this->get_mocked_area('area1');
  60          $areas[] = 'String';
  61          $areas[] = 1;
  62          $areas[] = '12';
  63          $areas[] = true;
  64          $areas[] = false;
  65          $areas[] = null;
  66          $areas[] = [$this->get_mocked_area('area2')];
  67          $areas[] = $this;
  68          $areas[] = new stdClass();
  69          $areas[] = $this->get_mocked_area('area3');
  70          $areas[] = $this->get_mocked_area('area4');
  71  
  72          return $areas;
  73      }
  74  
  75      /**
  76       * Test default values.
  77       */
  78      public function test_default_values() {
  79          $category = new \core_search\area_category('test_name', 'test_visiblename');
  80  
  81          $this->assertEquals('test_name', $category->get_name());
  82          $this->assertEquals('test_visiblename', $category->get_visiblename());
  83          $this->assertEquals(0, $category->get_order());
  84          $this->assertEquals([], $category->get_areas());
  85      }
  86  
  87      /**
  88       * Test that all get functions work as expected.
  89       */
  90      public function test_getters() {
  91          $category = new \core_search\area_category('test_name', 'test_visiblename', 4, $this->get_areas());
  92  
  93          $this->assertEquals('test_name', $category->get_name());
  94          $this->assertEquals('test_visiblename', $category->get_visiblename());
  95          $this->assertEquals(4, $category->get_order());
  96  
  97          $this->assertTrue(is_array($category->get_areas()));
  98          $this->assertCount(3, $category->get_areas());
  99          $this->assertTrue(key_exists('area1', $category->get_areas()));
 100          $this->assertTrue(key_exists('area3', $category->get_areas()));
 101          $this->assertTrue(key_exists('area4', $category->get_areas()));
 102      }
 103  
 104      /**
 105       * Test that a list of areas could be set correctly.
 106       */
 107      public function test_list_of_areas_could_be_set() {
 108          $category = new \core_search\area_category('test_name', 'test_visiblename');
 109          $this->assertEquals([], $category->get_areas());
 110  
 111          $category->set_areas($this->get_areas());
 112  
 113          $this->assertTrue(is_array($category->get_areas()));
 114          $this->assertCount(3, $category->get_areas());
 115          $this->assertTrue(key_exists('area1', $category->get_areas()));
 116          $this->assertTrue(key_exists('area3', $category->get_areas()));
 117          $this->assertTrue(key_exists('area4', $category->get_areas()));
 118      }
 119  
 120  }