Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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_usersessions
21
 * @copyright  2015 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_usersessions;
25
 
26
defined('MOODLE_INTERNAL') || die();
27
 
28
global $CFG;
29
 
30
require_once($CFG->dirroot. '/report/usersessions/lib.php');
31
 
32
/**
33
 * Class report_stats_lib_testcase
34
 *
35
 * @package    report_usersessions
36
 * @copyright  2014 onwards Ankit agarwal <ankit.agrr@gmail.com>
37
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
38
 */
39
class lib_test extends \advanced_testcase {
40
 
41
    /**
42
     * @var stdClass The user.
43
     */
44
    private $user;
45
 
46
    /**
47
     * @var stdClass The course.
48
     */
49
    private $course;
50
 
51
    /**
52
     * @var \core_user\output\myprofile\tree The navigation tree.
53
     */
54
    private $tree;
55
 
56
    public function setUp(): void {
57
        $this->user = $this->getDataGenerator()->create_user();
58
        $this->course = $this->getDataGenerator()->create_course();
59
        $this->tree = new \core_user\output\myprofile\tree();
60
        $this->resetAfterTest();
61
    }
62
 
63
    /**
64
     * Tests the report_userssesions_myprofile_navigation() function as an admin.
65
     */
11 efrain 66
    public function test_report_usersessions_myprofile_navigation_as_admin(): void {
1 efrain 67
        $this->setAdminUser();
68
        $iscurrentuser = false;
69
 
70
        // Not even admins allowed to pick at other user's sessions.
71
        report_usersessions_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course);
72
        $reflector = new \ReflectionObject($this->tree);
73
        $nodes = $reflector->getProperty('nodes');
74
        $this->assertArrayNotHasKey('usersessions', $nodes->getValue($this->tree));
75
    }
76
 
77
    /**
78
     * Tests the report_userssesions_myprofile_navigation() function as the currently logged in user.
79
     */
11 efrain 80
    public function test_report_usersessions_myprofile_navigation_as_current_user(): void {
1 efrain 81
        $this->setUser($this->user);
82
        $iscurrentuser = true;
83
 
84
        report_usersessions_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course);
85
        $reflector = new \ReflectionObject($this->tree);
86
        $nodes = $reflector->getProperty('nodes');
87
        $this->assertArrayHasKey('usersessions', $nodes->getValue($this->tree));
88
    }
89
 
90
    /**
91
     * Tests the report_userssesions_myprofile_navigation() function as a guest.
92
     */
11 efrain 93
    public function test_report_usersessions_myprofile_navigation_as_guest(): void {
1 efrain 94
        $this->setGuestUser();
95
        $iscurrentuser = true;
96
 
97
        report_usersessions_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course);
98
        $reflector = new \ReflectionObject($this->tree);
99
        $nodes = $reflector->getProperty('nodes');
100
        $this->assertArrayNotHasKey('usersessions', $nodes->getValue($this->tree));
101
    }
102
 
103
    /**
104
     * Tests the report_userssesions_myprofile_navigation() function as a user without permission.
105
     */
11 efrain 106
    public function test_report_usersessions_myprofile_navigation_without_permission(): void {
1 efrain 107
        // Try to see as a user without permission.
108
        $user2 = $this->getDataGenerator()->create_user();
109
        $this->setUser($user2);
110
        $iscurrentuser = false;
111
 
112
        report_usersessions_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course);
113
        $reflector = new \ReflectionObject($this->tree);
114
        $nodes = $reflector->getProperty('nodes');
115
        $this->assertArrayNotHasKey('usersessions', $nodes->getValue($this->tree));
116
 
117
    }
118
}