Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 310 and 400] [Versions 39 and 400] [Versions 400 and 402] [Versions 400 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_stats
  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_stats;
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  /**
  29   * Class report_stats_lib_testcase
  30   *
  31   * @package    report_stats
  32   * @copyright  2014 onwards Ankit agarwal <ankit.agrr@gmail.com>
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
  34   */
  35  class lib_test extends \advanced_testcase {
  36  
  37      /**
  38       * @var stdClass The user.
  39       */
  40      private $user;
  41  
  42      /**
  43       * @var stdClass The course.
  44       */
  45      private $course;
  46  
  47      /**
  48       * @var \core_user\output\myprofile\tree The navigation tree.
  49       */
  50      private $tree;
  51  
  52      public function setUp(): void {
  53          $this->user = $this->getDataGenerator()->create_user();
  54          $this->course = $this->getDataGenerator()->create_course();
  55          $this->tree = new \core_user\output\myprofile\tree();
  56          $this->resetAfterTest();
  57      }
  58  
  59      /**
  60       * Test report_log_supports_logstore.
  61       */
  62      public function test_report_participation_supports_logstore() {
  63          $logmanager = get_log_manager();
  64          $allstores = \core_component::get_plugin_list_with_class('logstore', 'log\store');
  65  
  66          $supportedstores = array(
  67              'logstore_legacy' => '\logstore_legacy\log\store',
  68              'logstore_standard' => '\logstore_standard\log\store'
  69          );
  70  
  71          // Make sure all supported stores are installed.
  72          $expectedstores = array_keys(array_intersect($allstores, $supportedstores));
  73          $stores = $logmanager->get_supported_logstores('report_stats');
  74          $stores = array_keys($stores);
  75          foreach ($expectedstores as $expectedstore) {
  76              $this->assertContains($expectedstore, $stores);
  77          }
  78      }
  79  
  80      /**
  81       * Tests the report_stats_myprofile_navigation() function.
  82       */
  83      public function test_report_stats_myprofile_navigation() {
  84          $this->setAdminUser();
  85          $iscurrentuser = false;
  86  
  87          // Enable stats.
  88          set_config('enablestats', true);
  89  
  90          report_stats_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course);
  91          $reflector = new \ReflectionObject($this->tree);
  92          $nodes = $reflector->getProperty('nodes');
  93          $nodes->setAccessible(true);
  94          $this->assertArrayHasKey('stats', $nodes->getValue($this->tree));
  95      }
  96  
  97      /**
  98       * Tests the report_stats_myprofile_navigation() function when stats are disabled.
  99       */
 100      public function test_report_stats_myprofile_navigation_stats_disabled() {
 101          $this->setAdminUser();
 102          $iscurrentuser = false;
 103  
 104          // Disable stats.
 105          set_config('enablestats', false);
 106  
 107          report_stats_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course);
 108          $reflector = new \ReflectionObject($this->tree);
 109          $nodes = $reflector->getProperty('nodes');
 110          $nodes->setAccessible(true);
 111          $this->assertArrayNotHasKey('stats', $nodes->getValue($this->tree));
 112      }
 113  
 114      /**
 115       * Tests the report_stats_myprofile_navigation() function without permission.
 116       */
 117      public function test_report_stats_myprofile_navigation_without_permission() {
 118          // Try to see as a user without permission.
 119          $this->setUser($this->user);
 120          $iscurrentuser = true;
 121  
 122          // Enable stats.
 123          set_config('enablestats', true);
 124  
 125          report_stats_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course);
 126          $reflector = new \ReflectionObject($this->tree);
 127          $nodes = $reflector->getProperty('nodes');
 128          $nodes->setAccessible(true);
 129          $this->assertArrayNotHasKey('stats', $nodes->getValue($this->tree));
 130      }
 131  }