Commit | Line | Data |
---|---|---|
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 | ||
25 | defined('MOODLE_INTERNAL') || die(); | |
26 | ||
27 | class 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 | ||
102 | // Add discussions to course 1 and 2 started by user1. | |
103 | $record = new stdClass(); | |
104 | $record->course = $course1->id; | |
105 | $record->userid = $user1->id; | |
106 | $record->forum = $forum1->id; | |
107 | $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); | |
108 | ||
109 | $record = new stdClass(); | |
110 | $record->course = $course2->id; | |
111 | $record->userid = $user1->id; | |
112 | $record->forum = $forum2->id; | |
113 | $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); | |
114 | ||
115 | // Add discussions to course 3 started by user2. | |
116 | $record = new stdClass(); | |
117 | $record->course = $course3->id; | |
118 | $record->userid = $user2->id; | |
119 | $record->forum = $forum3->id; | |
120 | $discussion3 = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); | |
121 | ||
122 | // Add post to course 3 by user1. | |
123 | $record = new stdClass(); | |
124 | $record->course = $course3->id; | |
125 | $record->userid = $user1->id; | |
126 | $record->forum = $forum3->id; | |
127 | $record->discussion = $discussion3->id; | |
128 | $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record); | |
129 | ||
130 | // User 3 hasn't posted anything, so shouldn't get any results. | |
131 | $user3courses = forum_get_courses_user_posted_in($user3); | |
132 | $this->assertEmpty($user3courses); | |
133 | ||
134 | // User 2 has only posted in course3. | |
135 | $user2courses = forum_get_courses_user_posted_in($user2); | |
136 | $this->assertCount(1, $user2courses); | |
137 | $user2course = array_shift($user2courses); | |
138 | $this->assertEquals($course3->id, $user2course->id); | |
139 | $this->assertEquals($course3->shortname, $user2course->shortname); | |
140 | ||
141 | // User 1 has posted in all 3 courses. | |
142 | $user1courses = forum_get_courses_user_posted_in($user1); | |
143 | $this->assertCount(3, $user1courses); | |
144 | foreach ($user1courses as $course) { | |
145 | $this->assertContains($course->id, array($course1->id, $course2->id, $course3->id)); | |
146 | $this->assertContains($course->shortname, array($course1->shortname, $course2->shortname, | |
147 | $course3->shortname)); | |
148 | ||
149 | } | |
150 | ||
151 | // User 1 has only started a discussion in course 1 and 2 though. | |
152 | $user1courses = forum_get_courses_user_posted_in($user1, true); | |
153 | $this->assertCount(2, $user1courses); | |
154 | foreach ($user1courses as $course) { | |
155 | $this->assertContains($course->id, array($course1->id, $course2->id)); | |
156 | $this->assertContains($course->shortname, array($course1->shortname, $course2->shortname)); | |
157 | } | |
158 | } | |
62b3b997 | 159 | } |