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