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]

   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  use externallib_advanced_testcase;
  20  
  21  defined('MOODLE_INTERNAL') || die();
  22  
  23  global $CFG;
  24  
  25  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  26  
  27  /**
  28   * Test Recently accessed items block external functions
  29   *
  30   * @package    block_recentlyaccesseditems
  31   * @category   external
  32   * @copyright  2018 Victor Deniz <victor@moodle.com>
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   * @since      Moodle 3.6
  35   */
  36  class externallib_test extends externallib_advanced_testcase {
  37  
  38      /**
  39       * Test the get_recent_items function.
  40       */
  41      public function test_get_recent_items() {
  42  
  43          $this->resetAfterTest();
  44          $generator = $this->getDataGenerator();
  45  
  46          // Add courses.
  47          $courses = array();
  48          for ($i = 1; $i < 4; $i++) {
  49              $courses[] = $generator->create_course();
  50          };
  51  
  52          // Add users.
  53          $student = $generator->create_user();
  54          $teacher = $generator->create_user();
  55  
  56          // Enrol users and add items to courses.
  57          foreach ($courses as $course) {
  58              $generator->enrol_user($student->id, $course->id, 'student');
  59              $forum[] = $this->getDataGenerator()->create_module('forum', array('course' => $course));
  60              $glossary[] = $this->getDataGenerator()->create_module('glossary', array('course' => $course));
  61              $chat[] = $this->getDataGenerator()->create_module('chat', array('course' => $course));
  62          }
  63          $generator->enrol_user($teacher->id, $courses[0]->id, 'teacher');
  64  
  65          $this->setUser($student);
  66  
  67          // No recent items.
  68          $result = \block_recentlyaccesseditems\external::get_recent_items();
  69          $this->assertCount(0, $result);
  70  
  71          // Student access all forums.
  72          foreach ($forum as $module) {
  73              $event = \mod_forum\event\course_module_viewed::create(array('context' => \context_module::instance($module->cmid),
  74                      'objectid' => $module->id));
  75              $event->trigger();
  76              $this->waitForSecond();
  77          }
  78  
  79          // Test that only access to forums are returned.
  80          $result = \block_recentlyaccesseditems\external::get_recent_items();
  81          $this->assertCount(count($forum), $result);
  82  
  83          // Student access all assignments.
  84          foreach ($chat as $module) {
  85              $event = \mod_chat\event\course_module_viewed::create(array('context' => \context_module::instance($module->cmid),
  86                      'objectid' => $module->id));
  87              $event->trigger();
  88              $this->waitForSecond();
  89          }
  90  
  91          // Test that results are sorted by timeaccess DESC (default).
  92          $result = \block_recentlyaccesseditems\external::get_recent_items();
  93          $this->assertCount((count($forum) + count($chat)), $result);
  94          foreach ($result as $key => $record) {
  95              if ($key == 0) {
  96                  continue;
  97              }
  98              $this->assertTrue($record->timeaccess < $result[$key - 1]->timeaccess);
  99          }
 100  
 101          // Delete a course and confirm it's activities don't get returned.
 102          delete_course($courses[0], false);
 103          $result = \block_recentlyaccesseditems\external::get_recent_items();
 104          $this->assertCount((count($forum) + count($chat)) - 2, $result);
 105  
 106          // Delete a single course module should still return.
 107          course_delete_module($forum[1]->cmid);
 108          $result = \block_recentlyaccesseditems\external::get_recent_items();
 109          $this->assertCount((count($forum) + count($chat)) - 3, $result);
 110      }
 111  }