Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [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      /** @var string Table name. */
  36      protected $table;
  37  
  38      /** @var \stdClass course data. */
  39      protected $course;
  40  
  41      /** @var \stdClass student data. */
  42      protected $student;
  43  
  44      /** @var \stdClass teacher data. */
  45      protected $teacher;
  46  
  47      /** @var \stdClass student role. */
  48      protected $studentrole;
  49  
  50      /** @var \stdClass teacher role. */
  51      protected $teacherrole;
  52  
  53      /** @var \stdClass course forum. */
  54      protected $forum;
  55  
  56      /** @var \stdClass course glossary. */
  57      protected $glossary;
  58  
  59      /** @var \stdClass course chat. */
  60      protected $chat;
  61  
  62      /**
  63       * Set up for every test
  64       */
  65      public function setUp(): void {
  66          global $DB;
  67  
  68          $this->resetAfterTest();
  69          $this->setAdminUser();
  70  
  71          // Block table name.
  72          $this->table = "block_recentlyaccesseditems";
  73  
  74          // Setup test data.
  75          $this->course = $this->getDataGenerator()->create_course();
  76  
  77          // Create users.
  78          $this->student = self::getDataGenerator()->create_user();
  79          $this->teacher = self::getDataGenerator()->create_user();
  80  
  81          // Users enrolments.
  82          $this->studentrole = $DB->get_record('role', array('shortname' => 'student'));
  83          $this->teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
  84          $this->getDataGenerator()->enrol_user($this->student->id, $this->course->id, $this->studentrole->id, 'manual');
  85          $this->getDataGenerator()->enrol_user($this->teacher->id, $this->course->id, $this->teacherrole->id, 'manual');
  86  
  87          // Create items.
  88          $this->forum = $this->getDataGenerator()->create_module('forum', array('course' => $this->course));
  89          $this->glossary = $this->getDataGenerator()->create_module('glossary', array('course' => $this->course));
  90          $this->chat = $this->getDataGenerator()->create_module('chat', array('course' => $this->course));
  91      }
  92  
  93      /**
  94       * Test items views are recorded
  95       *
  96       * When items events are triggered they are stored in the block_recentlyaccesseditems table.
  97       */
  98      public function test_item_view_recorded_testcase() {
  99          global $DB;
 100  
 101          // Empty table at the beggining.
 102          $records = $DB->count_records($this->table, array());
 103          $this->assertEquals(0, $records);
 104  
 105          // Teacher access forum activity.
 106          $this->setUser($this->teacher);
 107          $event = \mod_forum\event\course_module_viewed::create(array('context' => \context_module::instance($this->forum->cmid),
 108                  'objectid' => $this->forum->id));
 109          $event->trigger();
 110  
 111          // Student access chat activity.
 112          $this->setUser($this->student);
 113          $event1 = \mod_chat\event\course_module_viewed::create(array('context' => \context_module::instance($this->chat->cmid),
 114                  'objectid' => $this->chat->id));
 115          $event1->trigger();
 116  
 117          $records = $DB->count_records($this->table, array('userid' => $this->teacher->id, 'courseid' => $this->course->id,
 118                  'cmid' => $this->forum->cmid));
 119          $this->assertEquals(1, $records);
 120  
 121          $records = $DB->count_records($this->table, array('userid' => $this->student->id, 'courseid' => $this->course->id, 'cmid' =>
 122                  $this->chat->cmid));
 123          $this->assertEquals(1, $records);
 124  
 125          $this->waitForSecond();
 126          // Student access chat activity again after 1 second (no new record created, timeaccess updated).
 127          $event2 = \mod_chat\event\course_module_viewed::create(array('context' => \context_module::instance($this->chat->cmid),
 128                  'objectid' => $this->chat->id));
 129          $event2->trigger();
 130  
 131          $records = $DB->get_records($this->table, array('userid' => $this->student->id, 'courseid' => $this->course->id, 'cmid' =>
 132                  $this->chat->cmid));
 133          $this->assertCount(1, $records);
 134          $this->assertEquals($event2->timecreated, array_shift($records)->timeaccess);
 135  
 136      }
 137  
 138      /**
 139       * Test removed items records are deleted.
 140       *
 141       * When a course module is removed, the records associated in the block_recentlyaccesseditems table are deleted.
 142       */
 143      public function test_item_delete_record_testcase() {
 144          global $DB;
 145  
 146          // Empty table at the beggining.
 147          $records = $DB->count_records($this->table, array());
 148          $this->assertEquals(0, $records);
 149  
 150          // Teacher access forum activity.
 151          $this->setUser($this->teacher);
 152          $event = \mod_forum\event\course_module_viewed::create(array('context' => \context_module::instance($this->forum->cmid),
 153                  'objectid' => $this->forum->id));
 154          $event->trigger();
 155  
 156          // Teacher access chat activity.
 157          $event = \mod_chat\event\course_module_viewed::create(array('context' => \context_module::instance($this->chat->cmid),
 158                  'objectid' => $this->chat->id));
 159          $event->trigger();
 160  
 161          // Student access chat activity.
 162          $this->setUser($this->student);
 163          $event = \mod_chat\event\course_module_viewed::create(array('context' => \context_module::instance($this->chat->cmid),
 164                  'objectid' => $this->chat->id));
 165          $event->trigger();
 166  
 167          // Student access forum activity.
 168          $event = \mod_forum\event\course_module_viewed::create(array('context' => \context_module::instance($this->forum->cmid),
 169                  'objectid' => $this->forum->id));
 170          $event->trigger();
 171  
 172          $records = $DB->count_records($this->table, array('cmid' => $this->forum->cmid));
 173          $this->assertEquals(2, $records);
 174          course_delete_module($this->forum->cmid);
 175          $records = $DB->count_records($this->table, array('cmid' => $this->forum->cmid));
 176          $this->assertEquals(0, $records);
 177          $records = $DB->count_records($this->table, array('cmid' => $this->chat->cmid));
 178          $this->assertEquals(2, $records);
 179      }
 180  }