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]

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