Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

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