Wrapper for Koha's use of HTML::Scrubber, with test script on usage.
[koha.git] / t / Scrubber.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 10;
7 BEGIN {
8         use FindBin;
9         use lib $FindBin::Bin;
10         use override_context_prefs;
11         use_ok('C4::Scrubber');
12 }
13
14 sub pretty_line {
15         my $max = 54;
16         (@_) or return "#" x $max . "\n";
17         my $phrase = "  " . shift() . "  ";
18         my $half = "#" x (($max - length($phrase))/2);
19         return $half . $phrase . $half . "\n";
20 }
21
22 my ($scrubber,$html,$result,@types,$collapse);
23 $collapse = 1;
24 @types = qw(comment tag);
25 $html = q|
26 <![CDATA[selfdestruct]]&#x5d;>
27 <?php  echo(" EVIL EVIL EVIL "); ?>    <!-- COMMENT -->
28 <hr> <!-- TMPL_VAR NAME="password" -->
29 <style type="text/css">body{display:none;}</style>
30 <link media="screen" type="text/css" rev="stylesheet" rel="stylesheet" href="css.css">
31 <I FAKE="attribute" > I am ITALICS with fake="attribute" </I><br />
32 <em FAKE="attribute" > I am em with fake="attribute" </em><br />
33 <B> I am BOLD </B><br />
34 <span style="background-image: url(http://hackersite.cn/porno.jpg);"> I am a span w/ style.  Bad style.</span>
35 <span> I am a span trying to inject a link: &lt;a href="badlink.html"&gt; link &lt;a&gt;</span>
36 <br>
37 <A NAME="evil">
38         <A HREF="javascript:alert('OMG YOO R HACKED');">I am a link firing javascript.</A>
39         <br />
40         <A HREF="image/bigone.jpg" ONMOUSEOVER="alert('OMG YOO R HACKED');"> 
41                 <IMG SRC="image/smallone.jpg" ALT="ONMOUSEOVER JAVASCRIPT">
42         </A>
43 </A> <br> 
44 At the end here, I actually have some regular text.
45 |;
46
47 print pretty_line("Original HTML:"), $html, "\n", pretty_line();
48 $collapse and diag "Note: scrubber test output will have whitespace collapsed for readability\n";
49 ok($scrubber = C4::Scrubber->new(), "Constructor: C4::Scrubber->new()");
50 ok(printf("# scrubber settings: default %s, comment %s, process %s\n",
51         $scrubber->default(),$scrubber->comment(),$scrubber->process()),
52         "Outputting settings from scrubber object (type: [default])"
53 );
54 ok($result = $scrubber->scrub($html), "Getting scrubbed text (type: [default])");
55 $collapse and $result =~ s/\s*\n\s*/\n/g;
56 print pretty_line('default'), $result, "\n", pretty_line();
57
58 foreach(@types) {
59         ok($scrubber = C4::Scrubber->new($_), "Constructor: C4::Scrubber->new($_)");
60         ok(printf("# scrubber settings: default %s, comment %s, process %s\n",
61                 $scrubber->default(),$scrubber->comment(),$scrubber->process()),
62                 "Outputting settings from scrubber object (type: $_)"
63         );
64         ok($result = $scrubber->scrub($html), "Getting scrubbed text (type: $_)");
65         $collapse and $result =~ s/\s*\n\s*/\n/g;
66         print pretty_line($_), $result, "\n", pretty_line();
67 }
68 diag "done.\n";