Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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
namespace core_files\hook;
18
use coding_exception;
19
 
20
/**
21
 * Tests for before_file_created hook.
22
 *
23
 * @package    core_files
24
 * @category   test
25
 * @copyright  2024 Andrew Lyons <andrew@nicols.co.uk>
26
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27
 * @covers \core_files\hook\before_file_created
28
 */
29
final class before_file_created_test extends \advanced_testcase {
30
    public function test_init_with_file_and_content_throws_exception(): void {
31
        $this->expectException(\InvalidArgumentException::class);
32
        $this->expectExceptionMessage('Only one of $filepath or $filecontent can be set');
33
        new before_file_created(new \stdClass(), 'path', 'content');
34
    }
35
 
36
    public function test_init_with_no_file_and_no_content_throws_exception(): void {
37
        $this->expectException(\InvalidArgumentException::class);
38
        $this->expectExceptionMessage('Either $filepath or $filecontent must be set');
39
        new before_file_created(new \stdClass());
40
    }
41
 
42
    public function test_content_updated(): void {
43
        $hook = new before_file_created(new \stdClass(), filecontent: 'data');
44
        $this->assertFalse($hook->has_changed());
45
        $this->assertTrue($hook->has_filecontent());
46
        $this->assertFalse($hook->has_filepath());
47
 
48
        $hook->update_filecontent('data');
49
        $this->assertEquals('data', $hook->get_filecontent());
50
        $this->assertFalse($hook->has_changed());
51
        $this->assertNull($hook->get_filepath());
52
 
53
        $hook->update_filecontent('new data');
54
        $this->assertEquals('new data', $hook->get_filecontent());
55
        $this->assertTrue($hook->has_changed());
56
        $this->assertNull($hook->get_filepath());
57
    }
58
 
59
    public function test_file_updated(): void {
60
        $initialdata = self::get_fixture_path('core_files', 'hook/before_file_created_hooks.php');
61
        $newdata = __FILE__;
62
 
63
        $hook = new before_file_created(
64
            new \stdClass(),
65
            filepath: $initialdata,
66
        );
67
        $this->assertFalse($hook->has_changed());
68
        $this->assertFalse($hook->has_filecontent());
69
        $this->assertTrue($hook->has_filepath());
70
 
71
        $hook->update_filepath($initialdata);
72
        $this->assertNull($hook->get_filecontent());
73
        $this->assertEquals($initialdata, $hook->get_filepath());
74
        $this->assertFalse($hook->has_changed());
75
 
76
        $hook->update_filepath($newdata);
77
        $this->assertNull($hook->get_filecontent());
78
        $this->assertEquals($newdata, $hook->get_filepath());
79
        $this->assertTrue($hook->has_changed());
80
    }
81
 
82
    public function test_cannot_update_file_when_content_set(): void {
83
        $hook = new before_file_created(new \stdClass(), filecontent: 'data');
84
        $this->expectException(coding_exception::class);
85
        $this->expectExceptionMessage('Cannot update file path when the file path is not set');
86
        $hook->update_filepath('new path');
87
    }
88
 
89
    public function test_cannot_update_content_when_file_ste(): void {
90
        $hook = new before_file_created(new \stdClass(), filepath: __FILE__);
91
        $this->expectException(coding_exception::class);
92
        $this->expectExceptionMessage('Cannot update file content when the file content is not set');
93
        $hook->update_filecontent('new path');
94
    }
95
}