| 1 | 
           efrain | 
           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 | 
              | 
        
        
           | 1441 | 
           ariadna | 
           28 | 
           use context_system;
  | 
        
        
            | 
            | 
           29 | 
              | 
        
        
           | 1 | 
           efrain | 
           30 | 
           /**
  | 
        
        
            | 
            | 
           31 | 
            * Test for draft file added event.
  | 
        
        
            | 
            | 
           32 | 
            *
  | 
        
        
            | 
            | 
           33 | 
            * @package   core
  | 
        
        
            | 
            | 
           34 | 
            * @category  test
  | 
        
        
            | 
            | 
           35 | 
            * @copyright 2023 The Open University.
  | 
        
        
            | 
            | 
           36 | 
            * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           37 | 
            * @covers    \core\event\draft_file_added
  | 
        
        
            | 
            | 
           38 | 
            */
  | 
        
        
           | 1441 | 
           ariadna | 
           39 | 
           final class draft_file_added_test extends \advanced_testcase {
  | 
        
        
           | 1 | 
           efrain | 
           40 | 
               /**
  | 
        
        
            | 
            | 
           41 | 
                * Test draft file added event.
  | 
        
        
            | 
            | 
           42 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           43 | 
               public function test_event(): void {
  | 
        
        
           | 1 | 
           efrain | 
           44 | 
                   $this->resetAfterTest();
  | 
        
        
            | 
            | 
           45 | 
                   $user = $this->getDataGenerator()->create_user();
  | 
        
        
            | 
            | 
           46 | 
                   $this->setUser($user);
  | 
        
        
            | 
            | 
           47 | 
                   $usercontext = \context_user::instance($user->id);
  | 
        
        
            | 
            | 
           48 | 
              | 
        
        
            | 
            | 
           49 | 
                   $sink = $this->redirectEvents();
  | 
        
        
            | 
            | 
           50 | 
                   $fs = get_file_storage();
  | 
        
        
            | 
            | 
           51 | 
              | 
        
        
            | 
            | 
           52 | 
                   $filerecord = [
  | 
        
        
            | 
            | 
           53 | 
                           'contextid' => $usercontext->id,
  | 
        
        
            | 
            | 
           54 | 
                           'component' => 'core',
  | 
        
        
            | 
            | 
           55 | 
                           'filearea' => 'unittest',
  | 
        
        
            | 
            | 
           56 | 
                           'itemid' => 0,
  | 
        
        
            | 
            | 
           57 | 
                           'filepath' => '/',
  | 
        
        
            | 
            | 
           58 | 
                           'filename' => 'test.txt',
  | 
        
        
            | 
            | 
           59 | 
                           'source' => 'Copyright stuff',
  | 
        
        
            | 
            | 
           60 | 
                   ];
  | 
        
        
            | 
            | 
           61 | 
                   $originalfile = $fs->create_file_from_string($filerecord, 'Test content');
  | 
        
        
            | 
            | 
           62 | 
                   $nbsp = "\xc2\xa0";
  | 
        
        
            | 
            | 
           63 | 
              | 
        
        
            | 
            | 
           64 | 
                   // Event data for logging.
  | 
        
        
            | 
            | 
           65 | 
                   $eventdata = [
  | 
        
        
           | 1441 | 
           ariadna | 
           66 | 
                       'objectid' => $originalfile->get_id(),
  | 
        
        
            | 
            | 
           67 | 
                       'context' => $usercontext,
  | 
        
        
            | 
            | 
           68 | 
                       'other' => [
  | 
        
        
            | 
            | 
           69 | 
                           'itemid' => $originalfile->get_itemid(),
  | 
        
        
            | 
            | 
           70 | 
                           'filename' => $originalfile->get_filename(),
  | 
        
        
            | 
            | 
           71 | 
                           'filesize' => $originalfile->get_filesize(),
  | 
        
        
            | 
            | 
           72 | 
                           'filepath' => $originalfile->get_filepath(),
  | 
        
        
            | 
            | 
           73 | 
                           'contenthash' => $originalfile->get_contenthash(),
  | 
        
        
            | 
            | 
           74 | 
                           'avscantime' => '1.234',
  | 
        
        
            | 
            | 
           75 | 
                       ],
  | 
        
        
           | 1 | 
           efrain | 
           76 | 
                   ];
  | 
        
        
           | 1441 | 
           ariadna | 
           77 | 
                   $event = draft_file_added::create($eventdata);
  | 
        
        
           | 1 | 
           efrain | 
           78 | 
                   $event->trigger();
  | 
        
        
            | 
            | 
           79 | 
              | 
        
        
            | 
            | 
           80 | 
                   $events = $sink->get_events();
  | 
        
        
            | 
            | 
           81 | 
                   $this->assertCount(1, $events);
  | 
        
        
            | 
            | 
           82 | 
                   $event = reset($events);
  | 
        
        
            | 
            | 
           83 | 
              | 
        
        
            | 
            | 
           84 | 
                   $this->assertEquals($usercontext, $event->get_context());
  | 
        
        
            | 
            | 
           85 | 
                   $expected = "The user with id '{$user->id}' has uploaded file '/test.txt' to the draft file area with item id 0. ".
  | 
        
        
            | 
            | 
           86 | 
                       "Size: 12{$nbsp}bytes. Content hash: {$originalfile->get_contenthash()}.";
  | 
        
        
            | 
            | 
           87 | 
                   $this->assertSame($expected, $event->get_description());
  | 
        
        
           | 1441 | 
           ariadna | 
           88 | 
                   $this->assertSame(1.234, $event->other['avscantime']);
  | 
        
        
           | 1 | 
           efrain | 
           89 | 
               }
  | 
        
        
           | 1441 | 
           ariadna | 
           90 | 
              | 
        
        
            | 
            | 
           91 | 
               public function test_avscantime_optional(): void {
  | 
        
        
            | 
            | 
           92 | 
                   $eventdata = [
  | 
        
        
            | 
            | 
           93 | 
                       'objectid' => 123,
  | 
        
        
            | 
            | 
           94 | 
                       'context' => context_system::instance(),
  | 
        
        
            | 
            | 
           95 | 
                       'other' => [
  | 
        
        
            | 
            | 
           96 | 
                           'itemid' => 789,
  | 
        
        
            | 
            | 
           97 | 
                           'filename' => 'test.txt',
  | 
        
        
            | 
            | 
           98 | 
                           'filesize' => 42,
  | 
        
        
            | 
            | 
           99 | 
                           'filepath' => '/',
  | 
        
        
            | 
            | 
           100 | 
                           'contenthash' => 'a2653cc92420a875358f09942e4b4665351fa49b',
  | 
        
        
            | 
            | 
           101 | 
                       ],
  | 
        
        
            | 
            | 
           102 | 
                   ];
  | 
        
        
            | 
            | 
           103 | 
                   $event = draft_file_added::create($eventdata);
  | 
        
        
            | 
            | 
           104 | 
                   $event->trigger();
  | 
        
        
            | 
            | 
           105 | 
              | 
        
        
            | 
            | 
           106 | 
                   // Mainly, we are asserting that creating the event does not throw an exception.
  | 
        
        
            | 
            | 
           107 | 
                   $this->assertInstanceOf(draft_file_added::class, $event);
  | 
        
        
            | 
            | 
           108 | 
               }
  | 
        
        
            | 
            | 
           109 | 
              | 
        
        
            | 
            | 
           110 | 
               public function test_avscantime_must_be_float(): void {
  | 
        
        
            | 
            | 
           111 | 
                   $this->expectException(\coding_exception::class);
  | 
        
        
            | 
            | 
           112 | 
              | 
        
        
            | 
            | 
           113 | 
                   $eventdata = [
  | 
        
        
            | 
            | 
           114 | 
                       'objectid' => 123,
  | 
        
        
            | 
            | 
           115 | 
                       'context' => context_system::instance(),
  | 
        
        
            | 
            | 
           116 | 
                       'other' => [
  | 
        
        
            | 
            | 
           117 | 
                           'itemid' => 789,
  | 
        
        
            | 
            | 
           118 | 
                           'filename' => 'test.txt',
  | 
        
        
            | 
            | 
           119 | 
                           'filesize' => 42,
  | 
        
        
            | 
            | 
           120 | 
                           'filepath' => '/',
  | 
        
        
            | 
            | 
           121 | 
                           'contenthash' => 'a2653cc92420a875358f09942e4b4665351fa49b',
  | 
        
        
            | 
            | 
           122 | 
                           'avscantime' => 'frog',
  | 
        
        
            | 
            | 
           123 | 
                       ],
  | 
        
        
            | 
            | 
           124 | 
                   ];
  | 
        
        
            | 
            | 
           125 | 
                   draft_file_added::create($eventdata);
  | 
        
        
            | 
            | 
           126 | 
               }
  | 
        
        
           | 1 | 
           efrain | 
           127 | 
           }
  |