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 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  /**
  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          $expected = array($course->id, 'folder', 'edit', 'edit.php?id=' . $folder->cmid, $folder->id, $folder->cmid);
  68          $this->assertEventLegacyLogData($expected, $event);
  69          $this->assertEventContextNotUsed($event);
  70      }
  71  
  72      /**
  73       * Test the folder updated event.
  74       *
  75       * There is no external API for updating a folder, so the unit test will simply create
  76       * and trigger the event and ensure the legacy log data is returned as expected.
  77       */
  78      public function test_all_files_downloaded() {
  79          $this->setAdminUser();
  80          $course = $this->getDataGenerator()->create_course();
  81          $folder = $this->getDataGenerator()->create_module('folder', array('course' => $course->id));
  82          $context = \context_module::instance($folder->cmid);
  83          $cm = get_coursemodule_from_id('folder', $folder->cmid, $course->id, true, MUST_EXIST);
  84  
  85          $sink = $this->redirectEvents();
  86          folder_downloaded($folder, $course, $cm, $context);
  87          $events = $sink->get_events();
  88          $this->assertCount(1, $events);
  89          $event = reset($events);
  90  
  91          // Checking that the event contains the expected values.
  92          $this->assertInstanceOf('\mod_folder\event\all_files_downloaded', $event);
  93          $this->assertEquals(\context_module::instance($folder->cmid), $event->get_context());
  94          $this->assertEquals($folder->id, $event->objectid);
  95          $expected = array($course->id, 'folder', 'edit', 'edit.php?id=' . $folder->cmid, $folder->id, $folder->cmid);
  96          $this->assertEventContextNotUsed($event);
  97      }
  98  }