[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
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 self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); 112 self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); 113 self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); 114 115 // Check the discussions were correctly created. 116 $this->assertEquals(3, $DB->count_records_select('forum_discussions', 'forum = :forum', 117 array('forum' => $forum->id))); 118 } 119 120 /** 121 * Test create_post. 122 */ 123 public function test_create_post() { 124 global $DB; 125 126 $this->resetAfterTest(true); 127 128 // Create a bunch of users 129 $user1 = self::getDataGenerator()->create_user(); 130 $user2 = self::getDataGenerator()->create_user(); 131 $user3 = self::getDataGenerator()->create_user(); 132 $user4 = self::getDataGenerator()->create_user(); 133 134 // Create course to add the forum. 135 $course = self::getDataGenerator()->create_course(); 136 137 // The forum. 138 $record = new stdClass(); 139 $record->course = $course->id; 140 $forum = self::getDataGenerator()->create_module('forum', $record); 141 142 // Add a discussion. 143 $record->forum = $forum->id; 144 $record->userid = $user1->id; 145 $discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); 146 147 // Add a bunch of replies, changing the userid. 148 $record = new stdClass(); 149 $record->discussion = $discussion->id; 150 $record->userid = $user2->id; 151 self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record); 152 $record->userid = $user3->id; 153 self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record); 154 $record->userid = $user4->id; 155 self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record); 156 157 // Check the posts were correctly created, remember, when creating a discussion a post 158 // is generated as well, so we should have 4 posts, not 3. 159 $this->assertEquals(4, $DB->count_records_select('forum_posts', 'discussion = :discussion', 160 array('discussion' => $discussion->id))); 161 } 162 163 public function test_create_content() { 164 global $DB; 165 166 $this->resetAfterTest(true); 167 168 // Create a bunch of users 169 $user1 = self::getDataGenerator()->create_user(); 170 $user2 = self::getDataGenerator()->create_user(); 171 $user3 = self::getDataGenerator()->create_user(); 172 $user4 = self::getDataGenerator()->create_user(); 173 174 $this->setAdminUser(); 175 176 // Create course and forum. 177 $course = self::getDataGenerator()->create_course(); 178 $forum = self::getDataGenerator()->create_module('forum', array('course' => $course)); 179 180 $generator = self::getDataGenerator()->get_plugin_generator('mod_forum'); 181 // This should create discussion. 182 $post1 = $generator->create_content($forum); 183 // This should create posts in the discussion. 184 $post2 = $generator->create_content($forum, array('parent' => $post1->id)); 185 $post3 = $generator->create_content($forum, array('discussion' => $post1->discussion)); 186 // This should create posts answering another post. 187 $post4 = $generator->create_content($forum, array('parent' => $post2->id)); 188 189 $discussionrecords = $DB->get_records('forum_discussions', array('forum' => $forum->id)); 190 $postrecords = $DB->get_records('forum_posts'); 191 $postrecords2 = $DB->get_records('forum_posts', array('discussion' => $post1->discussion)); 192 $this->assertEquals(1, count($discussionrecords)); 193 $this->assertEquals(4, count($postrecords)); 194 $this->assertEquals(4, count($postrecords2)); 195 $this->assertEquals($post1->id, $discussionrecords[$post1->discussion]->firstpost); 196 $this->assertEquals($post1->id, $postrecords[$post2->id]->parent); 197 $this->assertEquals($post1->id, $postrecords[$post3->id]->parent); 198 $this->assertEquals($post2->id, $postrecords[$post4->id]->parent); 199 } 200 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |