Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 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 310]

   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   * Course global search unit tests.
  19   *
  20   * @package     core
  21   * @copyright   2016 Devang Gaur {@link http://www.devanggaur.com}
  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 . '/search/tests/fixtures/testable_core_search.php');
  29  
  30  /**
  31   * Provides the unit tests for course global search.
  32   *
  33   * @package     core
  34   * @copyright   2016 Devang Gaur {@link http://www.davidmonllao.com}
  35   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class user_search_testcase extends advanced_testcase {
  38  
  39      /**
  40       * @var string Area id
  41       */
  42      protected $userareaid = null;
  43  
  44      public function setUp(): void {
  45          $this->resetAfterTest(true);
  46          set_config('enableglobalsearch', true);
  47  
  48          $this->userareaid = \core_search\manager::generate_areaid('core_user', 'user');
  49  
  50          // Set \core_search::instance to the mock_search_engine as we don't require the search engine to be working to test this.
  51          $search = testable_core_search::instance();
  52      }
  53  
  54      /**
  55       * Indexing users contents.
  56       *
  57       * @return void
  58       */
  59      public function test_users_indexing() {
  60          global $SITE;
  61  
  62          // Returns the instance as long as the area is supported.
  63          $searcharea = \core_search\manager::get_search_area($this->userareaid);
  64          $this->assertInstanceOf('\core_user\search\user', $searcharea);
  65  
  66          $user1 = self::getDataGenerator()->create_user();
  67          $user2 = self::getDataGenerator()->create_user();
  68  
  69          // All records.
  70          // Recordset will produce 4 user records:
  71          // Guest User, Admin User and two above generated users.
  72          $recordset = $searcharea->get_recordset_by_timestamp(0);
  73          $this->assertTrue($recordset->valid());
  74          $nrecords = 0;
  75          foreach ($recordset as $record) {
  76              $this->assertInstanceOf('stdClass', $record);
  77              $doc = $searcharea->get_document($record);
  78              $this->assertInstanceOf('\core_search\document', $doc);
  79              $nrecords++;
  80          }
  81          // If there would be an error/failure in the foreach above the recordset would be closed on shutdown.
  82          $recordset->close();
  83          $this->assertEquals(4, $nrecords);
  84  
  85          // The +2 is to prevent race conditions.
  86          $recordset = $searcharea->get_recordset_by_timestamp(time() + 2);
  87  
  88          // No new records.
  89          $this->assertFalse($recordset->valid());
  90          $recordset->close();
  91  
  92          // Context support; first, try an unsupported context type.
  93          $coursecontext = context_course::instance($SITE->id);
  94          $this->assertNull($searcharea->get_document_recordset(0, $coursecontext));
  95  
  96          // Try a specific user, will only return 1 record (that user).
  97          $rs = $searcharea->get_document_recordset(0, context_user::instance($user1->id));
  98          $this->assertEquals(1, iterator_count($rs));
  99          $rs->close();
 100      }
 101  
 102      /**
 103       * Document contents.
 104       *
 105       * @return void
 106       */
 107      public function test_users_document() {
 108  
 109          // Returns the instance as long as the area is supported.
 110          $searcharea = \core_search\manager::get_search_area($this->userareaid);
 111          $this->assertInstanceOf('\core_user\search\user', $searcharea);
 112  
 113          $user = self::getDataGenerator()->create_user();
 114  
 115          $doc = $searcharea->get_document($user);
 116          $this->assertInstanceOf('\core_search\document', $doc);
 117          $this->assertEquals($user->id, $doc->get('itemid'));
 118          $this->assertEquals($this->userareaid . '-' . $user->id, $doc->get('id'));
 119          $this->assertEquals(SITEID, $doc->get('courseid'));
 120          $this->assertFalse($doc->is_set('userid'));
 121          $this->assertEquals(\core_search\manager::NO_OWNER_ID, $doc->get('owneruserid'));
 122          $this->assertEquals(content_to_text(fullname($user), false), $searcharea->get_document_display_title($doc));
 123          $this->assertEquals(content_to_text($user->description, $user->descriptionformat), $doc->get('content'));
 124      }
 125  
 126      /**
 127       * Document accesses.
 128       *
 129       * @return void
 130       */
 131      public function test_users_access() {
 132          global $CFG;
 133  
 134          // Returns the instance as long as the area is supported.
 135          $searcharea = \core_search\manager::get_search_area($this->userareaid);
 136  
 137          $user1 = self::getDataGenerator()->create_user();
 138          $user2 = self::getDataGenerator()->create_user();
 139          $user3 = self::getDataGenerator()->create_user();
 140          $user4 = self::getDataGenerator()->create_user();
 141          $user5 = self::getDataGenerator()->create_user();
 142          $user5->id = 0; // Visitor (not guest).
 143  
 144          $deleteduser = self::getDataGenerator()->create_user(array('deleted' => 1));
 145          $unconfirmeduser = self::getDataGenerator()->create_user(array('confirmed' => 0));
 146          $suspendeduser = self::getDataGenerator()->create_user(array('suspended' => 1));
 147  
 148          $course1 = self::getDataGenerator()->create_course();
 149          $course2 = self::getDataGenerator()->create_course();
 150  
 151          $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
 152          $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
 153  
 154          $this->getDataGenerator()->enrol_user($user1->id, $course1->id, 'teacher');
 155          $this->getDataGenerator()->enrol_user($user2->id, $course1->id, 'student');
 156          $this->getDataGenerator()->enrol_user($user2->id, $course2->id, 'student');
 157          $this->getDataGenerator()->enrol_user($user3->id, $course2->id, 'student');
 158          $this->getDataGenerator()->enrol_user($user4->id, $course2->id, 'student');
 159          $this->getDataGenerator()->enrol_user($suspendeduser->id, $course1->id, 'student');
 160  
 161          $this->getDataGenerator()->create_group_member(array('userid' => $user2->id, 'groupid' => $group1->id));
 162          $this->getDataGenerator()->create_group_member(array('userid' => $user3->id, 'groupid' => $group1->id));
 163          $this->getDataGenerator()->create_group_member(array('userid' => $user4->id, 'groupid' => $group2->id));
 164  
 165          $this->setAdminUser();
 166          $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($user1->id));
 167          $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($user2->id));
 168          $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($user3->id));
 169          $this->assertEquals(\core_search\manager::ACCESS_DELETED, $searcharea->check_access($deleteduser->id));
 170          $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($unconfirmeduser->id));
 171          $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($suspendeduser->id));
 172          $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access(2));
 173  
 174          $this->setUser($user1);
 175          $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($user1->id));
 176          $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($user2->id));
 177          $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access($user3->id));
 178          $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access($user4->id));
 179          $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access(1));// Guest user can't be accessed.
 180          $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access(2));// Admin user can't be accessed.
 181          $this->assertEquals(\core_search\manager::ACCESS_DELETED, $searcharea->check_access(-123));
 182          $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access($unconfirmeduser->id));
 183          $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($suspendeduser->id));
 184  
 185          $this->setUser($user2);
 186          $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($user1->id));
 187          $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($user2->id));
 188          $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($user3->id));
 189          $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($user4->id));
 190  
 191          $this->setUser($user3);
 192          $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access($user1->id));
 193          $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($user2->id));
 194          $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($user3->id));
 195          $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access($suspendeduser->id));
 196  
 197          $this->setGuestUser();
 198          $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access($user1->id));
 199          $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access($user2->id));
 200          $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access($user3->id));
 201  
 202          $CFG->forceloginforprofiles = 0;
 203          $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($user1->id));
 204          $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($user2->id));
 205          $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($user3->id));
 206  
 207          $this->setUser($user5);
 208          $CFG->forceloginforprofiles = 1;
 209          $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access($user1->id));
 210          $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access($user2->id));
 211          $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access($user3->id));
 212  
 213          $CFG->forceloginforprofiles = 0;
 214          $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($user1->id));
 215          $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($user2->id));
 216          $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($user3->id));
 217      }
 218  
 219      /**
 220       * Test document icon.
 221       */
 222      public function test_get_doc_icon() {
 223          $searcharea = \core_search\manager::get_search_area($this->userareaid);
 224          $user = self::getDataGenerator()->create_user();
 225          $doc = $searcharea->get_document($user);
 226  
 227          $result = $searcharea->get_doc_icon($doc);
 228  
 229          $this->assertEquals('i/user', $result->get_name());
 230          $this->assertEquals('moodle', $result->get_component());
 231      }
 232  
 233      /**
 234       * Test assigned search categories.
 235       */
 236      public function test_get_category_names() {
 237          $searcharea = \core_search\manager::get_search_area($this->userareaid);
 238  
 239          $expected = ['core-users'];
 240          $this->assertEquals($expected, $searcharea->get_category_names());
 241      }
 242  }