Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

   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 gradereport_user library functions.
  19   *
  20   * @package    gradereport_user
  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 gradereport_user;
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  require_once($CFG->dirroot . '/grade/report/user/lib.php');
  30  
  31  /**
  32   * Class gradereport_user_lib_testcase.
  33   *
  34   * @package    gradereport_user
  35   * @copyright  2015 onwards Ankit agarwal <ankit.agrr@gmail.com>
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
  37   */
  38  class lib_test 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 gradereport_user_myprofile_navigation() function.
  64       */
  65      public function test_gradereport_user_myprofile_navigation() {
  66          $this->setAdminUser();
  67          $iscurrentuser = false;
  68  
  69          gradereport_user_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course);
  70          $reflector = new \ReflectionObject($this->tree);
  71          $nodes = $reflector->getProperty('nodes');
  72          $nodes->setAccessible(true);
  73          $this->assertArrayHasKey('grade', $nodes->getValue($this->tree));
  74      }
  75  
  76      /**
  77       * Tests the gradereport_user_myprofile_navigation() function for a user
  78       * without permission to view the grade node.
  79       */
  80      public function test_gradereport_user_myprofile_navigation_without_permission() {
  81          $this->setUser($this->user);
  82          $iscurrentuser = true;
  83  
  84          gradereport_user_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->assertArrayNotHasKey('grade', $nodes->getValue($this->tree));
  89      }
  90  }