Merge remote-tracking branch 'origin/master'
[ikiwiki] / t / img.t
1 #!/usr/bin/perl
2 #
3 # unit test that creates test images (png, svg, multi-page pdf), runs ikiwiki
4 # on them, checks the resulting images for plausibility based on their image
5 # sizes, and checks if they vanish when not required in the build process any
6 # more
7 #
8 # if you have trouble here, be aware that there are three debian packages that
9 # can provide Image::Magick: perlmagick, libimage-magick-perl and
10 # graphicsmagick-libmagick-dev-compat
11 #
12 package IkiWiki;
13
14 use warnings;
15 use strict;
16 use Cwd qw(getcwd);
17 use Test::More;
18 plan(skip_all => "Image::Magick not available")
19         unless eval q{use Image::Magick; 1};
20
21 use IkiWiki;
22
23 my $installed = $ENV{INSTALLED_TESTS};
24
25 my @command;
26 if ($installed) {
27         @command = qw(ikiwiki);
28 }
29 else {
30         ok(! system("make -s ikiwiki.out"));
31         @command = ("perl", "-I".getcwd, qw(./ikiwiki.out
32                 --underlaydir=underlays/basewiki
33                 --set underlaydirbase=underlays
34                 --templatedir=templates));
35 }
36
37 push @command, qw(--set usedirs=0 --plugin img t/tmp/in t/tmp/out --verbose);
38
39 my $magick = new Image::Magick;
40
41 $magick->Read("t/img/twopages.pdf");
42 my $PDFS_WORK = defined $magick->Get("width");
43
44 ok(! system("rm -rf t/tmp; mkdir -p t/tmp/in"));
45
46 ok(! system("cp t/img/redsquare.bmp t/tmp/in/redsquare.bmp"));
47 ok(! system("cp t/img/redsquare.png t/tmp/in/redsquare.png"));
48 ok(! system("cp t/img/redsquare.jpg t/tmp/in/redsquare.jpg"));
49 ok(! system("cp t/img/redsquare.jpg t/tmp/in/redsquare.jpeg"));
50 ok(! system("cp t/img/redsquare.jpg t/tmp/in/SHOUTY.JPG"));
51 # colons in filenames are a corner case for img
52 ok(! system("cp t/img/redsquare.png t/tmp/in/hello:world.png"));
53 ok(! system("cp t/img/redsquare.png t/tmp/in/a:b:c.png"));
54 ok(! system("cp t/img/redsquare.png t/tmp/in/a:b:c:d.png"));
55 ok(! system("cp t/img/redsquare.png t/tmp/in/a:b:c:d:e:f:g:h:i:j.png"));
56
57 writefile("bluesquare.svg", "t/tmp/in",
58         '<svg width="30" height="30"><rect x="0" y="0" width="30" height="30" fill="blue"/></svg>');
59 ok(! system("cp t/tmp/in/bluesquare.svg t/tmp/in/really-svg.png"));
60 ok(! system("cp t/tmp/in/bluesquare.svg t/tmp/in/really-svg.bmp"));
61 ok(! system("cp t/tmp/in/bluesquare.svg t/tmp/in/really-svg.pdf"));
62
63 # using different image sizes for different pages, so the pagenumber selection can be tested easily
64 ok(! system("cp t/img/twopages.pdf t/tmp/in/twopages.pdf"));
65 ok(! system("cp t/img/twopages.pdf t/tmp/in/really-pdf.JPEG"));
66 ok(! system("cp t/img/twopages.pdf t/tmp/in/really-pdf.jpg"));
67 ok(! system("cp t/img/twopages.pdf t/tmp/in/really-pdf.png"));
68 ok(! system("cp t/img/twopages.pdf t/tmp/in/really-pdf.svg"));
69
70 my $maybe_pdf_img = "";
71 if ($PDFS_WORK) {
72         $maybe_pdf_img = <<EOF;
73 [[!img twopages.pdf size=12x]]
74 [[!img twopages.pdf size=16x pagenumber=1]]
75 EOF
76 }
77
78 writefile("imgconversions.mdwn", "t/tmp/in", <<EOF
79 [[!img redsquare.png]]
80 [[!img redsquare.jpg size=11x]]
81 [[!img redsquare.jpeg size=12x]]
82 [[!img SHOUTY.JPG size=13x]]
83 [[!img redsquare.png size=10x]]
84 [[!img redsquare.png size=30x50]] expecting 30x30
85 [[!img hello:world.png size=x8]] expecting 8x8
86 [[!img a:b:c.png size=x4]]
87 [[!img a:b:c:d:e:f:g:h:i:j.png size=x6]]
88 [[!img bluesquare.svg size=42x]] expecting 42x
89 [[!img bluesquare.svg size=x43]] expecting x43
90 [[!img bluesquare.svg size=42x43]] expecting 42x43 because aspect ratio not preserved
91 $maybe_pdf_img
92
93 # bad ideas
94 [[!img really-svg.png size=666x]]
95 [[!img really-svg.bmp size=666x]]
96 [[!img really-svg.pdf size=666x]]
97 [[!img really-pdf.JPEG size=666x]]
98 [[!img really-pdf.jpg size=666x]]
99 [[!img really-pdf.png size=666x]]
100 [[!img really-pdf.svg size=666x]]
101 [[!img redsquare.bmp size=16x]] expecting 16x16 if bmp enabled
102 EOF
103 );
104 ok(utime(333333333, 333333333, "t/tmp/in/imgconversions.mdwn"));
105
106 ok(! system(@command, '--set-yaml', 'img_allowed_formats=[JPEG, PNG, svg, pdf]'));
107
108 sub size($) {
109         my $filename = shift;
110         my $decoder;
111         if ($filename =~ m/\.png$/i) {
112                 $decoder = 'png';
113         }
114         elsif ($filename =~ m/\.jpe?g$/i) {
115                 $decoder = 'jpeg';
116         }
117         elsif ($filename =~ m/\.bmp$/i) {
118                 $decoder = 'bmp';
119         }
120         else {
121                 die "Unexpected extension in '$filename'";
122         }
123         my $im = Image::Magick->new();
124         my $r = $im->Read("$decoder:$filename");
125         return "no image: $r" if $r;
126         my $w = $im->Get("width");
127         my $h = $im->Get("height");
128         return "${w}x${h}";
129 }
130
131 my $outpath = "t/tmp/out/imgconversions";
132 my $outhtml = readfile("$outpath.html");
133
134 is(size("$outpath/10x-redsquare.png"), "10x10");
135 ok(! -e "$outpath/30x-redsquare.png");
136 ok($outhtml =~ /width="30" height="30".*expecting 30x30/);
137 ok($outhtml =~ /width="42".*expecting 42x/);
138 ok($outhtml =~ /height="43".*expecting x43/);
139 ok($outhtml =~ /width="42" height="43".*expecting 42x43/);
140
141 SKIP: {
142         skip "PDF support not installed (try ghostscript)", 2
143                 unless $PDFS_WORK;
144         is(size("$outpath/12x-twopages.png"), "12x12");
145         is(size("$outpath/16x-p1-twopages.png"), "16x2");
146 }
147
148 ok($outhtml =~ /width="8" height="8".*expecting 8x8/);
149 is(size("$outpath/x8-hello:world.png"), "8x8");
150 is(size("$outpath/x4-a:b:c.png"), "4x4");
151 is(size("$outpath/x6-a:b:c:d:e:f:g:h:i:j.png"), "6x6");
152
153 is(size("$outpath/11x-redsquare.jpg"), "11x11");
154 is(size("$outpath/12x-redsquare.jpeg"), "12x12");
155 is(size("$outpath/13x-SHOUTY.JPG"), "13x13");
156 like($outhtml, qr{src="(\./)?imgconversions/11x-redsquare\.jpg" width="11" height="11"});
157 like($outhtml, qr{src="(\./)?imgconversions/12x-redsquare\.jpeg" width="12" height="12"});
158 like($outhtml, qr{src="(\./)?imgconversions/13x-SHOUTY\.JPG" width="13" height="13"});
159
160 # We do not misinterpret images
161 my $quot = qr/(?:"|&quot;)/;
162 like($outhtml, qr/${quot}really-svg\.png${quot} does not seem to be a valid png file/);
163 ok(! -e "$outpath/666x-really-svg.png");
164 ok(! -e "$outpath/666x-really-svg.bmp");
165 like($outhtml, qr/${quot}really-pdf\.JPEG${quot} does not seem to be a valid jpeg file/);
166 ok(! -e "$outpath/666x-really-pdf.jpeg");
167 ok(! -e "$outpath/666x-really-pdf.JPEG");
168 like($outhtml, qr/${quot}really-pdf\.jpg${quot} does not seem to be a valid jpeg file/);
169 ok(! -e "$outpath/666x-really-pdf.jpg");
170 like($outhtml, qr/${quot}really-pdf\.png${quot} does not seem to be a valid png file/);
171 ok(! -e "$outpath/666x-really-pdf.png");
172
173 # We do not auto-detect file content unless specifically asked to do so
174 ok(! -e "$outpath/16x-redsquare.bmp");
175 like($outhtml, qr{${quot}?bmp${quot}? image processing disabled in img_allowed_formats configuration});
176
177 # resize is deterministic when deterministic=1
178 ok(utime(333333333, 333333333, "t/tmp/in/redsquare.png"));
179 ok(! system("rm $outpath/10x-redsquare.png"));
180 ok(! system(@command, '--set-yaml', 'img_allowed_formats=[JPEG, PNG, svg, pdf]', '--set', 'deterministic=1', "--rebuild"));
181 ok(! system("cp $outpath/10x-redsquare.png $outpath/10x-redsquare.png.orig"));
182 ok(utime(444444444, 444444444, "t/tmp/in/redsquare.png"));
183 ok(! system("rm $outpath/10x-redsquare.png"));
184 ok(! system(@command, '--set-yaml', 'img_allowed_formats=[JPEG, PNG, svg, pdf]', '--set', 'deterministic=1', "--rebuild"));
185 ok(
186         ! system("cmp $outpath/10x-redsquare.png $outpath/10x-redsquare.png.orig"),
187         "resize is deterministic when configured with deterministic=1"
188 );
189
190 # disable support for uncommon formats and try again
191 ok(! system(@command, "--rebuild"));
192 ok(! -e "$outpath/10x-bluesquare.png");
193 ok(! -e "$outpath/12x-twopages.png");
194 ok(! -e "$outpath/16x-p1-twopages.png");
195 ok(! -e "$outpath/16x-redsquare.bmp");
196 like($outhtml, qr{${quot}?bmp${quot}? image processing disabled in img_allowed_formats configuration});
197
198 # enable support for everything (this is a terrible idea if you have
199 # untrusted users)
200 ok(! system(@command, '--set-yaml', 'img_allowed_formats=[everything]', '--rebuild'));
201
202 $outhtml = readfile("$outpath.html");
203 is(size("$outpath/16x-redsquare.bmp"), "16x16");
204 like($outhtml, qr{src="(\./)?imgconversions/16x-redsquare\.bmp" width="16" height="16"});
205 unlike($outhtml, qr{${quot}bmp${quot} image processing disabled in img_allowed_formats configuration});
206
207 is(size("$outpath/10x-redsquare.png"), "10x10");
208 ok(! -e "$outpath/30x-redsquare.png");
209 ok($outhtml =~ /width="30" height="30".*expecting 30x30/);
210 ok($outhtml =~ /width="42".*expecting 42x/);
211 ok($outhtml =~ /height="43".*expecting x43/);
212 ok($outhtml =~ /width="42" height="43".*expecting 42x43/);
213
214 SKIP: {
215         skip "PDF support not installed (try ghostscript)", 2
216                 unless $PDFS_WORK;
217         is(size("$outpath/12x-twopages.png"), "12x12");
218         is(size("$outpath/16x-p1-twopages.png"), "16x2");
219 }
220
221 ok($outhtml =~ /width="8" height="8".*expecting 8x8/);
222 is(size("$outpath/x8-hello:world.png"), "8x8");
223 is(size("$outpath/x4-a:b:c.png"), "4x4");
224 is(size("$outpath/x6-a:b:c:d:e:f:g:h:i:j.png"), "6x6");
225
226 is(size("$outpath/11x-redsquare.jpg"), "11x11");
227 is(size("$outpath/12x-redsquare.jpeg"), "12x12");
228 is(size("$outpath/13x-SHOUTY.JPG"), "13x13");
229 like($outhtml, qr{src="(\./)?imgconversions/11x-redsquare\.jpg" width="11" height="11"});
230 like($outhtml, qr{src="(\./)?imgconversions/12x-redsquare\.jpeg" width="12" height="12"});
231 like($outhtml, qr{src="(\./)?imgconversions/13x-SHOUTY\.JPG" width="13" height="13"});
232
233 # We still do not misinterpret images
234 like($outhtml, qr/${quot}really-svg\.png${quot} does not seem to be a valid png file/);
235 ok(! -e "$outpath/666x-really-svg.png");
236 ok(! -e "$outpath/666x-really-svg.bmp");
237 like($outhtml, qr/${quot}really-pdf\.JPEG${quot} does not seem to be a valid jpeg file/);
238 ok(! -e "$outpath/666x-really-pdf.jpeg");
239 ok(! -e "$outpath/666x-really-pdf.JPEG");
240 like($outhtml, qr/${quot}really-pdf\.jpg${quot} does not seem to be a valid jpeg file/);
241 ok(! -e "$outpath/666x-really-pdf.jpg");
242 like($outhtml, qr/${quot}really-pdf\.png${quot} does not seem to be a valid png file/);
243 ok(! -e "$outpath/666x-really-pdf.png");
244
245 # now let's remove them again
246
247 if (1) { # for easier testing
248         writefile("imgconversions.mdwn", "t/tmp/in", "nothing to see here");
249
250         ok(! system(@command, "--refresh"));
251
252         ok(! -e "$outpath/10x-redsquare.png");
253         ok(! -e "$outpath/10x-bluesquare.png");
254         ok(! -e "$outpath/12x-twopages.png");
255         ok(! -e "$outpath/13x-SHOUTY.JPG");
256         ok(! -e "$outpath/16x-p1-twopages.png");
257         ok(! -e "$outpath/x8-hello:world.png");
258         ok(! -e "$outpath/x4-a:b:c.png");
259         ok(! -e "$outpath/x6-a:b:c:d:e:f:g:h:i:j.png");
260 }
261 done_testing;
262
263 1;