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] [Versions 39 and 310]

   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   * Block recentlyaccesseditems observer tests.
  19   *
  20   * @package    block_recentlyaccesseditems
  21   * @copyright  2018 Victor Deniz <victor@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   * @since      Moodle 3.6
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  require_once($CFG->dirroot . '/mod/assign/tests/generator.php');
  30  
  31  /**
  32   * Block Recently accessed items observer tests.
  33   *
  34   * @package    block_recentlyaccesseditems
  35   * @copyright  2018 Victor Deniz <victor@moodle.com>
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   * @since      Moodle 3.6
  38   */
  39  class block_recentlyaccesseditems_observer_testcase extends advanced_testcase {
  40      use mod_assign_test_generator;
  41  
  42      /**
  43       * Set up for every test
  44       */
  45      public function setUp(): void {
  46          global $DB;
  47  
  48          $this->resetAfterTest();
  49          $this->setAdminUser();
  50  
  51          // Block table name.
  52          $this->table = "block_recentlyaccesseditems";
  53  
  54          // Setup test data.
  55          $this->course = $this->getDataGenerator()->create_course();
  56  
  57          // Create users.
  58          $this->student = self::getDataGenerator()->create_user();
  59          $this->teacher = self::getDataGenerator()->create_user();
  60  
  61          // Users enrolments.
  62          $this->studentrole = $DB->get_record('role', array('shortname' => 'student'));
  63          $this->teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
  64          $this->getDataGenerator()->enrol_user($this->student->id, $this->course->id, $this->studentrole->id, 'manual');
  65          $this->getDataGenerator()->enrol_user($this->teacher->id, $this->course->id, $this->teacherrole->id, 'manual');
  66  
  67          // Create items.
  68          $this->forum = $this->getDataGenerator()->create_module('forum', array('course' => $this->course));
  69          $this->glossary = $this->getDataGenerator()->create_module('glossary', array('course' => $this->course));
  70          $this->chat = $this->getDataGenerator()->create_module('chat', array('course' => $this->course));
  71      }
  72  
  73      /**
  74       * Test items views are recorded
  75       *
  76       * When items events are triggered they are stored in the block_recentlyaccesseditems table.
  77       */
  78      public function test_item_view_recorded_testcase() {
  79          global $DB;
  80  
  81          // Empty table at the beggining.
  82          $records = $DB->count_records($this->table, array());
  83          $this->assertEquals(0, $records);
  84  
  85          // Teacher access forum activity.
  86          $this->setUser($this->teacher);
  87          $event = \mod_forum\event\course_module_viewed::create(array('context' => context_module::instance($this->forum->cmid),
  88                  'objectid' => $this->forum->id));
  89          $event->trigger();
  90  
  91          // Student access chat activity.
  92          $this->setUser($this->student);
  93          $event1 = \mod_chat\event\course_module_viewed::create(array('context' => context_module::instance($this->chat->cmid),
  94                  'objectid' => $this->chat->id));
  95          $event1->trigger();
  96  
  97          $records = $DB->count_records($this->table, array('userid' => $this->teacher->id, 'courseid' => $this->course->id,
  98                  'cmid' => $this->forum->cmid));
  99          $this->assertEquals(1, $records);
 100  
 101          $records = $DB->count_records($this->table, array('userid' => $this->student->id, 'courseid' => $this->course->id, 'cmid' =>
 102                  $this->chat->cmid));
 103          $this->assertEquals(1, $records);
 104  
 105          $this->waitForSecond();
 106          // Student access chat activity again after 1 second (no new record created, timeaccess updated).
 107          $event2 = \mod_chat\event\course_module_viewed::create(array('context' => context_module::instance($this->chat->cmid),
 108                  'objectid' => $this->chat->id));
 109          $event2->trigger();
 110  
 111          $records = $DB->get_records($this->table, array('userid' => $this->student->id, 'courseid' => $this->course->id, 'cmid' =>
 112                  $this->chat->cmid));
 113          $this->assertCount(1, $records);
 114          $this->assertEquals($event2->timecreated, array_shift($records)->timeaccess);
 115  
 116      }
 117  
 118      /**
 119       * Test removed items records are deleted.
 120       *
 121       * When a course module is removed, the records associated in the block_recentlyaccesseditems table are deleted.
 122       */
 123      public function test_item_delete_record_testcase() {
 124          global $DB;
 125  
 126          // Empty table at the beggining.
 127          $records = $DB->count_records($this->table, array());
 128          $this->assertEquals(0, $records);
 129  
 130          // Teacher access forum activity.
 131          $this->setUser($this->teacher);
 132          $event = \mod_forum\event\course_module_viewed::create(array('context' => context_module::instance($this->forum->cmid),
 133                  'objectid' => $this->forum->id));
 134          $event->trigger();
 135  
 136          // Teacher access chat activity.
 137          $event = \mod_chat\event\course_module_viewed::create(array('context' => context_module::instance($this->chat->cmid),
 138                  'objectid' => $this->chat->id));
 139          $event->trigger();
 140  
 141          // Student access chat activity.
 142          $this->setUser($this->student);
 143          $event = \mod_chat\event\course_module_viewed::create(array('context' => context_module::instance($this->chat->cmid),
 144                  'objectid' => $this->chat->id));
 145          $event->trigger();
 146  
 147          // Student access forum activity.
 148          $event = \mod_forum\event\course_module_viewed::create(array('context' => context_module::instance($this->forum->cmid),
 149                  'objectid' => $this->forum->id));
 150          $event->trigger();
 151  
 152          $records = $DB->count_records($this->table, array('cmid' => $this->forum->cmid));
 153          $this->assertEquals(2, $records);
 154          course_delete_module($this->forum->cmid);
 155          $records = $DB->count_records($this->table, array('cmid' => $this->forum->cmid));
 156          $this->assertEquals(0, $records);
 157          $records = $DB->count_records($this->table, array('cmid' => $this->chat->cmid));
 158          $this->assertEquals(2, $records);
 159      }
 160  }