6 eval q{use XML::Feed; use HTML::Parser; use HTML::LinkExtor; use File::MimeInfo};
8 eval q{use Test::More skip_all =>
9 "XML::Feed and/or HTML::Parser or File::MimeInfo not available"};
12 eval q{use Test::More tests => 137};
19 my $installed = $ENV{INSTALLED_TESTS};
23 ok(1, "running installed");
24 @base_command = qw(ikiwiki);
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);
35 my $statedir = 't/tinypodcast/.ikiwiki';
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"),
46 ok(! system(@command),
50 'simplepost' => undef,
51 'piano.mp3' => 'audio/mpeg',
52 'scroll.3gp' => 'video/3gpp',
53 'walter.ogg' => 'audio/ogg',
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,
70 is($feed->generator, "ikiwiki",
71 qq{$format feed generator});
74 for my $entry ($feed->entries) {
75 my $title = $entry->title;
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});
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});
100 my $expected_id = "$baseurl/$title/";
101 $expected_id =~ s/\ /_/g;
103 is($url, $expected_id,
104 qq{$format $title id});
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});
118 is($enclosure, undef,
119 qq{$format $title no enclosure});
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"),
134 ok(! system(@command),
137 my $html = "$tmp/out/pianopost/index.html";
138 like(_extract_html_content($html, 'content'), qr/has content and/m,
140 like(_extract_html_content($html, 'enclosure'), qr/Download/m,
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,
149 like(_extract_html_content($html, 'enclosure'), qr/Download/m,
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"),
177 ok(! system(@command),
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,
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);
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 {
215 }, "tagname,self,attr");
217 $p->parse_file($file) || die $!;
222 sub _extract_html_links {
223 my ($file, $desired_value) = @_;
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);
241 inlined_pages_html();