Files
moodle/files/tests/hook/before_file_created_test.php
T
Andrew Nicols b9a5ed7737 MDL-83245 core_files: Update file redaction API
This change:
- Moves the API to use the `before_file_created` hook
- Remove the newly created `$notify` param for `after_file_created` hook
- Stop persisting redactable content
- Update manager to not deal with `stored_file` instances
- Correct namespace from `\core\filereact` to `\core_files\redactor`
- Add `redactor` as a valid L2 namespace within the `core_files` API
- Correct config setting names
- Adds missing unit tests
- Disables the service for PHPUnit tests

AMOS BEGIN
  MOV [fileredact,core_files],[redactor,core_files]
  MOV [fileredact,core_files],[redactor:exifremover,core_files]
  MOV [fileredact,core_files],[redactor:exifremover:emptyremovetags,core_files]
  MOV [fileredact,core_files],[redactor:exifremover:enabled,core_files]
  MOV [fileredact,core_files],[redactor:exifremover:enabled_desc,core_files]
  MOV [fileredact,core_files],[redactor:exifremover:failedprocessexiftool,core_files]
  MOV [fileredact,core_files],[redactor:exifremover:failedprocessgd,core_files]
  MOV [fileredact,core_files],[redactor:exifremover:heading,core_files]
  MOV [fileredact,core_files],[redactor:exifremover:mimetype,core_files]
  MOV [fileredact,core_files],[redactor:exifremover:mimetype_desc,core_files]
  MOV [fileredact,core_files],[redactor:exifremover:removetags,core_files]
  MOV [fileredact,core_files],[redactor:exifremover:removetags_desc,core_files]
  MOV [fileredact,core_files],[redactor:exifremover:tag:all,core_files]
  MOV [fileredact,core_files],[redactor:exifremover:tag:gps,core_files]
  MOV [fileredact,core_files],[redactor:exifremover:tooldoesnotexist,core_files]
  MOV [fileredact,core_files],[redactor:exifremover:toolpath,core_files]
  MOV [fileredact,core_files],[redactor:exifremover:toolpath_desc,core_files]
AMOS END
2024-09-26 11:24:17 +08:00

96 lines
3.9 KiB
PHP

<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core_files\hook;
use coding_exception;
/**
* Tests for before_file_created hook.
*
* @package core_files
* @category test
* @copyright 2024 Andrew Lyons <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \core_files\hook\before_file_created
*/
final class before_file_created_test extends \advanced_testcase {
public function test_init_with_file_and_content_throws_exception(): void {
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Only one of $filepath or $filecontent can be set');
new before_file_created(new \stdClass(), 'path', 'content');
}
public function test_init_with_no_file_and_no_content_throws_exception(): void {
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Either $filepath or $filecontent must be set');
new before_file_created(new \stdClass());
}
public function test_content_updated(): void {
$hook = new before_file_created(new \stdClass(), filecontent: 'data');
$this->assertFalse($hook->has_changed());
$this->assertTrue($hook->has_filecontent());
$this->assertFalse($hook->has_filepath());
$hook->update_filecontent('data');
$this->assertEquals('data', $hook->get_filecontent());
$this->assertFalse($hook->has_changed());
$this->assertNull($hook->get_filepath());
$hook->update_filecontent('new data');
$this->assertEquals('new data', $hook->get_filecontent());
$this->assertTrue($hook->has_changed());
$this->assertNull($hook->get_filepath());
}
public function test_file_updated(): void {
$initialdata = self::get_fixture_path('core_files', 'hook/before_file_created_hooks.php');
$newdata = __FILE__;
$hook = new before_file_created(
new \stdClass(),
filepath: $initialdata,
);
$this->assertFalse($hook->has_changed());
$this->assertFalse($hook->has_filecontent());
$this->assertTrue($hook->has_filepath());
$hook->update_filepath($initialdata);
$this->assertNull($hook->get_filecontent());
$this->assertEquals($initialdata, $hook->get_filepath());
$this->assertFalse($hook->has_changed());
$hook->update_filepath($newdata);
$this->assertNull($hook->get_filecontent());
$this->assertEquals($newdata, $hook->get_filepath());
$this->assertTrue($hook->has_changed());
}
public function test_cannot_update_file_when_content_set(): void {
$hook = new before_file_created(new \stdClass(), filecontent: 'data');
$this->expectException(coding_exception::class);
$this->expectExceptionMessage('Cannot update file path when the file path is not set');
$hook->update_filepath('new path');
}
public function test_cannot_update_content_when_file_ste(): void {
$hook = new before_file_created(new \stdClass(), filepath: __FILE__);
$this->expectException(coding_exception::class);
$this->expectExceptionMessage('Cannot update file content when the file content is not set');
$hook->update_filecontent('new path');
}
}