]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - t/podcast.t
Detect image type from .JPG just like .jpg (etc.).
[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 => 136};
13         }
14 }
16 use Cwd;
17 use File::Basename;
19 my $tmp = 't/tmp';
20 my $statedir = 't/tinypodcast/.ikiwiki';
22 sub podcast {
23         my $podcast_style = shift;
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";
31         ok(! system("mkdir $tmp"),
32                 q{setup});
33         ok(! system(@command),
34                 q{build});
36         my %media_types = (
37                 'simplepost'    => undef,
38                 'piano.mp3'     => 'audio/mpeg',
39                 'scroll.3gp'    => 'video/3gpp',
40                 'walter.ogg'    => 'audio/ogg',
41         );
43         for my $format (qw(atom rss)) {
44                 my $feed = XML::Feed->parse("$tmp/out/$podcast_style/index.$format");
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                 }
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;
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});
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;
90                                 is($url, $expected_id,
91                                         qq{$format $title id});
92                                 isnt($body, undef,
93                                         qq{$format $title body text});
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         }
112         ok(! system("rm -rf $tmp $statedir"), q{teardown});
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";
121         ok(! system("mkdir $tmp"),
122                 q{setup});
123         ok(! system(@command),
124                 q{build});
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});
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});
144         my $baseurl = 'http://example.com';
145         ok(! system(@command, "-url=$baseurl", q{--rebuild}));
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});
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});
157         ok(! system("rm -rf $tmp $statedir"), q{teardown});
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";
166         ok(! system("mkdir $tmp"),
167                 q{setup});
168         ok(! system(@command),
169                 q{build});
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});
187         ok(! system("rm -rf $tmp $statedir"), q{teardown});
190 sub _extract_html_content {
191         my ($file, $desired_id, $desired_tag) = @_;
192         $desired_tag = 'div' unless defined $desired_tag;
194         my $p = HTML::Parser->new(api_version => 3);
195         my $content = '';
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;
202                 $self->handler(text => sub {
203                         my ($dtext) = @_;
204                         $content .= $dtext;
205                 }, "dtext");
206         }, "tagname,self,attr");
208         $p->parse_file($file) || die $!;
210         return $content;
213 sub _extract_html_links {
214         my ($file, $desired_value) = @_;
216         my @hrefs = ();
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);
225         $p->parse_file($file);
227         return @hrefs;
230 podcast('simple');
231 single_page_html();
232 inlined_pages_html();
233 podcast('fancy');