From b95db263ca2ab1498a13a5036b6b73118b682bd2 Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Thu, 8 May 2014 17:50:11 +0200 Subject: [PATCH] MDL-45296 tests: Fix unit tests to have objectid 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 | 18 ++++++++++++++ mod/forum/tests/generator/lib.php | 40 +++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/mod/forum/tests/events_test.php b/mod/forum/tests/events_test.php index 424d5a935a9..f7cf595cae3 100644 --- a/mod/forum/tests/events_test.php +++ b/mod/forum/tests/events_test.php @@ -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, ); diff --git a/mod/forum/tests/generator/lib.php b/mod/forum/tests/generator/lib.php index 8d91e307849..77198d712de 100644 --- a/mod/forum/tests/generator/lib.php +++ b/mod/forum/tests/generator/lib.php @@ -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. *