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_badges;
18
 
19
use core_badges\png_metadata_handler;
20
 
21
/**
22
 * Unit tests for PNG metadata handler
23
 *
24
 * @package    core_badges
25
 * @covers     \core_badges\png_metadata_handler
26
 * @copyright  2025 Open University
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 * @author     Dai Nguyen Trong <ngtrdai@hotmail.com>
29
 * @author     Sara Arjona <sara@moodle.com>
30
 */
31
final class png_metadata_handler_test extends \advanced_testcase {
32
 
33
    /**
34
     * Create a valid PNG file content for testing
35
     *
36
     * @return string The PNG file content
37
     */
38
    protected function create_test_png(): string {
39
        global $CFG;
40
 
41
        $badgepath = $CFG->dirroot . '/badges/tests/behat/badge.png';
42
        return file_get_contents($badgepath);
43
    }
44
 
45
    /**
46
     * Create a valid JPG file content for testing
47
     *
48
     * @return string The PNG file content
49
     */
50
    protected function create_test_jpg(): string {
51
        global $CFG;
52
 
53
        $badgepath = $CFG->dirroot . '/badges/tests/fixtures/badge.jpg';
54
        return file_get_contents($badgepath);
55
    }
56
 
57
    /**
58
     * Test PNG metadata handler constructor with valid PNG.
59
     */
60
    public function test_constructor_valid_png(): void {
61
        $this->resetAfterTest();
62
 
63
        $content = $this->create_test_png();
64
        $handler = new png_metadata_handler($content);
65
        $this->assertInstanceOf(png_metadata_handler::class, $handler);
66
    }
67
 
68
    /**
69
     * Test constructor with invalid PNG.
70
     */
71
    public function test_constructor_invalid_png(): void {
72
        $this->resetAfterTest();
73
 
74
        $content = $this->create_test_jpg();
75
        $handler = new png_metadata_handler($content);
76
        $this->assertDebuggingCalled('This is not a valid PNG image');
77
        $this->assertInstanceOf(png_metadata_handler::class, $handler);
78
    }
79
 
80
    /**
81
     * Test add_chunks method with valid chunks.
82
     *
83
     * @dataProvider add_chunks_provider
84
     * @param string $type The chunk type
85
     * @param string $key The key to add
86
     * @param string|null $value The value to add
87
     */
88
    public function test_add_chunks(string $type, string $key, ?string $value = null): void {
89
        $this->resetAfterTest();
90
 
91
        $content = $this->create_test_png();
92
        $handler = new png_metadata_handler($content);
93
        $this->assertTrue($handler->check_chunks($type, 'openbadge'));
94
 
95
        $newcontent = $handler->add_chunks($type, $key, $value);
96
 
97
        // Create new handler with modified content to verify.
98
        $newhandler = new png_metadata_handler($newcontent);
99
        $this->assertFalse($newhandler->check_chunks($type, $key));
100
        $this->assertDebuggingCalled('Key "' . $key . '" already exists in "' . $type . '" chunk.');
101
    }
102
 
103
    /**
104
     * Data provider for add_chunks test.
105
     *
106
     * @return array The data provider array
107
     */
108
    public static function add_chunks_provider(): array {
109
        return [
110
            'tEXt' => [
111
                'type' => 'tEXt',
112
                'key' => 'openbadge',
113
                'value' => 'http://example.com/badge',
114
            ],
115
            'iTXt' => [
116
                'type' => 'iTXt',
117
                'key' => 'openbadge',
118
                'value' => 'http://example.com/badge',
119
            ],
120
        ];
121
    }
122
 
123
    /**
124
     * Test add_chunks method with invalid chunk type.
125
     */
126
    public function test_add_chunks_invalid_type(): void {
127
        $this->resetAfterTest();
128
 
129
        $content = $this->create_test_png();
130
        $handler = new png_metadata_handler($content);
131
 
132
        $this->expectException(\moodle_exception::class);
133
        $this->expectExceptionMessage('Unsupported chunk type: zTXt');
134
 
135
        $handler->add_chunks('zTXt', 'openbadge', 'http://example.com/badge');
136
    }
137
 
138
    /**
139
     * Test add_chunks method with too long key.
140
     */
141
    public function test_add_chunks_long_key(): void {
142
        $this->resetAfterTest();
143
 
144
        $content = $this->create_test_png();
145
        $handler = new png_metadata_handler($content);
146
 
147
        $longkey = str_repeat('a', 80);
148
        $this->assertTrue($handler->check_chunks('tEXt', $longkey));
149
        $newcontent = $handler->add_chunks('tEXt', $longkey, 'http://example.com/badge');
150
        $this->assertDebuggingCalled('Key is too big');
151
 
152
        $newhandler = new png_metadata_handler($newcontent);
153
        $this->assertFalse($newhandler->check_chunks('tEXt', $longkey));
154
        $this->assertDebuggingCalled('Key "' . $longkey . '" already exists in "tEXt" chunk.');
155
    }
156
}