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
26namespace core;
27
28use moodle_url;
29use core\report_helper;
30
31/**
32 * Tests the functions for report_helper class.
33 */
34class 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
66        $this->assertEquals($USER->course_last_report[$courseid1], $url1);
67
68        $url2 = new moodle_url($courseurlid[1]['url']);
69        $courseid2 = $courseurlid[1]['id'];
70        report_helper::save_selected_report($courseid2, $url2);
71
72        $this->assertEquals($USER->course_last_report[$courseid2], $url2);
73    }
74
75    /**
76     * Testing the report selector dropdown shown.
77     *
78     * Verify that the dropdowns have the pages to be displayed.
79     *
80     * @return void
81     */
82    public function test_print_report_selector():void {
83        global $PAGE;
84
85        $this->resetAfterTest();
86
87        $user = $this->getDataGenerator()->create_user();
88
89        $PAGE->set_url('/');
90
91        $course = $this->getDataGenerator()->create_course();
92        $PAGE->set_course($course);
93
94        $this->getDataGenerator()->enrol_user($user->id, $course->id, 'teacher');
95
96        $this->setUser($user);
97
98        ob_start();
99        report_helper::print_report_selector('Logs');
100        $output = $this->getActualOutput();
101        ob_end_clean();
102
103        $log = '<option value="/report/log/index.php?id=' . $course->id .'" selected>Logs</option>';
104        $competency = '<option value="/report/competency/index.php?id=' . $course->id . '" >Competency breakdown</option>';
105        $loglive = '<option value="/report/loglive/index.php?id=' . $course->id . '" >Live logs</option>';
106        $participation = '<option value="/report/participation/index.php?id=' . $course->id . '" >Course participation</option>';
107        $this->assertStringContainsString($log, $output);
108        $this->assertStringContainsString($competency, $output);
109        $this->assertStringContainsString($loglive, $output);
110        $this->assertStringContainsString($participation, $output);
111    }
112}
113