Proyectos de Subversion Moodle

Rev

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