]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - t/podcast.t
Add ikistrap plugin for ikistrap theme.
[git.ikiwiki.info.git] / t / podcast.t
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
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};
13         }
14 }
16 use Cwd;
17 use File::Basename;
19 my $installed = $ENV{INSTALLED_TESTS};
21 my @base_command;
22 if ($installed) {
23         ok(1, "running installed");
24         @base_command = qw(ikiwiki);
25 }
26 else {
27         ok(! system("make -s ikiwiki.out"));
28         @base_command = ("perl", "-I".getcwd, qw(./ikiwiki.out
29                 --underlaydir=underlays/basewiki
30                 --set underlaydirbase=underlays
31                 --templatedir=templates));
32 }
34 my $tmp = 't/tmp';
35 my $statedir = 't/tinypodcast/.ikiwiki';
37 sub podcast {
38         my $podcast_style = shift;
40         my $baseurl = 'http://example.com';
41         my @command = (@base_command, qw(--plugin inline --rss --atom));
42         push @command, "--url=$baseurl", qw(t/tinypodcast), "$tmp/out";
44         ok(! system("rm -rf $tmp $statedir"), q{setup});
46         ok(! system("mkdir $tmp"),
47                 q{setup});
48         ok(! system(@command),
49                 q{build});
51         my %media_types = (
52                 'simplepost'    => undef,
53                 'piano.mp3'     => 'audio/mpeg',
54                 'scroll.3gp'    => 'video/3gpp',
55                 'walter.ogg'    => 'audio/ogg',
56         );
58         for my $format (qw(atom rss)) {
59                 my $feed = XML::Feed->parse("$tmp/out/$podcast_style/index.$format");
61                 is($feed->title, $podcast_style,
62                         qq{$format feed title});
63                 is($feed->link, "$baseurl/$podcast_style/",
64                         qq{$format feed link});
65                 is($feed->description, 'wiki',
66                         qq{$format feed description});
67                 if ('atom' eq $format) {
68                         is($feed->author, $feed->description,
69                                 qq{$format feed author});
70                         is($feed->id, $feed->link,
71                                 qq{$format feed id});
72                         is($feed->generator, "ikiwiki",
73                                 qq{$format feed generator});
74                 }
76                 for my $entry ($feed->entries) {
77                         my $title = $entry->title;
78                         my $url = $entry->id;
79                         my $body = $entry->content->body;
80                         my $enclosure = $entry->enclosure;
82                         is($entry->link, $url, qq{$format $title link});
83                         isnt($entry->issued, undef,
84                                 qq{$format $title issued date});
85                         isnt($entry->modified, undef,
86                                 qq{$format $title modified date});
88                         if (defined $media_types{$title}) {
89                                 is($url, "$baseurl/$title",
90                                         qq{$format $title id});
91                                 is($body, undef,
92                                         qq{$format $title no body text});
93                                 is($enclosure->url, $url,
94                                         qq{$format $title enclosure url});
95                                 is($enclosure->type, $media_types{$title},
96                                         qq{$format $title enclosure type});
97                                 cmp_ok($enclosure->length, '>', 0,
98                                         qq{$format $title enclosure length});
99                         }
100                         else {
101                                 # XXX hack hack hack
102                                 my $expected_id = "$baseurl/$title/";
103                                 $expected_id =~ s/\ /_/g;
105                                 is($url, $expected_id,
106                                         qq{$format $title id});
107                                 isnt($body, undef,
108                                         qq{$format $title body text});
110                                 if ('fancy' eq $podcast_style) {
111                                         isnt($enclosure, undef,
112                                                 qq{$format $title enclosure});
113                                         my $filename = basename($enclosure->url);
114                                         is($enclosure->type, $media_types{$filename},
115                                                 qq{$format $title enclosure type});
116                                         cmp_ok($enclosure->length, '>', 0,
117                                                 qq{$format $title enclosure length});
118                                 }
119                                 else {
120                                         is($enclosure, undef,
121                                                 qq{$format $title no enclosure});
122                                 }
123                         }
124                 }
125         }
128 sub single_page_html {
129         my @command = @base_command;
130         push @command, qw(t/tinypodcast), "$tmp/out";
132         ok(! system("rm -rf $tmp $statedir"), q{setup});
134         ok(! system("mkdir $tmp"),
135                 q{setup});
136         ok(! system(@command),
137                 q{build});
139         my $html = "$tmp/out/pianopost/index.html";
140         like(_extract_html_content($html, 'content'), qr/has content and/m,
141                 q{html body text});
142         like(_extract_html_content($html, 'enclosure'), qr/Download/m,
143                 q{html enclosure});
144         my ($href) = _extract_html_links($html, 'piano');
145         is($href, '/piano.mp3',
146                 q{html enclosure sans --url is site-absolute});
148         $html = "$tmp/out/attempted_multiple_enclosures/index.html";
149         like(_extract_html_content($html, 'content'), qr/has content and/m,
150                 q{html body text});
151         like(_extract_html_content($html, 'enclosure'), qr/Download/m,
152                 q{html enclosure});
153         ($href) = _extract_html_links($html, 'walter');
154         is($href, '/walter.ogg',
155                 q{html enclosure sans --url is site-absolute});
157         my $baseurl = 'http://example.com';
158         ok(! system(@command, "--url=$baseurl", q{--rebuild}));
160         $html = "$tmp/out/pianopost/index.html";
161         ($href) = _extract_html_links($html, 'piano');
162         is($href, "$baseurl/piano.mp3",
163                 q{html enclosure with --url is fully absolute});
165         $html = "$tmp/out/attempted_multiple_enclosures/index.html";
166         ($href) = _extract_html_links($html, 'walter');
167         is($href, "$baseurl/walter.ogg",
168                 q{html enclosure with --url is fully absolute});
170         ok(! system("rm -rf $tmp $statedir"), q{teardown});
173 sub inlined_pages_html {
174         my @command = (@base_command, qw(--plugin inline));
175         push @command, qw(t/tinypodcast), "$tmp/out";
177         ok(! system("rm -rf $tmp $statedir"), q{setup});
179         ok(! system("mkdir $tmp"),
180                 q{setup});
181         ok(! system(@command),
182                 q{build});
184         my $html = "$tmp/out/fancy/index.html";
185         my $contents = _extract_html_content($html, 'content');
186         like($contents, qr/has content and an/m,
187                 q{html body text from pianopost});
188         like($contents, qr/has content and only one/m,
189                 q{html body text from attempted_multiple_enclosures});
190         my $enclosures = _extract_html_content($html, 'inlineenclosure');
191         like($enclosures, qr/Download/m,
192                 q{html enclosure});
193         my ($href) = _extract_html_links($html, 'piano.mp3');
194         is($href, '/piano.mp3',
195                 q{html enclosure from pianopost sans --url});
196         ($href) = _extract_html_links($html, 'walter.ogg');
197         is($href, '/walter.ogg',
198                 q{html enclosure from attempted_multiple_enclosures sans --url});
200         ok(! system("rm -rf $tmp $statedir"), q{teardown});
203 sub _extract_html_content {
204         my ($file, $desired_id, $desired_tag) = @_;
205         $desired_tag = 'div' unless defined $desired_tag;
207         my $p = HTML::Parser->new(api_version => 3);
208         my $content = '';
210         $p->handler(start => sub {
211                 my ($tag, $self, $attr) = @_;
212                 return if $tag ne $desired_tag;
213                 return unless exists $attr->{id} && $attr->{id} eq $desired_id;
215                 $self->handler(text => sub {
216                         my ($dtext) = @_;
217                         $content .= $dtext;
218                 }, "dtext");
219         }, "tagname,self,attr");
221         $p->parse_file($file) || die $!;
223         return $content;
226 sub _extract_html_links {
227         my ($file, $desired_value) = @_;
229         my @hrefs = ();
231         my $p = HTML::LinkExtor->new(sub {
232                 my ($tag, %attr) = @_;
233                 return if $tag ne 'a';
234                 return unless $attr{href} =~ qr/$desired_value/;
235                 push(@hrefs, values %attr);
236         }, getcwd() . '/' . $file);
238         $p->parse_file($file);
240         return @hrefs;
243 podcast('simple');
244 single_page_html();
245 inlined_pages_html();
246 podcast('fancy');
248 done_testing;