f036c3113e4a1620b00dd50541be4a0a8956c18e
[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; use File::MimeInfo};
7         if ($@) {
8                 eval q{use Test::More skip_all =>
9                         "XML::Feed and/or HTML::Parser or File::MimeInfo not available"};
10         }
11         else {
12                 eval q{use Test::More tests => 136};
13         }
14 }
15
16 use Cwd;
17 use File::Basename;
18
19 my $tmp = 't/tmp';
20 my $statedir = 't/tinypodcast/.ikiwiki';
21
22 sub podcast {
23         my $podcast_style = shift;
24
25         my $baseurl = 'http://example.com';
26         my @command = (qw(./ikiwiki.out -plugin inline -rss -atom));
27         push @command, qw(-underlaydir=underlays/basewiki);
28         push @command, qw(-set underlaydirbase=underlays -templatedir=templates);
29         push @command, "-url=$baseurl", qw(t/tinypodcast), "$tmp/out";
30
31         ok(! system("mkdir $tmp"),
32                 q{setup});
33         ok(! system(@command),
34                 q{build});
35
36         my %media_types = (
37                 'simplepost'    => undef,
38                 'piano.mp3'     => 'audio/mpeg',
39                 'scroll.3gp'    => 'video/3gpp',
40                 'walter.ogg'    => 'video/x-theora+ogg',
41         );
42
43         for my $format (qw(atom rss)) {
44                 my $feed = XML::Feed->parse("$tmp/out/$podcast_style/index.$format");
45
46                 is($feed->title, $podcast_style,
47                         qq{$format feed title});
48                 is($feed->link, "$baseurl/$podcast_style/",
49                         qq{$format feed link});
50                 is($feed->description, 'wiki',
51                         qq{$format feed description});
52                 if ('atom' eq $format) {
53                         is($feed->author, $feed->description,
54                                 qq{$format feed author});
55                         is($feed->id, $feed->link,
56                                 qq{$format feed id});
57                         is($feed->generator, "ikiwiki",
58                                 qq{$format feed generator});
59                 }
60
61                 for my $entry ($feed->entries) {
62                         my $title = $entry->title;
63                         my $url = $entry->id;
64                         my $body = $entry->content->body;
65                         my $enclosure = $entry->enclosure;
66
67                         is($entry->link, $url, qq{$format $title link});
68                         isnt($entry->issued, undef,
69                                 qq{$format $title issued date});
70                         isnt($entry->modified, undef,
71                                 qq{$format $title modified date});
72
73                         if (defined $media_types{$title}) {
74                                 is($url, "$baseurl/$title",
75                                         qq{$format $title id});
76                                 is($body, undef,
77                                         qq{$format $title no body text});
78                                 is($enclosure->url, $url,
79                                         qq{$format $title enclosure url});
80                                 is($enclosure->type, $media_types{$title},
81                                         qq{$format $title enclosure type});
82                                 cmp_ok($enclosure->length, '>', 0,
83                                         qq{$format $title enclosure length});
84                         }
85                         else {
86                                 # XXX hack hack hack
87                                 my $expected_id = "$baseurl/$title/";
88                                 $expected_id =~ s/\ /_/g;
89
90                                 is($url, $expected_id,
91                                         qq{$format $title id});
92                                 isnt($body, undef,
93                                         qq{$format $title body text});
94
95                                 if ('fancy' eq $podcast_style) {
96                                         isnt($enclosure, undef,
97                                                 qq{$format $title enclosure});
98                                         my $filename = basename($enclosure->url);
99                                         is($enclosure->type, $media_types{$filename},
100                                                 qq{$format $title enclosure type});
101                                         cmp_ok($enclosure->length, '>', 0,
102                                                 qq{$format $title enclosure length});
103                                 }
104                                 else {
105                                         is($enclosure, undef,
106                                                 qq{$format $title no enclosure});
107                                 }
108                         }
109                 }
110         }
111
112         ok(! system("rm -rf $tmp $statedir"), q{teardown});
113 }
114
115 sub single_page_html {
116         my @command = (qw(./ikiwiki.out));
117         push @command, qw(-underlaydir=underlays/basewiki);
118         push @command, qw(-set underlaydirbase=underlays -templatedir=templates);
119         push @command, qw(t/tinypodcast), "$tmp/out";
120
121         ok(! system("mkdir $tmp"),
122                 q{setup});
123         ok(! system(@command),
124                 q{build});
125
126         my $html = "$tmp/out/pianopost/index.html";
127         like(_extract_html_content($html, 'content'), qr/has content and/m,
128                 q{html body text});
129         like(_extract_html_content($html, 'enclosure'), qr/Download/m,
130                 q{html enclosure});
131         my ($href) = _extract_html_links($html, 'piano');
132         is($href, '/piano.mp3',
133                 q{html enclosure sans -url is site-absolute});
134
135         $html = "$tmp/out/attempted_multiple_enclosures/index.html";
136         like(_extract_html_content($html, 'content'), qr/has content and/m,
137                 q{html body text});
138         like(_extract_html_content($html, 'enclosure'), qr/Download/m,
139                 q{html enclosure});
140         ($href) = _extract_html_links($html, 'walter');
141         is($href, '/walter.ogg',
142                 q{html enclosure sans -url is site-absolute});
143
144         my $baseurl = 'http://example.com';
145         ok(! system(@command, "-url=$baseurl", q{--rebuild}));
146
147         $html = "$tmp/out/pianopost/index.html";
148         ($href) = _extract_html_links($html, 'piano');
149         is($href, "$baseurl/piano.mp3",
150                 q{html enclosure with -url is fully absolute});
151
152         $html = "$tmp/out/attempted_multiple_enclosures/index.html";
153         ($href) = _extract_html_links($html, 'walter');
154         is($href, "$baseurl/walter.ogg",
155                 q{html enclosure with -url is fully absolute});
156
157         ok(! system("rm -rf $tmp $statedir"), q{teardown});
158 }
159
160 sub inlined_pages_html {
161         my @command = (qw(./ikiwiki.out -plugin inline));
162         push @command, qw(-underlaydir=underlays/basewiki);
163         push @command, qw(-set underlaydirbase=underlays -templatedir=templates);
164         push @command, qw(t/tinypodcast), "$tmp/out";
165
166         ok(! system("mkdir $tmp"),
167                 q{setup});
168         ok(! system(@command),
169                 q{build});
170
171         my $html = "$tmp/out/fancy/index.html";
172         my $contents = _extract_html_content($html, 'content');
173         like($contents, qr/has content and an/m,
174                 q{html body text from pianopost});
175         like($contents, qr/has content and only one/m,
176                 q{html body text from attempted_multiple_enclosures});
177         my $enclosures = _extract_html_content($html, 'inlineenclosure');
178         like($enclosures, qr/Download/m,
179                 q{html enclosure});
180         my ($href) = _extract_html_links($html, 'piano.mp3');
181         is($href, '/piano.mp3',
182                 q{html enclosure from pianopost sans -url});
183         ($href) = _extract_html_links($html, 'walter.ogg');
184         is($href, '/walter.ogg',
185                 q{html enclosure from attempted_multiple_enclosures sans -url});
186
187         ok(! system("rm -rf $tmp $statedir"), q{teardown});
188 }
189
190 sub _extract_html_content {
191         my ($file, $desired_id, $desired_tag) = @_;
192         $desired_tag = 'div' unless defined $desired_tag;
193
194         my $p = HTML::Parser->new(api_version => 3);
195         my $content = '';
196
197         $p->handler(start => sub {
198                 my ($tag, $self, $attr) = @_;
199                 return if $tag ne $desired_tag;
200                 return unless exists $attr->{id} && $attr->{id} eq $desired_id;
201
202                 $self->handler(text => sub {
203                         my ($dtext) = @_;
204                         $content .= $dtext;
205                 }, "dtext");
206         }, "tagname,self,attr");
207
208         $p->parse_file($file) || die $!;
209
210         return $content;
211 }
212
213 sub _extract_html_links {
214         my ($file, $desired_value) = @_;
215
216         my @hrefs = ();
217
218         my $p = HTML::LinkExtor->new(sub {
219                 my ($tag, %attr) = @_;
220                 return if $tag ne 'a';
221                 return unless $attr{href} =~ qr/$desired_value/;
222                 push(@hrefs, values %attr);
223         }, getcwd() . '/' . $file);
224
225         $p->parse_file($file);
226
227         return @hrefs;
228 }
229
230 podcast('simple');
231 single_page_html();
232 inlined_pages_html();
233 podcast('fancy');