Skip to content

Commit

Permalink
Merge branch 'MDL-65429-master' of git://github.com/peterRd/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Sep 11, 2019
2 parents 6739739 + 474a96d commit 15f13cb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
3 changes: 3 additions & 0 deletions mod/forum/classes/subscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -514,9 +514,12 @@ public static function fill_discussion_subscription_cache($forumid, $userid = nu
'userid' => $userid,
'forum' => $forumid,
), null, 'id, discussion, preference');

self::$forumdiscussioncache[$userid][$forumid] = array();
foreach ($subscriptions as $id => $data) {
self::add_to_discussion_cache($forumid, $userid, $data->discussion, $data->preference);
}

$subscriptions->close();
}
} else {
Expand Down
30 changes: 28 additions & 2 deletions mod/forum/tests/subscriptions_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1036,7 +1036,7 @@ public function test_discussion_subscription_cache_prefill() {
// Create a course, with a forum.
$course = $this->getDataGenerator()->create_course();

$options = array('course' => $course->id, 'forcesubscribe' => FORUM_INITIALSUBSCRIBE);
$options = array('course' => $course->id, 'forcesubscribe' => FORUM_FORCESUBSCRIBE);
$forum = $this->getDataGenerator()->create_module('forum', $options);

// Create some users.
Expand All @@ -1045,6 +1045,8 @@ public function test_discussion_subscription_cache_prefill() {
// Post some discussions to the forum.
$discussions = array();
$author = $users[0];
$userwithnosubs = $users[1];

for ($i = 0; $i < 20; $i++) {
list($discussion, $post) = $this->helper_post_to_forum($forum, $author);
$discussions[] = $discussion;
Expand All @@ -1053,15 +1055,20 @@ public function test_discussion_subscription_cache_prefill() {
// Unsubscribe half the users from the half the discussions.
$forumcount = 0;
$usercount = 0;
$userwithsubs = null;
foreach ($discussions as $data) {
// Unsubscribe user from all discussions.
\mod_forum\subscriptions::unsubscribe_user_from_discussion($userwithnosubs->id, $data);

if ($forumcount % 2) {
continue;
}
foreach ($users as $user) {
if ($usercount % 2) {
$userwithsubs = $user;
continue;
}
\mod_forum\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion);
\mod_forum\subscriptions::unsubscribe_user_from_discussion($user->id, $data);
$usercount++;
}
$forumcount++;
Expand All @@ -1071,6 +1078,25 @@ public function test_discussion_subscription_cache_prefill() {
\mod_forum\subscriptions::reset_forum_cache();
\mod_forum\subscriptions::reset_discussion_cache();

// A user with no subscriptions should only be fetched once.
$this->assertNull(\mod_forum\subscriptions::fill_discussion_subscription_cache($forum->id, $userwithnosubs->id));
$startcount = $DB->perf_get_reads();
$this->assertNull(\mod_forum\subscriptions::fill_discussion_subscription_cache($forum->id, $userwithnosubs->id));
$this->assertEquals($startcount, $DB->perf_get_reads());

// Confirm subsequent calls properly tries to fetch subs.
$this->assertNull(\mod_forum\subscriptions::fill_discussion_subscription_cache($forum->id, $userwithsubs->id));
$this->assertNotEquals($startcount, $DB->perf_get_reads());

// Another read should be performed to get all subscriptions for the forum.
$startcount = $DB->perf_get_reads();
$this->assertNull(\mod_forum\subscriptions::fill_discussion_subscription_cache($forum->id));
$this->assertNotEquals($startcount, $DB->perf_get_reads());

// Reset the subscription caches.
\mod_forum\subscriptions::reset_forum_cache();
\mod_forum\subscriptions::reset_discussion_cache();

// Filling the discussion subscription cache should only use a single query.
$startcount = $DB->perf_get_reads();
$this->assertNull(\mod_forum\subscriptions::fill_discussion_subscription_cache($forum->id));
Expand Down

0 comments on commit 15f13cb

Please sign in to comment.