MDL-65071 forum: Add unit tests
[moodle.git] / mod / forum / tests / vaults_discussion_list_test.php
index 510ff19..58c09d9 100644 (file)
@@ -82,41 +82,143 @@ class mod_forum_vaults_discussion_list_testcase extends advanced_testcase {
         $course = $datagenerator->create_course();
         $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
 
         $course = $datagenerator->create_course();
         $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
 
-        $this->assertEquals([], $vault->get_from_forum_id($forum->id, true, $user->id, null, 0, 0, $user));
+        $this->assertEquals([], $vault->get_from_forum_id($forum->id, true, $user->id, null,
+            0, 0, $user));
 
         $now = time();
         [$discussion1, $post1] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now - 10, 'timemodified' => 1]);
         [$discussion2, $post2] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now - 9, 'timemodified' => 2]);
         [$hiddendiscussion, $post3] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now + 10, 'timemodified' => 3]);
 
         $now = time();
         [$discussion1, $post1] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now - 10, 'timemodified' => 1]);
         [$discussion2, $post2] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now - 9, 'timemodified' => 2]);
         [$hiddendiscussion, $post3] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now + 10, 'timemodified' => 3]);
+        [$discussion3, $post4] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now - 8, 'timemodified' => 4]);
 
 
-        $summaries = array_values($vault->get_from_forum_id($forum->id, false, null, null, 0, 0, $user));
-        $this->assertCount(2, $summaries);
-        $this->assertEquals($discussion2->id, $summaries[0]->get_discussion()->get_id());
-        $this->assertEquals($discussion1->id, $summaries[1]->get_discussion()->get_id());
-
-        $summaries = array_values($vault->get_from_forum_id($forum->id, true, null, null, 0, 0, $user));
+        // By default orders the discussions by last post.
+        $summaries = array_values($vault->get_from_forum_id($forum->id, false, null, null,
+            0, 0, $user));
         $this->assertCount(3, $summaries);
         $this->assertCount(3, $summaries);
-        $this->assertEquals($hiddendiscussion->id, $summaries[0]->get_discussion()->get_id());
+        $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());
 
         $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, false, $user->id, null, 0, 0, $user));
-        $this->assertCount(3, $summaries);
+        $summaries = array_values($vault->get_from_forum_id($forum->id, true, null, null,
+            0, 0, $user));
+        $this->assertCount(4, $summaries);
         $this->assertEquals($hiddendiscussion->id, $summaries[0]->get_discussion()->get_id());
         $this->assertEquals($hiddendiscussion->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());
+        $this->assertEquals($discussion3->id, $summaries[1]->get_discussion()->get_id());
+        $this->assertEquals($discussion2->id, $summaries[2]->get_discussion()->get_id());
+        $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));
+        $summaries = array_values($vault->get_from_forum_id($forum->id, false, $user->id, null,
+            0, 0, $user));
+        $this->assertCount(4, $summaries);
+        $this->assertEquals($hiddendiscussion->id, $summaries[0]->get_discussion()->get_id());
+        $this->assertEquals($discussion3->id, $summaries[1]->get_discussion()->get_id());
+        $this->assertEquals($discussion2->id, $summaries[2]->get_discussion()->get_id());
+        $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));
         $this->assertCount(1, $summaries);
         $this->assertEquals($hiddendiscussion->id, $summaries[0]->get_discussion()->get_id());
 
         $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));
+        $summaries = array_values($vault->get_from_forum_id($forum->id, true, null, null,
+            1, 1, $user));
         $this->assertCount(1, $summaries);
         $this->assertCount(1, $summaries);
-        $this->assertEquals($discussion2->id, $summaries[0]->get_discussion()->get_id());
+        $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));
+        $summaries = array_values($vault->get_from_forum_id($forum->id, true, null, null,
+            1, 2, $user));
         $this->assertCount(1, $summaries);
         $this->assertCount(1, $summaries);
+        $this->assertEquals($discussion2->id, $summaries[0]->get_discussion()->get_id());
+
+        // Create 2 replies for $post1.
+        $this->helper_reply_to_post($post1, $user);
+        $this->helper_reply_to_post($post1, $user);
+        // Create 3 replies for $post2.
+        $this->helper_reply_to_post($post2, $user);
+        $this->helper_reply_to_post($post2, $user);
+        $this->helper_reply_to_post($post2, $user);
+        // Create 1 reply for $post3.
+        $this->helper_reply_to_post($post3, $user);
+
+        // 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));
+        $this->assertCount(4, $summaries);
+        $this->assertEquals($hiddendiscussion->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());
+        $this->assertEquals($discussion3->id, $summaries[3]->get_discussion()->get_id());
+
+        // 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));
+        $this->assertCount(4, $summaries);
+        $this->assertEquals($discussion3->id, $summaries[0]->get_discussion()->get_id());
+        $this->assertEquals($discussion1->id, $summaries[1]->get_discussion()->get_id());
+        $this->assertEquals($discussion2->id, $summaries[2]->get_discussion()->get_id());
+        $this->assertEquals($hiddendiscussion->id, $summaries[3]->get_discussion()->get_id());
+
+        // Sort discussions by replies DESC.
+        $summaries = array_values($vault->get_from_forum_id($forum->id, true, null,
+            $vault::SORTORDER_REPLIES_DESC, 0, 0, $user));
+        $this->assertCount(4, $summaries);
+        $this->assertEquals($discussion2->id, $summaries[0]->get_discussion()->get_id());
+        $this->assertEquals($discussion1->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 by replies ASC.
+        $summaries = array_values($vault->get_from_forum_id($forum->id, true, null,
+            $vault::SORTORDER_REPLIES_ASC, 0, 0, $user));
+        $this->assertCount(4, $summaries);
+        $this->assertEquals($discussion3->id, $summaries[0]->get_discussion()->get_id());
+        $this->assertEquals($hiddendiscussion->id, $summaries[1]->get_discussion()->get_id());
+        $this->assertEquals($discussion1->id, $summaries[2]->get_discussion()->get_id());
+        $this->assertEquals($discussion2->id, $summaries[3]->get_discussion()->get_id());
+
+        // 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));
+        $this->assertCount(4, $summaries);
+        $this->assertEquals($discussion3->id, $summaries[0]->get_discussion()->get_id());
+        $this->assertEquals($hiddendiscussion->id, $summaries[1]->get_discussion()->get_id());
+        $this->assertEquals($discussion2->id, $summaries[2]->get_discussion()->get_id());
+        $this->assertEquals($discussion1->id, $summaries[3]->get_discussion()->get_id());
+
+        // 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));
+        $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 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));
+        $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($discussion2->id, $summaries[2]->get_discussion()->get_id());
+        $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));
+        $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($discussion2->id, $summaries[2]->get_discussion()->get_id());
+        $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));
+        $this->assertCount(4, $summaries);
         $this->assertEquals($discussion1->id, $summaries[0]->get_discussion()->get_id());
         $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());
     }
 
     /**
     }
 
     /**
@@ -132,7 +234,8 @@ class mod_forum_vaults_discussion_list_testcase extends advanced_testcase {
         $course = $datagenerator->create_course();
         $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
 
         $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, true,
+            null, 0, 0, $user));
 
         $now = time();
         [$discussion1, $post1] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now - 10, 'timemodified' => 1]);
 
         $now = time();
         [$discussion1, $post1] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now - 10, 'timemodified' => 1]);
@@ -154,7 +257,8 @@ class mod_forum_vaults_discussion_list_testcase extends advanced_testcase {
             ['timestart' => $now + 11, 'timemodified' => 6, 'groupid' => 3]
         );
 
             ['timestart' => $now + 11, 'timemodified' => 6, 'groupid' => 3]
         );
 
-        $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [1, 2, 3], true, null, null, 0, 0, $user));
+        $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [1, 2, 3], true,
+            null, null, 0, 0, $user));
         $this->assertCount(6, $summaries);
         $this->assertEquals($hiddengroupdiscussion->id, $summaries[0]->get_discussion()->get_id());
         $this->assertEquals($hiddendiscussion->id, $summaries[1]->get_discussion()->get_id());
         $this->assertCount(6, $summaries);
         $this->assertEquals($hiddengroupdiscussion->id, $summaries[0]->get_discussion()->get_id());
         $this->assertEquals($hiddendiscussion->id, $summaries[1]->get_discussion()->get_id());
@@ -163,7 +267,8 @@ class mod_forum_vaults_discussion_list_testcase extends advanced_testcase {
         $this->assertEquals($discussion2->id, $summaries[4]->get_discussion()->get_id());
         $this->assertEquals($discussion1->id, $summaries[5]->get_discussion()->get_id());
 
         $this->assertEquals($discussion2->id, $summaries[4]->get_discussion()->get_id());
         $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));
+        $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [1, 2, 3], false,
+            $user->id, null, 0, 0, $user));
         $this->assertCount(6, $summaries);
         $this->assertEquals($hiddengroupdiscussion->id, $summaries[0]->get_discussion()->get_id());
         $this->assertEquals($hiddendiscussion->id, $summaries[1]->get_discussion()->get_id());
         $this->assertCount(6, $summaries);
         $this->assertEquals($hiddengroupdiscussion->id, $summaries[0]->get_discussion()->get_id());
         $this->assertEquals($hiddendiscussion->id, $summaries[1]->get_discussion()->get_id());
@@ -172,32 +277,157 @@ class mod_forum_vaults_discussion_list_testcase extends advanced_testcase {
         $this->assertEquals($discussion2->id, $summaries[4]->get_discussion()->get_id());
         $this->assertEquals($discussion1->id, $summaries[5]->get_discussion()->get_id());
 
         $this->assertEquals($discussion2->id, $summaries[4]->get_discussion()->get_id());
         $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));
+        $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [1, 2, 3], true,
+            null, null, 1, 0, $user));
         $this->assertCount(1, $summaries);
         $this->assertEquals($hiddengroupdiscussion->id, $summaries[0]->get_discussion()->get_id());
 
         $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));
+        $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [1, 2, 3], true,
+            null, null, 1, 1, $user));
         $this->assertCount(1, $summaries);
         $this->assertEquals($hiddendiscussion->id, $summaries[0]->get_discussion()->get_id());
 
         $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));
+        $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [1, 2, 3], true,
+            null, null, 1, 2, $user));
         $this->assertCount(1, $summaries);
         $this->assertEquals($groupdiscussion2->id, $summaries[0]->get_discussion()->get_id());
 
         $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));
+        $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [1, 2, 3], false,
+            null, null, 0, 0, $user));
         $this->assertCount(4, $summaries);
         $this->assertEquals($groupdiscussion2->id, $summaries[0]->get_discussion()->get_id());
         $this->assertEquals($groupdiscussion1->id, $summaries[1]->get_discussion()->get_id());
         $this->assertEquals($discussion2->id, $summaries[2]->get_discussion()->get_id());
         $this->assertEquals($discussion1->id, $summaries[3]->get_discussion()->get_id());
 
         $this->assertCount(4, $summaries);
         $this->assertEquals($groupdiscussion2->id, $summaries[0]->get_discussion()->get_id());
         $this->assertEquals($groupdiscussion1->id, $summaries[1]->get_discussion()->get_id());
         $this->assertEquals($discussion2->id, $summaries[2]->get_discussion()->get_id());
         $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));
+        $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [], true,
+            null, null, 0, 0, $user));
         $this->assertCount(3, $summaries);
         $this->assertEquals($hiddendiscussion->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());
         $this->assertCount(3, $summaries);
         $this->assertEquals($hiddendiscussion->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());
-    }
 
 
+        // Add 4 replies to $post1.
+        $this->helper_reply_to_post($post1, $user);
+        $this->helper_reply_to_post($post1, $user);
+        $this->helper_reply_to_post($post1, $user);
+        $this->helper_reply_to_post($post1, $user);
+        // Add 5 replies to $post2.
+        $this->helper_reply_to_post($post2, $user);
+        $this->helper_reply_to_post($post2, $user);
+        $this->helper_reply_to_post($post2, $user);
+        $this->helper_reply_to_post($post2, $user);
+        $this->helper_reply_to_post($post2, $user);
+        // Add 3 replies to $post3.
+        $this->helper_reply_to_post($post3, $user);
+        $this->helper_reply_to_post($post3, $user);
+        $this->helper_reply_to_post($post3, $user);
+        // Add 2 replies to $post4.
+        $this->helper_reply_to_post($post4, $user);
+        $this->helper_reply_to_post($post4, $user);
+        // Add 1 reply to $post5.
+        $this->helper_reply_to_post($post5, $user);
+
+        // 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));
+        $this->assertCount(6, $summaries);
+        $this->assertEquals($groupdiscussion2->id, $summaries[0]->get_discussion()->get_id());
+        $this->assertEquals($groupdiscussion1->id, $summaries[1]->get_discussion()->get_id());
+        $this->assertEquals($hiddendiscussion->id, $summaries[2]->get_discussion()->get_id());
+        $this->assertEquals($hiddengroupdiscussion->id, $summaries[3]->get_discussion()->get_id());
+        $this->assertEquals($discussion2->id, $summaries[4]->get_discussion()->get_id());
+        $this->assertEquals($discussion1->id, $summaries[5]->get_discussion()->get_id());
+
+        // 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));
+        $this->assertCount(6, $summaries);
+        $this->assertEquals($discussion1->id, $summaries[0]->get_discussion()->get_id());
+        $this->assertEquals($discussion2->id, $summaries[1]->get_discussion()->get_id());
+        $this->assertEquals($hiddengroupdiscussion->id, $summaries[2]->get_discussion()->get_id());
+        $this->assertEquals($hiddendiscussion->id, $summaries[3]->get_discussion()->get_id());
+        $this->assertEquals($groupdiscussion1->id, $summaries[4]->get_discussion()->get_id());
+        $this->assertEquals($groupdiscussion2->id, $summaries[5]->get_discussion()->get_id());
+
+        // 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));
+        $this->assertCount(6, $summaries);
+        $this->assertEquals($discussion2->id, $summaries[0]->get_discussion()->get_id());
+        $this->assertEquals($discussion1->id, $summaries[1]->get_discussion()->get_id());
+        $this->assertEquals($hiddendiscussion->id, $summaries[2]->get_discussion()->get_id());
+        $this->assertEquals($groupdiscussion1->id, $summaries[3]->get_discussion()->get_id());
+        $this->assertEquals($groupdiscussion2->id, $summaries[4]->get_discussion()->get_id());
+        $this->assertEquals($hiddengroupdiscussion->id, $summaries[5]->get_discussion()->get_id());
+
+        // 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));
+        $this->assertCount(6, $summaries);
+        $this->assertEquals($hiddengroupdiscussion->id, $summaries[0]->get_discussion()->get_id());
+        $this->assertEquals($groupdiscussion2->id, $summaries[1]->get_discussion()->get_id());
+        $this->assertEquals($groupdiscussion1->id, $summaries[2]->get_discussion()->get_id());
+        $this->assertEquals($hiddendiscussion->id, $summaries[3]->get_discussion()->get_id());
+        $this->assertEquals($discussion1->id, $summaries[4]->get_discussion()->get_id());
+        $this->assertEquals($discussion2->id, $summaries[5]->get_discussion()->get_id());
+
+        // 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));
+        $this->assertCount(6, $summaries);
+        $this->assertEquals($hiddengroupdiscussion->id, $summaries[0]->get_discussion()->get_id());
+        $this->assertEquals($groupdiscussion2->id, $summaries[1]->get_discussion()->get_id());
+        $this->assertEquals($groupdiscussion1->id, $summaries[2]->get_discussion()->get_id());
+        $this->assertEquals($hiddendiscussion->id, $summaries[3]->get_discussion()->get_id());
+        $this->assertEquals($discussion2->id, $summaries[4]->get_discussion()->get_id());
+        $this->assertEquals($discussion1->id, $summaries[5]->get_discussion()->get_id());
+
+        // 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));
+        $this->assertCount(6, $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($groupdiscussion1->id, $summaries[3]->get_discussion()->get_id());
+        $this->assertEquals($groupdiscussion2->id, $summaries[4]->get_discussion()->get_id());
+        $this->assertEquals($hiddengroupdiscussion->id, $summaries[5]->get_discussion()->get_id());
+
+        // Sort discussions when there is a pinned discussion.
+        $this->pin_discussion($discussion1);
+        $this->pin_discussion($hiddendiscussion);
+
+        $summaries = array_values($vault->get_from_forum_id($forum->id, true, null,
+            $vault::SORTORDER_LASTPOST_DESC, 0, 0, $user));
+        $this->assertCount(6, $summaries);
+        $this->assertEquals($hiddendiscussion->id, $summaries[0]->get_discussion()->get_id());
+        $this->assertEquals($discussion1->id, $summaries[1]->get_discussion()->get_id());
+        $this->assertEquals($groupdiscussion2->id, $summaries[2]->get_discussion()->get_id());
+        $this->assertEquals($groupdiscussion1->id, $summaries[3]->get_discussion()->get_id());
+        $this->assertEquals($hiddengroupdiscussion->id, $summaries[4]->get_discussion()->get_id());
+        $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));
+        $this->assertCount(6, $summaries);
+        $this->assertEquals($discussion1->id, $summaries[0]->get_discussion()->get_id());
+        $this->assertEquals($hiddendiscussion->id, $summaries[1]->get_discussion()->get_id());
+        $this->assertEquals($discussion2->id, $summaries[2]->get_discussion()->get_id());
+        $this->assertEquals($hiddengroupdiscussion->id, $summaries[3]->get_discussion()->get_id());
+        $this->assertEquals($groupdiscussion1->id, $summaries[4]->get_discussion()->get_id());
+        $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));
+        $this->assertCount(6, $summaries);
+        $this->assertEquals($discussion1->id, $summaries[0]->get_discussion()->get_id());
+        $this->assertEquals($hiddendiscussion->id, $summaries[1]->get_discussion()->get_id());
+        $this->assertEquals($discussion2->id, $summaries[2]->get_discussion()->get_id());
+        $this->assertEquals($groupdiscussion1->id, $summaries[3]->get_discussion()->get_id());
+        $this->assertEquals($groupdiscussion2->id, $summaries[4]->get_discussion()->get_id());
+        $this->assertEquals($hiddengroupdiscussion->id, $summaries[5]->get_discussion()->get_id());
+    }
 
     /**
      * Test get_total_discussion_count_from_forum_id.
 
     /**
      * Test get_total_discussion_count_from_forum_id.
@@ -211,19 +441,22 @@ class mod_forum_vaults_discussion_list_testcase extends advanced_testcase {
         $course = $datagenerator->create_course();
         $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
 
         $course = $datagenerator->create_course();
         $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
 
-        $this->assertEquals(0, $vault->get_total_discussion_count_from_forum_id($forum->id, true, null));
+        $this->assertEquals(0, $vault->get_total_discussion_count_from_forum_id($forum->id, true,
+            null));
 
         $now = time();
         [$discussion1, $post1] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now - 10, 'timemodified' => 1]);
         [$discussion2, $post2] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now - 9, 'timemodified' => 2]);
         [$hiddendiscussion, $post3] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now + 10, 'timemodified' => 3]);
 
 
         $now = time();
         [$discussion1, $post1] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now - 10, 'timemodified' => 1]);
         [$discussion2, $post2] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now - 9, 'timemodified' => 2]);
         [$hiddendiscussion, $post3] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now + 10, 'timemodified' => 3]);
 
-        $this->assertEquals(2, $vault->get_total_discussion_count_from_forum_id($forum->id, false, null));
-        $this->assertEquals(3, $vault->get_total_discussion_count_from_forum_id($forum->id, true, null));
-        $this->assertEquals(3, $vault->get_total_discussion_count_from_forum_id($forum->id, false, $user->id));
+        $this->assertEquals(2, $vault->get_total_discussion_count_from_forum_id($forum->id, false,
+            null));
+        $this->assertEquals(3, $vault->get_total_discussion_count_from_forum_id($forum->id, true,
+            null));
+        $this->assertEquals(3, $vault->get_total_discussion_count_from_forum_id($forum->id, false,
+            $user->id));
     }
 
     }
 
-
     /**
      * Test get_total_discussion_count_from_forum_id_and_group_id.
      */
     /**
      * Test get_total_discussion_count_from_forum_id_and_group_id.
      */
@@ -237,7 +470,8 @@ class mod_forum_vaults_discussion_list_testcase extends advanced_testcase {
         $course = $datagenerator->create_course();
         $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
 
         $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, true,
+            null, 0, 0, $user));
 
         $now = time();
         [$discussion1, $post1] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now - 10, 'timemodified' => 1]);
 
         $now = time();
         [$discussion1, $post1] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now - 10, 'timemodified' => 1]);
@@ -259,14 +493,29 @@ class mod_forum_vaults_discussion_list_testcase extends advanced_testcase {
             ['timestart' => $now + 11, 'timemodified' => 6, 'groupid' => 3]
         );
 
             ['timestart' => $now + 11, 'timemodified' => 6, 'groupid' => 3]
         );
 
-        $this->assertEquals(6, $vault->get_total_discussion_count_from_forum_id_and_group_id($forum->id, [1, 2, 3], true, null));
+        $this->assertEquals(6, $vault->get_total_discussion_count_from_forum_id_and_group_id($forum->id, [1, 2, 3],
+            true, null));
         $this->assertEquals(6, $vault->get_total_discussion_count_from_forum_id_and_group_id(
             $forum->id,
             [1, 2, 3],
             false,
             $user->id
         ));
         $this->assertEquals(6, $vault->get_total_discussion_count_from_forum_id_and_group_id(
             $forum->id,
             [1, 2, 3],
             false,
             $user->id
         ));
-        $this->assertEquals(4, $vault->get_total_discussion_count_from_forum_id_and_group_id($forum->id, [1, 2, 3], false, null));
-        $this->assertEquals(3, $vault->get_total_discussion_count_from_forum_id_and_group_id($forum->id, [], true, null));
+        $this->assertEquals(4, $vault->get_total_discussion_count_from_forum_id_and_group_id($forum->id, [1, 2, 3],
+            false, null));
+        $this->assertEquals(3, $vault->get_total_discussion_count_from_forum_id_and_group_id($forum->id, [],
+            true, null));
+    }
+
+    /**
+     * Pin a duscussion.
+     *
+     * @param stdClass $discussion
+     */
+    private function pin_discussion(stdClass $discussion) {
+        global $DB;
+
+        $DB->update_record('forum_discussions',
+            (object) array('id' => $discussion->id, 'pinned' => FORUM_DISCUSSION_PINNED));
     }
 }
     }
 }