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