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 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [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   * Tests for report library functions.
  19   *
  20   * @package    report_usersessions
  21   * @copyright  2015 onwards Ankit agarwal <ankit.agrr@gmail.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  
  29  require_once($CFG->dirroot. '/report/usersessions/lib.php');
  30  
  31  /**
  32   * Class report_stats_lib_testcase
  33   *
  34   * @package    report_usersessions
  35   * @copyright  2014 onwards Ankit agarwal <ankit.agrr@gmail.com>
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
  37   */
  38  class report_usersessions_lib_testcase extends advanced_testcase {
  39  
  40      /**
  41       * @var stdClass The user.
  42       */
  43      private $user;
  44  
  45      /**
  46       * @var stdClass The course.
  47       */
  48      private $course;
  49  
  50      /**
  51       * @var \core_user\output\myprofile\tree The navigation tree.
  52       */
  53      private $tree;
  54  
  55      public function setUp(): void {
  56          $this->user = $this->getDataGenerator()->create_user();
  57          $this->course = $this->getDataGenerator()->create_course();
  58          $this->tree = new \core_user\output\myprofile\tree();
  59          $this->resetAfterTest();
  60      }
  61  
  62      /**
  63       * Tests the report_userssesions_myprofile_navigation() function as an admin.
  64       */
  65      public function test_report_usersessions_myprofile_navigation_as_admin() {
  66          $this->setAdminUser();
  67          $iscurrentuser = false;
  68  
  69          // Not even admins allowed to pick at other user's sessions.
  70          report_usersessions_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course);
  71          $reflector = new ReflectionObject($this->tree);
  72          $nodes = $reflector->getProperty('nodes');
  73          $nodes->setAccessible(true);
  74          $this->assertArrayNotHasKey('usersessions', $nodes->getValue($this->tree));
  75      }
  76  
  77      /**
  78       * Tests the report_userssesions_myprofile_navigation() function as the currently logged in user.
  79       */
  80      public function test_report_usersessions_myprofile_navigation_as_current_user() {
  81          $this->setUser($this->user);
  82          $iscurrentuser = true;
  83  
  84          report_usersessions_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course);
  85          $reflector = new ReflectionObject($this->tree);
  86          $nodes = $reflector->getProperty('nodes');
  87          $nodes->setAccessible(true);
  88          $this->assertArrayHasKey('usersessions', $nodes->getValue($this->tree));
  89      }
  90  
  91      /**
  92       * Tests the report_userssesions_myprofile_navigation() function as a guest.
  93       */
  94      public function test_report_usersessions_myprofile_navigation_as_guest() {
  95          $this->setGuestUser();
  96          $iscurrentuser = true;
  97  
  98          report_usersessions_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course);
  99          $reflector = new ReflectionObject($this->tree);
 100          $nodes = $reflector->getProperty('nodes');
 101          $nodes->setAccessible(true);
 102          $this->assertArrayNotHasKey('usersessions', $nodes->getValue($this->tree));
 103      }
 104  
 105      /**
 106       * Tests the report_userssesions_myprofile_navigation() function as a user without permission.
 107       */
 108      public function test_report_usersessions_myprofile_navigation_without_permission() {
 109          // Try to see as a user without permission.
 110          $user2 = $this->getDataGenerator()->create_user();
 111          $this->setUser($user2);
 112          $iscurrentuser = false;
 113  
 114          report_usersessions_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course);
 115          $reflector = new ReflectionObject($this->tree);
 116          $nodes = $reflector->getProperty('nodes');
 117          $nodes->setAccessible(true);
 118          $this->assertArrayNotHasKey('usersessions', $nodes->getValue($this->tree));
 119  
 120      }
 121  }