Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 311 and 402] [Versions 400 and 402] [Versions 401 and 402]

   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   * Events tests.
  19   *
  20   * @package    mod_folder
  21   * @category   test
  22   * @copyright  2013 Mark Nelson <markn@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace mod_folder\event;
  27  
  28  class events_test extends \advanced_testcase {
  29  
  30      /**
  31       * Tests set up.
  32       */
  33      public function setUp(): void {
  34          $this->resetAfterTest();
  35      }
  36  
  37      /**
  38       * Test the folder updated event.
  39       *
  40       * There is no external API for updating a folder, so the unit test will simply create
  41       * and trigger the event and ensure the legacy log data is returned as expected.
  42       */
  43      public function test_folder_updated() {
  44          $this->setAdminUser();
  45          $course = $this->getDataGenerator()->create_course();
  46          $folder = $this->getDataGenerator()->create_module('folder', array('course' => $course->id));
  47  
  48          $params = array(
  49              'context' => \context_module::instance($folder->cmid),
  50              'objectid' => $folder->id,
  51              'courseid' => $course->id
  52          );
  53          $event = \mod_folder\event\folder_updated::create($params);
  54          $event->add_record_snapshot('folder', $folder);
  55  
  56          // Trigger and capturing the event.
  57          $sink = $this->redirectEvents();
  58          $event->trigger();
  59          $events = $sink->get_events();
  60          $this->assertCount(1, $events);
  61          $event = reset($events);
  62  
  63          // Checking that the event contains the expected values.
  64          $this->assertInstanceOf('\mod_folder\event\folder_updated', $event);
  65          $this->assertEquals(\context_module::instance($folder->cmid), $event->get_context());
  66          $this->assertEquals($folder->id, $event->objectid);
  67      }
  68  
  69      /**
  70       * Test the folder updated event.
  71       *
  72       * There is no external API for updating a folder, so the unit test will simply create
  73       * and trigger the event and ensure the legacy log data is returned as expected.
  74       */
  75      public function test_all_files_downloaded() {
  76          $this->setAdminUser();
  77          $course = $this->getDataGenerator()->create_course();
  78          $folder = $this->getDataGenerator()->create_module('folder', array('course' => $course->id));
  79          $context = \context_module::instance($folder->cmid);
  80          $cm = get_coursemodule_from_id('folder', $folder->cmid, $course->id, true, MUST_EXIST);
  81  
  82          $sink = $this->redirectEvents();
  83          folder_downloaded($folder, $course, $cm, $context);
  84          $events = $sink->get_events();
  85          $this->assertCount(1, $events);
  86          $event = reset($events);
  87  
  88          // Checking that the event contains the expected values.
  89          $this->assertInstanceOf('\mod_folder\event\all_files_downloaded', $event);
  90          $this->assertEquals(\context_module::instance($folder->cmid), $event->get_context());
  91          $this->assertEquals($folder->id, $event->objectid);
  92          $expected = array($course->id, 'folder', 'edit', 'edit.php?id=' . $folder->cmid, $folder->id, $folder->cmid);
  93          $this->assertEventContextNotUsed($event);
  94      }
  95  }