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 => 78};
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"), q{setup});
29 ok(! system(@command), q{build});
32 'simplepost' => undef,
33 'piano.mp3' => 'audio/mpeg',
34 'scroll.3gp' => 'video/3gpp',
35 'walter.ogg' => 'video/x-theora+ogg',
38 for my $format (qw(atom rss)) {
39 my $feed = XML::Feed->parse("$tmp/out/simple/index.$format");
41 is($feed->title, 'simple',
42 qq{$format feed title});
43 is($feed->link, "$baseurl/simple/",
44 qq{$format feed link});
45 is($feed->description, 'wiki',
46 qq{$format feed description});
47 if ('atom' eq $format) {
48 is($feed->author, $feed->description,
49 qq{$format feed author});
50 is($feed->id, $feed->link,
52 is($feed->generator, "ikiwiki",
53 qq{$format feed generator});
56 for my $entry ($feed->entries) {
57 my $title = $entry->title;
59 my $body = $entry->content->body;
60 my $enclosure = $entry->enclosure;
62 is($entry->link, $url, qq{$format $title link});
63 isnt($entry->issued, undef,
64 qq{$format $title issued date});
65 isnt($entry->modified, undef,
66 qq{$format $title modified date});
68 if (defined $media_types{$title}) {
69 is($url, "$baseurl/$title",
70 qq{$format $title id});
72 qq{$format $title no body text});
73 is($enclosure->url, $url,
74 qq{$format $title enclosure url});
75 is($enclosure->type, $media_types{$title},
76 qq{$format $title enclosure type});
77 cmp_ok($enclosure->length, '>', 0,
78 qq{$format $title enclosure length});
81 is($url, "$baseurl/$title/",
82 qq{$format $title id});
84 qq{$format $title body text});
86 qq{$format $title no enclosure});
91 ok(! system("rm -rf $tmp $statedir"), q{teardown});
94 sub single_page_html {
95 my @command = (qw(./ikiwiki.out));
96 push @command, qw(-underlaydir=underlays/basewiki);
97 push @command, qw(-set underlaydirbase=underlays -templatedir=templates);
98 push @command, qw(t/tinypodcast), "$tmp/out";
100 ok(! system("mkdir $tmp"), q{setup});
101 ok(! system(@command), q{build});
103 my $html = "$tmp/out/pianopost/index.html";
105 my $body = _extract_html_content($html, 'content');
106 like($body, qr/article has content and/m, q{html body text});
108 my $enclosure = _extract_html_content($html, 'enclosure');
109 like($enclosure, qr/Download this episode/m, q{html enclosure});
111 my ($href) = _extract_html_links($html, 'piano');
112 ok(-f $href, q{html enclosure exists});
114 # XXX die if more than one enclosure is specified
116 ok(! system("rm -rf $tmp $statedir"), q{teardown});
119 sub _extract_html_content {
120 my ($file, $desired_id, $desired_tag) = @_;
121 $desired_tag = 'div' unless defined $desired_tag;
123 my $p = HTML::Parser->new(api_version => 3);
126 $p->handler(start => sub {
127 my ($tag, $self, $attr) = @_;
128 return if $tag ne $desired_tag;
129 return unless exists $attr->{id} && $attr->{id} eq $desired_id;
131 $self->handler(text => sub {
136 $self->handler(end => sub {
137 my ($tag, $self) = @_;
138 $self->eof if $tag eq $desired_tag;
140 }, "tagname,self,attr");
142 $p->parse_file($file) || die $!;
147 sub _extract_html_links {
148 my ($file, $desired_value) = @_;
152 my $p = HTML::LinkExtor->new(sub {
153 my ($tag, %attr) = @_;
154 return if $tag ne 'a';
155 return unless $attr{href} =~ qr/$desired_value/;
156 push(@hrefs, values %attr);
157 }, getcwd() . '/' . $file);
159 $p->parse_file($file);