Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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