Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   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  /**
  18   * The post read receipt collection vault tests.
  19   *
  20   * @package    mod_forum
  21   * @copyright  2019 Ryan Wyllie <ryan@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  require_once (__DIR__ . '/generator_trait.php');
  28  
  29  /**
  30   * The post read receipt collection 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 mod_forum_vaults_post_read_receipt_collection_testcase extends advanced_testcase {
  37      // Make use of the test generator trait.
  38      use mod_forum_tests_generator_trait;
  39  
  40      /**
  41       * Test get_from_user_id_and_post_ids.
  42       */
  43      public function test_get_from_user_id_and_post_ids() {
  44          global $DB;
  45          $this->resetAfterTest();
  46  
  47          $entityfactory = \mod_forum\local\container::get_entity_factory();
  48          $vaultfactory = \mod_forum\local\container::get_vault_factory();
  49          $vault = $vaultfactory->get_post_read_receipt_collection_vault();
  50          $datagenerator = $this->getDataGenerator();
  51          $user = $datagenerator->create_user();
  52          $otheruser = $datagenerator->create_user();
  53          $course = $datagenerator->create_course();
  54          $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
  55          [$discussion1, $post1] = $this->helper_post_to_forum($forum, $user);
  56          [$discussion2, $post2] = $this->helper_post_to_forum($forum, $user);
  57          $post3 = $this->helper_reply_to_post($post2, $user);
  58  
  59          $DB->insert_record('forum_read', [
  60              'userid' => $user->id,
  61              'forumid' => $forum->id,
  62              'discussionid' => $discussion1->id,
  63              'postid' => $post1->id,
  64              'firstread' => time(),
  65              'lastread' => time()
  66          ]);
  67          // Receipt for other user.
  68          $DB->insert_record('forum_read', [
  69              'userid' => $otheruser->id,
  70              'forumid' => $forum->id,
  71              'discussionid' => $discussion1->id,
  72              'postid' => $post1->id,
  73              'firstread' => time(),
  74              'lastread' => time()
  75          ]);
  76          $DB->insert_record('forum_read', [
  77              'userid' => $user->id,
  78              'forumid' => $forum->id,
  79              'discussionid' => $discussion2->id,
  80              'postid' => $post3->id,
  81              'firstread' => time(),
  82              'lastread' => time()
  83          ]);
  84  
  85          $post1 = $entityfactory->get_post_from_stdclass($post1);
  86          $post2 = $entityfactory->get_post_from_stdclass($post2);
  87          $post3 = $entityfactory->get_post_from_stdclass($post3);
  88          $collection = $vault->get_from_user_id_and_post_ids($user->id, [$post1->get_id(), $post2->get_id()]);
  89  
  90          // True because there is a read receipt for this user.
  91          $this->assertTrue($collection->has_user_read_post($user, $post1));
  92          // False because other user wasn't included in the fetch.
  93          $this->assertFalse($collection->has_user_read_post($otheruser, $post1));
  94          // No read receipt.
  95          $this->assertFalse($collection->has_user_read_post($user, $post2));
  96          // Wasn't included in fetch.
  97          $this->assertFalse($collection->has_user_read_post($user, $post3));
  98      }
  99  }