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] [Versions 401 and 402] [Versions 401 and 403]

   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->user2 = $this->getDataGenerator()->create_user();
  67          $this->course = $this->getDataGenerator()->create_course();
  68          $this->tree = new \core_user\output\myprofile\tree();
  69          $this->coursecontext = \context_course::instance($this->course->id);
  70          $this->roleid = create_role('Dummy role', 'dummyrole', 'dummy role description');
  71          $this->resetAfterTest();
  72      }
  73  
  74      /**
  75       * Test report_log_supports_logstore.
  76       */
  77      public function test_report_participation_supports_logstore() {
  78          $logmanager = get_log_manager();
  79          $allstores = \core_component::get_plugin_list_with_class('logstore', 'log\store');
  80  
  81          $supportedstores = array(
  82              'logstore_legacy' => '\logstore_legacy\log\store',
  83              'logstore_standard' => '\logstore_standard\log\store'
  84          );
  85  
  86          // Make sure all supported stores are installed.
  87          $expectedstores = array_keys(array_intersect($allstores, $supportedstores));
  88          $stores = $logmanager->get_supported_logstores('report_outline');
  89          $stores = array_keys($stores);
  90          foreach ($expectedstores as $expectedstore) {
  91              $this->assertContains($expectedstore, $stores);
  92          }
  93      }
  94  
  95      /**
  96       * Tests the report_outline_myprofile_navigation() function as an admin user.
  97       */
  98      public function test_report_outline_myprofile_navigation() {
  99          $this->setAdminUser();
 100          $iscurrentuser = false;
 101  
 102          report_outline_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course);
 103          $reflector = new \ReflectionObject($this->tree);
 104          $nodes = $reflector->getProperty('nodes');
 105          $nodes->setAccessible(true);
 106          $this->assertArrayHasKey('outline', $nodes->getValue($this->tree));
 107          $this->assertArrayHasKey('complete', $nodes->getValue($this->tree));
 108      }
 109  
 110      /**
 111       * Tests the report_outline_myprofile_navigation() function as a user without permission.
 112       */
 113      public function test_report_outline_myprofile_navigation_without_permission() {
 114          $this->setUser($this->user);
 115          $iscurrentuser = true;
 116  
 117          report_outline_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course);
 118          $reflector = new \ReflectionObject($this->tree);
 119          $nodes = $reflector->getProperty('nodes');
 120          $nodes->setAccessible(true);
 121          $this->assertArrayNotHasKey('outline', $nodes->getValue($this->tree));
 122          $this->assertArrayNotHasKey('complete', $nodes->getValue($this->tree));
 123      }
 124  
 125      /**
 126       * Test that the current user can not access user report without report/outline:viewuserreport permission.
 127       */
 128      public function test_report_outline_can_not_access_user_report_without_viewuserreport_permission() {
 129          $this->getDataGenerator()->role_assign($this->roleid, $this->user->id, $this->coursecontext->id);
 130          $this->setUser($this->user);
 131  
 132          $this->assertFalse(report_outline_can_access_user_report($this->user, $this->course));
 133      }
 134  
 135      /**
 136       * Test that the current user can access user report with report/outline:viewuserreport permission.
 137       */
 138      public function test_report_outline_can_access_user_report_with_viewuserreport_permission() {
 139          assign_capability('report/outline:viewuserreport', CAP_ALLOW, $this->roleid, $this->coursecontext->id, true);
 140          $this->getDataGenerator()->role_assign($this->roleid, $this->user->id, $this->coursecontext->id);
 141          $this->setUser($this->user);
 142  
 143          $this->assertTrue(report_outline_can_access_user_report($this->user, $this->course));
 144      }
 145  }