Bug 36520: Add tests
[koha.git] / t / Scrubber.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 $| = 1;
6 use Test::More tests => 31;
7 use Test::Warn;
8
9 BEGIN {
10     use FindBin;
11     use lib $FindBin::Bin;
12     use_ok('C4::Scrubber');
13 }
14
15 sub pretty_line {
16         my $max = 54;
17         (@_) or return "#" x $max . "\n";
18         my $phrase = "  " . shift() . "  ";
19         my $half = "#" x (($max - length($phrase))/2);
20         return $half . $phrase . $half . "\n";
21 }
22
23 my ($scrubber,$html,$result,@types,$collapse);
24 $collapse = 1;
25 @types = qw(default comment tag staff);
26 $html = q|
27 <![CDATA[selfdestruct]]&#x5d;>
28 <?php  echo(" EVIL EVIL EVIL "); ?>    <!-- COMMENT -->
29 <hr> <!-- TMPL_VAR NAME="password" -->
30 <style type="text/css">body{display:none;}</style>
31 <link media="screen" type="text/css" rev="stylesheet" rel="stylesheet" href="css.css">
32 <I FAKE="attribute" > I am ITALICS with fake="attribute" </I><br />
33 <em FAKE="attribute" > I am em with fake="attribute" </em><br />
34 <B> I am BOLD </B><br />
35 <span style="background-image: url(http://hackersite.cn/porno.jpg);"> I am a span w/ style.  Bad style.</span>
36 <span> I am a span trying to inject a link: &lt;a href="badlink.html"&gt; link &lt;/a&gt;</span>
37 <br>
38 <A NAME="evil">
39         <A HREF="javascript:alert('OMG YOO R HACKED');">I am a link firing javascript.</A>
40         <br />
41         <A HREF="image/bigone.jpg" ONMOUSEOVER="alert('OMG YOO R HACKED');"> 
42                 <IMG SRC="image/smallone.jpg" ALT="ONMOUSEOVER JAVASCRIPT">
43         </A>
44 </A> <br> 
45 At the end here, I actually have some regular text.
46 |;
47
48 ok($scrubber = C4::Scrubber->new(), "Constructor: C4::Scrubber->new()");
49
50 isa_ok($scrubber, 'HTML::Scrubber', 'Constructor returns HTML::Scrubber object');
51
52 warning_like { $scrubber->default() } '', "\$scrubber->default ran without fault.";
53 warning_like { $scrubber->comment() } '', "\$scrubber->comment ran without fault.";
54 warning_like { $scrubber->process() } '', "\$scrubber->process ran without fault.";
55
56 ok($result = $scrubber->scrub($html), "Getting scrubbed text (type: [default])");
57
58 foreach(@types) {
59         ok($scrubber = C4::Scrubber->new($_), "testing Constructor: C4::Scrubber->new($_)");
60
61         warning_like { $scrubber->default() } '', "\$scrubber->default ran without fault.";
62         warning_like { $scrubber->comment() } '', "\$scrubber->comment ran without fault.";
63         warning_like { $scrubber->process() } '', "\$scrubber->process ran without fault.";
64
65         ok($result = $scrubber->scrub($html), "Getting scrubbed text (type: $_)");
66 }
67
68 #Test for invalid new entry
69 eval{
70         C4::Scrubber->new("");
71         fail("test should fail on entry of ''");
72 };
73 if ($@) {
74     pass("Test should have failed on entry of '' (empty string) and it did. YAY!");
75 }
76
77 eval{
78         C4::Scrubber->new("Client");
79         fail("test should fail on entry of 'Client'");
80 };
81 if ($@) {
82     pass("Test should have failed on entry of 'Client' and it did. YAY!");
83 }
84
85 my $scrub_text =
86     '<div><span><p><b>bold</b><i>ital</i><em>emphatic</em><big>embiggen</big><small>shrink</small><strong>strongbad</strong><br><u>under</u><hr></p></span></div>';
87 my $scrub_comment =
88     '<b>bold</b><i>ital</i><em>emphatic</em><big>embiggen</big><small>shrink</small><strong>strongbad</strong><br>under';
89 is( C4::Scrubber->new('comment')->scrub($scrub_text), $scrub_comment, "Comment scrubber removes expected elements" );
90 is(
91     C4::Scrubber->new('note')->scrub($scrub_text), $scrub_text,
92     "Note scrubber removes (additional) expected elements"
93 );