trail: new plugin (3rd attempt)
[ikiwiki] / t / trail.t
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use Test::More 'no_plan';
5 use IkiWiki;
6
7 my $blob;
8
9 ok(! system("rm -rf t/tmp"));
10 ok(! system("mkdir t/tmp"));
11
12 # Use a rather stylized template to override the default rendering, to make
13 # it easy to search for the desired results
14 writefile("templates/trails.tmpl", "t/tmp/in", <<EOF
15 <TMPL_LOOP TRAILLOOP>
16 <TMPL_IF __FIRST__><nav></TMPL_IF>
17 <div>
18 trail=<TMPL_VAR TRAILPAGE> n=<TMPL_VAR NEXTPAGE> p=<TMPL_VAR PREVPAGE>
19 </div>
20 <div>
21 <TMPL_IF PREVURL>
22 <a href="<TMPL_VAR PREVURL>">&lt; <TMPL_VAR PREVTITLE></a>
23 </TMPL_IF> |
24 <a href="<TMPL_VAR TRAILURL>">^ <TMPL_VAR TRAILTITLE> ^</a>
25 | <TMPL_IF NEXTURL>
26 <a href="<TMPL_VAR NEXTURL>"><TMPL_VAR NEXTTITLE> &gt;</a>
27 </TMPL_IF>
28 </div>
29 <TMPL_IF __LAST__></nav></TMPL_IF>
30 </TMPL_LOOP>
31 EOF
32 );
33 writefile("badger.mdwn", "t/tmp/in", "[[!meta title=\"The Breezy Badger\"]]\ncontent of badger");
34 writefile("mushroom.mdwn", "t/tmp/in", "content of mushroom");
35 writefile("snake.mdwn", "t/tmp/in", "content of snake");
36 writefile("ratty.mdwn", "t/tmp/in", "content of ratty");
37 writefile("mr_toad.mdwn", "t/tmp/in", "content of mr toad");
38 writefile("add.mdwn", "t/tmp/in", '[[!trail pagenames="add/a add/b add/c add/d add/e"]]');
39 writefile("add/b.mdwn", "t/tmp/in", "b");
40 writefile("add/d.mdwn", "t/tmp/in", "d");
41 writefile("del.mdwn", "t/tmp/in", '[[!trail pages="del/*" sort=title]]');
42 writefile("del/a.mdwn", "t/tmp/in", "a");
43 writefile("del/b.mdwn", "t/tmp/in", "b");
44 writefile("del/c.mdwn", "t/tmp/in", "c");
45 writefile("del/d.mdwn", "t/tmp/in", "d");
46 writefile("del/e.mdwn", "t/tmp/in", "e");
47 writefile("self_referential.mdwn", "t/tmp/in", '[[!trail pagenames="self_referential" circular=yes]]');
48
49 writefile("meme.mdwn", "t/tmp/in", <<EOF
50 [[!trail]]
51 * [[!traillink badger]]
52 * [[!traillink badger text="This is a link to badger, with a title"]]
53 * [[!traillink That_is_the_badger|badger]]
54 * [[!traillink badger]]
55 * [[!traillink mushroom]]
56 * [[!traillink mushroom]]
57 * [[!traillink snake]]
58 * [[!traillink snake]]
59 EOF
60 );
61
62 writefile("wind_in_the_willows.mdwn", "t/tmp/in", <<EOF
63 [[!trail circular=yes sort=title pages="ratty or badger or mr_toad"]]
64 [[!trailitem moley]]
65 EOF
66 );
67
68 ok(! system("make -s ikiwiki.out"));
69
70 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";
71
72 ok(! system($command));
73
74 ok(! system("$command -refresh"));
75
76 $blob = readfile("t/tmp/out/meme.html");
77 ok($blob =~ /<a href="(\.\/)?badger.html">badger<\/a>/m);
78 ok($blob =~ /<a href="(\.\/)?badger.html">This is a link to badger, with a title<\/a>/m);
79 ok($blob =~ /<a href="(\.\/)?badger.html">That is the badger<\/a>/m);
80
81 $blob = readfile("t/tmp/out/badger.html");
82 ok($blob =~ /^trail=meme n=mushroom p=$/m);
83 ok($blob =~ /^trail=wind_in_the_willows n=mr_toad p=ratty$/m);
84
85 ok(! -f "t/tmp/out/moley.html");
86
87 $blob = readfile("t/tmp/out/mr_toad.html");
88 ok($blob !~ /^trail=meme/m);
89 ok($blob =~ /^trail=wind_in_the_willows n=ratty p=badger$/m);
90 # meta title is respected for pages that have one
91 ok($blob =~ /">&lt; The Breezy Badger<\/a>/m);
92 # pagetitle for pages that don't
93 ok($blob =~ /">ratty &gt;<\/a>/m);
94
95 $blob = readfile("t/tmp/out/ratty.html");
96 ok($blob !~ /^trail=meme/m);
97 ok($blob =~ /^trail=wind_in_the_willows n=badger p=mr_toad$/m);
98
99 $blob = readfile("t/tmp/out/mushroom.html");
100 ok($blob =~ /^trail=meme n=snake p=badger$/m);
101 ok($blob !~ /^trail=wind_in_the_willows/m);
102
103 $blob = readfile("t/tmp/out/snake.html");
104 ok($blob =~ /^trail=meme n= p=mushroom$/m);
105 ok($blob !~ /^trail=wind_in_the_willows/m);
106
107 $blob = readfile("t/tmp/out/self_referential.html");
108 ok($blob =~ /^trail=self_referential n= p=$/m);
109
110 $blob = readfile("t/tmp/out/add/b.html");
111 ok($blob =~ /^trail=add n=add\/d p=$/m);
112 $blob = readfile("t/tmp/out/add/d.html");
113 ok($blob =~ /^trail=add n= p=add\/b$/m);
114 ok(! -f "t/tmp/out/add/a.html");
115 ok(! -f "t/tmp/out/add/c.html");
116 ok(! -f "t/tmp/out/add/e.html");
117
118 $blob = readfile("t/tmp/out/del/a.html");
119 ok($blob =~ /^trail=del n=del\/b p=$/m);
120 $blob = readfile("t/tmp/out/del/b.html");
121 ok($blob =~ /^trail=del n=del\/c p=del\/a$/m);
122 $blob = readfile("t/tmp/out/del/c.html");
123 ok($blob =~ /^trail=del n=del\/d p=del\/b$/m);
124 $blob = readfile("t/tmp/out/del/d.html");
125 ok($blob =~ /^trail=del n=del\/e p=del\/c$/m);
126 $blob = readfile("t/tmp/out/del/e.html");
127 ok($blob =~ /^trail=del n= p=del\/d$/m);
128
129 # Make some changes and refresh
130
131 writefile("add/a.mdwn", "t/tmp/in", "a");
132 writefile("add/c.mdwn", "t/tmp/in", "c");
133 writefile("add/e.mdwn", "t/tmp/in", "e");
134 ok(unlink("t/tmp/in/del/a.mdwn"));
135 ok(unlink("t/tmp/in/del/c.mdwn"));
136 ok(unlink("t/tmp/in/del/e.mdwn"));
137
138 ok(! system("$command -refresh"));
139
140 $blob = readfile("t/tmp/out/add/a.html");
141 ok($blob =~ /^trail=add n=add\/b p=$/m);
142 $blob = readfile("t/tmp/out/add/b.html");
143 ok($blob =~ /^trail=add n=add\/c p=add\/a$/m);
144 $blob = readfile("t/tmp/out/add/c.html");
145 ok($blob =~ /^trail=add n=add\/d p=add\/b$/m);
146 $blob = readfile("t/tmp/out/add/d.html");
147 ok($blob =~ /^trail=add n=add\/e p=add\/c$/m);
148 $blob = readfile("t/tmp/out/add/e.html");
149 ok($blob =~ /^trail=add n= p=add\/d$/m);
150
151 $blob = readfile("t/tmp/out/del/b.html");
152 ok($blob =~ /^trail=del n=del\/d p=$/m);
153 $blob = readfile("t/tmp/out/del/d.html");
154 ok($blob =~ /^trail=del n= p=del\/b$/m);
155 ok(! -f "t/tmp/out/del/a.html");
156 ok(! -f "t/tmp/out/del/c.html");
157 ok(! -f "t/tmp/out/del/e.html");
158
159 #ok(! system("rm -rf t/tmp"));