Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 39 and 401]

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