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 311 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_helper.
  19   *
  20   * @package    core
  21   * @category   test
  22   * @copyright  2021 Sujith Haridasan
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace core;
  27  
  28  use moodle_url;
  29  use core\report_helper;
  30  
  31  /**
  32   * Tests the functions for report_helper class.
  33   */
  34  class report_helper_test extends \advanced_testcase {
  35      /**
  36       * Data provider for testing selected report for same and different courses
  37       *
  38       * @return array
  39       */
  40      public function data_selected_report():array {
  41          return [
  42              ['course_url_id' => [
  43                  ['url' => '/test', 'id' => 1],
  44                  ['url' => '/foo', 'id' => 1]]
  45              ],
  46              ['course_url_id' => [
  47                  ['url' => '/test', 'id' => 1],
  48                  ['url' => '/foo/bar', 'id' => 2]]
  49              ]
  50          ];
  51      }
  52  
  53      /**
  54       * Testing selected report saved in $USER session.
  55       *
  56       * @dataProvider data_selected_report
  57       * @param array $courseurlid The array has both course url and course id
  58       */
  59      public function test_save_selected_report(array $courseurlid):void {
  60          global $USER;
  61  
  62          $url1 = new moodle_url($courseurlid[0]['url']);
  63          $courseid1 = $courseurlid[0]['id'];
  64          report_helper::save_selected_report($courseid1, $url1);
  65          $this->assertDebuggingCalled('save_selected_report() has been deprecated because it is no ' .
  66              'longer used and will be removed in future versions of Moodle');
  67  
  68          $this->assertEquals($USER->course_last_report[$courseid1], $url1);
  69  
  70          $url2 = new moodle_url($courseurlid[1]['url']);
  71          $courseid2 = $courseurlid[1]['id'];
  72          report_helper::save_selected_report($courseid2, $url2);
  73          $this->assertDebuggingCalled('save_selected_report() has been deprecated because it is no ' .
  74              'longer used and will be removed in future versions of Moodle');
  75  
  76          $this->assertEquals($USER->course_last_report[$courseid2], $url2);
  77      }
  78  
  79      /**
  80       * Testing the report selector dropdown shown.
  81       *
  82       * Verify that the dropdowns have the pages to be displayed.
  83       *
  84       * @return void
  85       */
  86      public function test_print_report_selector():void {
  87          global $PAGE;
  88  
  89          $this->resetAfterTest();
  90  
  91          $user = $this->getDataGenerator()->create_user();
  92  
  93          $PAGE->set_url('/');
  94  
  95          $course = $this->getDataGenerator()->create_course();
  96          $PAGE->set_course($course);
  97  
  98          $this->getDataGenerator()->enrol_user($user->id, $course->id, 'teacher');
  99  
 100          $this->setUser($user);
 101  
 102          ob_start();
 103          report_helper::print_report_selector('Logs');
 104          $output = $this->getActualOutput();
 105          ob_end_clean();
 106  
 107          $log = '<option value="/report/log/index.php?id=' . $course->id .'" selected>Logs</option>';
 108          $competency = '<option value="/report/competency/index.php?id=' . $course->id . '" >Competency breakdown</option>';
 109          $loglive = '<option value="/report/loglive/index.php?id=' . $course->id . '" >Live logs</option>';
 110          $participation = '<option value="/report/participation/index.php?id=' . $course->id . '" >Course participation</option>';
 111          $this->assertStringContainsString($log, $output);
 112          $this->assertStringContainsString($competency, $output);
 113          $this->assertStringContainsString($loglive, $output);
 114          $this->assertStringContainsString($participation, $output);
 115      }
 116  }