]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - t/podcast.t
Make enclosures absolute (in feeds they have to be).
[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};
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 => 92};
13         }
14 }
16 use Cwd;
18 my $tmp = 't/tmp';
19 my $statedir = 't/tinypodcast/.ikiwiki';
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";
28         ok(! system("mkdir $tmp"),
29                 q{setup});
30         ok(! system(@command),
31                 q{build});
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         );
40         for my $format (qw(atom rss)) {
41                 my $feed = XML::Feed->parse("$tmp/out/simple/index.$format");
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                 }
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;
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});
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                                 is($enclosure->url, $url,
76                                         qq{$format $title enclosure url});
77                                 is($enclosure->type, $media_types{$title},
78                                         qq{$format $title enclosure type});
79                                 cmp_ok($enclosure->length, '>', 0,
80                                         qq{$format $title enclosure length});
81                         }
82                         else {
83                                 is($url, "$baseurl/$title/",
84                                         qq{$format $title id});
85                                 isnt($body, undef,
86                                         qq{$format $title body text});
87                                 is($enclosure, undef,
88                                         qq{$format $title no enclosure});
89                         }
90                 }
91         }
93         ok(! system("rm -rf $tmp $statedir"), q{teardown});
94 }
96 sub single_page_html {
97         my @command = (qw(./ikiwiki.out));
98         push @command, qw(-underlaydir=underlays/basewiki);
99         push @command, qw(-set underlaydirbase=underlays -templatedir=templates);
100         push @command, qw(t/tinypodcast), "$tmp/out";
102         ok(! system("mkdir $tmp"),
103                 q{setup});
104         ok(! system(@command),
105                 q{build});
107         my $html = "$tmp/out/pianopost/index.html";
108         like(_extract_html_content($html, 'content'), qr/has content and/m,
109                 q{html body text});
110         like(_extract_html_content($html, 'enclosure'), qr/this episode/m,
111                 q{html enclosure});
112         my ($href) = _extract_html_links($html, 'piano');
113         is($href, '/piano.mp3',
114                 q{html enclosure sans -url is site-absolute});
116         $html = "$tmp/out/attempted_multiple_enclosures/index.html";
117         like(_extract_html_content($html, 'content'), qr/has content and/m,
118                 q{html body text});
119         like(_extract_html_content($html, 'enclosure'), qr/this episode/m,
120                 q{html enclosure});
121         ($href) = _extract_html_links($html, 'walter');
122         is($href, '/walter.ogg',
123                 q{html enclosure sans -url is site-absolute});
125         my $baseurl = 'http://example.com';
126         ok(! system(@command, "-url=$baseurl", q{--rebuild}));
128         $html = "$tmp/out/pianopost/index.html";
129         ($href) = _extract_html_links($html, 'piano');
130         is($href, "$baseurl/piano.mp3",
131                 q{html enclosure with -url is fully absolute});
133         $html = "$tmp/out/attempted_multiple_enclosures/index.html";
134         ($href) = _extract_html_links($html, 'walter');
135         is($href, "$baseurl/walter.ogg",
136                 q{html enclosure with -url is fully absolute});
138         ok(! system("rm -rf $tmp $statedir"), q{teardown});
141 sub inlined_pages_html {
142         my @command = (qw(./ikiwiki.out -plugin inline));
143         push @command, qw(-underlaydir=underlays/basewiki);
144         push @command, qw(-set underlaydirbase=underlays -templatedir=templates);
145         push @command, qw(t/tinypodcast), "$tmp/out";
147         ok(! system("mkdir $tmp"),
148                 q{setup});
149         ok(! system(@command),
150                 q{build});
152         my $html = "$tmp/out/fancy/index.html";
153         my $contents = _extract_html_content($html, 'content');
154         like($contents, qr/has content and an/m,
155                 q{html body text from pianopost});
156         like($contents, qr/has content and only one/m,
157                 q{html body text from attempted_multiple_enclosures});
158         my $enclosures = _extract_html_content($html, 'inlineenclosure');
159         like($enclosures, qr/this episode/m,
160                 q{html enclosure});
161         my ($href) = _extract_html_links($html, 'piano.mp3');
162         is($href, '/piano.mp3',
163                 q{html enclosure from pianopost sans -url});
164         ($href) = _extract_html_links($html, 'walter.ogg');
165         is($href, '/walter.ogg',
166                 q{html enclosure from attempted_multiple_enclosures sans -url});
168         ok(! system("rm -rf $tmp $statedir"), q{teardown});
171 sub _extract_html_content {
172         my ($file, $desired_id, $desired_tag) = @_;
173         $desired_tag = 'div' unless defined $desired_tag;
175         my $p = HTML::Parser->new(api_version => 3);
176         my $content = '';
178         $p->handler(start => sub {
179                 my ($tag, $self, $attr) = @_;
180                 return if $tag ne $desired_tag;
181                 return unless exists $attr->{id} && $attr->{id} eq $desired_id;
183                 $self->handler(text => sub {
184                         my ($dtext) = @_;
185                         $content .= $dtext;
186                 }, "dtext");
187         }, "tagname,self,attr");
189         $p->parse_file($file) || die $!;
191         return $content;
194 sub _extract_html_links {
195         my ($file, $desired_value) = @_;
197         my @hrefs = ();
199         my $p = HTML::LinkExtor->new(sub {
200                 my ($tag, %attr) = @_;
201                 return if $tag ne 'a';
202                 return unless $attr{href} =~ qr/$desired_value/;
203                 push(@hrefs, values %attr);
204         }, getcwd() . '/' . $file);
206         $p->parse_file($file);
208         return @hrefs;
211 simple_podcast();
212 single_page_html();
213 inlined_pages_html();