Proyectos de Subversion Moodle

Rev

| 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_course\task;
18
 
19
use availability_date\condition;
20
use context_user;
21
use core_availability\tree;
22
 
23
/**
24
 * Contains tests for course related notifications.
25
 *
26
 * @package    core
27
 * @subpackage course
28
 * @covers     \core_course\task\content_notification_task
29
 * @copyright  2021 Juan Leyva <juan@moodle.com>
30
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31
 */
32
class content_notification_task_test extends \advanced_testcase {
33
 
34
    /**
35
     * Test execution of task
36
     */
37
    public function test_execute(): void {
38
        global $DB, $CFG, $USER;
39
 
40
        $this->resetAfterTest();
41
        $this->setAdminUser();
42
 
43
        // Create course, with a course image.
44
        $draft = get_file_storage()->create_file_from_pathname([
45
            'component' => 'user',
46
            'filearea' => 'draft',
47
            'contextid' => context_user::instance($USER->id)->id,
48
            'itemid' => file_get_unused_draft_itemid(),
49
            'filename' => 'gd-logo.png',
50
            'filepath' => '/',
51
        ], "{$CFG->libdir}/tests/fixtures/gd-logo.png");
52
 
53
        $course = $this->getDataGenerator()->create_course(['overviewfiles_filemanager' => $draft->get_itemid()]);
54
 
55
        // Enrol couple of students to receive a notification and one unactive enrolment.
56
        $user1 = self::getDataGenerator()->create_user();
57
        $user2 = self::getDataGenerator()->create_user();
58
        $user3 = self::getDataGenerator()->create_user();
59
        self::getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
60
        self::getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
61
        self::getDataGenerator()->enrol_user($user3->id, $course->id, 'student', 'manual', time() - YEARSECS, time() - WEEKSECS);
62
 
63
        $url = self::getDataGenerator()->create_module('url', ['course' => $course]);
64
 
65
        // Test update.
66
        $moduleavailability = tree::get_root_json([condition::get_json(condition::DIRECTION_FROM, time() + HOURSECS)]);
67
        $moduleinfo = $DB->get_record('course_modules', array('id' => $url->cmid));
68
        $moduleinfo->modulename = 'url';
69
        $moduleinfo->coursemodule = $url->cmid;
70
        $moduleinfo->display = 1;
71
        $moduleinfo->availability = json_encode($moduleavailability);
72
        $moduleinfo->externalurl = '';
73
        $moduleinfo->update = 1;
74
        $draftid = 0;
75
        file_prepare_draft_area($draftid, \context_module::instance($url->cmid)->id, 'mod_url', 'intro', 0);
76
        $moduleinfo->introeditor = [
77
            'itemid' => $draftid,
78
            'text' => '<p>Yo</p>',
79
            'format' => FORMAT_HTML
80
        ];
81
        $modurl = (new \moodle_url('/mod/url/view.php', ['id' => $url->cmid]))->out(false);
82
 
83
        // Check course content changed notifications.
84
        $moduleinfo->coursecontentnotification = 1;
85
 
86
        // Create the module.
87
        update_module(clone $moduleinfo);   // We use clone to keep the original object untouch for later use.
88
 
89
        // Redirect messages to sink and stop buffer output from CLI task.
90
        $sink = $this->redirectMessages();
91
        ob_start();
92
        $this->runAdhocTasks('\core_course\task\content_notification_task');
93
        $output = ob_get_contents();
94
        ob_end_clean();
95
        $messages = $sink->get_messages();
96
        $sink->close();
97
 
98
        // The module isn't available for one hour, there should be no notifications.
99
        $this->assertCount(0, $messages);
100
 
101
        // Remove availability condition.
102
        $this->setAdminUser();
103
        $moduleinfo->availability = null;
104
        update_module(clone $moduleinfo);
105
 
106
        // Redirect messages to sink and stop buffer output from CLI task.
107
        $sink = $this->redirectMessages();
108
        ob_start();
109
        $this->runAdhocTasks('\core_course\task\content_notification_task');
110
        $output = ob_get_contents();
111
        ob_end_clean();
112
        $messages = $sink->get_messages();
113
        $sink->close();
114
 
115
        // We have 3 students, one with a non-active enrolment that should not receive a notification.
116
        $this->assertCount(2, $messages);
117
        foreach ($messages as $message) {
118
            $this->assertEquals('coursecontentupdated', $message->eventtype);
119
            $this->assertEquals($modurl, $message->contexturl);
120
 
121
            $messagecustomdata = json_decode($message->customdata);
122
            $this->assertEquals($course->id, $messagecustomdata->courseid);
123
            $this->assertObjectHasProperty('notificationiconurl', $messagecustomdata);
124
            $this->assertObjectHasProperty('notificationpictureurl', $messagecustomdata);
125
        }
126
 
127
        // Now, set the course to not visible.
128
        $DB->set_field('course', 'visible', 0, ['id' => $course->id]);
129
        $this->setAdminUser();
130
        update_module(clone $moduleinfo);
131
 
132
        // Redirect messages to sink and stop buffer output from CLI task.
133
        $sink = $this->redirectMessages();
134
        ob_start();
135
        $this->runAdhocTasks('\core_course\task\content_notification_task');
136
        $output = ob_get_contents();
137
        ob_end_clean();
138
        $messages = $sink->get_messages();
139
        $sink->close();
140
        // No messages, course not visible.
141
        $this->assertCount(0, $messages);
142
 
143
        // Now, set the module to not visible.
144
        $DB->set_field('course', 'visible', 1, ['id' => $course->id]);
145
        $this->setAdminUser();
146
        $moduleinfo->visible = 0;
147
        update_module(clone $moduleinfo);
148
 
149
        // Redirect messages to sink and stop buffer output from CLI task.
150
        $sink = $this->redirectMessages();
151
        ob_start();
152
        $this->runAdhocTasks('\core_course\task\content_notification_task');
153
        $output = ob_get_contents();
154
        ob_end_clean();
155
        $messages = $sink->get_messages();
156
        $sink->close();
157
        // No messages, module not visible.
158
        $this->assertCount(0, $messages);
159
    }
160
}