See Release Notes
Long Term Support Release
Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]
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 26 defined('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 */ 37 class mod_forum_generator_testcase extends advanced_testcase { 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 51 public function test_generator() { 52 global $DB; 53 54 $this->resetAfterTest(true); 55 56 $this->assertEquals(0, $DB->count_records('forum')); 57 58 $course = $this->getDataGenerator()->create_course(); 59 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 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)); 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); 73 $this->assertEquals($course->id, $cm->course); 74 75 $context = context_module::instance($cm->id); 76 $this->assertEquals($forum->cmid, $context->instanceid); 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); 85 } 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; 111 $record['pinned'] = FORUM_DISCUSSION_PINNED; // Pin one discussion. 112 self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); 113 $record['pinned'] = FORUM_DISCUSSION_UNPINNED; // No pin for others. 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 $record['tags'] = array('Cats', 'mice'); 122 $record = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); 123 $this->assertEquals(array('Cats', 'mice'), 124 array_values(core_tag_tag::get_item_tags_array('mod_forum', 'forum_posts', $record->firstpost))); 125 } 126 127 /** 128 * Test create_post. 129 */ 130 public function test_create_post() { 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('forum_posts', 'discussion = :discussion', 167 array('discussion' => $discussion->id))); 168 169 $record->tags = array('Cats', 'mice'); 170 $record = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record); 171 $this->assertEquals(array('Cats', 'mice'), 172 array_values(core_tag_tag::get_item_tags_array('mod_forum', 'forum_posts', $record->id))); 173 } 174 175 public function test_create_content() { 176 global $DB; 177 178 $this->resetAfterTest(true); 179 180 // Create a bunch of users 181 $user1 = self::getDataGenerator()->create_user(); 182 $user2 = self::getDataGenerator()->create_user(); 183 $user3 = self::getDataGenerator()->create_user(); 184 $user4 = self::getDataGenerator()->create_user(); 185 186 $this->setAdminUser(); 187 188 // Create course and forum. 189 $course = self::getDataGenerator()->create_course(); 190 $forum = self::getDataGenerator()->create_module('forum', array('course' => $course)); 191 192 $generator = self::getDataGenerator()->get_plugin_generator('mod_forum'); 193 // This should create discussion. 194 $post1 = $generator->create_content($forum); 195 // This should create posts in the discussion. 196 $post2 = $generator->create_content($forum, array('parent' => $post1->id)); 197 $post3 = $generator->create_content($forum, array('discussion' => $post1->discussion)); 198 // This should create posts answering another post. 199 $post4 = $generator->create_content($forum, array('parent' => $post2->id)); 200 // This should create post with tags. 201 $post5 = $generator->create_content($forum, array('parent' => $post2->id, 'tags' => array('Cats', 'mice'))); 202 203 $discussionrecords = $DB->get_records('forum_discussions', array('forum' => $forum->id)); 204 $postrecords = $DB->get_records('forum_posts'); 205 $postrecords2 = $DB->get_records('forum_posts', array('discussion' => $post1->discussion)); 206 $this->assertEquals(1, count($discussionrecords)); 207 $this->assertEquals(5, count($postrecords)); 208 $this->assertEquals(5, count($postrecords2)); 209 $this->assertEquals($post1->id, $discussionrecords[$post1->discussion]->firstpost); 210 $this->assertEquals($post1->id, $postrecords[$post2->id]->parent); 211 $this->assertEquals($post1->id, $postrecords[$post3->id]->parent); 212 $this->assertEquals($post2->id, $postrecords[$post4->id]->parent); 213 214 $this->assertEquals(array('Cats', 'mice'), 215 array_values(core_tag_tag::get_item_tags_array('mod_forum', 'forum_posts', $post5->id))); 216 } 217 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body