Proyectos de Subversion Moodle

Rev

Rev 11 | | 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
 */
1441 ariadna 39
final class lib_test extends \advanced_testcase {
1 efrain 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 {
1441 ariadna 57
        parent::setUp();
1 efrain 58
        $this->user = $this->getDataGenerator()->create_user();
59
        $this->course = $this->getDataGenerator()->create_course();
60
        $this->tree = new \core_user\output\myprofile\tree();
61
        $this->resetAfterTest();
62
    }
63
 
64
    /**
65
     * Tests the report_userssesions_myprofile_navigation() function as an admin.
66
     */
11 efrain 67
    public function test_report_usersessions_myprofile_navigation_as_admin(): void {
1 efrain 68
        $this->setAdminUser();
69
        $iscurrentuser = false;
70
 
71
        // Not even admins allowed to pick at other user's sessions.
72
        report_usersessions_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course);
73
        $reflector = new \ReflectionObject($this->tree);
74
        $nodes = $reflector->getProperty('nodes');
75
        $this->assertArrayNotHasKey('usersessions', $nodes->getValue($this->tree));
76
    }
77
 
78
    /**
79
     * Tests the report_userssesions_myprofile_navigation() function as the currently logged in user.
80
     */
11 efrain 81
    public function test_report_usersessions_myprofile_navigation_as_current_user(): void {
1 efrain 82
        $this->setUser($this->user);
83
        $iscurrentuser = true;
84
 
85
        report_usersessions_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course);
86
        $reflector = new \ReflectionObject($this->tree);
87
        $nodes = $reflector->getProperty('nodes');
88
        $this->assertArrayHasKey('usersessions', $nodes->getValue($this->tree));
89
    }
90
 
91
    /**
92
     * Tests the report_userssesions_myprofile_navigation() function as a guest.
93
     */
11 efrain 94
    public function test_report_usersessions_myprofile_navigation_as_guest(): void {
1 efrain 95
        $this->setGuestUser();
96
        $iscurrentuser = true;
97
 
98
        report_usersessions_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course);
99
        $reflector = new \ReflectionObject($this->tree);
100
        $nodes = $reflector->getProperty('nodes');
101
        $this->assertArrayNotHasKey('usersessions', $nodes->getValue($this->tree));
102
    }
103
 
104
    /**
105
     * Tests the report_userssesions_myprofile_navigation() function as a user without permission.
106
     */
11 efrain 107
    public function test_report_usersessions_myprofile_navigation_without_permission(): void {
1 efrain 108
        // Try to see as a user without permission.
109
        $user2 = $this->getDataGenerator()->create_user();
110
        $this->setUser($user2);
111
        $iscurrentuser = false;
112
 
113
        report_usersessions_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course);
114
        $reflector = new \ReflectionObject($this->tree);
115
        $nodes = $reflector->getProperty('nodes');
116
        $this->assertArrayNotHasKey('usersessions', $nodes->getValue($this->tree));
117
 
118
    }
119
}