Merge branch 'MDL-53166' of https://github.com/eugeneventer/moodle-fixes
[moodle.git] / mod / forum / tests / generator_test.php
CommitLineData
f8965a3d
PS
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 * PHPUnit data generator tests
19 *
20 * @package mod_forum
21 * @category phpunit
22 * @copyright 2012 Petr Skoda {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28
29/**
30 * PHPUnit data generator testcase
31 *
32 * @package mod_forum
33 * @category phpunit
34 * @copyright 2012 Petr Skoda {@link http://skodak.org}
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
37class mod_forum_generator_testcase extends advanced_testcase {
59075a43
AN
38
39 public function setUp() {
40 // We must clear the subscription caches. This has to be done both before each test, and after in case of other
41 // tests using these functions.
42 \mod_forum\subscriptions::reset_forum_cache();
43 }
44
45 public function tearDown() {
46 // We must clear the subscription caches. This has to be done both before each test, and after in case of other
47 // tests using these functions.
48 \mod_forum\subscriptions::reset_forum_cache();
49 }
50
9bb2b515 51 public function test_generator() {
6b04fdc0 52 global $DB;
f8965a3d
PS
53
54 $this->resetAfterTest(true);
55
56 $this->assertEquals(0, $DB->count_records('forum'));
57
6b04fdc0
PS
58 $course = $this->getDataGenerator()->create_course();
59
f8965a3d
PS
60 /** @var mod_forum_generator $generator */
61 $generator = $this->getDataGenerator()->get_plugin_generator('mod_forum');
62 $this->assertInstanceOf('mod_forum_generator', $generator);
63 $this->assertEquals('forum', $generator->get_modulename());
64
6b04fdc0
PS
65 $generator->create_instance(array('course'=>$course->id));
66 $generator->create_instance(array('course'=>$course->id));
67 $forum = $generator->create_instance(array('course'=>$course->id));
f8965a3d
PS
68 $this->assertEquals(3, $DB->count_records('forum'));
69
70 $cm = get_coursemodule_from_instance('forum', $forum->id);
71 $this->assertEquals($forum->id, $cm->instance);
72 $this->assertEquals('forum', $cm->modname);
6b04fdc0 73 $this->assertEquals($course->id, $cm->course);
f8965a3d
PS
74
75 $context = context_module::instance($cm->id);
76 $this->assertEquals($forum->cmid, $context->instanceid);
6b04fdc0
PS
77
78 // test gradebook integration using low level DB access - DO NOT USE IN PLUGIN CODE!
79 $forum = $generator->create_instance(array('course'=>$course->id, 'assessed'=>1, 'scale'=>100));
80 $gitem = $DB->get_record('grade_items', array('courseid'=>$course->id, 'itemtype'=>'mod', 'itemmodule'=>'forum', 'iteminstance'=>$forum->id));
81 $this->assertNotEmpty($gitem);
82 $this->assertEquals(100, $gitem->grademax);
83 $this->assertEquals(0, $gitem->grademin);
84 $this->assertEquals(GRADE_TYPE_VALUE, $gitem->gradetype);
f8965a3d 85 }
c3f31a3d
PS
86
87 /**
88 * Test create_discussion.
89 */
90 public function test_create_discussion() {
91 global $DB;
92
93 $this->resetAfterTest(true);
94
95 // User that will create the forum.
96 $user = self::getDataGenerator()->create_user();
97
98 // Create course to add the forum to.
99 $course = self::getDataGenerator()->create_course();
100
101 // The forum.
102 $record = new stdClass();
103 $record->course = $course->id;
104 $forum = self::getDataGenerator()->create_module('forum', $record);
105
106 // Add a few discussions.
107 $record = array();
108 $record['course'] = $course->id;
109 $record['forum'] = $forum->id;
110 $record['userid'] = $user->id;
f9621c56 111 $record['pinned'] = FORUM_DISCUSSION_PINNED; // Pin one discussion.
c3f31a3d 112 self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
f9621c56 113 $record['pinned'] = FORUM_DISCUSSION_UNPINNED; // No pin for others.
c3f31a3d
PS
114 self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
115 self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
116
117 // Check the discussions were correctly created.
118 $this->assertEquals(3, $DB->count_records_select('forum_discussions', 'forum = :forum',
119 array('forum' => $forum->id)));
120 }
121
122 /**
123 * Test create_post.
124 */
125 public function test_create_post() {
126 global $DB;
127
128 $this->resetAfterTest(true);
129
130 // Create a bunch of users
131 $user1 = self::getDataGenerator()->create_user();
132 $user2 = self::getDataGenerator()->create_user();
133 $user3 = self::getDataGenerator()->create_user();
134 $user4 = self::getDataGenerator()->create_user();
135
136 // Create course to add the forum.
137 $course = self::getDataGenerator()->create_course();
138
139 // The forum.
140 $record = new stdClass();
141 $record->course = $course->id;
142 $forum = self::getDataGenerator()->create_module('forum', $record);
143
144 // Add a discussion.
145 $record->forum = $forum->id;
146 $record->userid = $user1->id;
147 $discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
148
149 // Add a bunch of replies, changing the userid.
150 $record = new stdClass();
151 $record->discussion = $discussion->id;
152 $record->userid = $user2->id;
153 self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
154 $record->userid = $user3->id;
155 self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
156 $record->userid = $user4->id;
157 self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
158
159 // Check the posts were correctly created, remember, when creating a discussion a post
160 // is generated as well, so we should have 4 posts, not 3.
161 $this->assertEquals(4, $DB->count_records_select('forum_posts', 'discussion = :discussion',
162 array('discussion' => $discussion->id)));
163 }
b6affde2
MG
164
165 public function test_create_content() {
166 global $DB;
167
168 $this->resetAfterTest(true);
169
170 // Create a bunch of users
171 $user1 = self::getDataGenerator()->create_user();
172 $user2 = self::getDataGenerator()->create_user();
173 $user3 = self::getDataGenerator()->create_user();
174 $user4 = self::getDataGenerator()->create_user();
175
176 $this->setAdminUser();
177
178 // Create course and forum.
179 $course = self::getDataGenerator()->create_course();
180 $forum = self::getDataGenerator()->create_module('forum', array('course' => $course));
181
182 $generator = self::getDataGenerator()->get_plugin_generator('mod_forum');
183 // This should create discussion.
184 $post1 = $generator->create_content($forum);
185 // This should create posts in the discussion.
186 $post2 = $generator->create_content($forum, array('parent' => $post1->id));
187 $post3 = $generator->create_content($forum, array('discussion' => $post1->discussion));
188 // This should create posts answering another post.
189 $post4 = $generator->create_content($forum, array('parent' => $post2->id));
190
191 $discussionrecords = $DB->get_records('forum_discussions', array('forum' => $forum->id));
192 $postrecords = $DB->get_records('forum_posts');
193 $postrecords2 = $DB->get_records('forum_posts', array('discussion' => $post1->discussion));
194 $this->assertEquals(1, count($discussionrecords));
195 $this->assertEquals(4, count($postrecords));
196 $this->assertEquals(4, count($postrecords2));
197 $this->assertEquals($post1->id, $discussionrecords[$post1->discussion]->firstpost);
198 $this->assertEquals($post1->id, $postrecords[$post2->id]->parent);
199 $this->assertEquals($post1->id, $postrecords[$post3->id]->parent);
200 $this->assertEquals($post2->id, $postrecords[$post4->id]->parent);
201 }
f8965a3d 202}