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