Skip to content

Commit eceedb9

Browse files
committed
fixup! MDL-83886 badges: Move bakerlib.php to classes/png_metadata_handler
1 parent a5caa31 commit eceedb9

File tree

2 files changed

+63
-44
lines changed

2 files changed

+63
-44
lines changed

badges/tests/fixtures/badge.jpg

7.44 KB
Loading

badges/tests/png_metadata_handler_test.php

+63-44
Original file line numberDiff line numberDiff line change
@@ -28,89 +28,103 @@
2828
* @author Dai Nguyen Trong <ngtrdai@hotmail.com>
2929
*/
3030
final class png_metadata_handler_test extends \advanced_testcase {
31+
3132
/**
32-
* Set up function for tests
33+
* Create a valid PNG file content for testing
34+
*
35+
* @return string The PNG file content
3336
*/
34-
protected function setUp(): void {
35-
parent::setUp();
36-
$this->resetAfterTest();
37+
protected function create_test_png(): string {
38+
global $CFG;
39+
40+
$badgepath = $CFG->dirroot . '/badges/tests/behat/badge.png';
41+
return file_get_contents($badgepath);
3742
}
3843

3944
/**
40-
* Create a valid PNG file content for testing
45+
* Create a valid JPG file content for testing
4146
*
4247
* @return string The PNG file content
4348
*/
44-
protected function create_test_png(): string {
45-
// PNG signature.
46-
$content = pack("C8", 137, 80, 78, 71, 13, 10, 26, 10);
47-
48-
// IHDR chunk.
49-
$ihdr = pack("N", 13);
50-
$ihdr .= "IHDR";
51-
$ihdr .= pack("N*", 100, 100);
52-
$ihdr .= pack("C*", 8, 6, 0, 0, 0);
53-
$ihdr .= pack("N", crc32("IHDR" . pack("N*", 100, 100) . pack("C*", 8, 6, 0, 0, 0)));
54-
55-
// IEND chunk.
56-
$iend = pack("N", 0);
57-
$iend .= "IEND";
58-
$iend .= pack("N", crc32("IEND"));
59-
60-
return $content . $ihdr . $iend;
49+
protected function create_test_jpg(): string {
50+
global $CFG;
51+
52+
$badgepath = $CFG->dirroot . '/badges/tests/fixtures/badge.jpg';
53+
return file_get_contents($badgepath);
6154
}
6255

6356
/**
6457
* Test PNG metadata handler constructor with valid PNG.
6558
*/
6659
public function test_constructor_valid_png(): void {
60+
$this->resetAfterTest();
61+
6762
$content = $this->create_test_png();
6863
$handler = new png_metadata_handler($content);
6964
$this->assertInstanceOf(png_metadata_handler::class, $handler);
7065
}
7166

7267
/**
73-
* Test check_chunks method with non-existent chunk.
68+
* Test constructor with invalid PNG.
7469
*/
75-
public function test_check_chunks_non_existent(): void {
76-
$content = $this->create_test_png();
77-
$handler = new png_metadata_handler($content);
70+
public function test_constructor_invalid_png(): void {
71+
$this->resetAfterTest();
7872

79-
$this->assertTrue($handler->check_chunks('tEXt', 'openbadge'));
73+
$content = $this->create_test_jpg();
74+
$handler = new png_metadata_handler($content);
75+
$this->assertDebuggingCalled('This is not a valid PNG image');
76+
$this->assertInstanceOf(png_metadata_handler::class, $handler);
8077
}
8178

8279
/**
83-
* Test add_chunks method with tEXt chunk.
80+
* Test add_chunks method with valid chunks.
81+
*
82+
* @dataProvider add_chunks_provider
83+
* @param string $chunk The chunk type
84+
* @param string $key The key to add
85+
* @param string|null $value The value to add
8486
*/
85-
public function test_add_chunks_text(): void {
87+
public function test_add_chunks(string $type, string $key, ?string $value = null): void {
88+
$this->resetAfterTest();
89+
8690
$content = $this->create_test_png();
8791
$handler = new png_metadata_handler($content);
92+
$this->assertTrue($handler->check_chunks($type, 'openbadge'));
8893

89-
$newcontent = $handler->add_chunks('tEXt', 'openbadge', 'http://example.com/badge');
94+
$newcontent = $handler->add_chunks($type, $key, $value);
9095

9196
// Create new handler with modified content to verify.
9297
$newhandler = new png_metadata_handler($newcontent);
93-
$this->assertFalse($newhandler->check_chunks('tEXt', 'openbadge'));
98+
$this->assertFalse($newhandler->check_chunks($type, $key));
99+
$this->assertDebuggingCalled('Key "' . $key . '" already exists in "' . $type . '" chunk.');
94100
}
95101

96102
/**
97-
* Test add_chunks method with iTXt chunk.
103+
* Data provider for add_chunks test.
104+
*
105+
* @return array The data provider array
98106
*/
99-
public function test_add_chunks_itext(): void {
100-
$content = $this->create_test_png();
101-
$handler = new png_metadata_handler($content);
102-
103-
$newcontent = $handler->add_chunks('iTXt', 'openbadge', 'http://example.com/badge');
104-
105-
// Create new handler with modified content to verify.
106-
$newhandler = new png_metadata_handler($newcontent);
107-
$this->assertFalse($newhandler->check_chunks('iTXt', 'openbadge'));
107+
public static function add_chunks_provider(): array {
108+
return [
109+
'tEXt' => [
110+
'type' => 'tEXt',
111+
'key' => 'openbadge',
112+
'value' => 'http://example.com/badge',
113+
],
114+
'iTXt' => [
115+
'type' => 'iTXt',
116+
'key' => 'openbadge',
117+
'value' => 'http://example.com/badge',
118+
],
119+
];
108120
}
109121

110122
/**
111123
* Test add_chunks method with invalid chunk type.
112124
*/
113125
public function test_add_chunks_invalid_type(): void {
126+
$this->resetAfterTest();
127+
114128
$content = $this->create_test_png();
115129
$handler = new png_metadata_handler($content);
116130

@@ -124,13 +138,18 @@ public function test_add_chunks_invalid_type(): void {
124138
* Test add_chunks method with too long key.
125139
*/
126140
public function test_add_chunks_long_key(): void {
127-
$this->resetDebugging();
141+
$this->resetAfterTest();
142+
128143
$content = $this->create_test_png();
129144
$handler = new png_metadata_handler($content);
130145

131146
$longkey = str_repeat('a', 80);
132-
$handler->add_chunks('tEXt', $longkey, 'http://example.com/badge');
133-
147+
$this->assertTrue($handler->check_chunks('tEXt', $longkey));
148+
$newcontent = $handler->add_chunks('tEXt', $longkey, 'http://example.com/badge');
134149
$this->assertDebuggingCalled('Key is too big');
150+
151+
$newhandler = new png_metadata_handler($newcontent);
152+
$this->assertFalse($newhandler->check_chunks('tEXt', $longkey));
153+
$this->assertDebuggingCalled('Key "' . $longkey . '" already exists in "tEXt" chunk.');
135154
}
136155
}

0 commit comments

Comments
 (0)