]> sipb.mit.edu Git - ikiwiki.git/blob - t/podcast.t
Render fancy podcast enclosures.
[ikiwiki.git] / t / podcast.t
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4
5 BEGIN {
6         eval q{use XML::Feed; use HTML::Parser; use HTML::LinkExtor};
7         if ($@) {
8                 eval q{use Test::More skip_all =>
9                         "XML::Feed and/or HTML::Parser not available"};
10         }
11         else {
12                 eval q{use Test::More tests => 136};
13         }
14 }
15
16 use Cwd;
17
18 my $tmp = 't/tmp';
19 my $statedir = 't/tinypodcast/.ikiwiki';
20
21 sub simple_podcast {
22         my $baseurl = 'http://example.com';
23         my @command = (qw(./ikiwiki.out -plugin inline -rss -atom));
24         push @command, qw(-underlaydir=underlays/basewiki);
25         push @command, qw(-set underlaydirbase=underlays -templatedir=templates);
26         push @command, "-url=$baseurl", qw(t/tinypodcast), "$tmp/out";
27
28         ok(! system("mkdir $tmp"),
29                 q{setup});
30         ok(! system(@command),
31                 q{build});
32
33         my %media_types = (
34                 'simplepost'    => undef,
35                 'piano.mp3'     => 'audio/mpeg',
36                 'scroll.3gp'    => 'video/3gpp',
37                 'walter.ogg'    => 'video/x-theora+ogg',
38         );
39
40         for my $format (qw(atom rss)) {
41                 my $feed = XML::Feed->parse("$tmp/out/simple/index.$format");
42
43                 is($feed->title, 'simple',
44                         qq{$format feed title});
45                 is($feed->link, "$baseurl/simple/",
46                         qq{$format feed link});
47                 is($feed->description, 'wiki',
48                         qq{$format feed description});
49                 if ('atom' eq $format) {
50                         is($feed->author, $feed->description,
51                                 qq{$format feed author});
52                         is($feed->id, $feed->link,
53                                 qq{$format feed id});
54                         is($feed->generator, "ikiwiki",
55                                 qq{$format feed generator});
56                 }
57
58                 for my $entry ($feed->entries) {
59                         my $title = $entry->title;
60                         my $url = $entry->id;
61                         my $body = $entry->content->body;
62                         my $enclosure = $entry->enclosure;
63
64                         is($entry->link, $url, qq{$format $title link});
65                         isnt($entry->issued, undef,
66                                 qq{$format $title issued date});
67                         isnt($entry->modified, undef,
68                                 qq{$format $title modified date});
69
70                         if (defined $media_types{$title}) {
71                                 is($url, "$baseurl/$title",
72                                         qq{$format $title id});
73                                 is($body, undef,
74                                         qq{$format $title no body text});
75
76                                 # prevent undef method killing test harness
77                                 $enclosure = XML::Feed::Enclosure->new({})
78                                         unless defined $enclosure;
79
80                                 is($enclosure->url, $url,
81                                         qq{$format $title enclosure url});
82                                 is($enclosure->type, $media_types{$title},
83                                         qq{$format $title enclosure type});
84                                 cmp_ok($enclosure->length, '>', 0,
85                                         qq{$format $title enclosure length});
86                         }
87                         else {
88                                 is($url, "$baseurl/$title/",
89                                         qq{$format $title id});
90                                 isnt($body, undef,
91                                         qq{$format $title body text});
92                                 is($enclosure, undef,
93                                         qq{$format $title no enclosure});
94                         }
95                 }
96         }
97
98         ok(! system("rm -rf $tmp $statedir"), q{teardown});
99 }
100
101 sub fancy_podcast {
102         my $baseurl = 'http://example.com';
103         my @command = (qw(./ikiwiki.out -plugin inline -rss -atom));
104         push @command, qw(-underlaydir=underlays/basewiki);
105         push @command, qw(-set underlaydirbase=underlays -templatedir=templates);
106         push @command, "-url=$baseurl", qw(t/tinypodcast), "$tmp/out";
107
108         ok(! system("mkdir $tmp"),
109                 q{setup});
110         ok(! system(@command),
111                 q{build});
112
113         my %media_types = (
114                 'piano.mp3'     => 'audio/mpeg',
115                 'walter.ogg'    => 'video/x-theora+ogg',
116         );
117
118         for my $format (qw(atom rss)) {
119                 my $feed = XML::Feed->parse("$tmp/out/fancy/index.$format");
120
121                 is($feed->title, 'fancy',
122                         qq{$format feed title});
123                 is($feed->link, "$baseurl/fancy/",
124                         qq{$format feed link});
125                 is($feed->description, 'wiki',
126                         qq{$format feed description});
127                 if ('atom' eq $format) {
128                         is($feed->author, $feed->description,
129                                 qq{$format feed author});
130                         is($feed->id, $feed->link,
131                                 qq{$format feed id});
132                         is($feed->generator, "ikiwiki",
133                                 qq{$format feed generator});
134                 }
135
136                 # XXX compare against simple_podcast
137                 # XXX make them table-driven shared code
138                 for my $entry ($feed->entries) {
139                         my $title = $entry->title;
140                         my $url = $entry->id;
141                         my $body = $entry->content->body;
142                         my $enclosure = $entry->enclosure;
143
144                         is($entry->link, $url, qq{$format $title link});
145                         isnt($entry->issued, undef,
146                                 qq{$format $title issued date});
147                         isnt($entry->modified, undef,
148                                 qq{$format $title modified date});
149
150                         if (defined $media_types{$title}) {
151                                 is($url, "$baseurl/$title",
152                                         qq{$format $title id});
153                                 is($body, undef,
154                                         qq{$format $title no body text});
155                                 is($enclosure->url, $url,
156                                         qq{$format $title enclosure url});
157                                 is($enclosure->type, $media_types{$title},
158                                         qq{$format $title enclosure type});
159                                 cmp_ok($enclosure->length, '>', 0,
160                                         qq{$format $title enclosure length});
161                         }
162                         else {
163                                 my $expected_id = "$baseurl/$title/";
164                                 $expected_id =~ s/\ /_/g;
165
166                                 is($url, $expected_id,
167                                         qq{$format $title id});
168                                 isnt($body, undef,
169                                         qq{$format $title body text});
170                                 isnt($enclosure, undef,
171                                         qq{$format $title enclosure});
172                                 use File::Basename;
173                                 my $filename = basename($enclosure->url);
174                                 is($enclosure->type, $media_types{$filename},
175                                         qq{$format $title enclosure type});
176                                 cmp_ok($enclosure->length, '>', 0,
177                                         qq{$format $title enclosure length});
178                         }
179                 }
180         }
181
182         ok(! system("rm -rf $tmp $statedir"), q{teardown});
183 }
184
185 sub single_page_html {
186         my @command = (qw(./ikiwiki.out));
187         push @command, qw(-underlaydir=underlays/basewiki);
188         push @command, qw(-set underlaydirbase=underlays -templatedir=templates);
189         push @command, qw(t/tinypodcast), "$tmp/out";
190
191         ok(! system("mkdir $tmp"),
192                 q{setup});
193         ok(! system(@command),
194                 q{build});
195
196         my $html = "$tmp/out/pianopost/index.html";
197         like(_extract_html_content($html, 'content'), qr/has content and/m,
198                 q{html body text});
199         like(_extract_html_content($html, 'enclosure'), qr/this episode/m,
200                 q{html enclosure});
201         my ($href) = _extract_html_links($html, 'piano');
202         is($href, '/piano.mp3',
203                 q{html enclosure sans -url is site-absolute});
204
205         $html = "$tmp/out/attempted_multiple_enclosures/index.html";
206         like(_extract_html_content($html, 'content'), qr/has content and/m,
207                 q{html body text});
208         like(_extract_html_content($html, 'enclosure'), qr/this episode/m,
209                 q{html enclosure});
210         ($href) = _extract_html_links($html, 'walter');
211         is($href, '/walter.ogg',
212                 q{html enclosure sans -url is site-absolute});
213
214         my $baseurl = 'http://example.com';
215         ok(! system(@command, "-url=$baseurl", q{--rebuild}));
216
217         $html = "$tmp/out/pianopost/index.html";
218         ($href) = _extract_html_links($html, 'piano');
219         is($href, "$baseurl/piano.mp3",
220                 q{html enclosure with -url is fully absolute});
221
222         $html = "$tmp/out/attempted_multiple_enclosures/index.html";
223         ($href) = _extract_html_links($html, 'walter');
224         is($href, "$baseurl/walter.ogg",
225                 q{html enclosure with -url is fully absolute});
226
227         ok(! system("rm -rf $tmp $statedir"), q{teardown});
228 }
229
230 sub inlined_pages_html {
231         my @command = (qw(./ikiwiki.out -plugin inline));
232         push @command, qw(-underlaydir=underlays/basewiki);
233         push @command, qw(-set underlaydirbase=underlays -templatedir=templates);
234         push @command, qw(t/tinypodcast), "$tmp/out";
235
236         ok(! system("mkdir $tmp"),
237                 q{setup});
238         ok(! system(@command),
239                 q{build});
240
241         my $html = "$tmp/out/fancy/index.html";
242         my $contents = _extract_html_content($html, 'content');
243         like($contents, qr/has content and an/m,
244                 q{html body text from pianopost});
245         like($contents, qr/has content and only one/m,
246                 q{html body text from attempted_multiple_enclosures});
247         my $enclosures = _extract_html_content($html, 'inlineenclosure');
248         like($enclosures, qr/this episode/m,
249                 q{html enclosure});
250         my ($href) = _extract_html_links($html, 'piano.mp3');
251         is($href, '/piano.mp3',
252                 q{html enclosure from pianopost sans -url});
253         ($href) = _extract_html_links($html, 'walter.ogg');
254         is($href, '/walter.ogg',
255                 q{html enclosure from attempted_multiple_enclosures sans -url});
256
257         ok(! system("rm -rf $tmp $statedir"), q{teardown});
258 }
259
260 sub _extract_html_content {
261         my ($file, $desired_id, $desired_tag) = @_;
262         $desired_tag = 'div' unless defined $desired_tag;
263
264         my $p = HTML::Parser->new(api_version => 3);
265         my $content = '';
266
267         $p->handler(start => sub {
268                 my ($tag, $self, $attr) = @_;
269                 return if $tag ne $desired_tag;
270                 return unless exists $attr->{id} && $attr->{id} eq $desired_id;
271
272                 $self->handler(text => sub {
273                         my ($dtext) = @_;
274                         $content .= $dtext;
275                 }, "dtext");
276         }, "tagname,self,attr");
277
278         $p->parse_file($file) || die $!;
279
280         return $content;
281 }
282
283 sub _extract_html_links {
284         my ($file, $desired_value) = @_;
285
286         my @hrefs = ();
287
288         my $p = HTML::LinkExtor->new(sub {
289                 my ($tag, %attr) = @_;
290                 return if $tag ne 'a';
291                 return unless $attr{href} =~ qr/$desired_value/;
292                 push(@hrefs, values %attr);
293         }, getcwd() . '/' . $file);
294
295         $p->parse_file($file);
296
297         return @hrefs;
298 }
299
300 simple_podcast();
301 single_page_html();
302 inlined_pages_html();
303 fancy_podcast();