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 310] [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   * Unit tests for the condition.
  19   *
  20   * @package availability_group
  21   * @copyright 2014 The Open University
  22   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  use availability_group\condition;
  28  
  29  /**
  30   * Unit tests for the condition.
  31   *
  32   * @package availability_group
  33   * @copyright 2014 The Open University
  34   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class availability_group_condition_testcase extends advanced_testcase {
  37      /**
  38       * Load required classes.
  39       */
  40      public function setUp() {
  41          // Load the mock info class so that it can be used.
  42          global $CFG;
  43          require_once($CFG->dirroot . '/availability/tests/fixtures/mock_info.php');
  44      }
  45  
  46      /**
  47       * Tests constructing and using condition.
  48       */
  49      public function test_usage() {
  50          global $CFG, $USER;
  51          $this->resetAfterTest();
  52          $CFG->enableavailability = true;
  53  
  54          // Erase static cache before test.
  55          condition::wipe_static_cache();
  56  
  57          // Make a test course and user.
  58          $generator = $this->getDataGenerator();
  59          $course = $generator->create_course();
  60          $user = $generator->create_user();
  61          $generator->enrol_user($user->id, $course->id);
  62          $info = new \core_availability\mock_info($course, $user->id);
  63  
  64          // Make 2 test groups, one in a grouping and one not.
  65          $grouping = $generator->create_grouping(array('courseid' => $course->id));
  66          $group1 = $generator->create_group(array('courseid' => $course->id, 'name' => 'G1!'));
  67          groups_assign_grouping($grouping->id, $group1->id);
  68          $group2 = $generator->create_group(array('courseid' => $course->id, 'name' => 'G2!'));
  69  
  70          // Do test (not in group).
  71          $cond = new condition((object)array('id' => (int)$group1->id));
  72  
  73          // Check if available (when not available).
  74          $this->assertFalse($cond->is_available(false, $info, true, $user->id));
  75          $information = $cond->get_description(false, false, $info);
  76          $this->assertRegExp('~You belong to.*G1!~', $information);
  77          $this->assertTrue($cond->is_available(true, $info, true, $user->id));
  78  
  79          // Add user to groups and refresh cache.
  80          groups_add_member($group1, $user);
  81          groups_add_member($group2, $user);
  82          get_fast_modinfo($course->id, 0, true);
  83  
  84          // Recheck.
  85          $this->assertTrue($cond->is_available(false, $info, true, $user->id));
  86          $this->assertFalse($cond->is_available(true, $info, true, $user->id));
  87          $information = $cond->get_description(false, true, $info);
  88          $this->assertRegExp('~do not belong to.*G1!~', $information);
  89  
  90          // Check group 2 works also.
  91          $cond = new condition((object)array('id' => (int)$group2->id));
  92          $this->assertTrue($cond->is_available(false, $info, true, $user->id));
  93  
  94          // What about an 'any group' condition?
  95          $cond = new condition((object)array());
  96          $this->assertTrue($cond->is_available(false, $info, true, $user->id));
  97          $this->assertFalse($cond->is_available(true, $info, true, $user->id));
  98          $information = $cond->get_description(false, true, $info);
  99          $this->assertRegExp('~do not belong to any~', $information);
 100  
 101          // Admin user doesn't belong to a group, but they can access it
 102          // either way (positive or NOT).
 103          $this->setAdminUser();
 104          $this->assertTrue($cond->is_available(false, $info, true, $USER->id));
 105          $this->assertTrue($cond->is_available(true, $info, true, $USER->id));
 106  
 107          // Group that doesn't exist uses 'missing' text.
 108          $cond = new condition((object)array('id' => $group2->id + 1000));
 109          $this->assertFalse($cond->is_available(false, $info, true, $user->id));
 110          $information = $cond->get_description(false, false, $info);
 111          $this->assertRegExp('~You belong to.*\(Missing group\)~', $information);
 112      }
 113  
 114      /**
 115       * Tests the constructor including error conditions. Also tests the
 116       * string conversion feature (intended for debugging only).
 117       */
 118      public function test_constructor() {
 119          // Invalid id (not int).
 120          $structure = (object)array('id' => 'bourne');
 121          try {
 122              $cond = new condition($structure);
 123              $this->fail();
 124          } catch (coding_exception $e) {
 125              $this->assertContains('Invalid ->id', $e->getMessage());
 126          }
 127  
 128          // Valid (with id).
 129          $structure->id = 123;
 130          $cond = new condition($structure);
 131          $this->assertEquals('{group:#123}', (string)$cond);
 132  
 133          // Valid (no id).
 134          unset($structure->id);
 135          $cond = new condition($structure);
 136          $this->assertEquals('{group:any}', (string)$cond);
 137      }
 138  
 139      /**
 140       * Tests the save() function.
 141       */
 142      public function test_save() {
 143          $structure = (object)array('id' => 123);
 144          $cond = new condition($structure);
 145          $structure->type = 'group';
 146          $this->assertEquals($structure, $cond->save());
 147  
 148          $structure = (object)array();
 149          $cond = new condition($structure);
 150          $structure->type = 'group';
 151          $this->assertEquals($structure, $cond->save());
 152      }
 153  
 154      /**
 155       * Tests the update_dependency_id() function.
 156       */
 157      public function test_update_dependency_id() {
 158          $cond = new condition((object)array('id' => 123));
 159          $this->assertFalse($cond->update_dependency_id('frogs', 123, 456));
 160          $this->assertFalse($cond->update_dependency_id('groups', 12, 34));
 161          $this->assertTrue($cond->update_dependency_id('groups', 123, 456));
 162          $after = $cond->save();
 163          $this->assertEquals(456, $after->id);
 164      }
 165  
 166      /**
 167       * Tests the filter_users (bulk checking) function. Also tests the SQL
 168       * variant get_user_list_sql.
 169       */
 170      public function test_filter_users() {
 171          global $DB;
 172          $this->resetAfterTest();
 173  
 174          // Erase static cache before test.
 175          condition::wipe_static_cache();
 176  
 177          // Make a test course and some users.
 178          $generator = $this->getDataGenerator();
 179          $course = $generator->create_course();
 180          $roleids = $DB->get_records_menu('role', null, '', 'shortname, id');
 181          $teacher = $generator->create_user();
 182          $generator->enrol_user($teacher->id, $course->id, $roleids['editingteacher']);
 183          $allusers = array($teacher->id => $teacher);
 184          $students = array();
 185          for ($i = 0; $i < 3; $i++) {
 186              $student = $generator->create_user();
 187              $students[$i] = $student;
 188              $generator->enrol_user($student->id, $course->id, $roleids['student']);
 189              $allusers[$student->id] = $student;
 190          }
 191          $info = new \core_availability\mock_info($course);
 192  
 193          // Make test groups.
 194          $group1 = $generator->create_group(array('courseid' => $course->id));
 195          $group2 = $generator->create_group(array('courseid' => $course->id));
 196  
 197          // Assign students to groups as follows (teacher is not in a group):
 198          // 0: no groups.
 199          // 1: in group 1.
 200          // 2: in group 2.
 201          groups_add_member($group1, $students[1]);
 202          groups_add_member($group2, $students[2]);
 203  
 204          // Test 'any group' condition.
 205          $checker = new \core_availability\capability_checker($info->get_context());
 206          $cond = new condition((object)array());
 207          $result = array_keys($cond->filter_user_list($allusers, false, $info, $checker));
 208          ksort($result);
 209          $expected = array($teacher->id, $students[1]->id, $students[2]->id);
 210          $this->assertEquals($expected, $result);
 211  
 212          // Test it with get_user_list_sql.
 213          list ($sql, $params) = $cond->get_user_list_sql(false, $info, true);
 214          $result = $DB->get_fieldset_sql($sql, $params);
 215          sort($result);
 216          $this->assertEquals($expected, $result);
 217  
 218          // Test NOT version (note that teacher can still access because AAG works
 219          // both ways).
 220          $result = array_keys($cond->filter_user_list($allusers, true, $info, $checker));
 221          ksort($result);
 222          $expected = array($teacher->id, $students[0]->id);
 223          $this->assertEquals($expected, $result);
 224  
 225          // Test with get_user_list_sql.
 226          list ($sql, $params) = $cond->get_user_list_sql(true, $info, true);
 227          $result = $DB->get_fieldset_sql($sql, $params);
 228          sort($result);
 229          $this->assertEquals($expected, $result);
 230  
 231          // Test specific group.
 232          $cond = new condition((object)array('id' => (int)$group1->id));
 233          $result = array_keys($cond->filter_user_list($allusers, false, $info, $checker));
 234          ksort($result);
 235          $expected = array($teacher->id, $students[1]->id);
 236          $this->assertEquals($expected, $result);
 237  
 238          list ($sql, $params) = $cond->get_user_list_sql(false, $info, true);
 239          $result = $DB->get_fieldset_sql($sql, $params);
 240          sort($result);
 241          $this->assertEquals($expected, $result);
 242  
 243          $result = array_keys($cond->filter_user_list($allusers, true, $info, $checker));
 244          ksort($result);
 245          $expected = array($teacher->id, $students[0]->id, $students[2]->id);
 246          $this->assertEquals($expected, $result);
 247  
 248          list ($sql, $params) = $cond->get_user_list_sql(true, $info, true);
 249          $result = $DB->get_fieldset_sql($sql, $params);
 250          sort($result);
 251          $this->assertEquals($expected, $result);
 252      }
 253  }