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).
This commit is contained in:
Eloy Lafuente (stronk7)
2014-05-08 20:03:17 +02:00
parent 98b69989ff
commit b95db263ca
2 changed files with 58 additions and 0 deletions
+18
View 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,
);
+40
View 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.
*