]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - t/podcast.t
img: make img_allowed_formats case-insensitive
[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 tests => 137};
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 = qw(perl -I. ./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("mkdir $tmp"),
45                 q{setup});
46         ok(! system(@command),
47                 q{build});
49         my %media_types = (
50                 'simplepost'    => undef,
51                 'piano.mp3'     => 'audio/mpeg',
52                 'scroll.3gp'    => 'video/3gpp',
53                 'walter.ogg'    => 'audio/ogg',
54         );
56         for my $format (qw(atom rss)) {
57                 my $feed = XML::Feed->parse("$tmp/out/$podcast_style/index.$format");
59                 is($feed->title, $podcast_style,
60                         qq{$format feed title});
61                 is($feed->link, "$baseurl/$podcast_style/",
62                         qq{$format feed link});
63                 is($feed->description, 'wiki',
64                         qq{$format feed description});
65                 if ('atom' eq $format) {
66                         is($feed->author, $feed->description,
67                                 qq{$format feed author});
68                         is($feed->id, $feed->link,
69                                 qq{$format feed id});
70                         is($feed->generator, "ikiwiki",
71                                 qq{$format feed generator});
72                 }
74                 for my $entry ($feed->entries) {
75                         my $title = $entry->title;
76                         my $url = $entry->id;
77                         my $body = $entry->content->body;
78                         my $enclosure = $entry->enclosure;
80                         is($entry->link, $url, qq{$format $title link});
81                         isnt($entry->issued, undef,
82                                 qq{$format $title issued date});
83                         isnt($entry->modified, undef,
84                                 qq{$format $title modified date});
86                         if (defined $media_types{$title}) {
87                                 is($url, "$baseurl/$title",
88                                         qq{$format $title id});
89                                 is($body, undef,
90                                         qq{$format $title no body text});
91                                 is($enclosure->url, $url,
92                                         qq{$format $title enclosure url});
93                                 is($enclosure->type, $media_types{$title},
94                                         qq{$format $title enclosure type});
95                                 cmp_ok($enclosure->length, '>', 0,
96                                         qq{$format $title enclosure length});
97                         }
98                         else {
99                                 # XXX hack hack hack
100                                 my $expected_id = "$baseurl/$title/";
101                                 $expected_id =~ s/\ /_/g;
103                                 is($url, $expected_id,
104                                         qq{$format $title id});
105                                 isnt($body, undef,
106                                         qq{$format $title body text});
108                                 if ('fancy' eq $podcast_style) {
109                                         isnt($enclosure, undef,
110                                                 qq{$format $title enclosure});
111                                         my $filename = basename($enclosure->url);
112                                         is($enclosure->type, $media_types{$filename},
113                                                 qq{$format $title enclosure type});
114                                         cmp_ok($enclosure->length, '>', 0,
115                                                 qq{$format $title enclosure length});
116                                 }
117                                 else {
118                                         is($enclosure, undef,
119                                                 qq{$format $title no enclosure});
120                                 }
121                         }
122                 }
123         }
125         ok(! system("rm -rf $tmp $statedir"), q{teardown});
128 sub single_page_html {
129         my @command = @base_command;
130         push @command, qw(t/tinypodcast), "$tmp/out";
132         ok(! system("mkdir $tmp"),
133                 q{setup});
134         ok(! system(@command),
135                 q{build});
137         my $html = "$tmp/out/pianopost/index.html";
138         like(_extract_html_content($html, 'content'), qr/has content and/m,
139                 q{html body text});
140         like(_extract_html_content($html, 'enclosure'), qr/Download/m,
141                 q{html enclosure});
142         my ($href) = _extract_html_links($html, 'piano');
143         is($href, '/piano.mp3',
144                 q{html enclosure sans --url is site-absolute});
146         $html = "$tmp/out/attempted_multiple_enclosures/index.html";
147         like(_extract_html_content($html, 'content'), qr/has content and/m,
148                 q{html body text});
149         like(_extract_html_content($html, 'enclosure'), qr/Download/m,
150                 q{html enclosure});
151         ($href) = _extract_html_links($html, 'walter');
152         is($href, '/walter.ogg',
153                 q{html enclosure sans --url is site-absolute});
155         my $baseurl = 'http://example.com';
156         ok(! system(@command, "--url=$baseurl", q{--rebuild}));
158         $html = "$tmp/out/pianopost/index.html";
159         ($href) = _extract_html_links($html, 'piano');
160         is($href, "$baseurl/piano.mp3",
161                 q{html enclosure with --url is fully absolute});
163         $html = "$tmp/out/attempted_multiple_enclosures/index.html";
164         ($href) = _extract_html_links($html, 'walter');
165         is($href, "$baseurl/walter.ogg",
166                 q{html enclosure with --url is fully absolute});
168         ok(! system("rm -rf $tmp $statedir"), q{teardown});
171 sub inlined_pages_html {
172         my @command = (@base_command, qw(--plugin inline));
173         push @command, qw(t/tinypodcast), "$tmp/out";
175         ok(! system("mkdir $tmp"),
176                 q{setup});
177         ok(! system(@command),
178                 q{build});
180         my $html = "$tmp/out/fancy/index.html";
181         my $contents = _extract_html_content($html, 'content');
182         like($contents, qr/has content and an/m,
183                 q{html body text from pianopost});
184         like($contents, qr/has content and only one/m,
185                 q{html body text from attempted_multiple_enclosures});
186         my $enclosures = _extract_html_content($html, 'inlineenclosure');
187         like($enclosures, qr/Download/m,
188                 q{html enclosure});
189         my ($href) = _extract_html_links($html, 'piano.mp3');
190         is($href, '/piano.mp3',
191                 q{html enclosure from pianopost sans --url});
192         ($href) = _extract_html_links($html, 'walter.ogg');
193         is($href, '/walter.ogg',
194                 q{html enclosure from attempted_multiple_enclosures sans --url});
196         ok(! system("rm -rf $tmp $statedir"), q{teardown});
199 sub _extract_html_content {
200         my ($file, $desired_id, $desired_tag) = @_;
201         $desired_tag = 'div' unless defined $desired_tag;
203         my $p = HTML::Parser->new(api_version => 3);
204         my $content = '';
206         $p->handler(start => sub {
207                 my ($tag, $self, $attr) = @_;
208                 return if $tag ne $desired_tag;
209                 return unless exists $attr->{id} && $attr->{id} eq $desired_id;
211                 $self->handler(text => sub {
212                         my ($dtext) = @_;
213                         $content .= $dtext;
214                 }, "dtext");
215         }, "tagname,self,attr");
217         $p->parse_file($file) || die $!;
219         return $content;
222 sub _extract_html_links {
223         my ($file, $desired_value) = @_;
225         my @hrefs = ();
227         my $p = HTML::LinkExtor->new(sub {
228                 my ($tag, %attr) = @_;
229                 return if $tag ne 'a';
230                 return unless $attr{href} =~ qr/$desired_value/;
231                 push(@hrefs, values %attr);
232         }, getcwd() . '/' . $file);
234         $p->parse_file($file);
236         return @hrefs;
239 podcast('simple');
240 single_page_html();
241 inlined_pages_html();
242 podcast('fancy');