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 => 89};
19 my $statedir = 't/tinypodcast/.ikiwiki';
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";
28 ok(! system("mkdir $tmp"),
30 ok(! system(@command),
34 'simplepost' => undef,
35 'piano.mp3' => 'audio/mpeg',
36 'scroll.3gp' => 'video/3gpp',
37 'walter.ogg' => 'video/x-theora+ogg',
40 for my $format (qw(atom rss)) {
41 my $feed = XML::Feed->parse("$tmp/out/simple/index.$format");
43 is($feed->title, 'simple',
44 qq{$format feed title});
45 is($feed->link, "$baseurl/simple/",
46 qq{$format feed link});
47 is($feed->description, 'wiki',
48 qq{$format feed description});
49 if ('atom' eq $format) {
50 is($feed->author, $feed->description,
51 qq{$format feed author});
52 is($feed->id, $feed->link,
54 is($feed->generator, "ikiwiki",
55 qq{$format feed generator});
58 for my $entry ($feed->entries) {
59 my $title = $entry->title;
61 my $body = $entry->content->body;
62 my $enclosure = $entry->enclosure;
64 is($entry->link, $url, qq{$format $title link});
65 isnt($entry->issued, undef,
66 qq{$format $title issued date});
67 isnt($entry->modified, undef,
68 qq{$format $title modified date});
70 if (defined $media_types{$title}) {
71 is($url, "$baseurl/$title",
72 qq{$format $title id});
74 qq{$format $title no body text});
75 is($enclosure->url, $url,
76 qq{$format $title enclosure url});
77 is($enclosure->type, $media_types{$title},
78 qq{$format $title enclosure type});
79 cmp_ok($enclosure->length, '>', 0,
80 qq{$format $title enclosure length});
83 is($url, "$baseurl/$title/",
84 qq{$format $title id});
86 qq{$format $title body text});
88 qq{$format $title no enclosure});
93 ok(! system("rm -rf $tmp $statedir"), q{teardown});
96 sub single_page_html {
97 my @command = (qw(./ikiwiki.out));
98 push @command, qw(-underlaydir=underlays/basewiki);
99 push @command, qw(-set underlaydirbase=underlays -templatedir=templates);
100 push @command, qw(t/tinypodcast), "$tmp/out";
102 ok(! system("mkdir $tmp"),
104 ok(! system(@command),
107 my $html = "$tmp/out/pianopost/index.html";
108 like(_extract_html_content($html, 'content'), qr/has content and/m,
110 like(_extract_html_content($html, 'enclosure'), qr/this episode/m,
112 my ($href) = _extract_html_links($html, 'piano');
114 q{html enclosure exists});
116 $html = "$tmp/out/attempted_multiple_enclosures/index.html";
117 like(_extract_html_content($html, 'content'), qr/has content and/m,
119 like(_extract_html_content($html, 'enclosure'), qr/this episode/m,
121 ($href) = _extract_html_links($html, 'walter');
123 q{html enclosure exists});
125 ok(! system("rm -rf $tmp $statedir"), q{teardown});
128 sub inlined_pages_html {
129 my @command = (qw(./ikiwiki.out -plugin inline));
130 push @command, qw(-underlaydir=underlays/basewiki);
131 push @command, qw(-set underlaydirbase=underlays -templatedir=templates);
132 push @command, qw(t/tinypodcast), "$tmp/out";
134 ok(! system("mkdir $tmp"),
136 ok(! system(@command),
139 my $html = "$tmp/out/fancy/index.html";
140 my $contents = _extract_html_content($html, 'content');
141 like($contents, qr/has content and an/m,
142 q{html body text from pianopost});
143 like($contents, qr/has content and only one/m,
144 q{html body text from attempted_multiple_enclosures});
145 my $enclosures = _extract_html_content($html, 'inlineenclosure');
146 like($enclosures, qr/this episode/m,
148 my ($href) = _extract_html_links($html, 'piano.mp3');
150 q{html enclosure from pianopost exists});
151 ($href) = _extract_html_links($html, 'walter.ogg');
153 q{html enclosure from attempted_multiple_enclosures exists});
155 ok(! system("rm -rf $tmp $statedir"), q{teardown});
158 sub _extract_html_content {
159 my ($file, $desired_id, $desired_tag) = @_;
160 $desired_tag = 'div' unless defined $desired_tag;
162 my $p = HTML::Parser->new(api_version => 3);
165 $p->handler(start => sub {
166 my ($tag, $self, $attr) = @_;
167 return if $tag ne $desired_tag;
168 return unless exists $attr->{id} && $attr->{id} eq $desired_id;
170 $self->handler(text => sub {
174 }, "tagname,self,attr");
176 $p->parse_file($file) || die $!;
181 sub _extract_html_links {
182 my ($file, $desired_value) = @_;
186 my $p = HTML::LinkExtor->new(sub {
187 my ($tag, %attr) = @_;
188 return if $tag ne 'a';
189 return unless $attr{href} =~ qr/$desired_value/;
190 push(@hrefs, values %attr);
191 }, getcwd() . '/' . $file);
193 $p->parse_file($file);
200 inlined_pages_html();