Make enclosure follow WikiLink LinkingRules.
[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};
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 => 78};
13         }
14 }
15
16 use Cwd;
17
18 my $tmp = 't/tmp';
19 my $statedir = 't/tinypodcast/.ikiwiki';
20
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";
27
28         ok(! system("mkdir $tmp"), q{setup});
29         ok(! system(@command), q{build});
30
31         my %media_types = (
32                 'simplepost'    => undef,
33                 'piano.mp3'     => 'audio/mpeg',
34                 'scroll.3gp'    => 'video/3gpp',
35                 'walter.ogg'    => 'video/x-theora+ogg',
36         );
37
38         for my $format (qw(atom rss)) {
39                 my $feed = XML::Feed->parse("$tmp/out/simple/index.$format");
40
41                 is($feed->title, 'simple',
42                         qq{$format feed title});
43                 is($feed->link, "$baseurl/simple/",
44                         qq{$format feed link});
45                 is($feed->description, 'wiki',
46                         qq{$format feed description});
47                 if ('atom' eq $format) {
48                         is($feed->author, $feed->description,
49                                 qq{$format feed author});
50                         is($feed->id, $feed->link,
51                                 qq{$format feed id});
52                         is($feed->generator, "ikiwiki",
53                                 qq{$format feed generator});
54                 }
55
56                 for my $entry ($feed->entries) {
57                         my $title = $entry->title;
58                         my $url = $entry->id;
59                         my $body = $entry->content->body;
60                         my $enclosure = $entry->enclosure;
61
62                         is($entry->link, $url, qq{$format $title link});
63                         isnt($entry->issued, undef,
64                                 qq{$format $title issued date});
65                         isnt($entry->modified, undef,
66                                 qq{$format $title modified date});
67
68                         if (defined $media_types{$title}) {
69                                 is($url, "$baseurl/$title",
70                                         qq{$format $title id});
71                                 is($body, undef,
72                                         qq{$format $title no body text});
73                                 is($enclosure->url, $url,
74                                         qq{$format $title enclosure url});
75                                 is($enclosure->type, $media_types{$title},
76                                         qq{$format $title enclosure type});
77                                 cmp_ok($enclosure->length, '>', 0,
78                                         qq{$format $title enclosure length});
79                         }
80                         else {
81                                 is($url, "$baseurl/$title/",
82                                         qq{$format $title id});
83                                 isnt($body, undef,
84                                         qq{$format $title body text});
85                                 is($enclosure, undef,
86                                         qq{$format $title no enclosure});
87                         }
88                 }
89         }
90
91         ok(! system("rm -rf $tmp $statedir"), q{teardown});
92 }
93
94 sub single_page_html {
95         my @command = (qw(./ikiwiki.out));
96         push @command, qw(-underlaydir=underlays/basewiki);
97         push @command, qw(-set underlaydirbase=underlays -templatedir=templates);
98         push @command, qw(t/tinypodcast), "$tmp/out";
99
100         ok(! system("mkdir $tmp"), q{setup});
101         ok(! system(@command), q{build});
102
103         my $html = "$tmp/out/pianopost/index.html";
104
105         my $body = _extract_html_content($html, 'content');
106         like($body, qr/article has content and/m, q{html body text});
107
108         my $enclosure = _extract_html_content($html, 'enclosure');
109         like($enclosure, qr/Download this episode/m, q{html enclosure});
110
111         my ($href) = _extract_html_links($html, 'piano');
112         ok(-f $href, q{html enclosure exists});
113
114         # XXX die if more than one enclosure is specified
115
116         ok(! system("rm -rf $tmp $statedir"), q{teardown});
117 }
118
119 sub _extract_html_content {
120         my ($file, $desired_id, $desired_tag) = @_;
121         $desired_tag = 'div' unless defined $desired_tag;
122
123         my $p = HTML::Parser->new(api_version => 3);
124         my $content = '';
125
126         $p->handler(start => sub {
127                 my ($tag, $self, $attr) = @_;
128                 return if $tag ne $desired_tag;
129                 return unless exists $attr->{id} && $attr->{id} eq $desired_id;
130
131                 $self->handler(text => sub {
132                         my ($dtext) = @_;
133                         $content .= $dtext;
134                 }, "dtext");
135
136                 $self->handler(end  => sub {
137                         my ($tag, $self) = @_;
138                         $self->eof if $tag eq $desired_tag;
139                 }, "tagname,self");
140         }, "tagname,self,attr");
141
142         $p->parse_file($file) || die $!;
143
144         return $content;
145 }
146
147 sub _extract_html_links {
148         my ($file, $desired_value) = @_;
149
150         my @hrefs = ();
151
152         my $p = HTML::LinkExtor->new(sub {
153                 my ($tag, %attr) = @_;
154                 return if $tag ne 'a';
155                 return unless $attr{href} =~ qr/$desired_value/;
156                 push(@hrefs, values %attr);
157         }, getcwd() . '/' . $file);
158
159         $p->parse_file($file);
160
161         return @hrefs;
162 }
163
164 simple_podcast();
165 single_page_html();