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.
   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   * File added to draft area test events.
  19   *
  20   * @package   core
  21   * @category  test
  22   * @copyright 2023 The Open University.
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace core\event;
  27  
  28  /**
  29   * Test for draft file added event.
  30   *
  31   * @package   core
  32   * @category  test
  33   * @copyright 2023 The Open University.
  34   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   * @covers    \core\event\draft_file_added
  36   */
  37  class draft_file_added_test extends \advanced_testcase {
  38      /**
  39       * Test draft file added event.
  40       */
  41      public function test_event() {
  42          $this->resetAfterTest();
  43          $user = $this->getDataGenerator()->create_user();
  44          $this->setUser($user);
  45          $usercontext = \context_user::instance($user->id);
  46  
  47          $sink = $this->redirectEvents();
  48          $fs = get_file_storage();
  49  
  50          $filerecord = [
  51                  'contextid' => $usercontext->id,
  52                  'component' => 'core',
  53                  'filearea' => 'unittest',
  54                  'itemid' => 0,
  55                  'filepath' => '/',
  56                  'filename' => 'test.txt',
  57                  'source' => 'Copyright stuff',
  58          ];
  59          $originalfile = $fs->create_file_from_string($filerecord, 'Test content');
  60          $nbsp = "\xc2\xa0";
  61  
  62          // Event data for logging.
  63          $eventdata = [
  64                  'objectid' => $originalfile->get_id(),
  65                  'context' => $usercontext,
  66                  'other' => [
  67                          'itemid' => $originalfile->get_itemid(),
  68                          'filename' => $originalfile->get_filename(),
  69                          'filesize' => $originalfile->get_filesize(),
  70                          'filepath' => $originalfile->get_filepath(),
  71                          'contenthash' => $originalfile->get_contenthash(),
  72                  ]
  73          ];
  74          $event = \core\event\draft_file_added::create($eventdata);
  75          $event->trigger();
  76  
  77          $events = $sink->get_events();
  78          $this->assertCount(1, $events);
  79          $event = reset($events);
  80  
  81          $this->assertEquals($usercontext, $event->get_context());
  82          $expected = "The user with id '{$user->id}' has uploaded file '/test.txt' to the draft file area with item id 0. ".
  83              "Size: 12{$nbsp}bytes. Content hash: {$originalfile->get_contenthash()}.";
  84          $this->assertSame($expected, $event->get_description());
  85      }
  86  }