MDL-45296 tests: Fix unit tests to have objectid
authorEloy Lafuente (stronk7) <stronk7@moodle.org>
Thu, 8 May 2014 15:50:11 +0000 (17:50 +0200)
committerEloy Lafuente (stronk7) <stronk7@moodle.org>
Thu, 8 May 2014 18:03:17 +0000 (20:03 +0200)
In order to create the needed subscriptions to
make the event tests reliable a subscriptions
generator has been added. It performs raw-sql
subscriptions because by using the forum API
events would be fired (and that's something
we don't want to happen).

mod/forum/tests/events_test.php
mod/forum/tests/generator/lib.php

index 424d5a9..f7cf595 100644 (file)
@@ -621,10 +621,19 @@ class mod_forum_events_testcase extends advanced_testcase {
         $user = $this->getDataGenerator()->create_user();
         $course = $this->getDataGenerator()->create_course();
         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
+        $user = $this->getDataGenerator()->create_user();
         $context = context_module::instance($forum->cmid);
 
+        // Add a subscription.
+        $record = array();
+        $record['course'] = $course->id;
+        $record['forum'] = $forum->id;
+        $record['userid'] = $user->id;
+        $subscription = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_subscription($record);
+
         $params = array(
             'context' => $context,
+            'objectid' => $subscription->id,
             'other' => array('forumid' => $forum->id),
             'relateduserid' => $user->id,
         );
@@ -709,10 +718,19 @@ class mod_forum_events_testcase extends advanced_testcase {
         $user = $this->getDataGenerator()->create_user();
         $course = $this->getDataGenerator()->create_course();
         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
+        $user = $this->getDataGenerator()->create_user();
         $context = context_module::instance($forum->cmid);
 
+        // Add a subscription.
+        $record = array();
+        $record['course'] = $course->id;
+        $record['forum'] = $forum->id;
+        $record['userid'] = $user->id;
+        $subscription = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_subscription($record);
+
         $params = array(
             'context' => $context,
+            'objectid' => $subscription->id,
             'other' => array('forumid' => $forum->id),
             'relateduserid' => $user->id,
         );
index 8d91e30..77198d7 100644 (file)
@@ -46,6 +46,11 @@ class mod_forum_generator extends testing_module_generator {
      */
     protected $forumpostcount = 0;
 
+    /**
+     * @var int keep track of how many forum subscriptions have been created.
+     */
+    protected $forumsubscriptionscount = 0;
+
     /**
      * To be called from data reset code only,
      * do not use in tests.
@@ -54,6 +59,7 @@ class mod_forum_generator extends testing_module_generator {
     public function reset() {
         $this->forumdiscussioncount = 0;
         $this->forumpostcount = 0;
+        $this->forumsubscriptionscount = 0;
 
         parent::reset();
     }
@@ -79,6 +85,40 @@ class mod_forum_generator extends testing_module_generator {
         return parent::create_instance($record, (array)$options);
     }
 
+    /**
+     * Function to create a dummy subscription.
+     *
+     * @param array|stdClass $record
+     * @return stdClass the subscription object
+     */
+    public function create_subscription($record = null) {
+        global $DB;
+
+        // Increment the forum subscription count.
+        $this->forumsubscriptionscount++;
+
+        $record = (array)$record;
+
+        if (!isset($record['course'])) {
+            throw new coding_exception('course must be present in phpunit_util::create_subscription() $record');
+        }
+
+        if (!isset($record['forum'])) {
+            throw new coding_exception('forum must be present in phpunit_util::create_subscription() $record');
+        }
+
+        if (!isset($record['userid'])) {
+            throw new coding_exception('userid must be present in phpunit_util::create_subscription() $record');
+        }
+
+        $record = (object)$record;
+
+        // Add the subscription.
+        $record->id = $DB->insert_record('forum_subscriptions', $record);
+
+        return $record;
+    }
+
     /**
      * Function to create a dummy discussion.
      *