Permalink
Browse files

MDL-30102 mod_forum: adding webservice function to allow for the retr…

…ieval of forum discussions
  • Loading branch information...
markn86 committed Jan 4, 2013
1 parent b3778a0 commit a9a0cb691c5eb812f9427d44af07140defc54b8a
@@ -41,6 +41,16 @@ class testing_data_generator {
protected $groupcount = 0;
protected $groupingcount = 0;
+ /**
+ * @var int keep track of how many forum discussions have been created.
+ */
+ protected $forumdiscussioncount = 0;
+
+ /**
+ * @var int keep track of how many forum posts have been created.
+ */
+ protected $forumpostcount = 0;
+
/** @var array list of plugin generators */
protected $generators = array();
@@ -82,6 +92,8 @@ public function reset() {
$this->categorycount = 0;
$this->coursecount = 0;
$this->scalecount = 0;
+ $this->forumdiscussioncount = 0;
+ $this->forumpostcount = 0;
foreach ($this->generators as $generator) {
$generator->reset();
@@ -600,6 +612,136 @@ public function enrol_user($userid, $courseid, $roleid = null, $enrol = 'manual'
return true;
}
+
+ /**
+ * Function to create a dummy discussion.
+ *
+ * @param array|stdClass $record
+ * @return stdClass the discussion object
+ */
+ public function create_forum_discussion($record = null) {
+ global $DB;
+
+ // Increment the forum discussion count.
+ $this->forumdiscussioncount++;
+
+ $record = (array) $record;
+
+ if (!isset($record['course'])) {
+ throw new coding_exception('course must be present in phpunit_util::create_forum_discussion() $record');
+ }
+
+ if (!isset($record['forum'])) {
+ throw new coding_exception('forum must be present in phpunit_util::create_forum_discussion() $record');
+ }
+
+ if (!isset($record['userid'])) {
+ throw new coding_exception('userid must be present in phpunit_util::create_forum_discussion() $record');
+ }
+
+ if (!isset($record['name'])) {
+ $record['name'] = "Discussion " . $this->forumdiscussioncount;
+ }
+
+ if (!isset($record['subject'])) {
+ $record['subject'] = "Subject for discussion " . $this->forumdiscussioncount;
+ }
+
+ if (!isset($record['message'])) {
+ $record['message'] = html_writer::tag('p', 'Message for discussion ' . $this->forumdiscussioncount);
+ }
+
+ if (!isset($record['messageformat'])) {
+ $record['messageformat'] = editors_get_preferred_format();
+ }
+
+ if (!isset($record['messagetrust'])) {
+ $record['messagetrust'] = "";
+ }
+
+ if (!isset($record['assessed'])) {
+ $record['assessed'] = '1';
+ }
+
+ if (!isset($record['groupid'])) {
+ $record['groupid'] = "-1";
+ }
+
+ if (!isset($record['timestart'])) {
+ $record['timestart'] = "0";
+ }
+
+ if (!isset($record['timeend'])) {
+ $record['timeend'] = "0";
+ }
+
+ if (!isset($record['mailnow'])) {
+ $record['mailnow'] = "0";
+ }
+
+ $record = (object) $record;
+
+ // Add the discussion.
+ $record->id = forum_add_discussion($record, null, null, $record->userid);
+
+ return $record;
+ }
+
+ /**
+ * Function to create a dummy post.
+ *
+ * @param array|stdClass $record
+ * @return stdClass the post object
+ */
+ public function create_forum_post($record = null) {
+ global $DB;
+
+ // Increment the forum post count.
+ $this->forumpostcount++;
+
+ // Variable to store time.
+ $time = time() + $this->forumpostcount;
+
+ $record = (array) $record;
+
+ if (!isset($record['discussion'])) {
+ throw new coding_exception('discussion must be present in phpunit_util::create_forum_post() $record');
+ }
+
+ if (!isset($record['userid'])) {
+ throw new coding_exception('userid must be present in phpunit_util::create_forum_post() $record');
+ }
+
+ if (!isset($record['parent'])) {
+ $record['parent'] = 0;
+ }
+
+ if (!isset($record['subject'])) {
+ $record['subject'] = 'Forum post subject ' . $this->forumpostcount;
+ }
+
+ if (!isset($record['message'])) {
+ $record['message'] = html_writer::tag('p', 'Forum message post ' . $this->forumpostcount);
+ }
+
+ if (!isset($record['created'])) {
+ $record['created'] = $time;
+ }
+
+ if (!isset($record['modified'])) {
+ $record['modified'] = $time;
+ }
+
+ $record = (object) $record;
+
+ // Add the post.
+ $record->id = $DB->insert_record('forum_posts', $record);
+
+ // Update the last post.
+ forum_discussion_update_last_post($record->discussion);
+
+ return $record;
+ }
}
/**
@@ -203,4 +203,80 @@ public function test_enrol_user() {
$this->assertFalse($result);
}
+
+ /**
+ * Test create_forum_discussion.
+ */
+ public function test_create_forum_discussion() {
+ global $DB;
+
+ $this->resetAfterTest(true);
+
+ // User that will create the forum.
+ $user = self::getDataGenerator()->create_user();
+
+ // Create course to add the forum to.
+ $course = self::getDataGenerator()->create_course();
+
+ // The forum.
+ $record = new stdClass();
+ $record->course = $course->id;
+ $forum = self::getDataGenerator()->create_module('forum', $record);
+
+ // Add a few discussions.
+ $record = array();
+ $record['course'] = $course->id;
+ $record['forum'] = $forum->id;
+ $record['userid'] = $user->id;
+ self::getDataGenerator()->create_forum_discussion($record);
+ self::getDataGenerator()->create_forum_discussion($record);
+ self::getDataGenerator()->create_forum_discussion($record);
+
+ // Check the discussions were correctly created.
+ $this->assertEquals(3, $DB->count_records_select('forum_discussions', 'forum = :forum',
+ array('forum' => $forum->id)));
+ }
+
+ /**
+ * Test create_forum_post.
+ */
+ public function test_create_forum_post() {
+ global $DB;
+
+ $this->resetAfterTest(true);
+
+ // Create a bunch of users
+ $user1 = self::getDataGenerator()->create_user();
+ $user2 = self::getDataGenerator()->create_user();
+ $user3 = self::getDataGenerator()->create_user();
+ $user4 = self::getDataGenerator()->create_user();
+
+ // Create course to add the forum.
+ $course = self::getDataGenerator()->create_course();
+
+ // The forum.
+ $record = new stdClass();
+ $record->course = $course->id;
+ $forum = self::getDataGenerator()->create_module('forum', $record);
+
+ // Add a discussion.
+ $record->forum = $forum->id;
+ $record->userid = $user1->id;
+ $discussion = self::getDataGenerator()->create_forum_discussion($record);
+
+ // Add a bunch of replies, changing the userid.
+ $record = new stdClass();
+ $record->discussion = $discussion->id;
+ $record->userid = $user2->id;
+ self::getDataGenerator()->create_forum_post($record);
+ $record->userid = $user3->id;
+ self::getDataGenerator()->create_forum_post($record);
+ $record->userid = $user4->id;
+ self::getDataGenerator()->create_forum_post($record);
+
+ // Check the posts were correctly created, remember, when creating a discussion a post
+ // is generated as well, so we should have 4 posts, not 3.
+ $this->assertEquals(4, $DB->count_records_select('forum_posts', 'discussion = :discussion',
+ array('discussion' => $discussion->id)));
+ }
}
@@ -34,5 +34,14 @@
returned.',
'type' => 'read',
'capabilities' => 'mod/forum:viewdiscussion'
+ ),
+
+ 'mod_forum_get_forum_discussions' => array(
+ 'classname' => 'mod_forum_external',
+ 'methodname' => 'get_forum_discussions',
+ 'classpath' => 'mod/forum/externallib.php',
+ 'description' => 'Returns a list of forum discussions contained within a given set of forums.',
+ 'type' => 'read',
+ 'capabilities' => 'mod/forum:viewdiscussion, mod/forum:viewqandawithoutposting'
)
);
Oops, something went wrong.

0 comments on commit a9a0cb6

Please sign in to comment.