weekly release 2.6dev
[moodle.git] / mod / forum / tests / lib_test.php
CommitLineData
62b3b997
FM
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 * The module forums tests
19 *
20 * @package mod_forum
21 * @copyright 2013 Frédéric Massart
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27class mod_forum_lib_testcase extends advanced_testcase {
28
29 public function test_forum_trigger_content_uploaded_event() {
30 $this->resetAfterTest();
31
32 $user = $this->getDataGenerator()->create_user();
33 $course = $this->getDataGenerator()->create_course();
34 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
35 $context = context_module::instance($forum->cmid);
36
37 $this->setUser($user->id);
38 $fakepost = (object) array('id' => 123, 'message' => 'Yay!', 'discussion' => 100);
39 $cm = get_coursemodule_from_instance('forum', $forum->cmid);
40
41 $fs = get_file_storage();
42 $dummy = (object) array(
43 'contextid' => $context->id,
44 'component' => 'mod_forum',
45 'filearea' => 'attachment',
46 'itemid' => $fakepost->id,
47 'filepath' => '/',
48 'filename' => 'myassignmnent.pdf'
49 );
50 $fi = $fs->create_file_from_string($dummy, 'Content of ' . $dummy->filename);
51
52 $data = new stdClass();
53 $sink = $this->redirectEvents();
54 forum_trigger_content_uploaded_event($fakepost, $cm, 'some triggered from value');
55 $events = $sink->get_events();
56
57 $this->assertCount(1, $events);
58 $event = reset($events);
59 $this->assertInstanceOf('\mod_forum\event\assessable_uploaded', $event);
60 $this->assertEquals($context->id, $event->contextid);
61 $this->assertEquals($fakepost->id, $event->objectid);
62 $this->assertEquals($fakepost->message, $event->other['content']);
63 $this->assertEquals($fakepost->discussion, $event->other['discussionid']);
64 $this->assertCount(1, $event->other['pathnamehashes']);
65 $this->assertEquals($fi->get_pathnamehash(), $event->other['pathnamehashes'][0]);
66 $expected = new stdClass();
67 $expected->modulename = 'forum';
68 $expected->name = 'some triggered from value';
69 $expected->cmid = $forum->cmid;
70 $expected->itemid = $fakepost->id;
71 $expected->courseid = $course->id;
72 $expected->userid = $user->id;
73 $expected->content = $fakepost->message;
74 $expected->pathnamehashes = array($fi->get_pathnamehash());
75 $this->assertEventLegacyData($expected, $event);
76 }
77
de316687
DP
78 public function test_forum_get_courses_user_posted_in() {
79 $this->resetAfterTest();
80
81 $user1 = $this->getDataGenerator()->create_user();
82 $user2 = $this->getDataGenerator()->create_user();
83 $user3 = $this->getDataGenerator()->create_user();
84
85 $course1 = $this->getDataGenerator()->create_course();
86 $course2 = $this->getDataGenerator()->create_course();
87 $course3 = $this->getDataGenerator()->create_course();
88
89 // Create 3 forums, one in each course.
90 $record = new stdClass();
91 $record->course = $course1->id;
92 $forum1 = $this->getDataGenerator()->create_module('forum', $record);
93
94 $record = new stdClass();
95 $record->course = $course2->id;
96 $forum2 = $this->getDataGenerator()->create_module('forum', $record);
97
98 $record = new stdClass();
99 $record->course = $course3->id;
100 $forum3 = $this->getDataGenerator()->create_module('forum', $record);
101
72c2c0bb
DP
102 // Add a second forum in course 1.
103 $record = new stdClass();
104 $record->course = $course1->id;
105 $forum4 = $this->getDataGenerator()->create_module('forum', $record);
106
107 // Add discussions to course 1 started by user1.
de316687
DP
108 $record = new stdClass();
109 $record->course = $course1->id;
110 $record->userid = $user1->id;
111 $record->forum = $forum1->id;
112 $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
113
72c2c0bb
DP
114 $record = new stdClass();
115 $record->course = $course1->id;
116 $record->userid = $user1->id;
117 $record->forum = $forum4->id;
118 $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
119
120 // Add discussions to course2 started by user1.
de316687
DP
121 $record = new stdClass();
122 $record->course = $course2->id;
123 $record->userid = $user1->id;
124 $record->forum = $forum2->id;
125 $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
126
127 // Add discussions to course 3 started by user2.
128 $record = new stdClass();
129 $record->course = $course3->id;
130 $record->userid = $user2->id;
131 $record->forum = $forum3->id;
132 $discussion3 = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
133
134 // Add post to course 3 by user1.
135 $record = new stdClass();
136 $record->course = $course3->id;
137 $record->userid = $user1->id;
138 $record->forum = $forum3->id;
139 $record->discussion = $discussion3->id;
140 $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
141
142 // User 3 hasn't posted anything, so shouldn't get any results.
143 $user3courses = forum_get_courses_user_posted_in($user3);
144 $this->assertEmpty($user3courses);
145
146 // User 2 has only posted in course3.
147 $user2courses = forum_get_courses_user_posted_in($user2);
148 $this->assertCount(1, $user2courses);
149 $user2course = array_shift($user2courses);
150 $this->assertEquals($course3->id, $user2course->id);
151 $this->assertEquals($course3->shortname, $user2course->shortname);
152
153 // User 1 has posted in all 3 courses.
154 $user1courses = forum_get_courses_user_posted_in($user1);
155 $this->assertCount(3, $user1courses);
156 foreach ($user1courses as $course) {
157 $this->assertContains($course->id, array($course1->id, $course2->id, $course3->id));
158 $this->assertContains($course->shortname, array($course1->shortname, $course2->shortname,
159 $course3->shortname));
160
161 }
162
163 // User 1 has only started a discussion in course 1 and 2 though.
164 $user1courses = forum_get_courses_user_posted_in($user1, true);
165 $this->assertCount(2, $user1courses);
166 foreach ($user1courses as $course) {
167 $this->assertContains($course->id, array($course1->id, $course2->id));
168 $this->assertContains($course->shortname, array($course1->shortname, $course2->shortname));
169 }
170 }
bb78e249
RT
171
172 /**
173 * Test user_enrolment_deleted observer.
174 */
175 public function test_user_enrolment_deleted_observer() {
176 global $DB;
177
178 $this->resetAfterTest();
179
180 $metaplugin = enrol_get_plugin('meta');
181 $user1 = $this->getDataGenerator()->create_user();
182 $course1 = $this->getDataGenerator()->create_course();
183 $course2 = $this->getDataGenerator()->create_course();
184 $student = $DB->get_record('role', array('shortname'=>'student'));
185
186 $e1 = $metaplugin->add_instance($course2, array('customint1' => $course1->id));
187 $enrol1 = $DB->get_record('enrol', array('id' => $e1));
188
189 // Enrol user.
190 $metaplugin->enrol_user($enrol1, $user1->id, $student->id);
191 $this->assertEquals(1, $DB->count_records('user_enrolments'));
192
193 // Unenrol user and capture event.
194 $sink = $this->redirectEvents();
195 $metaplugin->unenrol_user($enrol1, $user1->id);
196 $events = $sink->get_events();
197 $sink->close();
198 $event = array_pop($events);
199
200 $this->assertEquals(0, $DB->count_records('user_enrolments'));
201 $this->assertInstanceOf('\core\event\user_enrolment_deleted', $event);
202 $this->assertEquals('user_unenrolled', $event->get_legacy_eventname());
203 }
62b3b997 204}