img: add support for objects too
[ikiwiki] / t / trail.t
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use Cwd qw(getcwd);
5 use Test::More;
6 use IkiWiki;
7
8 sub check_trail {
9         my $file=shift;
10         my $expected=shift;
11         my $trailname=shift || qr/\w+/;
12         my $blob=readfile("t/tmp/out/$file");
13         my ($trailline)=$blob=~/^trail=$trailname\s+(.*)$/m;
14         is($trailline, $expected, "expected $expected in $file");
15 }
16
17 sub check_no_trail {
18         my $file=shift;
19         my $trailname=shift || qr/\w+/;
20         my $blob=readfile("t/tmp/out/$file");
21         my ($trailline)=$blob=~/^trail=$trailname\s+(.*)$/m;
22         $trailline="" unless defined $trailline;
23         ok($trailline !~ /^trail=$trailname\s+/, "no trail $trailname in $file");
24 }
25
26 my $blob;
27
28 ok(! system("rm -rf t/tmp"));
29 ok(! system("mkdir t/tmp"));
30
31 my $installed = $ENV{INSTALLED_TESTS};
32
33 my @command;
34 if ($installed) {
35         @command = qw(ikiwiki);
36 }
37 else {
38         ok(! system("make -s ikiwiki.out"));
39         @command = ("perl", "-I".getcwd, qw(./ikiwiki.out
40                 --underlaydir=underlays/basewiki
41                 --set underlaydirbase=underlays
42                 --templatedir=templates));
43 }
44
45 push @command, qw(--set usedirs=0 --plugin trail --plugin inline
46         --url=http://example.com --cgiurl=http://example.com/ikiwiki.cgi
47         --rss --atom t/tmp/in t/tmp/out --verbose);
48
49 # Write files with a date in the past, so that when we refresh,
50 # the update is detected.
51 sub write_old_file {
52         my $name = shift;
53         my $content = shift;
54
55         writefile($name, "t/tmp/in", $content);
56         ok(utime(333333333, 333333333, "t/tmp/in/$name"));
57 }
58
59 # Use a rather stylized template to override the default rendering, to make
60 # it easy to search for the desired results
61 write_old_file("templates/trails.tmpl", <<EOF
62 <TMPL_LOOP TRAILLOOP>
63 <TMPL_IF __FIRST__><nav></TMPL_IF>
64 <div>
65 trail=<TMPL_VAR TRAILPAGE> n=<TMPL_VAR NEXTPAGE> p=<TMPL_VAR PREVPAGE>
66 </div>
67 <div>
68 <TMPL_IF PREVURL>
69 <a href="<TMPL_VAR PREVURL>">&lt; <TMPL_VAR PREVTITLE></a>
70 </TMPL_IF> |
71 <a href="<TMPL_VAR TRAILURL>">^ <TMPL_VAR TRAILTITLE> ^</a>
72 | <TMPL_IF NEXTURL>
73 <a href="<TMPL_VAR NEXTURL>"><TMPL_VAR NEXTTITLE> &gt;</a>
74 </TMPL_IF>
75 </div>
76 <TMPL_IF __LAST__></nav></TMPL_IF>
77 </TMPL_LOOP>
78 EOF
79 );
80 write_old_file("badger.mdwn", "[[!meta title=\"The Breezy Badger\"]]\ncontent of badger");
81 write_old_file("mushroom.mdwn", "content of mushroom");
82 write_old_file("snake.mdwn", "content of snake");
83 write_old_file("ratty.mdwn", "content of ratty");
84 write_old_file("mr_toad.mdwn", "content of mr toad");
85 write_old_file("add.mdwn", '[[!trailitems pagenames="add/a add/b add/c add/d add/e"]]');
86 write_old_file("add/b.mdwn", "b");
87 write_old_file("add/d.mdwn", "d");
88 write_old_file("del.mdwn", '[[!trailitems pages="del/*" sort=title]]');
89 write_old_file("del/a.mdwn", "a");
90 write_old_file("del/b.mdwn", "b");
91 write_old_file("del/c.mdwn", "c");
92 write_old_file("del/d.mdwn", "d");
93 write_old_file("del/e.mdwn", "e");
94 write_old_file("self_referential.mdwn", '[[!trailitems pagenames="self_referential" circular=yes]]');
95 write_old_file("sorting/linked.mdwn", "linked");
96 write_old_file("sorting/a/b.mdwn", "a/b");
97 write_old_file("sorting/a/c.mdwn", "a/c");
98 write_old_file("sorting/z/a.mdwn", "z/a");
99 write_old_file("sorting/beginning.mdwn", "beginning");
100 write_old_file("sorting/middle.mdwn", "middle");
101 write_old_file("sorting/end.mdwn", "end");
102 write_old_file("sorting/new.mdwn", "new");
103 write_old_file("sorting/old.mdwn", "old");
104 write_old_file("sorting/ancient.mdwn", "ancient");
105 write_old_file("unicode.mdwn", "[[!trailitems pagenames=\"unicode/\xAC unicode/\x{04D2} unicode/\x{2260} unicode/\x{0001F4A9}\"]]");
106 write_old_file("unicode2.mdwn", "[[!trailitems pagenames=\"unicode/__172__ unicode/\x{04D2} unicode/__8800__ unicode/__128169__\"]]");
107 write_old_file("unicode/__172__.mdwn", "trail item '\xAC'");
108 write_old_file("unicode/\x{04D2}.mdwn", "trail item '\x{04D2}'");
109 write_old_file("unicode/__8800__.mdwn", "trail item '\x{2260}'");
110 write_old_file("unicode/__128169__.mdwn", "trail item '\x{0001F4A9}'");
111 # These three need to be in the appropriate age order
112 ok(utime(333333333, 333333333, "t/tmp/in/sorting/new.mdwn"));
113 ok(utime(222222222, 222222222, "t/tmp/in/sorting/old.mdwn"));
114 ok(utime(111111111, 111111111, "t/tmp/in/sorting/ancient.mdwn"));
115 write_old_file("sorting/linked2.mdwn", "linked2");
116 # This initially uses the default sort order: age for the inline, and path
117 # for trailitems. We change it later.
118 write_old_file("sorting.mdwn",
119         '[[!traillink linked]] ' .
120         '[[!trailitems pages="sorting/z/a or sorting/a/b or sorting/a/c"]] ' .
121         '[[!trailitems pagenames="sorting/beginning sorting/middle sorting/end"]] ' .
122         '[[!inline pages="sorting/old or sorting/ancient or sorting/new" trail="yes"]] ' .
123         '[[!traillink linked2]]');
124 write_old_file("limited/a.mdwn", "a");
125 write_old_file("limited/b.mdwn", "b");
126 write_old_file("limited/c.mdwn", "c");
127 write_old_file("limited/d.mdwn", "d");
128 write_old_file("limited.mdwn",
129         '[[!inline pages="limited/*" trail="yes" show=2 sort=title]]');
130 write_old_file("untrail/a.mdwn", "a");
131 write_old_file("untrail/b.mdwn", "b");
132 write_old_file("untrail.mdwn", "[[!traillink a]] [[!traillink b]]");
133 write_old_file("retitled/a.mdwn", "a");
134 write_old_file("retitled.mdwn",
135         '[[!meta title="the old title"]][[!traillink a]]');
136
137 write_old_file("meme.mdwn", <<EOF
138 [[!trail]]
139 * [[!traillink badger]]
140 * [[!traillink badger text="This is a link to badger, with a title"]]
141 * [[!traillink That_is_the_badger|badger]]
142 * [[!traillink badger]]
143 * [[!traillink mushroom]]
144 * [[!traillink mushroom]]
145 * [[!traillink snake]]
146 * [[!traillink snake]]
147 EOF
148 );
149
150 write_old_file("wind_in_the_willows.mdwn", <<EOF
151 [[!trailoptions circular=yes sort=title]]
152 [[!trailitems pages="ratty or badger or mr_toad"]]
153 [[!trailitem moley]]
154 EOF
155 );
156
157 ok(! system(@command));
158 ok(! system(@command, "--refresh"));
159
160 $blob = readfile("t/tmp/out/meme.html");
161 ok($blob =~ /<a href="(\.\/)?badger.html">badger<\/a>/m);
162 ok($blob =~ /<a href="(\.\/)?badger.html">This is a link to badger, with a title<\/a>/m);
163 ok($blob =~ /<a href="(\.\/)?badger.html">That is the badger<\/a>/m);
164
165 check_trail("badger.html", "n=mushroom p=", "meme");
166 check_trail("badger.html", "n=mr_toad p=ratty", "wind_in_the_willows");
167
168 ok(! -f "t/tmp/out/moley.html");
169
170 check_trail("mr_toad.html", "n=ratty p=badger", "wind_in_the_willows");
171 check_no_trail("mr_toad.html", "meme");
172 # meta title is respected for pages that have one
173 $blob = readfile("t/tmp/out/mr_toad.html");
174 ok($blob =~ /">&lt; The Breezy Badger<\/a>/m);
175 # pagetitle for pages that don't
176 ok($blob =~ /">ratty &gt;<\/a>/m);
177
178 check_no_trail("ratty.html", "meme");
179 check_trail("ratty.html", "n=badger p=mr_toad", "wind_in_the_willows");
180
181 check_trail("mushroom.html", "n=snake p=badger", "meme");
182 check_no_trail("mushroom.html", "wind_in_the_willows");
183
184 check_trail("snake.html", "n= p=mushroom", "meme");
185 check_no_trail("snake.html", "wind_in_the_willows");
186
187 check_trail("self_referential.html", "n= p=", "self_referential");
188
189 check_trail("unicode/__172__.html", "n=unicode/\x{04D2} p=", "unicode");
190 check_trail("unicode/\x{04D2}.html", "n=unicode/__8800__ p=unicode/__172__", "unicode");
191 check_trail("unicode/__8800__.html", "n=unicode/__128169__ p=unicode/\x{04D2}", "unicode");
192 check_trail("unicode/__128169__.html", "n= p=unicode/__8800__", "unicode");
193
194 check_trail("unicode/__172__.html", "n=unicode/\x{04D2} p=", "unicode2");
195 check_trail("unicode/\x{04D2}.html", "n=unicode/__8800__ p=unicode/__172__", "unicode2");
196 check_trail("unicode/__8800__.html", "n=unicode/__128169__ p=unicode/\x{04D2}", "unicode2");
197 check_trail("unicode/__128169__.html", "n= p=unicode/__8800__", "unicode2");
198
199 check_trail("add/b.html", "n=add/d p=", "add");
200 check_trail("add/d.html", "n= p=add/b", "add");
201 ok(! -f "t/tmp/out/add/a.html");
202 ok(! -f "t/tmp/out/add/c.html");
203 ok(! -f "t/tmp/out/add/e.html");
204
205 check_trail("del/a.html", "n=del/b p=");
206 check_trail("del/b.html", "n=del/c p=del/a");
207 check_trail("del/c.html", "n=del/d p=del/b");
208 check_trail("del/d.html", "n=del/e p=del/c");
209 check_trail("del/e.html", "n= p=del/d");
210
211 check_trail("sorting/linked.html", "n=sorting/a/b p=");
212 check_trail("sorting/a/b.html", "n=sorting/a/c p=sorting/linked");
213 check_trail("sorting/a/c.html", "n=sorting/z/a p=sorting/a/b");
214 check_trail("sorting/z/a.html", "n=sorting/beginning p=sorting/a/c");
215 check_trail("sorting/beginning.html", "n=sorting/middle p=sorting/z/a");
216 check_trail("sorting/middle.html", "n=sorting/end p=sorting/beginning");
217 check_trail("sorting/end.html", "n=sorting/new p=sorting/middle");
218 check_trail("sorting/new.html", "n=sorting/old p=sorting/end");
219 check_trail("sorting/old.html", "n=sorting/ancient p=sorting/new");
220 check_trail("sorting/ancient.html", "n=sorting/linked2 p=sorting/old");
221 check_trail("sorting/linked2.html", "n= p=sorting/ancient");
222
223 # If the inline has a limited number of pages, the trail still contains
224 # everything.
225 $blob = readfile("t/tmp/out/limited.html");
226 ok($blob =~ /<a href="(\.\/)?limited\/a.html">a<\/a>/m);
227 ok($blob =~ /<a href="(\.\/)?limited\/b.html">b<\/a>/m);
228 ok($blob !~ /<a href="(\.\/)?limited\/c.html">/m);
229 ok($blob !~ /<a href="(\.\/)?limited\/d.html">/m);
230 check_trail("limited/a.html", "n=limited/b p=");
231 check_trail("limited/b.html", "n=limited/c p=limited/a");
232 check_trail("limited/c.html", "n=limited/d p=limited/b");
233 check_trail("limited/d.html", "n= p=limited/c");
234
235 check_trail("untrail/a.html", "n=untrail/b p=");
236 check_trail("untrail/b.html", "n= p=untrail/a");
237
238 $blob = readfile("t/tmp/out/retitled/a.html");
239 ok($blob =~ /\^ the old title \^/m);
240
241 # Make some changes and refresh. These writefile calls don't set an
242 # old mtime, so they're strictly newer than the "old" files.
243
244 writefile("add/a.mdwn", "t/tmp/in", "a");
245 writefile("add/c.mdwn", "t/tmp/in", "c");
246 writefile("add/e.mdwn", "t/tmp/in", "e");
247 ok(unlink("t/tmp/in/del/a.mdwn"));
248 ok(unlink("t/tmp/in/del/c.mdwn"));
249 ok(unlink("t/tmp/in/del/e.mdwn"));
250
251 writefile("sorting.mdwn", "t/tmp/in",
252         readfile("t/tmp/in/sorting.mdwn") .
253         '[[!trailoptions sort="title" reverse="yes"]]'); 
254
255 writefile("retitled.mdwn", "t/tmp/in",
256         '[[!meta title="the new title"]][[!traillink a]]');
257
258 # If the inline has a limited number of pages, the trail still depends on
259 # everything.
260 writefile("limited.html", "t/tmp/out", "[this gets rebuilt]");
261 writefile("limited/c.mdwn", "t/tmp/in", '[[!meta title="New C page"]]c');
262
263 writefile("untrail.mdwn", "t/tmp/in", "no longer a trail");
264
265 ok(! system(@command, "--refresh"));
266
267 check_trail("add/a.html", "n=add/b p=");
268 check_trail("add/b.html", "n=add/c p=add/a");
269 check_trail("add/c.html", "n=add/d p=add/b");
270 check_trail("add/d.html", "n=add/e p=add/c");
271 check_trail("add/e.html", "n= p=add/d");
272
273 check_trail("del/b.html", "n=del/d p=");
274 check_trail("del/d.html", "n= p=del/b");
275 ok(! -f "t/tmp/out/del/a.html");
276 ok(! -f "t/tmp/out/del/c.html");
277 ok(! -f "t/tmp/out/del/e.html");
278
279 check_trail("sorting/old.html", "n=sorting/new p=");
280 check_trail("sorting/new.html", "n=sorting/middle p=sorting/old");
281 check_trail("sorting/middle.html", "n=sorting/linked2 p=sorting/new");
282 check_trail("sorting/linked2.html", "n=sorting/linked p=sorting/middle");
283 check_trail("sorting/linked.html", "n=sorting/end p=sorting/linked2");
284 check_trail("sorting/end.html", "n=sorting/a/c p=sorting/linked");
285 check_trail("sorting/a/c.html", "n=sorting/beginning p=sorting/end");
286 check_trail("sorting/beginning.html", "n=sorting/a/b p=sorting/a/c");
287 check_trail("sorting/a/b.html", "n=sorting/ancient p=sorting/beginning");
288 check_trail("sorting/ancient.html", "n=sorting/z/a p=sorting/a/b");
289 check_trail("sorting/z/a.html", "n= p=sorting/ancient");
290
291 # If the inline has a limited number of pages, the trail still depends on
292 # everything, so it gets rebuilt even though it doesn't strictly need it.
293 # This means we could use it as a way to recompute the order of members
294 # and the contents of their trail navbars, allowing us to fix the regression
295 # described in [[bugs/trail excess dependencies]] without a full content
296 # dependency.
297 $blob = readfile("t/tmp/out/limited.html");
298 ok($blob =~ /<a href="(\.\/)?limited\/a.html">a<\/a>/m);
299 ok($blob =~ /<a href="(\.\/)?limited\/b.html">b<\/a>/m);
300 ok($blob !~ /<a href="(\.\/)?limited\/c.html">/m);
301 ok($blob !~ /<a href="(\.\/)?limited\/d.html">/m);
302 check_trail("limited/a.html", "n=limited/b p=");
303 check_trail("limited/b.html", "n=limited/c p=limited/a");
304 check_trail("limited/c.html", "n=limited/d p=limited/b");
305 check_trail("limited/d.html", "n= p=limited/c");
306 # Also, b and d should pick up the change to c. This regressed with the
307 # change to using a presence dependency.
308 $blob = readfile("t/tmp/out/limited/b.html");
309 ok($blob =~ /New C page &gt;/m);
310 $blob = readfile("t/tmp/out/limited/d.html");
311 ok($blob =~ /&lt; New C page/m);
312
313 # Members of a retitled trail should pick up that change.
314 # This regressed with the change to using a presence dependency.
315 $blob = readfile("t/tmp/out/retitled/a.html");
316 ok($blob =~ /\^ the new title \^/m);
317
318 # untrail is no longer a trail, so these are no longer in it.
319 check_no_trail("untrail/a.html");
320 check_no_trail("untrail/b.html");
321
322 done_testing();