Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402]

   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_outline
  21   * @copyright  2014 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_outline;
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  
  30  /**
  31   * Class report_outline_lib_testcase
  32   *
  33   * @package    report_outline
  34   * @copyright  2014 onwards Ankit agarwal <ankit.agrr@gmail.com>
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
  36   */
  37  class lib_test extends \advanced_testcase {
  38  
  39      /**
  40       * @var stdClass The user.
  41       */
  42      private $user;
  43  
  44      /**
  45       * @var stdClass The course.
  46       */
  47      private $course;
  48  
  49      /**
  50       * @var context_course Course context.
  51       */
  52      private $coursecontext;
  53  
  54      /**
  55       * @var \core_user\output\myprofile\tree The navigation tree.
  56       */
  57      private $tree;
  58  
  59      /**
  60       * @var int Dummy role for testing.
  61       */
  62      private $roleid;
  63  
  64      public function setUp(): void {
  65          $this->user = $this->getDataGenerator()->create_user();
  66          $this->course = $this->getDataGenerator()->create_course();
  67          $this->tree = new \core_user\output\myprofile\tree();
  68          $this->coursecontext = \context_course::instance($this->course->id);
  69          $this->roleid = create_role('Dummy role', 'dummyrole', 'dummy role description');
  70          $this->resetAfterTest();
  71      }
  72  
  73      /**
  74       * Test report_log_supports_logstore.
  75       */
  76      public function test_report_participation_supports_logstore() {
  77          $logmanager = get_log_manager();
  78          $allstores = \core_component::get_plugin_list_with_class('logstore', 'log\store');
  79  
  80          $supportedstores = array(
  81              'logstore_standard' => '\logstore_standard\log\store'
  82          );
  83  
  84          // Make sure all supported stores are installed.
  85          $expectedstores = array_keys(array_intersect($allstores, $supportedstores));
  86          $stores = $logmanager->get_supported_logstores('report_outline');
  87          $stores = array_keys($stores);
  88          foreach ($expectedstores as $expectedstore) {
  89              $this->assertContains($expectedstore, $stores);
  90          }
  91      }
  92  
  93      /**
  94       * Tests the report_outline_myprofile_navigation() function as an admin user.
  95       */
  96      public function test_report_outline_myprofile_navigation() {
  97          $this->setAdminUser();
  98          $iscurrentuser = false;
  99  
 100          report_outline_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course);
 101          $reflector = new \ReflectionObject($this->tree);
 102          $nodes = $reflector->getProperty('nodes');
 103          $nodes->setAccessible(true);
 104          $this->assertArrayHasKey('outline', $nodes->getValue($this->tree));
 105          $this->assertArrayHasKey('complete', $nodes->getValue($this->tree));
 106      }
 107  
 108      /**
 109       * Tests the report_outline_myprofile_navigation() function as a user without permission.
 110       */
 111      public function test_report_outline_myprofile_navigation_without_permission() {
 112          $this->setUser($this->user);
 113          $iscurrentuser = true;
 114  
 115          report_outline_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('outline', $nodes->getValue($this->tree));
 120          $this->assertArrayNotHasKey('complete', $nodes->getValue($this->tree));
 121      }
 122  
 123      /**
 124       * Test that the current user can not access user report without report/outline:viewuserreport permission.
 125       */
 126      public function test_report_outline_can_not_access_user_report_without_viewuserreport_permission() {
 127          $this->getDataGenerator()->role_assign($this->roleid, $this->user->id, $this->coursecontext->id);
 128          $this->setUser($this->user);
 129  
 130          $this->assertFalse(report_outline_can_access_user_report($this->user, $this->course));
 131      }
 132  
 133      /**
 134       * Test that the current user can access user report with report/outline:viewuserreport permission.
 135       */
 136      public function test_report_outline_can_access_user_report_with_viewuserreport_permission() {
 137          assign_capability('report/outline:viewuserreport', CAP_ALLOW, $this->roleid, $this->coursecontext->id, true);
 138          $this->getDataGenerator()->role_assign($this->roleid, $this->user->id, $this->coursecontext->id);
 139          $this->setUser($this->user);
 140  
 141          $this->assertTrue(report_outline_can_access_user_report($this->user, $this->course));
 142      }
 143  }