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
namespace core_courseformat\output;
18
 
19
use stdClass;
20
 
21
/**
22
 * Tests for activitybadge class.
23
 *
24
 * @package    core_courseformat
25
 * @copyright  2023 Sara Arjona <sara@moodle.com>
26
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27
 * @coversDefaultClass \core_courseformat\output\activitybadge
28
 */
29
class activitybadge_test extends \advanced_testcase {
30
 
31
    /**
32
     * Test the behaviour of create_instance() and export_for_template() attributes.
33
     * @runInSeparateProcess
34
     *
35
     * @covers ::export_for_template
36
     * @covers ::create_instance
37
     */
11 efrain 38
    public function test_activitybadge_export_for_template(): void {
1 efrain 39
        $this->resetAfterTest();
40
        $this->setAdminUser();
41
 
42
        $data = $this->setup_scenario();
43
 
44
        $user = $this->getDataGenerator()->create_user(['trackforums' => 1]);
45
        $this->getDataGenerator()->enrol_user(
46
            $user->id,
47
            $data->course->id,
48
            'student'
49
        );
50
        $this->setUser($user);
51
 
52
        $renderer = $data->renderer;
53
 
54
        // The activitybadge for a file with all options enabled shouldn't be empty.
55
        $class = activitybadge::create_instance($data->fileshowtype);
56
        $result = $class->export_for_template($renderer);
57
        $this->check_activitybadge($result, 'TXT', 'badge-none');
58
 
59
        // The activitybadge for a file with Show type option disabled should be empty.
60
        $class = activitybadge::create_instance($data->filehidetype);
61
        $result = $class->export_for_template($renderer);
62
        $this->check_activitybadge($result);
63
 
64
        // The activitybadge for a forum with unread messages shouldn't be empty.
65
        $class = activitybadge::create_instance($data->forumunread);
66
        $result = $class->export_for_template($renderer);
67
        $this->check_activitybadge($result, '1 unread post', 'bg-dark text-white');
68
 
69
        // The activitybadge for a forum without unread messages should be empty.
70
        $class = activitybadge::create_instance($data->forumread);
71
        $result = $class->export_for_template($renderer);
72
        $this->check_activitybadge($result);
73
 
74
        // The activitybadge for an assignment should be empty.
75
        $class = activitybadge::create_instance($data->assign);
76
        $this->assertNull($class);
77
 
78
        // The activitybadge for a label should be empty.
79
        $class = activitybadge::create_instance($data->label);
80
        $this->assertNull($class);
81
    }
82
 
83
    /**
84
     * Setup the default scenario, creating some activities:
85
     * - A forum with one unread message from the teacher.
86
     * - Another forum without unread messages.
87
     * - A file with all the appearance options enabled.
88
     * - A file with the "Show type" option disabled.
89
     * - An assignment.
90
     * - A label.
91
     *
92
     * @return stdClass the scenario instances.
93
     */
94
    private function setup_scenario(): stdClass {
95
        global $PAGE;
96
 
97
        $course = $this->getDataGenerator()->create_course(['numsections' => 1]);
98
 
99
        // Enrol editing teacher to the course.
100
        $teacher = $this->getDataGenerator()->create_user();
101
        $this->getDataGenerator()->enrol_user(
102
            $teacher->id,
103
            $course->id,
104
            'editingteacher'
105
        );
106
        $this->setUser($teacher);
107
 
108
        // Create a forum with tracking forced and add a discussion.
109
        $record = new stdClass();
110
        $record->introformat = FORMAT_HTML;
111
        $record->course = $course->id;
112
        $record->trackingtype = FORUM_TRACKING_FORCED;
113
        $forumread = $this->getDataGenerator()->create_module('forum', $record);
114
        $forumunread = $this->getDataGenerator()->create_module('forum', $record);
115
        $record = new stdClass();
116
        $record->course = $course->id;
117
        $record->userid = $teacher->id;
118
        $record->forum = $forumunread->id;
119
        $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
120
 
121
        // Create a file with all the options enabled.
122
        $record = (object)[
123
            'course' => $course->id,
124
            'showsize' => 1,
125
            'showtype' => 1,
126
            'showdate' => 1,
127
        ];
128
        $fileshowtype = self::getDataGenerator()->create_module('resource', $record);
129
 
130
        // Create a file with Show type disabled.
131
        $record = (object)[
132
            'course' => $course->id,
133
            'showsize' => 1,
134
            'showtype' => 0,
135
            'showdate' => 1,
136
        ];
137
        $filehidetype = self::getDataGenerator()->create_module('resource', $record);
138
 
139
        // Create an assignment and a label.
140
        $assign = $this->getDataGenerator()->create_module('assign', ['course' => $course->id]);
141
        $label = $this->getDataGenerator()->create_module('label', ['course' => $course->id]);
142
 
143
        rebuild_course_cache($course->id, true);
144
        $renderer = course_get_format($course->id)->get_renderer($PAGE);
145
        $modinfo = get_fast_modinfo($course->id);
146
 
147
        return (object)[
148
            'course' => $course,
149
            'forumunread' => $modinfo->get_cm($forumunread->cmid),
150
            'discussion' => $discussion,
151
            'forumread' => $modinfo->get_cm($forumread->cmid),
152
            'fileshowtype' => $modinfo->get_cm($fileshowtype->cmid),
153
            'filehidetype' => $modinfo->get_cm($filehidetype->cmid),
154
            'assign' => $modinfo->get_cm($assign->cmid),
155
            'label' => $modinfo->get_cm($label->cmid),
156
            'renderer' => $renderer,
157
        ];
158
    }
159
 
160
    /**
161
     * Method to check if the result of the export_from_template is the expected.
162
     *
163
     * @param stdClass $result The result of the export_from_template() call.
164
     * @param string|null $content The expected activitybadge content.
165
     * @param string|null $style The expected activitybadge style.
166
     * @param string|null $url The expected activitybadge url.
167
     * @param string|null $elementid The expected activitybadge element id.
168
     * @param array|null $extra The expected activitybadge extra attributes.
169
     */
170
    private function check_activitybadge(
171
        stdClass $result,
172
        ?string $content = null,
173
        ?string $style = null,
174
        ?string $url = null,
175
        ?string $elementid = null,
176
        ?array $extra = null
177
    ): void {
178
        if (is_null($content)) {
179
            $this->assertObjectNotHasProperty('badgecontent', $result);
180
        } else {
181
            $this->assertEquals($content, $result->badgecontent);
182
        }
183
 
184
        if (is_null($style)) {
185
            $this->assertObjectNotHasProperty('badgestyle', $result);
186
        } else {
187
            $this->assertEquals($style, $result->badgestyle);
188
        }
189
 
190
        if (is_null($url)) {
191
            $this->assertObjectNotHasProperty('badgeurl', $result);
192
        } else {
193
            $this->assertEquals($url, $result->badgeurl);
194
        }
195
 
196
        if (is_null($elementid)) {
197
            $this->assertObjectNotHasProperty('badgeelementid', $result);
198
        } else {
199
            $this->assertEquals($elementid, $result->badgeelementid);
200
        }
201
 
202
        if (is_null($extra)) {
203
            $this->assertObjectNotHasProperty('badgeextraattributes', $result);
204
        } else {
205
            $this->assertEquals($extra, $result->badgeextraattributes);
206
        }
207
    }
208
}