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 mod_forum;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* PHPUnit data generator testcase
|
|
|
21 |
*
|
|
|
22 |
* @package mod_forum
|
|
|
23 |
* @category test
|
|
|
24 |
* @copyright 2012 Petr Skoda {@link http://skodak.org}
|
|
|
25 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
26 |
* @covers \mod_forum_generator
|
|
|
27 |
*/
|
|
|
28 |
final class generator_test extends \advanced_testcase {
|
|
|
29 |
public function setUp(): void {
|
|
|
30 |
// We must clear the subscription caches. This has to be done both before each test, and after in case of other
|
|
|
31 |
// tests using these functions.
|
|
|
32 |
\mod_forum\subscriptions::reset_forum_cache();
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
public function tearDown(): void {
|
|
|
36 |
// We must clear the subscription caches. This has to be done both before each test, and after in case of other
|
|
|
37 |
// tests using these functions.
|
|
|
38 |
\mod_forum\subscriptions::reset_forum_cache();
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
public function test_generator(): void {
|
|
|
42 |
global $DB;
|
|
|
43 |
|
|
|
44 |
$this->resetAfterTest(true);
|
|
|
45 |
|
|
|
46 |
$this->assertEquals(0, $DB->count_records('forum'));
|
|
|
47 |
|
|
|
48 |
$course = $this->getDataGenerator()->create_course();
|
|
|
49 |
|
|
|
50 |
/** @var mod_forum_generator $generator */
|
|
|
51 |
$generator = $this->getDataGenerator()->get_plugin_generator('mod_forum');
|
|
|
52 |
$this->assertInstanceOf('mod_forum_generator', $generator);
|
|
|
53 |
$this->assertEquals('forum', $generator->get_modulename());
|
|
|
54 |
|
|
|
55 |
$generator->create_instance(['course' => $course->id]);
|
|
|
56 |
$generator->create_instance(['course' => $course->id]);
|
|
|
57 |
$forum = $generator->create_instance(['course' => $course->id]);
|
|
|
58 |
$this->assertEquals(3, $DB->count_records('forum'));
|
|
|
59 |
|
|
|
60 |
$cm = get_coursemodule_from_instance('forum', $forum->id);
|
|
|
61 |
$this->assertEquals($forum->id, $cm->instance);
|
|
|
62 |
$this->assertEquals('forum', $cm->modname);
|
|
|
63 |
$this->assertEquals($course->id, $cm->course);
|
|
|
64 |
|
|
|
65 |
$context = \context_module::instance($cm->id);
|
|
|
66 |
$this->assertEquals($forum->cmid, $context->instanceid);
|
|
|
67 |
|
|
|
68 |
// Test gradebook integration using low level DB access - DO NOT USE IN PLUGIN CODE.
|
|
|
69 |
$forum = $generator->create_instance(['course' => $course->id, 'assessed' => 1, 'scale' => 100]);
|
|
|
70 |
$gitem = $DB->get_record(
|
|
|
71 |
'grade_items',
|
|
|
72 |
['courseid' => $course->id, 'itemtype' => 'mod', 'itemmodule' => 'forum', 'iteminstance' => $forum->id]
|
|
|
73 |
);
|
|
|
74 |
$this->assertNotEmpty($gitem);
|
|
|
75 |
$this->assertEquals(100, $gitem->grademax);
|
|
|
76 |
$this->assertEquals(0, $gitem->grademin);
|
|
|
77 |
$this->assertEquals(GRADE_TYPE_VALUE, $gitem->gradetype);
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
/**
|
|
|
81 |
* Test create_discussion.
|
|
|
82 |
*/
|
|
|
83 |
public function test_create_discussion(): void {
|
|
|
84 |
global $DB;
|
|
|
85 |
|
|
|
86 |
$this->resetAfterTest(true);
|
|
|
87 |
|
|
|
88 |
// User that will create the forum.
|
|
|
89 |
$user = self::getDataGenerator()->create_user();
|
|
|
90 |
|
|
|
91 |
// Create course to add the forum to.
|
|
|
92 |
$course = self::getDataGenerator()->create_course();
|
|
|
93 |
|
|
|
94 |
// The forum.
|
|
|
95 |
$record = new \stdClass();
|
|
|
96 |
$record->course = $course->id;
|
|
|
97 |
$forum = self::getDataGenerator()->create_module('forum', $record);
|
|
|
98 |
|
|
|
99 |
// Add a few discussions.
|
|
|
100 |
$record = [];
|
|
|
101 |
$record['course'] = $course->id;
|
|
|
102 |
$record['forum'] = $forum->id;
|
|
|
103 |
$record['userid'] = $user->id;
|
|
|
104 |
$record['pinned'] = FORUM_DISCUSSION_PINNED; // Pin one discussion.
|
|
|
105 |
self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
|
|
|
106 |
$record['pinned'] = FORUM_DISCUSSION_UNPINNED; // No pin for others.
|
|
|
107 |
self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
|
|
|
108 |
self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
|
|
|
109 |
|
|
|
110 |
// Check the discussions were correctly created.
|
|
|
111 |
$this->assertEquals(3, $DB->count_records_select(
|
|
|
112 |
'forum_discussions',
|
|
|
113 |
'forum = :forum',
|
|
|
114 |
['forum' => $forum->id]
|
|
|
115 |
));
|
|
|
116 |
|
|
|
117 |
$record['tags'] = ['Cats', 'mice'];
|
|
|
118 |
$record = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
|
|
|
119 |
$this->assertEquals(
|
|
|
120 |
['Cats', 'mice'],
|
|
|
121 |
array_values(\core_tag_tag::get_item_tags_array('mod_forum', 'forum_posts', $record->firstpost))
|
|
|
122 |
);
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
/**
|
|
|
126 |
* Test create_post.
|
|
|
127 |
*/
|
|
|
128 |
public function test_create_post(): void {
|
|
|
129 |
global $DB;
|
|
|
130 |
|
|
|
131 |
$this->resetAfterTest(true);
|
|
|
132 |
|
|
|
133 |
// Create a bunch of users.
|
|
|
134 |
$user1 = self::getDataGenerator()->create_user();
|
|
|
135 |
$user2 = self::getDataGenerator()->create_user();
|
|
|
136 |
$user3 = self::getDataGenerator()->create_user();
|
|
|
137 |
$user4 = self::getDataGenerator()->create_user();
|
|
|
138 |
|
|
|
139 |
// Create course to add the forum.
|
|
|
140 |
$course = self::getDataGenerator()->create_course();
|
|
|
141 |
|
|
|
142 |
// The forum.
|
|
|
143 |
$record = new \stdClass();
|
|
|
144 |
$record->course = $course->id;
|
|
|
145 |
$forum = self::getDataGenerator()->create_module('forum', $record);
|
|
|
146 |
|
|
|
147 |
// Add a discussion.
|
|
|
148 |
$record->forum = $forum->id;
|
|
|
149 |
$record->userid = $user1->id;
|
|
|
150 |
$discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
|
|
|
151 |
|
|
|
152 |
// Add a bunch of replies, changing the userid.
|
|
|
153 |
$record = new \stdClass();
|
|
|
154 |
$record->discussion = $discussion->id;
|
|
|
155 |
$record->userid = $user2->id;
|
|
|
156 |
self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
|
|
|
157 |
$record->userid = $user3->id;
|
|
|
158 |
self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
|
|
|
159 |
$record->userid = $user4->id;
|
|
|
160 |
self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
|
|
|
161 |
|
|
|
162 |
// Check the posts were correctly created, remember, when creating a discussion a post
|
|
|
163 |
// is generated as well, so we should have 4 posts, not 3.
|
|
|
164 |
$this->assertEquals(4, $DB->count_records_select(
|
|
|
165 |
'forum_posts',
|
|
|
166 |
'discussion = :discussion',
|
|
|
167 |
['discussion' => $discussion->id]
|
|
|
168 |
));
|
|
|
169 |
|
|
|
170 |
$record->tags = ['Cats', 'mice'];
|
|
|
171 |
$record = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
|
|
|
172 |
$this->assertEquals(
|
|
|
173 |
['Cats', 'mice'],
|
|
|
174 |
array_values(\core_tag_tag::get_item_tags_array('mod_forum', 'forum_posts', $record->id))
|
|
|
175 |
);
|
|
|
176 |
}
|
|
|
177 |
|
|
|
178 |
public function test_create_content(): void {
|
|
|
179 |
global $DB;
|
|
|
180 |
|
|
|
181 |
$this->resetAfterTest(true);
|
|
|
182 |
|
|
|
183 |
// Create a bunch of users.
|
|
|
184 |
$user1 = self::getDataGenerator()->create_user();
|
|
|
185 |
$user2 = self::getDataGenerator()->create_user();
|
|
|
186 |
$user3 = self::getDataGenerator()->create_user();
|
|
|
187 |
$user4 = self::getDataGenerator()->create_user();
|
|
|
188 |
|
|
|
189 |
$this->setAdminUser();
|
|
|
190 |
|
|
|
191 |
// Create course and forum.
|
|
|
192 |
$course = self::getDataGenerator()->create_course();
|
|
|
193 |
$forum = self::getDataGenerator()->create_module('forum', ['course' => $course]);
|
|
|
194 |
|
|
|
195 |
$generator = self::getDataGenerator()->get_plugin_generator('mod_forum');
|
|
|
196 |
// This should create discussion.
|
|
|
197 |
$post1 = $generator->create_content($forum);
|
|
|
198 |
// This should create posts in the discussion.
|
|
|
199 |
$post2 = $generator->create_content($forum, ['parent' => $post1->id]);
|
|
|
200 |
$post3 = $generator->create_content($forum, ['discussion' => $post1->discussion]);
|
|
|
201 |
// This should create posts answering another post.
|
|
|
202 |
$post4 = $generator->create_content($forum, ['parent' => $post2->id]);
|
|
|
203 |
// This should create post with tags.
|
|
|
204 |
$post5 = $generator->create_content($forum, ['parent' => $post2->id, 'tags' => ['Cats', 'mice']]);
|
|
|
205 |
|
|
|
206 |
$discussionrecords = $DB->get_records('forum_discussions', ['forum' => $forum->id]);
|
|
|
207 |
$postrecords = $DB->get_records('forum_posts');
|
|
|
208 |
$postrecords2 = $DB->get_records('forum_posts', ['discussion' => $post1->discussion]);
|
|
|
209 |
$this->assertEquals(1, count($discussionrecords));
|
|
|
210 |
$this->assertEquals(5, count($postrecords));
|
|
|
211 |
$this->assertEquals(5, count($postrecords2));
|
|
|
212 |
$this->assertEquals($post1->id, $discussionrecords[$post1->discussion]->firstpost);
|
|
|
213 |
$this->assertEquals($post1->id, $postrecords[$post2->id]->parent);
|
|
|
214 |
$this->assertEquals($post1->id, $postrecords[$post3->id]->parent);
|
|
|
215 |
$this->assertEquals($post2->id, $postrecords[$post4->id]->parent);
|
|
|
216 |
|
|
|
217 |
$this->assertEquals(
|
|
|
218 |
['Cats', 'mice'],
|
|
|
219 |
array_values(\core_tag_tag::get_item_tags_array('mod_forum', 'forum_posts', $post5->id))
|
|
|
220 |
);
|
|
|
221 |
}
|
|
|
222 |
|
|
|
223 |
public function test_create_post_time_system(): void {
|
|
|
224 |
$this->resetAfterTest(true);
|
|
|
225 |
|
|
|
226 |
$user = self::getDataGenerator()->create_user();
|
|
|
227 |
$course = $this->getDataGenerator()->create_course();
|
|
|
228 |
$forum = self::getDataGenerator()->create_module('forum', (object) [
|
|
|
229 |
'course' => $course->id,
|
|
|
230 |
]);
|
|
|
231 |
|
|
|
232 |
$starttime = time();
|
|
|
233 |
|
|
|
234 |
// Add a discussion.
|
|
|
235 |
$discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion((object) [
|
|
|
236 |
'course' => $course->id,
|
|
|
237 |
'forum' => $forum->id,
|
|
|
238 |
'userid' => $user->id,
|
|
|
239 |
]);
|
|
|
240 |
|
|
|
241 |
// Add a post.
|
|
|
242 |
$post = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post((object) [
|
|
|
243 |
'discussion' => $discussion->id,
|
|
|
244 |
'userid' => $user->id,
|
|
|
245 |
]);
|
|
|
246 |
|
|
|
247 |
$this->assertGreaterThanOrEqual($starttime, $discussion->timemodified);
|
|
|
248 |
$this->assertGreaterThanOrEqual($starttime, $post->created);
|
|
|
249 |
|
|
|
250 |
// The fallback behavior is to add the number of created posts to the current time to avoid duplicates.
|
|
|
251 |
$this->assertLessThanOrEqual(time() + 1, $discussion->timemodified);
|
|
|
252 |
$this->assertLessThanOrEqual(time() + 2, $post->created);
|
|
|
253 |
}
|
|
|
254 |
|
|
|
255 |
public function test_create_post_time_frozen(): void {
|
|
|
256 |
$this->resetAfterTest(true);
|
|
|
257 |
|
|
|
258 |
$clock = $this->mock_clock_with_frozen(100);
|
|
|
259 |
|
|
|
260 |
$user = self::getDataGenerator()->create_user();
|
|
|
261 |
$course = $this->getDataGenerator()->create_course();
|
|
|
262 |
$forum = self::getDataGenerator()->create_module('forum', (object) [
|
|
|
263 |
'course' => $course->id,
|
|
|
264 |
]);
|
|
|
265 |
|
|
|
266 |
$starttime = time();
|
|
|
267 |
|
|
|
268 |
// Add a discussion.
|
|
|
269 |
$discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion((object) [
|
|
|
270 |
'course' => $course->id,
|
|
|
271 |
'forum' => $forum->id,
|
|
|
272 |
'userid' => $user->id,
|
|
|
273 |
]);
|
|
|
274 |
$this->assertEquals(100, $discussion->timemodified);
|
|
|
275 |
|
|
|
276 |
// Add a post.
|
|
|
277 |
$clock->set_to(200);
|
|
|
278 |
$post = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post((object) [
|
|
|
279 |
'discussion' => $discussion->id,
|
|
|
280 |
'userid' => $user->id,
|
|
|
281 |
]);
|
|
|
282 |
|
|
|
283 |
$this->assertEquals(200, $post->created);
|
|
|
284 |
}
|
|
|
285 |
}
|