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_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
 */
1441 ariadna 37
final class lib_test extends \advanced_testcase {
1 efrain 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 {
1441 ariadna 65
        parent::setUp();
1 efrain 66
        $this->user = $this->getDataGenerator()->create_user();
67
        $this->course = $this->getDataGenerator()->create_course();
68
        $this->tree = new \core_user\output\myprofile\tree();
69
        $this->coursecontext = \context_course::instance($this->course->id);
70
        $this->roleid = create_role('Dummy role', 'dummyrole', 'dummy role description');
71
        $this->resetAfterTest();
72
    }
73
 
74
    /**
75
     * Test report_log_supports_logstore.
76
     */
11 efrain 77
    public function test_report_participation_supports_logstore(): void {
1 efrain 78
        $logmanager = get_log_manager();
79
        $allstores = \core_component::get_plugin_list_with_class('logstore', 'log\store');
80
 
81
        $supportedstores = array(
82
            'logstore_standard' => '\logstore_standard\log\store'
83
        );
84
 
85
        // Make sure all supported stores are installed.
86
        $expectedstores = array_keys(array_intersect($allstores, $supportedstores));
87
        $stores = $logmanager->get_supported_logstores('report_outline');
88
        $stores = array_keys($stores);
89
        foreach ($expectedstores as $expectedstore) {
90
            $this->assertContains($expectedstore, $stores);
91
        }
92
    }
93
 
94
    /**
95
     * Tests the report_outline_myprofile_navigation() function as an admin user.
96
     */
11 efrain 97
    public function test_report_outline_myprofile_navigation(): void {
1 efrain 98
        $this->setAdminUser();
99
        $iscurrentuser = false;
100
 
101
        report_outline_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course);
102
        $reflector = new \ReflectionObject($this->tree);
103
        $nodes = $reflector->getProperty('nodes');
104
        $this->assertArrayHasKey('outline', $nodes->getValue($this->tree));
105
        $this->assertArrayHasKey('complete', $nodes->getValue($this->tree));
106
    }
107
 
108
    /**
109
     * Tests the report_outline_myprofile_navigation() function as a user without permission.
110
     */
11 efrain 111
    public function test_report_outline_myprofile_navigation_without_permission(): void {
1 efrain 112
        $this->setUser($this->user);
113
        $iscurrentuser = true;
114
 
115
        report_outline_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course);
116
        $reflector = new \ReflectionObject($this->tree);
117
        $nodes = $reflector->getProperty('nodes');
118
        $this->assertArrayNotHasKey('outline', $nodes->getValue($this->tree));
119
        $this->assertArrayNotHasKey('complete', $nodes->getValue($this->tree));
120
    }
121
 
122
    /**
123
     * Test that the current user can not access user report without report/outline:viewuserreport permission.
124
     */
11 efrain 125
    public function test_report_outline_can_not_access_user_report_without_viewuserreport_permission(): void {
1 efrain 126
        $this->getDataGenerator()->role_assign($this->roleid, $this->user->id, $this->coursecontext->id);
127
        $this->setUser($this->user);
128
 
129
        $this->assertFalse(report_outline_can_access_user_report($this->user, $this->course));
130
    }
131
 
132
    /**
133
     * Test that the current user can access user report with report/outline:viewuserreport permission.
134
     */
11 efrain 135
    public function test_report_outline_can_access_user_report_with_viewuserreport_permission(): void {
1 efrain 136
        assign_capability('report/outline:viewuserreport', CAP_ALLOW, $this->roleid, $this->coursecontext->id, true);
137
        $this->getDataGenerator()->role_assign($this->roleid, $this->user->id, $this->coursecontext->id);
138
        $this->setUser($this->user);
139
 
140
        $this->assertTrue(report_outline_can_access_user_report($this->user, $this->course));
141
    }
142
}