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_outline
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
namespace report_outline;
25
 
26
defined('MOODLE_INTERNAL') || die();
27
 
28
global $CFG;
29
 
30
/**
31
 * Class report_outline_lib_testcase
32
 *
33
 * @package    report_outline
34
 * @copyright  2014 onwards Ankit agarwal <ankit.agrr@gmail.com>
35
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
36
 */
37
class lib_test extends \advanced_testcase {
38
 
39
    /**
40
     * @var stdClass The user.
41
     */
42
    private $user;
43
 
44
    /**
45
     * @var stdClass The course.
46
     */
47
    private $course;
48
 
49
    /**
50
     * @var context_course Course context.
51
     */
52
    private $coursecontext;
53
 
54
    /**
55
     * @var \core_user\output\myprofile\tree The navigation tree.
56
     */
57
    private $tree;
58
 
59
    /**
60
     * @var int Dummy role for testing.
61
     */
62
    private $roleid;
63
 
64
    public function setUp(): void {
65
        $this->user = $this->getDataGenerator()->create_user();
66
        $this->course = $this->getDataGenerator()->create_course();
67
        $this->tree = new \core_user\output\myprofile\tree();
68
        $this->coursecontext = \context_course::instance($this->course->id);
69
        $this->roleid = create_role('Dummy role', 'dummyrole', 'dummy role description');
70
        $this->resetAfterTest();
71
    }
72
 
73
    /**
74
     * Test report_log_supports_logstore.
75
     */
11 efrain 76
    public function test_report_participation_supports_logstore(): void {
1 efrain 77
        $logmanager = get_log_manager();
78
        $allstores = \core_component::get_plugin_list_with_class('logstore', 'log\store');
79
 
80
        $supportedstores = array(
81
            'logstore_standard' => '\logstore_standard\log\store'
82
        );
83
 
84
        // Make sure all supported stores are installed.
85
        $expectedstores = array_keys(array_intersect($allstores, $supportedstores));
86
        $stores = $logmanager->get_supported_logstores('report_outline');
87
        $stores = array_keys($stores);
88
        foreach ($expectedstores as $expectedstore) {
89
            $this->assertContains($expectedstore, $stores);
90
        }
91
    }
92
 
93
    /**
94
     * Tests the report_outline_myprofile_navigation() function as an admin user.
95
     */
11 efrain 96
    public function test_report_outline_myprofile_navigation(): void {
1 efrain 97
        $this->setAdminUser();
98
        $iscurrentuser = false;
99
 
100
        report_outline_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course);
101
        $reflector = new \ReflectionObject($this->tree);
102
        $nodes = $reflector->getProperty('nodes');
103
        $this->assertArrayHasKey('outline', $nodes->getValue($this->tree));
104
        $this->assertArrayHasKey('complete', $nodes->getValue($this->tree));
105
    }
106
 
107
    /**
108
     * Tests the report_outline_myprofile_navigation() function as a user without permission.
109
     */
11 efrain 110
    public function test_report_outline_myprofile_navigation_without_permission(): void {
1 efrain 111
        $this->setUser($this->user);
112
        $iscurrentuser = true;
113
 
114
        report_outline_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course);
115
        $reflector = new \ReflectionObject($this->tree);
116
        $nodes = $reflector->getProperty('nodes');
117
        $this->assertArrayNotHasKey('outline', $nodes->getValue($this->tree));
118
        $this->assertArrayNotHasKey('complete', $nodes->getValue($this->tree));
119
    }
120
 
121
    /**
122
     * Test that the current user can not access user report without report/outline:viewuserreport permission.
123
     */
11 efrain 124
    public function test_report_outline_can_not_access_user_report_without_viewuserreport_permission(): void {
1 efrain 125
        $this->getDataGenerator()->role_assign($this->roleid, $this->user->id, $this->coursecontext->id);
126
        $this->setUser($this->user);
127
 
128
        $this->assertFalse(report_outline_can_access_user_report($this->user, $this->course));
129
    }
130
 
131
    /**
132
     * Test that the current user can access user report with report/outline:viewuserreport permission.
133
     */
11 efrain 134
    public function test_report_outline_can_access_user_report_with_viewuserreport_permission(): void {
1 efrain 135
        assign_capability('report/outline:viewuserreport', CAP_ALLOW, $this->roleid, $this->coursecontext->id, true);
136
        $this->getDataGenerator()->role_assign($this->roleid, $this->user->id, $this->coursecontext->id);
137
        $this->setUser($this->user);
138
 
139
        $this->assertTrue(report_outline_can_access_user_report($this->user, $this->course));
140
    }
141
}