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
namespace block_recentlyaccesseditems;
18
 
19
defined('MOODLE_INTERNAL') || die();
20
 
21
global $CFG;
22
require_once($CFG->dirroot . '/mod/assign/tests/generator.php');
23
 
24
/**
25
 * Block Recently accessed items observer tests.
26
 *
27
 * @package    block_recentlyaccesseditems
28
 * @copyright  2018 Victor Deniz <victor@moodle.com>
29
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30
 * @since      Moodle 3.6
31
 */
1441 ariadna 32
final class observer_test extends \advanced_testcase {
1 efrain 33
    use \mod_assign_test_generator;
34
 
35
    /** @var string Table name. */
36
    protected $table;
37
 
38
    /** @var \stdClass course data. */
39
    protected $course;
40
 
41
    /** @var \stdClass student data. */
42
    protected $student;
43
 
44
    /** @var \stdClass teacher data. */
45
    protected $teacher;
46
 
47
    /** @var \stdClass student role. */
48
    protected $studentrole;
49
 
50
    /** @var \stdClass teacher role. */
51
    protected $teacherrole;
52
 
53
    /** @var \stdClass course forum. */
54
    protected $forum;
55
 
56
    /** @var \stdClass course glossary. */
57
    protected $glossary;
58
 
1441 ariadna 59
    /** @var \stdClass course assignment. */
60
    protected $assign;
1 efrain 61
 
62
    /**
63
     * Set up for every test
64
     */
65
    public function setUp(): void {
66
        global $DB;
1441 ariadna 67
        parent::setUp();
1 efrain 68
 
69
        $this->resetAfterTest();
70
        $this->setAdminUser();
71
 
72
        // Block table name.
73
        $this->table = "block_recentlyaccesseditems";
74
 
75
        // Setup test data.
76
        $this->course = $this->getDataGenerator()->create_course();
77
 
78
        // Create users.
79
        $this->student = self::getDataGenerator()->create_user();
80
        $this->teacher = self::getDataGenerator()->create_user();
81
 
82
        // Users enrolments.
83
        $this->studentrole = $DB->get_record('role', array('shortname' => 'student'));
84
        $this->teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
85
        $this->getDataGenerator()->enrol_user($this->student->id, $this->course->id, $this->studentrole->id, 'manual');
86
        $this->getDataGenerator()->enrol_user($this->teacher->id, $this->course->id, $this->teacherrole->id, 'manual');
87
 
88
        // Create items.
89
        $this->forum = $this->getDataGenerator()->create_module('forum', array('course' => $this->course));
90
        $this->glossary = $this->getDataGenerator()->create_module('glossary', array('course' => $this->course));
1441 ariadna 91
        $this->assign = $this->getDataGenerator()->create_module('assign', ['course' => $this->course]);
1 efrain 92
    }
93
 
94
    /**
95
     * Test items views are recorded
96
     *
97
     * When items events are triggered they are stored in the block_recentlyaccesseditems table.
98
     */
11 efrain 99
    public function test_item_view_recorded_testcase(): void {
1 efrain 100
        global $DB;
101
 
102
        // Empty table at the beggining.
103
        $records = $DB->count_records($this->table, array());
104
        $this->assertEquals(0, $records);
105
 
106
        // Teacher access forum activity.
107
        $this->setUser($this->teacher);
108
        $event = \mod_forum\event\course_module_viewed::create(array('context' => \context_module::instance($this->forum->cmid),
109
                'objectid' => $this->forum->id));
110
        $event->trigger();
111
 
1441 ariadna 112
        // Student access assignment activity.
1 efrain 113
        $this->setUser($this->student);
1441 ariadna 114
        $event1 = \mod_assign\event\course_module_viewed::create(['context' => \context_module::instance($this->assign->cmid),
115
                'objectid' => $this->assign->id]);
1 efrain 116
        $event1->trigger();
117
 
118
        $records = $DB->count_records($this->table, array('userid' => $this->teacher->id, 'courseid' => $this->course->id,
119
                'cmid' => $this->forum->cmid));
120
        $this->assertEquals(1, $records);
121
 
1441 ariadna 122
        $records = $DB->count_records($this->table, ['userid' => $this->student->id, 'courseid' => $this->course->id, 'cmid' =>
123
                $this->assign->cmid]);
1 efrain 124
        $this->assertEquals(1, $records);
125
 
126
        $this->waitForSecond();
1441 ariadna 127
        // Student access assignment activity again after 1 second (no new record created, timeaccess updated).
128
        $event2 = \mod_assign\event\course_module_viewed::create(['context' => \context_module::instance($this->assign->cmid),
129
                'objectid' => $this->assign->id]);
1 efrain 130
        $event2->trigger();
131
 
1441 ariadna 132
        $records = $DB->get_records($this->table, ['userid' => $this->student->id, 'courseid' => $this->course->id, 'cmid' =>
133
                $this->assign->cmid]);
1 efrain 134
        $this->assertCount(1, $records);
135
        $this->assertEquals($event2->timecreated, array_shift($records)->timeaccess);
136
 
137
    }
138
 
139
    /**
140
     * Test removed items records are deleted.
141
     *
142
     * When a course module is removed, the records associated in the block_recentlyaccesseditems table are deleted.
143
     */
11 efrain 144
    public function test_item_delete_record_testcase(): void {
1 efrain 145
        global $DB;
146
 
147
        // Empty table at the beggining.
148
        $records = $DB->count_records($this->table, array());
149
        $this->assertEquals(0, $records);
150
 
151
        // Teacher access forum activity.
152
        $this->setUser($this->teacher);
153
        $event = \mod_forum\event\course_module_viewed::create(array('context' => \context_module::instance($this->forum->cmid),
154
                'objectid' => $this->forum->id));
155
        $event->trigger();
156
 
1441 ariadna 157
        // Teacher access assignment activity.
158
        $event = \mod_assign\event\course_module_viewed::create(['context' => \context_module::instance($this->assign->cmid),
159
                'objectid' => $this->assign->id]);
1 efrain 160
        $event->trigger();
161
 
1441 ariadna 162
        // Student access assignment activity.
1 efrain 163
        $this->setUser($this->student);
1441 ariadna 164
        $event = \mod_assign\event\course_module_viewed::create(['context' => \context_module::instance($this->assign->cmid),
165
                'objectid' => $this->assign->id]);
1 efrain 166
        $event->trigger();
167
 
168
        // Student access forum activity.
169
        $event = \mod_forum\event\course_module_viewed::create(array('context' => \context_module::instance($this->forum->cmid),
170
                'objectid' => $this->forum->id));
171
        $event->trigger();
172
 
173
        $records = $DB->count_records($this->table, array('cmid' => $this->forum->cmid));
174
        $this->assertEquals(2, $records);
175
        course_delete_module($this->forum->cmid);
176
        $records = $DB->count_records($this->table, array('cmid' => $this->forum->cmid));
177
        $this->assertEquals(0, $records);
1441 ariadna 178
        $records = $DB->count_records($this->table, ['cmid' => $this->assign->cmid]);
1 efrain 179
        $this->assertEquals(2, $records);
180
    }
181
}