28
28
* @author Dai Nguyen Trong <ngtrdai@hotmail.com>
29
29
*/
30
30
final class png_metadata_handler_test extends \advanced_testcase {
31
+
31
32
/**
32
- * Set up function for tests
33
+ * Create a valid PNG file content for testing
34
+ *
35
+ * @return string The PNG file content
33
36
*/
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 );
37
42
}
38
43
39
44
/**
40
- * Create a valid PNG file content for testing
45
+ * Create a valid JPG file content for testing
41
46
*
42
47
* @return string The PNG file content
43
48
*/
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 );
61
54
}
62
55
63
56
/**
64
57
* Test PNG metadata handler constructor with valid PNG.
65
58
*/
66
59
public function test_constructor_valid_png (): void {
60
+ $ this ->resetAfterTest ();
61
+
67
62
$ content = $ this ->create_test_png ();
68
63
$ handler = new png_metadata_handler ($ content );
69
64
$ this ->assertInstanceOf (png_metadata_handler::class, $ handler );
70
65
}
71
66
72
67
/**
73
- * Test check_chunks method with non-existent chunk .
68
+ * Test constructor with invalid PNG .
74
69
*/
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 ();
78
72
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 );
80
77
}
81
78
82
79
/**
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
84
86
*/
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
+
86
90
$ content = $ this ->create_test_png ();
87
91
$ handler = new png_metadata_handler ($ content );
92
+ $ this ->assertTrue ($ handler ->check_chunks ($ type , 'openbadge ' ));
88
93
89
- $ newcontent = $ handler ->add_chunks (' tEXt ' , ' openbadge ' , ' http://example.com/badge ' );
94
+ $ newcontent = $ handler ->add_chunks ($ type , $ key , $ value );
90
95
91
96
// Create new handler with modified content to verify.
92
97
$ 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. ' );
94
100
}
95
101
96
102
/**
97
- * Test add_chunks method with iTXt chunk.
103
+ * Data provider for add_chunks test.
104
+ *
105
+ * @return array The data provider array
98
106
*/
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
+ ];
108
120
}
109
121
110
122
/**
111
123
* Test add_chunks method with invalid chunk type.
112
124
*/
113
125
public function test_add_chunks_invalid_type (): void {
126
+ $ this ->resetAfterTest ();
127
+
114
128
$ content = $ this ->create_test_png ();
115
129
$ handler = new png_metadata_handler ($ content );
116
130
@@ -124,13 +138,18 @@ public function test_add_chunks_invalid_type(): void {
124
138
* Test add_chunks method with too long key.
125
139
*/
126
140
public function test_add_chunks_long_key (): void {
127
- $ this ->resetDebugging ();
141
+ $ this ->resetAfterTest ();
142
+
128
143
$ content = $ this ->create_test_png ();
129
144
$ handler = new png_metadata_handler ($ content );
130
145
131
146
$ 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 ' );
134
149
$ 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. ' );
135
154
}
136
155
}
0 commit comments