MDL-65071 forum: Unit test sorting of starred discussions
authorMihail Geshoski <mihail@moodle.com>
Wed, 1 May 2019 07:54:48 +0000 (15:54 +0800)
committerMihail Geshoski <mihail@moodle.com>
Wed, 1 May 2019 14:10:12 +0000 (22:10 +0800)
mod/forum/tests/vaults_discussion_list_test.php

index 58c09d9..cac3435 100644 (file)
@@ -24,6 +24,9 @@
 
 defined('MOODLE_INTERNAL') || die();
 
+global $CFG;
+
+require_once($CFG->dirroot . '/mod/forum/externallib.php');
 require_once(__DIR__ . '/generator_trait.php');
 
 /**
@@ -82,8 +85,10 @@ class mod_forum_vaults_discussion_list_testcase extends advanced_testcase {
         $course = $datagenerator->create_course();
         $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
 
+        $this->getDataGenerator()->enrol_user($user->id, $course->id, null, 'manual');
+
         $this->assertEquals([], $vault->get_from_forum_id($forum->id, true, $user->id, null,
-            0, 0, $user));
+            0, 0));
 
         $now = time();
         [$discussion1, $post1] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now - 10, 'timemodified' => 1]);
@@ -93,14 +98,14 @@ class mod_forum_vaults_discussion_list_testcase extends advanced_testcase {
 
         // By default orders the discussions by last post.
         $summaries = array_values($vault->get_from_forum_id($forum->id, false, null, null,
-            0, 0, $user));
+            0, 0));
         $this->assertCount(3, $summaries);
         $this->assertEquals($discussion3->id, $summaries[0]->get_discussion()->get_id());
         $this->assertEquals($discussion2->id, $summaries[1]->get_discussion()->get_id());
         $this->assertEquals($discussion1->id, $summaries[2]->get_discussion()->get_id());
 
         $summaries = array_values($vault->get_from_forum_id($forum->id, true, null, null,
-            0, 0, $user));
+            0, 0));
         $this->assertCount(4, $summaries);
         $this->assertEquals($hiddendiscussion->id, $summaries[0]->get_discussion()->get_id());
         $this->assertEquals($discussion3->id, $summaries[1]->get_discussion()->get_id());
@@ -108,7 +113,7 @@ class mod_forum_vaults_discussion_list_testcase extends advanced_testcase {
         $this->assertEquals($discussion1->id, $summaries[3]->get_discussion()->get_id());
 
         $summaries = array_values($vault->get_from_forum_id($forum->id, false, $user->id, null,
-            0, 0, $user));
+            0, 0));
         $this->assertCount(4, $summaries);
         $this->assertEquals($hiddendiscussion->id, $summaries[0]->get_discussion()->get_id());
         $this->assertEquals($discussion3->id, $summaries[1]->get_discussion()->get_id());
@@ -116,17 +121,17 @@ class mod_forum_vaults_discussion_list_testcase extends advanced_testcase {
         $this->assertEquals($discussion1->id, $summaries[3]->get_discussion()->get_id());
 
         $summaries = array_values($vault->get_from_forum_id($forum->id, true, null, null,
-            1, 0, $user));
+            1, 0));
         $this->assertCount(1, $summaries);
         $this->assertEquals($hiddendiscussion->id, $summaries[0]->get_discussion()->get_id());
 
         $summaries = array_values($vault->get_from_forum_id($forum->id, true, null, null,
-            1, 1, $user));
+            1, 1));
         $this->assertCount(1, $summaries);
         $this->assertEquals($discussion3->id, $summaries[0]->get_discussion()->get_id());
 
         $summaries = array_values($vault->get_from_forum_id($forum->id, true, null, null,
-            1, 2, $user));
+            1, 2));
         $this->assertCount(1, $summaries);
         $this->assertEquals($discussion2->id, $summaries[0]->get_discussion()->get_id());
 
@@ -142,7 +147,7 @@ class mod_forum_vaults_discussion_list_testcase extends advanced_testcase {
 
         // Sort discussions by last post DESC.
         $summaries = array_values($vault->get_from_forum_id($forum->id, true, null,
-            $vault::SORTORDER_LASTPOST_DESC, 0, 0, $user));
+            $vault::SORTORDER_LASTPOST_DESC, 0, 0));
         $this->assertCount(4, $summaries);
         $this->assertEquals($hiddendiscussion->id, $summaries[0]->get_discussion()->get_id());
         $this->assertEquals($discussion2->id, $summaries[1]->get_discussion()->get_id());
@@ -151,7 +156,7 @@ class mod_forum_vaults_discussion_list_testcase extends advanced_testcase {
 
         // Sort discussions by last post ASC.
         $summaries = array_values($vault->get_from_forum_id($forum->id, true, null,
-            $vault::SORTORDER_LASTPOST_ASC, 0, 0, $user));
+            $vault::SORTORDER_LASTPOST_ASC, 0, 0));
         $this->assertCount(4, $summaries);
         $this->assertEquals($discussion3->id, $summaries[0]->get_discussion()->get_id());
         $this->assertEquals($discussion1->id, $summaries[1]->get_discussion()->get_id());
@@ -160,7 +165,7 @@ class mod_forum_vaults_discussion_list_testcase extends advanced_testcase {
 
         // Sort discussions by replies DESC.
         $summaries = array_values($vault->get_from_forum_id($forum->id, true, null,
-            $vault::SORTORDER_REPLIES_DESC, 0, 0, $user));
+            $vault::SORTORDER_REPLIES_DESC, 0, 0));
         $this->assertCount(4, $summaries);
         $this->assertEquals($discussion2->id, $summaries[0]->get_discussion()->get_id());
         $this->assertEquals($discussion1->id, $summaries[1]->get_discussion()->get_id());
@@ -169,7 +174,7 @@ class mod_forum_vaults_discussion_list_testcase extends advanced_testcase {
 
         // Sort discussions by replies ASC.
         $summaries = array_values($vault->get_from_forum_id($forum->id, true, null,
-            $vault::SORTORDER_REPLIES_ASC, 0, 0, $user));
+            $vault::SORTORDER_REPLIES_ASC, 0, 0));
         $this->assertCount(4, $summaries);
         $this->assertEquals($discussion3->id, $summaries[0]->get_discussion()->get_id());
         $this->assertEquals($hiddendiscussion->id, $summaries[1]->get_discussion()->get_id());
@@ -178,7 +183,7 @@ class mod_forum_vaults_discussion_list_testcase extends advanced_testcase {
 
         // Sort discussions by discussion created DESC.
         $summaries = array_values($vault->get_from_forum_id($forum->id, true, null,
-            $vault::SORTORDER_CREATED_DESC, 0, 0, $user));
+            $vault::SORTORDER_CREATED_DESC, 0, 0));
         $this->assertCount(4, $summaries);
         $this->assertEquals($discussion3->id, $summaries[0]->get_discussion()->get_id());
         $this->assertEquals($hiddendiscussion->id, $summaries[1]->get_discussion()->get_id());
@@ -187,7 +192,7 @@ class mod_forum_vaults_discussion_list_testcase extends advanced_testcase {
 
         // Sort discussions by discussion created ASC.
         $summaries = array_values($vault->get_from_forum_id($forum->id, true, null,
-            $vault::SORTORDER_CREATED_ASC, 0, 0, $user));
+            $vault::SORTORDER_CREATED_ASC, 0, 0));
         $this->assertCount(4, $summaries);
         $this->assertEquals($discussion1->id, $summaries[0]->get_discussion()->get_id());
         $this->assertEquals($discussion2->id, $summaries[1]->get_discussion()->get_id());
@@ -197,7 +202,7 @@ class mod_forum_vaults_discussion_list_testcase extends advanced_testcase {
         // Sort discussions when there is a pinned discussion.
         $this->pin_discussion($discussion1);
         $summaries = array_values($vault->get_from_forum_id($forum->id, true, null,
-            $vault::SORTORDER_LASTPOST_ASC, 0, 0, $user));
+            $vault::SORTORDER_LASTPOST_ASC, 0, 0));
         $this->assertCount(4, $summaries);
         $this->assertEquals($discussion1->id, $summaries[0]->get_discussion()->get_id());
         $this->assertEquals($discussion3->id, $summaries[1]->get_discussion()->get_id());
@@ -205,7 +210,7 @@ class mod_forum_vaults_discussion_list_testcase extends advanced_testcase {
         $this->assertEquals($hiddendiscussion->id, $summaries[3]->get_discussion()->get_id());
 
         $summaries = array_values($vault->get_from_forum_id($forum->id, true, null,
-            $vault::SORTORDER_LASTPOST_DESC, 0, 0, $user));
+            $vault::SORTORDER_LASTPOST_DESC, 0, 0));
         $this->assertCount(4, $summaries);
         $this->assertEquals($discussion1->id, $summaries[0]->get_discussion()->get_id());
         $this->assertEquals($hiddendiscussion->id, $summaries[1]->get_discussion()->get_id());
@@ -213,12 +218,32 @@ class mod_forum_vaults_discussion_list_testcase extends advanced_testcase {
         $this->assertEquals($discussion3->id, $summaries[3]->get_discussion()->get_id());
 
         $summaries = array_values($vault->get_from_forum_id($forum->id, true, null,
-            $vault::SORTORDER_REPLIES_DESC, 0, 0, $user));
+            $vault::SORTORDER_REPLIES_DESC, 0, 0));
         $this->assertCount(4, $summaries);
         $this->assertEquals($discussion1->id, $summaries[0]->get_discussion()->get_id());
         $this->assertEquals($discussion2->id, $summaries[1]->get_discussion()->get_id());
         $this->assertEquals($hiddendiscussion->id, $summaries[2]->get_discussion()->get_id());
         $this->assertEquals($discussion3->id, $summaries[3]->get_discussion()->get_id());
+
+        // Sort discussions where there is a pinned discussion and several starred discussions.
+        $this->star_discussion($discussion3, 1);
+        $this->star_discussion($hiddendiscussion, 1);
+
+        $summaries = array_values($vault->get_from_forum_id($forum->id, true, $user->id,
+            $vault::SORTORDER_REPLIES_DESC, 0, 0));
+        $this->assertCount(4, $summaries);
+        $this->assertEquals($discussion1->id, $summaries[0]->get_discussion()->get_id());
+        $this->assertEquals($hiddendiscussion->id, $summaries[1]->get_discussion()->get_id());
+        $this->assertEquals($discussion3->id, $summaries[2]->get_discussion()->get_id());
+        $this->assertEquals($discussion2->id, $summaries[3]->get_discussion()->get_id());
+
+        $summaries = array_values($vault->get_from_forum_id($forum->id, true, $user->id,
+            $vault::SORTORDER_REPLIES_ASC, 0, 0));
+        $this->assertCount(4, $summaries);
+        $this->assertEquals($discussion1->id, $summaries[0]->get_discussion()->get_id());
+        $this->assertEquals($discussion3->id, $summaries[1]->get_discussion()->get_id());
+        $this->assertEquals($hiddendiscussion->id, $summaries[2]->get_discussion()->get_id());
+        $this->assertEquals($discussion2->id, $summaries[3]->get_discussion()->get_id());
     }
 
     /**
@@ -233,6 +258,7 @@ class mod_forum_vaults_discussion_list_testcase extends advanced_testcase {
         self::setUser($user);
         $course = $datagenerator->create_course();
         $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
+        $this->getDataGenerator()->enrol_user($user->id, $course->id, null, 'manual');
 
         $this->assertEquals([], $vault->get_from_forum_id($forum->id, true, true,
             null, 0, 0, $user));
@@ -258,7 +284,7 @@ class mod_forum_vaults_discussion_list_testcase extends advanced_testcase {
         );
 
         $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [1, 2, 3], true,
-            null, null, 0, 0, $user));
+            null, null, 0, 0));
         $this->assertCount(6, $summaries);
         $this->assertEquals($hiddengroupdiscussion->id, $summaries[0]->get_discussion()->get_id());
         $this->assertEquals($hiddendiscussion->id, $summaries[1]->get_discussion()->get_id());
@@ -268,7 +294,7 @@ class mod_forum_vaults_discussion_list_testcase extends advanced_testcase {
         $this->assertEquals($discussion1->id, $summaries[5]->get_discussion()->get_id());
 
         $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [1, 2, 3], false,
-            $user->id, null, 0, 0, $user));
+            $user->id, null, 0, 0));
         $this->assertCount(6, $summaries);
         $this->assertEquals($hiddengroupdiscussion->id, $summaries[0]->get_discussion()->get_id());
         $this->assertEquals($hiddendiscussion->id, $summaries[1]->get_discussion()->get_id());
@@ -278,22 +304,22 @@ class mod_forum_vaults_discussion_list_testcase extends advanced_testcase {
         $this->assertEquals($discussion1->id, $summaries[5]->get_discussion()->get_id());
 
         $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [1, 2, 3], true,
-            null, null, 1, 0, $user));
+            null, null, 1, 0));
         $this->assertCount(1, $summaries);
         $this->assertEquals($hiddengroupdiscussion->id, $summaries[0]->get_discussion()->get_id());
 
         $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [1, 2, 3], true,
-            null, null, 1, 1, $user));
+            null, null, 1, 1));
         $this->assertCount(1, $summaries);
         $this->assertEquals($hiddendiscussion->id, $summaries[0]->get_discussion()->get_id());
 
         $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [1, 2, 3], true,
-            null, null, 1, 2, $user));
+            null, null, 1, 2));
         $this->assertCount(1, $summaries);
         $this->assertEquals($groupdiscussion2->id, $summaries[0]->get_discussion()->get_id());
 
         $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [1, 2, 3], false,
-            null, null, 0, 0, $user));
+            null, null, 0, 0));
         $this->assertCount(4, $summaries);
         $this->assertEquals($groupdiscussion2->id, $summaries[0]->get_discussion()->get_id());
         $this->assertEquals($groupdiscussion1->id, $summaries[1]->get_discussion()->get_id());
@@ -301,7 +327,7 @@ class mod_forum_vaults_discussion_list_testcase extends advanced_testcase {
         $this->assertEquals($discussion1->id, $summaries[3]->get_discussion()->get_id());
 
         $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [], true,
-            null, null, 0, 0, $user));
+            null, null, 0, 0));
         $this->assertCount(3, $summaries);
         $this->assertEquals($hiddendiscussion->id, $summaries[0]->get_discussion()->get_id());
         $this->assertEquals($discussion2->id, $summaries[1]->get_discussion()->get_id());
@@ -330,7 +356,7 @@ class mod_forum_vaults_discussion_list_testcase extends advanced_testcase {
 
         // Sort discussions by last post DESC.
         $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [1, 2, 3], true,
-            $user->id, $vault::SORTORDER_LASTPOST_DESC, 0, 0, $user));
+            $user->id, $vault::SORTORDER_LASTPOST_DESC, 0, 0));
         $this->assertCount(6, $summaries);
         $this->assertEquals($groupdiscussion2->id, $summaries[0]->get_discussion()->get_id());
         $this->assertEquals($groupdiscussion1->id, $summaries[1]->get_discussion()->get_id());
@@ -341,7 +367,7 @@ class mod_forum_vaults_discussion_list_testcase extends advanced_testcase {
 
         // Sort discussions by last post ASC.
         $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [1, 2, 3], true,
-            $user->id, $vault::SORTORDER_LASTPOST_ASC, 0, 0, $user));
+            $user->id, $vault::SORTORDER_LASTPOST_ASC, 0, 0));
         $this->assertCount(6, $summaries);
         $this->assertEquals($discussion1->id, $summaries[0]->get_discussion()->get_id());
         $this->assertEquals($discussion2->id, $summaries[1]->get_discussion()->get_id());
@@ -352,7 +378,7 @@ class mod_forum_vaults_discussion_list_testcase extends advanced_testcase {
 
         // Sort discussions by replies DESC.
         $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [1, 2, 3], true,
-            $user->id, $vault::SORTORDER_REPLIES_DESC, 0, 0, $user));
+            $user->id, $vault::SORTORDER_REPLIES_DESC, 0, 0));
         $this->assertCount(6, $summaries);
         $this->assertEquals($discussion2->id, $summaries[0]->get_discussion()->get_id());
         $this->assertEquals($discussion1->id, $summaries[1]->get_discussion()->get_id());
@@ -363,7 +389,7 @@ class mod_forum_vaults_discussion_list_testcase extends advanced_testcase {
 
         // Sort discussions by replies ASC.
         $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [1, 2, 3], true,
-            $user->id, $vault::SORTORDER_REPLIES_ASC, 0, 0, $user));
+            $user->id, $vault::SORTORDER_REPLIES_ASC, 0, 0));
         $this->assertCount(6, $summaries);
         $this->assertEquals($hiddengroupdiscussion->id, $summaries[0]->get_discussion()->get_id());
         $this->assertEquals($groupdiscussion2->id, $summaries[1]->get_discussion()->get_id());
@@ -374,7 +400,7 @@ class mod_forum_vaults_discussion_list_testcase extends advanced_testcase {
 
         // Sort discussions by discussion created DESC.
         $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [1, 2, 3], true,
-            $user->id, $vault::SORTORDER_CREATED_DESC, 0, 0, $user));
+            $user->id, $vault::SORTORDER_CREATED_DESC, 0, 0));
         $this->assertCount(6, $summaries);
         $this->assertEquals($hiddengroupdiscussion->id, $summaries[0]->get_discussion()->get_id());
         $this->assertEquals($groupdiscussion2->id, $summaries[1]->get_discussion()->get_id());
@@ -385,7 +411,7 @@ class mod_forum_vaults_discussion_list_testcase extends advanced_testcase {
 
         // Sort discussions by discussion created ASC.
         $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [1, 2, 3], true,
-            $user->id, $vault::SORTORDER_CREATED_ASC, 0, 0, $user));
+            $user->id, $vault::SORTORDER_CREATED_ASC, 0, 0));
         $this->assertCount(6, $summaries);
         $this->assertEquals($discussion1->id, $summaries[0]->get_discussion()->get_id());
         $this->assertEquals($discussion2->id, $summaries[1]->get_discussion()->get_id());
@@ -399,7 +425,7 @@ class mod_forum_vaults_discussion_list_testcase extends advanced_testcase {
         $this->pin_discussion($hiddendiscussion);
 
         $summaries = array_values($vault->get_from_forum_id($forum->id, true, null,
-            $vault::SORTORDER_LASTPOST_DESC, 0, 0, $user));
+            $vault::SORTORDER_LASTPOST_DESC, 0, 0));
         $this->assertCount(6, $summaries);
         $this->assertEquals($hiddendiscussion->id, $summaries[0]->get_discussion()->get_id());
         $this->assertEquals($discussion1->id, $summaries[1]->get_discussion()->get_id());
@@ -409,7 +435,7 @@ class mod_forum_vaults_discussion_list_testcase extends advanced_testcase {
         $this->assertEquals($discussion2->id, $summaries[5]->get_discussion()->get_id());
 
         $summaries = array_values($vault->get_from_forum_id($forum->id, true, null,
-            $vault::SORTORDER_LASTPOST_ASC, 0, 0, $user));
+            $vault::SORTORDER_LASTPOST_ASC, 0, 0));
         $this->assertCount(6, $summaries);
         $this->assertEquals($discussion1->id, $summaries[0]->get_discussion()->get_id());
         $this->assertEquals($hiddendiscussion->id, $summaries[1]->get_discussion()->get_id());
@@ -419,7 +445,7 @@ class mod_forum_vaults_discussion_list_testcase extends advanced_testcase {
         $this->assertEquals($groupdiscussion2->id, $summaries[5]->get_discussion()->get_id());
 
         $summaries = array_values($vault->get_from_forum_id($forum->id, true, null,
-            $vault::SORTORDER_REPLIES_DESC, 0, 0, $user));
+            $vault::SORTORDER_REPLIES_DESC, 0, 0));
         $this->assertCount(6, $summaries);
         $this->assertEquals($discussion1->id, $summaries[0]->get_discussion()->get_id());
         $this->assertEquals($hiddendiscussion->id, $summaries[1]->get_discussion()->get_id());
@@ -470,8 +496,8 @@ class mod_forum_vaults_discussion_list_testcase extends advanced_testcase {
         $course = $datagenerator->create_course();
         $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
 
-        $this->assertEquals([], $vault->get_from_forum_id($forum->id, true, true,
-            null, 0, 0, $user));
+        $this->assertEquals([], $vault->get_from_forum_id($forum->id, true, null,
+            null, 0, 0));
 
         $now = time();
         [$discussion1, $post1] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now - 10, 'timemodified' => 1]);
@@ -518,4 +544,14 @@ class mod_forum_vaults_discussion_list_testcase extends advanced_testcase {
         $DB->update_record('forum_discussions',
             (object) array('id' => $discussion->id, 'pinned' => FORUM_DISCUSSION_PINNED));
     }
+
+    /**
+     * Star a duscussion.
+     *
+     * @param stdClass $discussion
+     * @param bool     $targetstate The new starred state of the discussion (0 => unstar, 1 => star)
+     */
+    private function star_discussion(stdClass $discussion, bool $targetstate) {
+        mod_forum_external::toggle_favourite_state($discussion->id, $targetstate);
+    }
 }