Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

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