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