Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 39 and 401]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  namespace mod_forum;
  18  
  19  use mod_forum_external;
  20  use mod_forum_tests_generator_trait;
  21  
  22  defined('MOODLE_INTERNAL') || die();
  23  
  24  global $CFG;
  25  
  26  require_once($CFG->dirroot . '/mod/forum/externallib.php');
  27  require_once (__DIR__ . '/generator_trait.php');
  28  
  29  /**
  30   * The discussion_list vault tests.
  31   *
  32   * @package    mod_forum
  33   * @copyright  2019 Ryan Wyllie <ryan@moodle.com>
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class vaults_discussion_list_test extends \advanced_testcase {
  37      // Make use of the test generator trait.
  38      use mod_forum_tests_generator_trait;
  39  
  40      /** @var \mod_forum\local\vaults\discussion_list */
  41      private $vault;
  42  
  43      /**
  44       * Set up function for tests.
  45       */
  46      public function setUp(): void {
  47          $vaultfactory = \mod_forum\local\container::get_vault_factory();
  48          $this->vault = $vaultfactory->get_discussions_in_forum_vault();
  49      }
  50  
  51      /**
  52       * Test get_from_id.
  53       */
  54      public function test_get_from_id() {
  55          $this->resetAfterTest();
  56  
  57          $datagenerator = $this->getDataGenerator();
  58          $vault = $this->vault;
  59          $user = $datagenerator->create_user();
  60          $course = $datagenerator->create_course();
  61          $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
  62          [$discussion, $post] = $this->helper_post_to_forum($forum, $user);
  63  
  64          $discussionlist = $vault->get_from_id($discussion->id);
  65  
  66          $this->assertEquals($discussion->id, $discussionlist->get_discussion()->get_id());
  67          $this->assertEquals($post->id, $discussionlist->get_first_post()->get_id());
  68          $this->assertEquals($user->id, $discussionlist->get_first_post_author()->get_id());
  69          $this->assertEquals($user->id, $discussionlist->get_latest_post_author()->get_id());
  70      }
  71  
  72      /**
  73       * Test get_from_forum_id.
  74       */
  75      public function test_get_from_forum_id() {
  76          $this->resetAfterTest();
  77  
  78          $datagenerator = $this->getDataGenerator();
  79          $vault = $this->vault;
  80          $user = $datagenerator->create_user();
  81          self::setUser($user);
  82          $course = $datagenerator->create_course();
  83          $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
  84  
  85          $this->getDataGenerator()->enrol_user($user->id, $course->id, null, 'manual');
  86  
  87          $this->assertEquals([], $vault->get_from_forum_id($forum->id, true, $user->id, null,
  88              0, 0));
  89  
  90          $now = time();
  91          [$discussion1, $post1] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now - 10, 'timemodified' => 1]);
  92          [$discussion2, $post2] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now - 9, 'timemodified' => 2]);
  93          [$hiddendiscussion, $post3] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now + 10, 'timemodified' => 3]);
  94          [$discussion3, $post4] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now - 8, 'timemodified' => 4]);
  95  
  96          // By default orders the discussions by last post.
  97          $summaries = array_values($vault->get_from_forum_id($forum->id, false, null, null,
  98              0, 0));
  99          $this->assertCount(3, $summaries);
 100          $this->assertEquals($discussion3->id, $summaries[0]->get_discussion()->get_id());
 101          $this->assertEquals($discussion2->id, $summaries[1]->get_discussion()->get_id());
 102          $this->assertEquals($discussion1->id, $summaries[2]->get_discussion()->get_id());
 103  
 104          $summaries = array_values($vault->get_from_forum_id($forum->id, true, null, null,
 105              0, 0));
 106          $this->assertCount(4, $summaries);
 107          $this->assertEquals($hiddendiscussion->id, $summaries[0]->get_discussion()->get_id());
 108          $this->assertEquals($discussion3->id, $summaries[1]->get_discussion()->get_id());
 109          $this->assertEquals($discussion2->id, $summaries[2]->get_discussion()->get_id());
 110          $this->assertEquals($discussion1->id, $summaries[3]->get_discussion()->get_id());
 111  
 112          $summaries = array_values($vault->get_from_forum_id($forum->id, false, $user->id, null,
 113              0, 0));
 114          $this->assertCount(4, $summaries);
 115          $this->assertEquals($hiddendiscussion->id, $summaries[0]->get_discussion()->get_id());
 116          $this->assertEquals($discussion3->id, $summaries[1]->get_discussion()->get_id());
 117          $this->assertEquals($discussion2->id, $summaries[2]->get_discussion()->get_id());
 118          $this->assertEquals($discussion1->id, $summaries[3]->get_discussion()->get_id());
 119  
 120          $summaries = array_values($vault->get_from_forum_id($forum->id, true, null, null,
 121              1, 0));
 122          $this->assertCount(1, $summaries);
 123          $this->assertEquals($hiddendiscussion->id, $summaries[0]->get_discussion()->get_id());
 124  
 125          $summaries = array_values($vault->get_from_forum_id($forum->id, true, null, null,
 126              1, 1));
 127          $this->assertCount(1, $summaries);
 128          $this->assertEquals($discussion3->id, $summaries[0]->get_discussion()->get_id());
 129  
 130          $summaries = array_values($vault->get_from_forum_id($forum->id, true, null, null,
 131              1, 2));
 132          $this->assertCount(1, $summaries);
 133          $this->assertEquals($discussion2->id, $summaries[0]->get_discussion()->get_id());
 134  
 135          // Create 2 replies for $post1.
 136          $this->helper_reply_to_post($post1, $user);
 137          $this->helper_reply_to_post($post1, $user);
 138          // Create 3 replies for $post2.
 139          $this->helper_reply_to_post($post2, $user);
 140          $this->helper_reply_to_post($post2, $user);
 141          $this->helper_reply_to_post($post2, $user);
 142          // Create 1 reply for $post3.
 143          $this->helper_reply_to_post($post3, $user);
 144  
 145          // Sort discussions by last post DESC.
 146          $summaries = array_values($vault->get_from_forum_id($forum->id, true, null,
 147              $vault::SORTORDER_LASTPOST_DESC, 0, 0));
 148          $this->assertCount(4, $summaries);
 149          $this->assertEquals($hiddendiscussion->id, $summaries[0]->get_discussion()->get_id());
 150          $this->assertEquals($discussion2->id, $summaries[1]->get_discussion()->get_id());
 151          $this->assertEquals($discussion1->id, $summaries[2]->get_discussion()->get_id());
 152          $this->assertEquals($discussion3->id, $summaries[3]->get_discussion()->get_id());
 153  
 154          // Sort discussions by last post ASC.
 155          $summaries = array_values($vault->get_from_forum_id($forum->id, true, null,
 156              $vault::SORTORDER_LASTPOST_ASC, 0, 0));
 157          $this->assertCount(4, $summaries);
 158          $this->assertEquals($discussion3->id, $summaries[0]->get_discussion()->get_id());
 159          $this->assertEquals($discussion1->id, $summaries[1]->get_discussion()->get_id());
 160          $this->assertEquals($discussion2->id, $summaries[2]->get_discussion()->get_id());
 161          $this->assertEquals($hiddendiscussion->id, $summaries[3]->get_discussion()->get_id());
 162  
 163          // Sort discussions by replies DESC.
 164          $summaries = array_values($vault->get_from_forum_id($forum->id, true, null,
 165              $vault::SORTORDER_REPLIES_DESC, 0, 0));
 166          $this->assertCount(4, $summaries);
 167          $this->assertEquals($discussion2->id, $summaries[0]->get_discussion()->get_id());
 168          $this->assertEquals($discussion1->id, $summaries[1]->get_discussion()->get_id());
 169          $this->assertEquals($hiddendiscussion->id, $summaries[2]->get_discussion()->get_id());
 170          $this->assertEquals($discussion3->id, $summaries[3]->get_discussion()->get_id());
 171  
 172          // Sort discussions by replies ASC.
 173          $summaries = array_values($vault->get_from_forum_id($forum->id, true, null,
 174              $vault::SORTORDER_REPLIES_ASC, 0, 0));
 175          $this->assertCount(4, $summaries);
 176          $this->assertEquals($discussion3->id, $summaries[0]->get_discussion()->get_id());
 177          $this->assertEquals($hiddendiscussion->id, $summaries[1]->get_discussion()->get_id());
 178          $this->assertEquals($discussion1->id, $summaries[2]->get_discussion()->get_id());
 179          $this->assertEquals($discussion2->id, $summaries[3]->get_discussion()->get_id());
 180  
 181          // Sort discussions by discussion created DESC.
 182          $summaries = array_values($vault->get_from_forum_id($forum->id, true, null,
 183              $vault::SORTORDER_CREATED_DESC, 0, 0));
 184          $this->assertCount(4, $summaries);
 185          $this->assertEquals($discussion3->id, $summaries[0]->get_discussion()->get_id());
 186          $this->assertEquals($hiddendiscussion->id, $summaries[1]->get_discussion()->get_id());
 187          $this->assertEquals($discussion2->id, $summaries[2]->get_discussion()->get_id());
 188          $this->assertEquals($discussion1->id, $summaries[3]->get_discussion()->get_id());
 189  
 190          // Sort discussions by discussion created ASC.
 191          $summaries = array_values($vault->get_from_forum_id($forum->id, true, null,
 192              $vault::SORTORDER_CREATED_ASC, 0, 0));
 193          $this->assertCount(4, $summaries);
 194          $this->assertEquals($discussion1->id, $summaries[0]->get_discussion()->get_id());
 195          $this->assertEquals($discussion2->id, $summaries[1]->get_discussion()->get_id());
 196          $this->assertEquals($hiddendiscussion->id, $summaries[2]->get_discussion()->get_id());
 197          $this->assertEquals($discussion3->id, $summaries[3]->get_discussion()->get_id());
 198  
 199          // Sort discussions when there is a pinned discussion.
 200          $this->pin_discussion($discussion1);
 201          $summaries = array_values($vault->get_from_forum_id($forum->id, true, null,
 202              $vault::SORTORDER_LASTPOST_ASC, 0, 0));
 203          $this->assertCount(4, $summaries);
 204          $this->assertEquals($discussion1->id, $summaries[0]->get_discussion()->get_id());
 205          $this->assertEquals($discussion3->id, $summaries[1]->get_discussion()->get_id());
 206          $this->assertEquals($discussion2->id, $summaries[2]->get_discussion()->get_id());
 207          $this->assertEquals($hiddendiscussion->id, $summaries[3]->get_discussion()->get_id());
 208  
 209          $summaries = array_values($vault->get_from_forum_id($forum->id, true, null,
 210              $vault::SORTORDER_LASTPOST_DESC, 0, 0));
 211          $this->assertCount(4, $summaries);
 212          $this->assertEquals($discussion1->id, $summaries[0]->get_discussion()->get_id());
 213          $this->assertEquals($hiddendiscussion->id, $summaries[1]->get_discussion()->get_id());
 214          $this->assertEquals($discussion2->id, $summaries[2]->get_discussion()->get_id());
 215          $this->assertEquals($discussion3->id, $summaries[3]->get_discussion()->get_id());
 216  
 217          $summaries = array_values($vault->get_from_forum_id($forum->id, true, null,
 218              $vault::SORTORDER_REPLIES_DESC, 0, 0));
 219          $this->assertCount(4, $summaries);
 220          $this->assertEquals($discussion1->id, $summaries[0]->get_discussion()->get_id());
 221          $this->assertEquals($discussion2->id, $summaries[1]->get_discussion()->get_id());
 222          $this->assertEquals($hiddendiscussion->id, $summaries[2]->get_discussion()->get_id());
 223          $this->assertEquals($discussion3->id, $summaries[3]->get_discussion()->get_id());
 224  
 225          // Sort discussions where there is a pinned discussion and several starred discussions.
 226          $this->star_discussion($discussion3, 1);
 227          $this->star_discussion($hiddendiscussion, 1);
 228  
 229          $summaries = array_values($vault->get_from_forum_id($forum->id, true, $user->id,
 230              $vault::SORTORDER_REPLIES_DESC, 0, 0));
 231          $this->assertCount(4, $summaries);
 232          $this->assertEquals($discussion1->id, $summaries[0]->get_discussion()->get_id());
 233          $this->assertEquals($hiddendiscussion->id, $summaries[1]->get_discussion()->get_id());
 234          $this->assertEquals($discussion3->id, $summaries[2]->get_discussion()->get_id());
 235          $this->assertEquals($discussion2->id, $summaries[3]->get_discussion()->get_id());
 236  
 237          $summaries = array_values($vault->get_from_forum_id($forum->id, true, $user->id,
 238              $vault::SORTORDER_REPLIES_ASC, 0, 0));
 239          $this->assertCount(4, $summaries);
 240          $this->assertEquals($discussion1->id, $summaries[0]->get_discussion()->get_id());
 241          $this->assertEquals($discussion3->id, $summaries[1]->get_discussion()->get_id());
 242          $this->assertEquals($hiddendiscussion->id, $summaries[2]->get_discussion()->get_id());
 243          $this->assertEquals($discussion2->id, $summaries[3]->get_discussion()->get_id());
 244      }
 245  
 246      /**
 247       * Test get_from_forum_id_and_group_id.
 248       */
 249      public function test_get_from_forum_id_and_group_id() {
 250          $this->resetAfterTest();
 251  
 252          $datagenerator = $this->getDataGenerator();
 253          $vault = $this->vault;
 254          $user = $datagenerator->create_user();
 255          self::setUser($user);
 256          $course = $datagenerator->create_course();
 257          $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
 258          $this->getDataGenerator()->enrol_user($user->id, $course->id, null, 'manual');
 259  
 260          $this->assertEquals([], $vault->get_from_forum_id($forum->id, true, true,
 261              null, 0, 0, $user));
 262  
 263          $now = time();
 264          [$discussion1, $post1] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now - 10, 'timemodified' => 1]);
 265          [$discussion2, $post2] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now - 9, 'timemodified' => 2]);
 266          [$hiddendiscussion, $post3] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now + 10, 'timemodified' => 3]);
 267          [$groupdiscussion1, $post4] = $this->helper_post_to_forum(
 268              $forum,
 269              $user,
 270              ['timestart' => $now - 8, 'timemodified' => 4, 'groupid' => 1]
 271          );
 272          [$groupdiscussion2, $post5] = $this->helper_post_to_forum(
 273              $forum,
 274              $user,
 275              ['timestart' => $now - 7, 'timemodified' => 5, 'groupid' => 2]
 276          );
 277          [$hiddengroupdiscussion, $post6] = $this->helper_post_to_forum(
 278              $forum,
 279              $user,
 280              ['timestart' => $now + 11, 'timemodified' => 6, 'groupid' => 3]
 281          );
 282  
 283          $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [1, 2, 3], true,
 284              null, null, 0, 0));
 285          $this->assertCount(6, $summaries);
 286          $this->assertEquals($hiddengroupdiscussion->id, $summaries[0]->get_discussion()->get_id());
 287          $this->assertEquals($hiddendiscussion->id, $summaries[1]->get_discussion()->get_id());
 288          $this->assertEquals($groupdiscussion2->id, $summaries[2]->get_discussion()->get_id());
 289          $this->assertEquals($groupdiscussion1->id, $summaries[3]->get_discussion()->get_id());
 290          $this->assertEquals($discussion2->id, $summaries[4]->get_discussion()->get_id());
 291          $this->assertEquals($discussion1->id, $summaries[5]->get_discussion()->get_id());
 292  
 293          $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [1, 2, 3], false,
 294              $user->id, null, 0, 0));
 295          $this->assertCount(6, $summaries);
 296          $this->assertEquals($hiddengroupdiscussion->id, $summaries[0]->get_discussion()->get_id());
 297          $this->assertEquals($hiddendiscussion->id, $summaries[1]->get_discussion()->get_id());
 298          $this->assertEquals($groupdiscussion2->id, $summaries[2]->get_discussion()->get_id());
 299          $this->assertEquals($groupdiscussion1->id, $summaries[3]->get_discussion()->get_id());
 300          $this->assertEquals($discussion2->id, $summaries[4]->get_discussion()->get_id());
 301          $this->assertEquals($discussion1->id, $summaries[5]->get_discussion()->get_id());
 302  
 303          $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [1, 2, 3], true,
 304              null, null, 1, 0));
 305          $this->assertCount(1, $summaries);
 306          $this->assertEquals($hiddengroupdiscussion->id, $summaries[0]->get_discussion()->get_id());
 307  
 308          $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [1, 2, 3], true,
 309              null, null, 1, 1));
 310          $this->assertCount(1, $summaries);
 311          $this->assertEquals($hiddendiscussion->id, $summaries[0]->get_discussion()->get_id());
 312  
 313          $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [1, 2, 3], true,
 314              null, null, 1, 2));
 315          $this->assertCount(1, $summaries);
 316          $this->assertEquals($groupdiscussion2->id, $summaries[0]->get_discussion()->get_id());
 317  
 318          $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [1, 2, 3], false,
 319              null, null, 0, 0));
 320          $this->assertCount(4, $summaries);
 321          $this->assertEquals($groupdiscussion2->id, $summaries[0]->get_discussion()->get_id());
 322          $this->assertEquals($groupdiscussion1->id, $summaries[1]->get_discussion()->get_id());
 323          $this->assertEquals($discussion2->id, $summaries[2]->get_discussion()->get_id());
 324          $this->assertEquals($discussion1->id, $summaries[3]->get_discussion()->get_id());
 325  
 326          $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [], true,
 327              null, null, 0, 0));
 328          $this->assertCount(3, $summaries);
 329          $this->assertEquals($hiddendiscussion->id, $summaries[0]->get_discussion()->get_id());
 330          $this->assertEquals($discussion2->id, $summaries[1]->get_discussion()->get_id());
 331          $this->assertEquals($discussion1->id, $summaries[2]->get_discussion()->get_id());
 332  
 333          // Add 4 replies to $post1.
 334          $this->helper_reply_to_post($post1, $user);
 335          $this->helper_reply_to_post($post1, $user);
 336          $this->helper_reply_to_post($post1, $user);
 337          $this->helper_reply_to_post($post1, $user);
 338          // Add 5 replies to $post2.
 339          $this->helper_reply_to_post($post2, $user);
 340          $this->helper_reply_to_post($post2, $user);
 341          $this->helper_reply_to_post($post2, $user);
 342          $this->helper_reply_to_post($post2, $user);
 343          $this->helper_reply_to_post($post2, $user);
 344          // Add 3 replies to $post3.
 345          $this->helper_reply_to_post($post3, $user);
 346          $this->helper_reply_to_post($post3, $user);
 347          $this->helper_reply_to_post($post3, $user);
 348          // Add 2 replies to $post4.
 349          $this->helper_reply_to_post($post4, $user);
 350          $this->helper_reply_to_post($post4, $user);
 351          // Add 1 reply to $post5.
 352          $this->helper_reply_to_post($post5, $user);
 353  
 354          // Sort discussions by last post DESC.
 355          $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [1, 2, 3], true,
 356              $user->id, $vault::SORTORDER_LASTPOST_DESC, 0, 0));
 357          $this->assertCount(6, $summaries);
 358          $this->assertEquals($groupdiscussion2->id, $summaries[0]->get_discussion()->get_id());
 359          $this->assertEquals($groupdiscussion1->id, $summaries[1]->get_discussion()->get_id());
 360          $this->assertEquals($hiddendiscussion->id, $summaries[2]->get_discussion()->get_id());
 361          $this->assertEquals($hiddengroupdiscussion->id, $summaries[3]->get_discussion()->get_id());
 362          $this->assertEquals($discussion2->id, $summaries[4]->get_discussion()->get_id());
 363          $this->assertEquals($discussion1->id, $summaries[5]->get_discussion()->get_id());
 364  
 365          // Sort discussions by last post ASC.
 366          $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [1, 2, 3], true,
 367              $user->id, $vault::SORTORDER_LASTPOST_ASC, 0, 0));
 368          $this->assertCount(6, $summaries);
 369          $this->assertEquals($discussion1->id, $summaries[0]->get_discussion()->get_id());
 370          $this->assertEquals($discussion2->id, $summaries[1]->get_discussion()->get_id());
 371          $this->assertEquals($hiddengroupdiscussion->id, $summaries[2]->get_discussion()->get_id());
 372          $this->assertEquals($hiddendiscussion->id, $summaries[3]->get_discussion()->get_id());
 373          $this->assertEquals($groupdiscussion1->id, $summaries[4]->get_discussion()->get_id());
 374          $this->assertEquals($groupdiscussion2->id, $summaries[5]->get_discussion()->get_id());
 375  
 376          // Sort discussions by replies DESC.
 377          $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [1, 2, 3], true,
 378              $user->id, $vault::SORTORDER_REPLIES_DESC, 0, 0));
 379          $this->assertCount(6, $summaries);
 380          $this->assertEquals($discussion2->id, $summaries[0]->get_discussion()->get_id());
 381          $this->assertEquals($discussion1->id, $summaries[1]->get_discussion()->get_id());
 382          $this->assertEquals($hiddendiscussion->id, $summaries[2]->get_discussion()->get_id());
 383          $this->assertEquals($groupdiscussion1->id, $summaries[3]->get_discussion()->get_id());
 384          $this->assertEquals($groupdiscussion2->id, $summaries[4]->get_discussion()->get_id());
 385          $this->assertEquals($hiddengroupdiscussion->id, $summaries[5]->get_discussion()->get_id());
 386  
 387          // Sort discussions by replies ASC.
 388          $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [1, 2, 3], true,
 389              $user->id, $vault::SORTORDER_REPLIES_ASC, 0, 0));
 390          $this->assertCount(6, $summaries);
 391          $this->assertEquals($hiddengroupdiscussion->id, $summaries[0]->get_discussion()->get_id());
 392          $this->assertEquals($groupdiscussion2->id, $summaries[1]->get_discussion()->get_id());
 393          $this->assertEquals($groupdiscussion1->id, $summaries[2]->get_discussion()->get_id());
 394          $this->assertEquals($hiddendiscussion->id, $summaries[3]->get_discussion()->get_id());
 395          $this->assertEquals($discussion1->id, $summaries[4]->get_discussion()->get_id());
 396          $this->assertEquals($discussion2->id, $summaries[5]->get_discussion()->get_id());
 397  
 398          // Sort discussions by discussion created DESC.
 399          $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [1, 2, 3], true,
 400              $user->id, $vault::SORTORDER_CREATED_DESC, 0, 0));
 401          $this->assertCount(6, $summaries);
 402          $this->assertEquals($hiddengroupdiscussion->id, $summaries[0]->get_discussion()->get_id());
 403          $this->assertEquals($groupdiscussion2->id, $summaries[1]->get_discussion()->get_id());
 404          $this->assertEquals($groupdiscussion1->id, $summaries[2]->get_discussion()->get_id());
 405          $this->assertEquals($hiddendiscussion->id, $summaries[3]->get_discussion()->get_id());
 406          $this->assertEquals($discussion2->id, $summaries[4]->get_discussion()->get_id());
 407          $this->assertEquals($discussion1->id, $summaries[5]->get_discussion()->get_id());
 408  
 409          // Sort discussions by discussion created ASC.
 410          $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [1, 2, 3], true,
 411              $user->id, $vault::SORTORDER_CREATED_ASC, 0, 0));
 412          $this->assertCount(6, $summaries);
 413          $this->assertEquals($discussion1->id, $summaries[0]->get_discussion()->get_id());
 414          $this->assertEquals($discussion2->id, $summaries[1]->get_discussion()->get_id());
 415          $this->assertEquals($hiddendiscussion->id, $summaries[2]->get_discussion()->get_id());
 416          $this->assertEquals($groupdiscussion1->id, $summaries[3]->get_discussion()->get_id());
 417          $this->assertEquals($groupdiscussion2->id, $summaries[4]->get_discussion()->get_id());
 418          $this->assertEquals($hiddengroupdiscussion->id, $summaries[5]->get_discussion()->get_id());
 419  
 420          // Sort discussions when there is a pinned discussion.
 421          $this->pin_discussion($discussion1);
 422          $this->pin_discussion($hiddendiscussion);
 423  
 424          $summaries = array_values($vault->get_from_forum_id($forum->id, true, null,
 425              $vault::SORTORDER_LASTPOST_DESC, 0, 0));
 426          $this->assertCount(6, $summaries);
 427          $this->assertEquals($hiddendiscussion->id, $summaries[0]->get_discussion()->get_id());
 428          $this->assertEquals($discussion1->id, $summaries[1]->get_discussion()->get_id());
 429          $this->assertEquals($groupdiscussion2->id, $summaries[2]->get_discussion()->get_id());
 430          $this->assertEquals($groupdiscussion1->id, $summaries[3]->get_discussion()->get_id());
 431          $this->assertEquals($hiddengroupdiscussion->id, $summaries[4]->get_discussion()->get_id());
 432          $this->assertEquals($discussion2->id, $summaries[5]->get_discussion()->get_id());
 433  
 434          $summaries = array_values($vault->get_from_forum_id($forum->id, true, null,
 435              $vault::SORTORDER_LASTPOST_ASC, 0, 0));
 436          $this->assertCount(6, $summaries);
 437          $this->assertEquals($discussion1->id, $summaries[0]->get_discussion()->get_id());
 438          $this->assertEquals($hiddendiscussion->id, $summaries[1]->get_discussion()->get_id());
 439          $this->assertEquals($discussion2->id, $summaries[2]->get_discussion()->get_id());
 440          $this->assertEquals($hiddengroupdiscussion->id, $summaries[3]->get_discussion()->get_id());
 441          $this->assertEquals($groupdiscussion1->id, $summaries[4]->get_discussion()->get_id());
 442          $this->assertEquals($groupdiscussion2->id, $summaries[5]->get_discussion()->get_id());
 443  
 444          $summaries = array_values($vault->get_from_forum_id($forum->id, true, null,
 445              $vault::SORTORDER_REPLIES_DESC, 0, 0));
 446          $this->assertCount(6, $summaries);
 447          $this->assertEquals($discussion1->id, $summaries[0]->get_discussion()->get_id());
 448          $this->assertEquals($hiddendiscussion->id, $summaries[1]->get_discussion()->get_id());
 449          $this->assertEquals($discussion2->id, $summaries[2]->get_discussion()->get_id());
 450          $this->assertEquals($groupdiscussion1->id, $summaries[3]->get_discussion()->get_id());
 451          $this->assertEquals($groupdiscussion2->id, $summaries[4]->get_discussion()->get_id());
 452          $this->assertEquals($hiddengroupdiscussion->id, $summaries[5]->get_discussion()->get_id());
 453      }
 454  
 455      /**
 456       * Test get_total_discussion_count_from_forum_id.
 457       */
 458      public function test_get_total_discussion_count_from_forum_id() {
 459          $this->resetAfterTest();
 460  
 461          $datagenerator = $this->getDataGenerator();
 462          $vault = $this->vault;
 463          $user = $datagenerator->create_user();
 464          $course = $datagenerator->create_course();
 465          $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
 466  
 467          $this->assertEquals(0, $vault->get_total_discussion_count_from_forum_id($forum->id, true,
 468              null));
 469  
 470          $now = time();
 471          [$discussion1, $post1] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now - 10, 'timemodified' => 1]);
 472          [$discussion2, $post2] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now - 9, 'timemodified' => 2]);
 473          [$hiddendiscussion, $post3] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now + 10, 'timemodified' => 3]);
 474  
 475          $this->assertEquals(2, $vault->get_total_discussion_count_from_forum_id($forum->id, false,
 476              null));
 477          $this->assertEquals(3, $vault->get_total_discussion_count_from_forum_id($forum->id, true,
 478              null));
 479          $this->assertEquals(3, $vault->get_total_discussion_count_from_forum_id($forum->id, false,
 480              $user->id));
 481      }
 482  
 483      /**
 484       * Test get_total_discussion_count_from_forum_id_and_group_id.
 485       */
 486      public function test_get_total_discussion_count_from_forum_id_and_group_id() {
 487          $this->resetAfterTest();
 488  
 489          $datagenerator = $this->getDataGenerator();
 490          $vault = $this->vault;
 491          $user = $datagenerator->create_user();
 492          self::setUser($user);
 493          $course = $datagenerator->create_course();
 494          $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
 495  
 496          $this->assertEquals([], $vault->get_from_forum_id($forum->id, true, null,
 497              null, 0, 0));
 498  
 499          $now = time();
 500          [$discussion1, $post1] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now - 10, 'timemodified' => 1]);
 501          [$discussion2, $post2] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now - 9, 'timemodified' => 2]);
 502          [$hiddendiscussion, $post3] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now + 10, 'timemodified' => 3]);
 503          [$groupdiscussion1, $post4] = $this->helper_post_to_forum(
 504              $forum,
 505              $user,
 506              ['timestart' => $now - 8, 'timemodified' => 4, 'groupid' => 1]
 507          );
 508          [$groupdiscussion2, $post5] = $this->helper_post_to_forum(
 509              $forum,
 510              $user,
 511              ['timestart' => $now - 7, 'timemodified' => 5, 'groupid' => 2]
 512          );
 513          [$hiddengroupdiscussion, $post6] = $this->helper_post_to_forum(
 514              $forum,
 515              $user,
 516              ['timestart' => $now + 11, 'timemodified' => 6, 'groupid' => 3]
 517          );
 518  
 519          $this->assertEquals(6, $vault->get_total_discussion_count_from_forum_id_and_group_id($forum->id, [1, 2, 3],
 520              true, null));
 521          $this->assertEquals(6, $vault->get_total_discussion_count_from_forum_id_and_group_id(
 522              $forum->id,
 523              [1, 2, 3],
 524              false,
 525              $user->id
 526          ));
 527          $this->assertEquals(4, $vault->get_total_discussion_count_from_forum_id_and_group_id($forum->id, [1, 2, 3],
 528              false, null));
 529          $this->assertEquals(3, $vault->get_total_discussion_count_from_forum_id_and_group_id($forum->id, [],
 530              true, null));
 531      }
 532  
 533      /**
 534       * Pin a duscussion.
 535       *
 536       * @param \stdClass $discussion
 537       */
 538      private function pin_discussion(\stdClass $discussion) {
 539          global $DB;
 540  
 541          $DB->update_record('forum_discussions',
 542              (object) array('id' => $discussion->id, 'pinned' => FORUM_DISCUSSION_PINNED));
 543      }
 544  
 545      /**
 546       * Star a duscussion.
 547       *
 548       * @param \stdClass $discussion
 549       * @param bool     $targetstate The new starred state of the discussion (0 => unstar, 1 => star)
 550       */
 551      private function star_discussion(\stdClass $discussion, bool $targetstate) {
 552          mod_forum_external::toggle_favourite_state($discussion->id, $targetstate);
 553      }
 554  }