6 eval q{use XML::Feed; use HTML::Parser; use HTML::LinkExtor};
8 eval q{use Test::More skip_all =>
9 "XML::Feed and/or HTML::Parser not available"};
12 eval q{use Test::More tests => 136};
20 my $statedir = 't/tinypodcast/.ikiwiki';
23 my $podcast_style = shift;
25 my $baseurl = 'http://example.com';
26 my @command = (qw(./ikiwiki.out -plugin inline -rss -atom));
27 push @command, qw(-underlaydir=underlays/basewiki);
28 push @command, qw(-set underlaydirbase=underlays -templatedir=templates);
29 push @command, "-url=$baseurl", qw(t/tinypodcast), "$tmp/out";
31 ok(! system("mkdir $tmp"),
33 ok(! system(@command),
37 'simplepost' => undef,
38 'piano.mp3' => 'audio/mpeg',
39 'scroll.3gp' => 'video/3gpp',
40 'walter.ogg' => 'video/x-theora+ogg',
43 for my $format (qw(atom rss)) {
44 my $feed = XML::Feed->parse("$tmp/out/$podcast_style/index.$format");
46 is($feed->title, $podcast_style,
47 qq{$format feed title});
48 is($feed->link, "$baseurl/$podcast_style/",
49 qq{$format feed link});
50 is($feed->description, 'wiki',
51 qq{$format feed description});
52 if ('atom' eq $format) {
53 is($feed->author, $feed->description,
54 qq{$format feed author});
55 is($feed->id, $feed->link,
57 is($feed->generator, "ikiwiki",
58 qq{$format feed generator});
61 for my $entry ($feed->entries) {
62 my $title = $entry->title;
64 my $body = $entry->content->body;
65 my $enclosure = $entry->enclosure;
67 is($entry->link, $url, qq{$format $title link});
68 isnt($entry->issued, undef,
69 qq{$format $title issued date});
70 isnt($entry->modified, undef,
71 qq{$format $title modified date});
73 if (defined $media_types{$title}) {
74 is($url, "$baseurl/$title",
75 qq{$format $title id});
77 qq{$format $title no body text});
78 is($enclosure->url, $url,
79 qq{$format $title enclosure url});
80 is($enclosure->type, $media_types{$title},
81 qq{$format $title enclosure type});
82 cmp_ok($enclosure->length, '>', 0,
83 qq{$format $title enclosure length});
87 my $expected_id = "$baseurl/$title/";
88 $expected_id =~ s/\ /_/g;
90 is($url, $expected_id,
91 qq{$format $title id});
93 qq{$format $title body text});
95 if ('fancy' eq $podcast_style) {
96 isnt($enclosure, undef,
97 qq{$format $title enclosure});
98 my $filename = basename($enclosure->url);
99 is($enclosure->type, $media_types{$filename},
100 qq{$format $title enclosure type});
101 cmp_ok($enclosure->length, '>', 0,
102 qq{$format $title enclosure length});
105 is($enclosure, undef,
106 qq{$format $title no enclosure});
112 ok(! system("rm -rf $tmp $statedir"), q{teardown});
115 sub single_page_html {
116 my @command = (qw(./ikiwiki.out));
117 push @command, qw(-underlaydir=underlays/basewiki);
118 push @command, qw(-set underlaydirbase=underlays -templatedir=templates);
119 push @command, qw(t/tinypodcast), "$tmp/out";
121 ok(! system("mkdir $tmp"),
123 ok(! system(@command),
126 my $html = "$tmp/out/pianopost/index.html";
127 like(_extract_html_content($html, 'content'), qr/has content and/m,
129 like(_extract_html_content($html, 'enclosure'), qr/this episode/m,
131 my ($href) = _extract_html_links($html, 'piano');
132 is($href, '/piano.mp3',
133 q{html enclosure sans -url is site-absolute});
135 $html = "$tmp/out/attempted_multiple_enclosures/index.html";
136 like(_extract_html_content($html, 'content'), qr/has content and/m,
138 like(_extract_html_content($html, 'enclosure'), qr/this episode/m,
140 ($href) = _extract_html_links($html, 'walter');
141 is($href, '/walter.ogg',
142 q{html enclosure sans -url is site-absolute});
144 my $baseurl = 'http://example.com';
145 ok(! system(@command, "-url=$baseurl", q{--rebuild}));
147 $html = "$tmp/out/pianopost/index.html";
148 ($href) = _extract_html_links($html, 'piano');
149 is($href, "$baseurl/piano.mp3",
150 q{html enclosure with -url is fully absolute});
152 $html = "$tmp/out/attempted_multiple_enclosures/index.html";
153 ($href) = _extract_html_links($html, 'walter');
154 is($href, "$baseurl/walter.ogg",
155 q{html enclosure with -url is fully absolute});
157 ok(! system("rm -rf $tmp $statedir"), q{teardown});
160 sub inlined_pages_html {
161 my @command = (qw(./ikiwiki.out -plugin inline));
162 push @command, qw(-underlaydir=underlays/basewiki);
163 push @command, qw(-set underlaydirbase=underlays -templatedir=templates);
164 push @command, qw(t/tinypodcast), "$tmp/out";
166 ok(! system("mkdir $tmp"),
168 ok(! system(@command),
171 my $html = "$tmp/out/fancy/index.html";
172 my $contents = _extract_html_content($html, 'content');
173 like($contents, qr/has content and an/m,
174 q{html body text from pianopost});
175 like($contents, qr/has content and only one/m,
176 q{html body text from attempted_multiple_enclosures});
177 my $enclosures = _extract_html_content($html, 'inlineenclosure');
178 like($enclosures, qr/this episode/m,
180 my ($href) = _extract_html_links($html, 'piano.mp3');
181 is($href, '/piano.mp3',
182 q{html enclosure from pianopost sans -url});
183 ($href) = _extract_html_links($html, 'walter.ogg');
184 is($href, '/walter.ogg',
185 q{html enclosure from attempted_multiple_enclosures sans -url});
187 ok(! system("rm -rf $tmp $statedir"), q{teardown});
190 sub _extract_html_content {
191 my ($file, $desired_id, $desired_tag) = @_;
192 $desired_tag = 'div' unless defined $desired_tag;
194 my $p = HTML::Parser->new(api_version => 3);
197 $p->handler(start => sub {
198 my ($tag, $self, $attr) = @_;
199 return if $tag ne $desired_tag;
200 return unless exists $attr->{id} && $attr->{id} eq $desired_id;
202 $self->handler(text => sub {
206 }, "tagname,self,attr");
208 $p->parse_file($file) || die $!;
213 sub _extract_html_links {
214 my ($file, $desired_value) = @_;
218 my $p = HTML::LinkExtor->new(sub {
219 my ($tag, %attr) = @_;
220 return if $tag ne 'a';
221 return unless $attr{href} =~ qr/$desired_value/;
222 push(@hrefs, values %attr);
223 }, getcwd() . '/' . $file);
225 $p->parse_file($file);
232 inlined_pages_html();