Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
   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 report_log;
  18  
  19  /**
  20   * Class report_log\renderable_test to cover functions in \report_log_renderable.
  21   *
  22   * @package    report_log
  23   * @copyright  2023 Stephan Robotta <stephan.robotta@bfh.ch>
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
  25   */
  26  class renderable_test extends \advanced_testcase {
  27  
  28      /**
  29       * @var [stdClass] The students.
  30       */
  31      private $student = [];
  32  
  33      /**
  34       * @var [stdClass] The teachers.
  35       */
  36      private $teacher = [];
  37  
  38      /**
  39       * @var [stdClass] The groups.
  40       */
  41      private $group = [];
  42  
  43      /**
  44       * @var stdClass The course.
  45       */
  46      private $course;
  47  
  48      /**
  49       * Set up a course with two groups, three students being each in one of the groups,
  50       * two teachers each in either group while the second teacher is also member of the other group.
  51       * @return void
  52       * @throws \coding_exception
  53       */
  54      public function setUp(): void {
  55          global $PAGE;
  56          $this->course = $this->getDataGenerator()->create_course(['groupmode' => 1]);
  57          $this->group[] = $this->getDataGenerator()->create_group(['courseid' => $this->course->id]);
  58          $this->group[] = $this->getDataGenerator()->create_group(['courseid' => $this->course->id]);
  59  
  60          for ($i = 0; $i < 3; $i++) {
  61              $this->student[$i] = $this->getDataGenerator()->create_user();
  62              $this->getDataGenerator()->enrol_user($this->student[$i]->id, $this->course->id, 'student');
  63              $this->getDataGenerator()->create_group_member([
  64                  'groupid' => $this->group[$i % 2]->id,
  65                  'userid' => $this->student[$i]->id,
  66              ]);
  67          }
  68          for ($i = 0; $i < 2; $i++) {
  69              $this->teacher[$i] = $this->getDataGenerator()->create_user();
  70              $this->getDataGenerator()->enrol_user($this->teacher[$i]->id, $this->course->id, 'editingteacher');
  71              $this->getDataGenerator()->create_group_member([
  72                  'groupid' => $this->group[$i]->id,
  73                  'userid' => $this->teacher[$i]->id,
  74              ]);
  75          }
  76          // Make teacher2 also member of group1.
  77          $this->getDataGenerator()->create_group_member([
  78              'groupid' => $this->group[0]->id,
  79              'userid' => $this->teacher[1]->id,
  80          ]);
  81  
  82          $PAGE->set_url('/report/log/index.php?id=' . $this->course->id);
  83          $this->resetAfterTest();
  84      }
  85  
  86      /**
  87       * Test report_log_renderable::get_user_list().
  88       * @covers \report_log_renderable::get_user_list
  89       * @return void
  90       */
  91      public function test_get_user_list() {
  92          // Fetch all users of group 1 and the guest user.
  93          $userids = $this->fetch_users_from_renderable((int)$this->student[0]->id);
  94          $this->assertCount(5, $userids);
  95          $this->assertContains((int)$this->student[0]->id, $userids); // His own group (group 1).
  96          $this->assertNotContains((int)$this->student[1]->id, $userids); // He is in group 2.
  97          $this->assertContains((int)$this->teacher[0]->id, $userids); // He is in group 1.
  98          $this->assertContains((int)$this->teacher[1]->id, $userids); // He is in both groups.
  99  
 100          // Fetch users of all groups and the guest user. The teacher has the capability moodle/site:accessallgroups.
 101          $this->setUser($this->teacher[1]->id);
 102          $renderable = new \report_log_renderable("", (int)$this->course->id, $this->teacher[1]->id);
 103          $users = $renderable->get_user_list();
 104          $this->assertCount(6, $users);
 105  
 106          // Fetch users of group 2 and the guest user.
 107          $userids = $this->fetch_users_from_renderable((int)$this->student[1]->id);
 108          $this->assertCount( 3, $userids);
 109          $this->assertNotContains((int)$this->student[0]->id, $userids);
 110          $this->assertContains((int)$this->student[1]->id, $userids);
 111          $this->assertNotContains((int)$this->teacher[0]->id, $userids);
 112          $this->assertContains((int)$this->teacher[1]->id, $userids);
 113  
 114          // Fetch users of group 2 and test user as teacher2 but limited to his group.
 115          $userids = $this->fetch_users_from_renderable((int)$this->teacher[1]->id, (int)$this->group[1]->id);
 116          $this->assertCount( 3, $userids);
 117          $this->assertNotContains((int)$this->student[0]->id, $userids);
 118          $this->assertContains((int)$this->student[1]->id, $userids);
 119          $this->assertNotContains((int)$this->teacher[0]->id, $userids);
 120          $this->assertContains((int)$this->teacher[1]->id, $userids);
 121  
 122      }
 123  
 124      /**
 125       * Helper function to return a list of user ids from the renderable object.
 126       * @param int $userid
 127       * @param ?int $groupid
 128       * @return array
 129       */
 130      protected function fetch_users_from_renderable(int $userid, ?int $groupid = 0): array {
 131          $this->setUser($userid);
 132          $renderable = new \report_log_renderable(
 133              "", (int)$this->course->id, $userid, 0, '', $groupid);
 134          $users = $renderable->get_user_list();
 135          return \array_keys($users);
 136      }
 137  
 138      /**
 139       * Test report_log_renderable::get_group_list().
 140       * @covers \report_log_renderable::get_group_list
 141       * @return void
 142       */
 143      public function test_get_group_list() {
 144  
 145          // The student sees his own group only.
 146          $this->setUser($this->student[0]->id);
 147          $renderable = new \report_log_renderable("", (int)$this->course->id, $this->student[0]->id);
 148          $groups = $renderable->get_group_list();
 149          $this->assertCount(1, $groups);
 150  
 151          // While the teacher is allowed to see all groups.
 152          $this->setUser($this->teacher[0]->id);
 153          $renderable = new \report_log_renderable("", (int)$this->course->id, $this->teacher[0]->id);
 154          $groups = $renderable->get_group_list();
 155          $this->assertCount(2, $groups);
 156  
 157      }
 158  }