Merge branch 'master' into cvs
[ikiwiki.git] / t / trail.t
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use Test::More 'no_plan';
5 use IkiWiki;
6
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 }
15
16 sub check_no_trail {
17         my $file=shift;
18         my $trailname=shift;
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 $trailname in $file");
23 }
24
25 my $blob;
26
27 ok(! system("rm -rf t/tmp"));
28 ok(! system("mkdir t/tmp"));
29
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;
35
36         writefile($name, "t/tmp/in", $content);
37         ok(utime(333333333, 333333333, "t/tmp/in/$name"));
38 }
39
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
100 write_old_file("meme.mdwn", <<EOF
101 [[!trail]]
102 * [[!traillink badger]]
103 * [[!traillink badger text="This is a link to badger, with a title"]]
104 * [[!traillink That_is_the_badger|badger]]
105 * [[!traillink badger]]
106 * [[!traillink mushroom]]
107 * [[!traillink mushroom]]
108 * [[!traillink snake]]
109 * [[!traillink snake]]
110 EOF
111 );
112
113 write_old_file("wind_in_the_willows.mdwn", <<EOF
114 [[!trailoptions circular=yes sort=title]]
115 [[!trailitems pages="ratty or badger or mr_toad"]]
116 [[!trailitem moley]]
117 EOF
118 );
119
120 ok(! system("make -s ikiwiki.out"));
121
122 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";
123
124 ok(! system($command));
125
126 ok(! system("$command -refresh"));
127
128 $blob = readfile("t/tmp/out/meme.html");
129 ok($blob =~ /<a href="(\.\/)?badger.html">badger<\/a>/m);
130 ok($blob =~ /<a href="(\.\/)?badger.html">This is a link to badger, with a title<\/a>/m);
131 ok($blob =~ /<a href="(\.\/)?badger.html">That is the badger<\/a>/m);
132
133 check_trail("badger.html", "n=mushroom p=", "meme");
134 check_trail("badger.html", "n=mr_toad p=ratty", "wind_in_the_willows");
135
136 ok(! -f "t/tmp/out/moley.html");
137
138 check_trail("mr_toad.html", "n=ratty p=badger", "wind_in_the_willows");
139 check_no_trail("mr_toad.html", "meme");
140 # meta title is respected for pages that have one
141 $blob = readfile("t/tmp/out/mr_toad.html");
142 ok($blob =~ /">&lt; The Breezy Badger<\/a>/m);
143 # pagetitle for pages that don't
144 ok($blob =~ /">ratty &gt;<\/a>/m);
145
146 check_no_trail("ratty.html", "meme");
147 check_trail("ratty.html", "n=badger p=mr_toad", "wind_in_the_willows");
148
149 check_trail("mushroom.html", "n=snake p=badger", "meme");
150 check_no_trail("mushroom.html", "wind_in_the_willows");
151
152 check_trail("snake.html", "n= p=mushroom", "meme");
153 check_no_trail("snake.html", "wind_in_the_willows");
154
155 check_trail("self_referential.html", "n= p=", "self_referential");
156
157 check_trail("add/b.html", "n=add/d p=", "add");
158 check_trail("add/d.html", "n= p=add/b", "add");
159 ok(! -f "t/tmp/out/add/a.html");
160 ok(! -f "t/tmp/out/add/c.html");
161 ok(! -f "t/tmp/out/add/e.html");
162
163 check_trail("del/a.html", "n=del/b p=");
164 check_trail("del/b.html", "n=del/c p=del/a");
165 check_trail("del/c.html", "n=del/d p=del/b");
166 check_trail("del/d.html", "n=del/e p=del/c");
167 check_trail("del/e.html", "n= p=del/d");
168
169 check_trail("sorting/linked.html", "n=sorting/a/b p=");
170 check_trail("sorting/a/b.html", "n=sorting/a/c p=sorting/linked");
171 check_trail("sorting/a/c.html", "n=sorting/z/a p=sorting/a/b");
172 check_trail("sorting/z/a.html", "n=sorting/beginning p=sorting/a/c");
173 check_trail("sorting/beginning.html", "n=sorting/middle p=sorting/z/a");
174 check_trail("sorting/middle.html", "n=sorting/end p=sorting/beginning");
175 check_trail("sorting/end.html", "n=sorting/new p=sorting/middle");
176 check_trail("sorting/new.html", "n=sorting/old p=sorting/end");
177 check_trail("sorting/old.html", "n=sorting/ancient p=sorting/new");
178 check_trail("sorting/ancient.html", "n=sorting/linked2 p=sorting/old");
179 check_trail("sorting/linked2.html", "n= p=sorting/ancient");
180
181 # Make some changes and refresh. These writefile calls don't set an
182 # old mtime, so they're strictly newer than the "old" files.
183
184 writefile("add/a.mdwn", "t/tmp/in", "a");
185 writefile("add/c.mdwn", "t/tmp/in", "c");
186 writefile("add/e.mdwn", "t/tmp/in", "e");
187 ok(unlink("t/tmp/in/del/a.mdwn"));
188 ok(unlink("t/tmp/in/del/c.mdwn"));
189 ok(unlink("t/tmp/in/del/e.mdwn"));
190
191 writefile("sorting.mdwn", "t/tmp/in",
192         readfile("t/tmp/in/sorting.mdwn") .
193         '[[!trailoptions sort="title" reverse="yes"]]'); 
194
195 ok(! system("$command -refresh"));
196
197 check_trail("add/a.html", "n=add/b p=");
198 check_trail("add/b.html", "n=add/c p=add/a");
199 check_trail("add/c.html", "n=add/d p=add/b");
200 check_trail("add/d.html", "n=add/e p=add/c");
201 check_trail("add/e.html", "n= p=add/d");
202
203 check_trail("del/b.html", "n=del/d p=");
204 check_trail("del/d.html", "n= p=del/b");
205 ok(! -f "t/tmp/out/del/a.html");
206 ok(! -f "t/tmp/out/del/c.html");
207 ok(! -f "t/tmp/out/del/e.html");
208
209 check_trail("sorting/old.html", "n=sorting/new p=");
210 check_trail("sorting/new.html", "n=sorting/middle p=sorting/old");
211 check_trail("sorting/middle.html", "n=sorting/linked2 p=sorting/new");
212 check_trail("sorting/linked2.html", "n=sorting/linked p=sorting/middle");
213 check_trail("sorting/linked.html", "n=sorting/end p=sorting/linked2");
214 check_trail("sorting/end.html", "n=sorting/a/c p=sorting/linked");
215 check_trail("sorting/a/c.html", "n=sorting/beginning p=sorting/end");
216 check_trail("sorting/beginning.html", "n=sorting/a/b p=sorting/a/c");
217 check_trail("sorting/a/b.html", "n=sorting/ancient p=sorting/beginning");
218 check_trail("sorting/ancient.html", "n=sorting/z/a p=sorting/a/b");
219 check_trail("sorting/z/a.html", "n= p=sorting/ancient");
220
221 ok(! system("rm -rf t/tmp"));