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};
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 = ("perl", "-I".getcwd, qw(./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("rm -rf $tmp $statedir"), q{setup});
46 ok(! system("mkdir $tmp"),
48 ok(! system(@command),
52 'simplepost' => undef,
53 'piano.mp3' => 'audio/mpeg',
54 'scroll.3gp' => 'video/3gpp',
55 'walter.ogg' => 'audio/ogg',
58 for my $format (qw(atom rss)) {
59 my $feed = XML::Feed->parse("$tmp/out/$podcast_style/index.$format");
61 is($feed->title, $podcast_style,
62 qq{$format feed title});
63 is($feed->link, "$baseurl/$podcast_style/",
64 qq{$format feed link});
65 is($feed->description, 'wiki',
66 qq{$format feed description});
67 if ('atom' eq $format) {
68 is($feed->author, $feed->description,
69 qq{$format feed author});
70 is($feed->id, $feed->link,
72 is($feed->generator, "ikiwiki",
73 qq{$format feed generator});
76 for my $entry ($feed->entries) {
77 my $title = $entry->title;
79 my $body = $entry->content->body;
80 my $enclosure = $entry->enclosure;
82 is($entry->link, $url, qq{$format $title link});
83 isnt($entry->issued, undef,
84 qq{$format $title issued date});
85 isnt($entry->modified, undef,
86 qq{$format $title modified date});
88 if (defined $media_types{$title}) {
89 is($url, "$baseurl/$title",
90 qq{$format $title id});
92 qq{$format $title no body text});
93 is($enclosure->url, $url,
94 qq{$format $title enclosure url});
95 is($enclosure->type, $media_types{$title},
96 qq{$format $title enclosure type});
97 cmp_ok($enclosure->length, '>', 0,
98 qq{$format $title enclosure length});
102 my $expected_id = "$baseurl/$title/";
103 $expected_id =~ s/\ /_/g;
105 is($url, $expected_id,
106 qq{$format $title id});
108 qq{$format $title body text});
110 if ('fancy' eq $podcast_style) {
111 isnt($enclosure, undef,
112 qq{$format $title enclosure});
113 my $filename = basename($enclosure->url);
114 is($enclosure->type, $media_types{$filename},
115 qq{$format $title enclosure type});
116 cmp_ok($enclosure->length, '>', 0,
117 qq{$format $title enclosure length});
120 is($enclosure, undef,
121 qq{$format $title no enclosure});
128 sub single_page_html {
129 my @command = @base_command;
130 push @command, qw(t/tinypodcast), "$tmp/out";
132 ok(! system("rm -rf $tmp $statedir"), q{setup});
134 ok(! system("mkdir $tmp"),
136 ok(! system(@command),
139 my $html = "$tmp/out/pianopost/index.html";
140 like(_extract_html_content($html, 'content'), qr/has content and/m,
142 like(_extract_html_content($html, 'enclosure'), qr/Download/m,
144 my ($href) = _extract_html_links($html, 'piano');
145 is($href, '/piano.mp3',
146 q{html enclosure sans --url is site-absolute});
148 $html = "$tmp/out/attempted_multiple_enclosures/index.html";
149 like(_extract_html_content($html, 'content'), qr/has content and/m,
151 like(_extract_html_content($html, 'enclosure'), qr/Download/m,
153 ($href) = _extract_html_links($html, 'walter');
154 is($href, '/walter.ogg',
155 q{html enclosure sans --url is site-absolute});
157 my $baseurl = 'http://example.com';
158 ok(! system(@command, "--url=$baseurl", q{--rebuild}));
160 $html = "$tmp/out/pianopost/index.html";
161 ($href) = _extract_html_links($html, 'piano');
162 is($href, "$baseurl/piano.mp3",
163 q{html enclosure with --url is fully absolute});
165 $html = "$tmp/out/attempted_multiple_enclosures/index.html";
166 ($href) = _extract_html_links($html, 'walter');
167 is($href, "$baseurl/walter.ogg",
168 q{html enclosure with --url is fully absolute});
170 ok(! system("rm -rf $tmp $statedir"), q{teardown});
173 sub inlined_pages_html {
174 my @command = (@base_command, qw(--plugin inline));
175 push @command, qw(t/tinypodcast), "$tmp/out";
177 ok(! system("rm -rf $tmp $statedir"), q{setup});
179 ok(! system("mkdir $tmp"),
181 ok(! system(@command),
184 my $html = "$tmp/out/fancy/index.html";
185 my $contents = _extract_html_content($html, 'content');
186 like($contents, qr/has content and an/m,
187 q{html body text from pianopost});
188 like($contents, qr/has content and only one/m,
189 q{html body text from attempted_multiple_enclosures});
190 my $enclosures = _extract_html_content($html, 'inlineenclosure');
191 like($enclosures, qr/Download/m,
193 my ($href) = _extract_html_links($html, 'piano.mp3');
194 is($href, '/piano.mp3',
195 q{html enclosure from pianopost sans --url});
196 ($href) = _extract_html_links($html, 'walter.ogg');
197 is($href, '/walter.ogg',
198 q{html enclosure from attempted_multiple_enclosures sans --url});
200 ok(! system("rm -rf $tmp $statedir"), q{teardown});
203 sub _extract_html_content {
204 my ($file, $desired_id, $desired_tag) = @_;
205 $desired_tag = 'div' unless defined $desired_tag;
207 my $p = HTML::Parser->new(api_version => 3);
210 $p->handler(start => sub {
211 my ($tag, $self, $attr) = @_;
212 return if $tag ne $desired_tag;
213 return unless exists $attr->{id} && $attr->{id} eq $desired_id;
215 $self->handler(text => sub {
219 }, "tagname,self,attr");
221 $p->parse_file($file) || die $!;
226 sub _extract_html_links {
227 my ($file, $desired_value) = @_;
231 my $p = HTML::LinkExtor->new(sub {
232 my ($tag, %attr) = @_;
233 return if $tag ne 'a';
234 return unless $attr{href} =~ qr/$desired_value/;
235 push(@hrefs, values %attr);
236 }, getcwd() . '/' . $file);
238 $p->parse_file($file);
245 inlined_pages_html();