Bug 19532: (follow-up) aria-hidden attr on OPAC, and more
[koha.git] / t / db_dependent / FrameworkPlugin.t
1 use Modern::Perl;
2
3 use CGI;
4 use File::Temp qw/tempfile/;
5 use Getopt::Long;
6 use Test::MockModule;
7 use Test::More tests => 6;
8
9 use t::lib::Mocks;
10 use t::lib::TestBuilder;
11
12 use C4::Auth qw( checkauth );
13 use C4::Output qw( output_html_with_http_headers );
14 use Koha::Database;
15 use Koha::FrameworkPlugin;
16 use Koha::Util::FrameworkPlugin qw( biblio_008 );
17
18 our @includes;
19 GetOptions( 'include=s{,}' => \@includes ); #not used by default !
20
21 my $schema  = Koha::Database->new->schema;
22 $schema->storage->txn_begin;
23 our $dbh = C4::Context->dbh;
24 our $builder = t::lib::TestBuilder->new;
25
26 subtest 'Test01 -- Simple tests for new and name' => sub {
27     plan tests => 7;
28     test01();
29 };
30 subtest 'Test02 -- test build with old styler and marc21_leader' => sub {
31     plan tests => 5;
32     test02();
33 };
34 subtest 'Test03 -- tests with bad plugins' => sub {
35     test03();
36 };
37 subtest 'Test04 -- tests with new style plugin' => sub {
38     plan tests => 5;
39     test04();
40 };
41 subtest 'Test05 -- tests with build and launch for default plugins' => sub {
42     test05( \@includes );
43 };
44
45 subtest 'Test06 -- test biblio_008' => sub {
46     plan tests => 5;
47     t::lib::Mocks::mock_preference('DefaultCountryField008', 'nl' ); # deliberately shorter than 3 pos
48     t::lib::Mocks::mock_preference('DefaultLanguageField008', 'dutch' ); # deliberately too long
49     my $field = biblio_008();
50     is( length($field), 40, 'Check length' );
51     is( substr($field, 15, 3), 'nl ', 'Check country right padded' );
52     is( substr($field, 35, 3), 'dut', 'Check language' );
53     t::lib::Mocks::mock_preference('DefaultCountryField008', '' );
54     $field = biblio_008();
55     is( substr($field, 15, 3), '|||', 'Check country fallback for empty string' );
56     t::lib::Mocks::mock_preference('DefaultCountryField008', undef );
57     $field = biblio_008();
58     is( substr($field, 15, 3), '|||', 'Check country fallback for undefined' );
59 };
60
61 $schema->storage->txn_rollback;
62
63 sub test01 {
64     #empty plugin
65     my $plugin= Koha::FrameworkPlugin->new;
66     is( ref($plugin), 'Koha::FrameworkPlugin', 'Got an object' );
67     isnt( $plugin->errstr, undef, 'We should have an error for missing name');
68     is( $plugin->build, undef, 'Build returns undef');
69
70     #tests for name and path, with/without hashref
71     $plugin= Koha::FrameworkPlugin->new( { name => 'marc21_leader.pl' } );
72     is( $plugin->name, 'marc21_leader.pl', 'Check name without path in hash' );
73     $plugin= Koha::FrameworkPlugin->new( 'marc21_leader.pl' );
74     is( $plugin->name, 'marc21_leader.pl', 'Check name without path' );
75     $plugin= Koha::FrameworkPlugin->new( 'cataloguing/value_builder/marc21_leader.pl' );
76     is( $plugin->name, 'marc21_leader.pl', 'Check name with path' );
77     $plugin= Koha::FrameworkPlugin->new({ path => 'cataloguing/value_builder', name => 'marc21_leader.pl' });
78     is( $plugin->name, 'marc21_leader.pl', 'Check name and path in hash' );
79 }
80
81 sub test02 {
82     # first test an old style item plugin
83     my $old = old01(); # plugin filename
84     my $path;
85     if( $old =~ /^(.*)\/([^\/]+)$/ ) { # extract path
86         $path = $1;
87         $old = $2;
88     }
89     my $plugin= Koha::FrameworkPlugin->new({
90         name => $old, path => $path, item_style => 1,
91     });
92     my $pars= { id => '234567' };
93     is( $plugin->build($pars), 1, 'Build oldstyler successful' );
94     is( length($plugin->javascript)>0 && !$plugin->noclick, 1,
95         'Checked javascript and noclick' );
96
97     # now test marc21_leader
98     $plugin= Koha::FrameworkPlugin->new( { name => 'marc21_leader.pl' } );
99     $pars= { dbh => $dbh, id => '123456' };
100     is( $plugin->build($pars), 1, 'Build marc21_leader successful' );
101     is( $plugin->javascript =~ /<script.*function.*\<\/script\>/s, 1,
102         'Javascript looks ok' );
103     is( $plugin->noclick, '', 'marc21_leader should have a popup');
104 }
105
106 sub test03 {
107     #file not found
108     my $plugin= Koha::FrameworkPlugin->new('file_does_not_exist');
109     $plugin->build;
110     is( $plugin->errstr =~ /not found/i, 1, 'File not found-message');
111
112     #three bad ones: no perl, syntax error, bad return value
113     foreach my $f ( bad01(), bad02(), bad03() ) {
114         next if !$f;
115         $plugin= Koha::FrameworkPlugin->new( $f );
116         $plugin->build({ id => '998877' });
117         is( defined($plugin->errstr), 1,
118             "Saw: ". ( $plugin->errstr//'no error??' ));
119     }
120     done_testing();
121 }
122
123 sub test04 {
124     #two simple new style plugins
125     my $plugin= Koha::FrameworkPlugin->new( good01() );
126     my $pars= { id => 'example_345' };
127     is( $plugin->build($pars), 1, 'Build 1 ok');
128     isnt( $plugin->javascript, '', 'Checked javascript property' );
129
130     $plugin= Koha::FrameworkPlugin->new( ugly01() );
131     $pars= { id => 'example_456' };
132     is( $plugin->build($pars), 1, 'Build 2 ok');
133     is( $plugin->build($pars), 1, 'Second build 2 ok');
134     is( $plugin->launch($pars), 'abc', 'Launcher returned something' );
135         #note: normally you will not call build and launch like that
136 }
137
138 sub test05 {
139     my ( $incl ) = @_;
140     #mock to simulate some authorization and eliminate lots of output
141     my $launched = 0;
142     my $mContext = Test::MockModule->new('C4::Context');
143     my $mAuth = Test::MockModule->new('C4::Auth');
144     my $mOutput = Test::MockModule->new('C4::Output');
145     $mContext->mock( 'userenv', \&mock_userenv );
146     $mAuth->mock( 'checkauth', sub { return ( 1, undef, 1, all_perms() ); } );
147     $mOutput->mock('output_html_with_http_headers',  sub { ++$launched; } );
148
149     my $cgi=CGI->new;
150     my ( $plugins, $min ) = selected_plugins( $incl );
151
152     # test building them
153     my $objs;
154     foreach my $f ( @$plugins ) {
155         $objs->{$f} = Koha::FrameworkPlugin->new( $f );
156         my $pars= { dbh => $dbh, id => $f };
157         is( $objs->{$f}->build($pars), 1, "Builded ".$objs->{$f}->name );
158     }
159
160     # test launching them (but we cannot verify returned results here)
161     undef $objs;
162     foreach my $f ( @$plugins ) {
163         $objs->{$f} = Koha::FrameworkPlugin->new( $f );
164         my $pars= { dbh => $dbh, id => $f };
165         $objs->{$f}->launch({ cgi => $cgi });
166             # may generate some uninitialized warnings for missing params
167         is( $objs->{$f}->errstr, undef, "Launched ".$objs->{$f}->name );
168     }
169     is( $launched >= $min, 1,
170             "$launched of ". scalar @$plugins.' plugins generated output ');
171     done_testing();
172 }
173
174 sub selected_plugins {
175     my ( $incl ) = @_;
176     #if you use includes, FIRST assure yourself that you do not
177     #include any destructive perl scripts! You know what you are doing..
178
179     my ( @fi, $min);
180     if( $incl && @$incl ) {
181         @fi = @$incl;
182         $min = 0; #not sure how many will output
183     } else { # some default MARC, UNIMARC and item plugins
184         @fi = qw| barcode.pl dateaccessioned.pl marc21_orgcode.pl
185 marc21_field_005.pl marc21_field_006.pl marc21_field_007.pl marc21_field_008.pl
186 marc21_field_008_authorities.pl marc21_leader.pl marc21_leader_authorities.pl
187 unimarc_leader.pl unimarc_field_100.pl unimarc_field_105.pl
188 unimarc_field_106.pl unimarc_field_110.pl unimarc_field_120.pl
189 unimarc_field_130.pl unimarc_field_140.pl unimarc_field_225a.pl
190 unimarc_field_4XX.pl |;
191         $min = 16; # the first four generate no output
192     }
193     @fi = grep
194         { !/ajax|callnumber(-KU)?\.pl|labs_theses/ } # skip these
195         @fi;
196     return ( \@fi, $min);
197 }
198
199 sub mock_userenv {
200     my $branch = $builder->build({ source => 'Branch' });
201     return { branch => $branch->{branchcode}, flags => 1, id => 1 };
202 }
203
204 sub all_perms {
205     my $p = $dbh->selectcol_arrayref("SELECT flag FROM userflags");
206     my $rv= {};
207     foreach my $module ( @$p ) {
208         $rv->{ $module } = 1;
209     }
210     return $rv;
211 }
212
213 sub mytempfile {
214     my ( $fh, $fn ) = tempfile( SUFFIX => '.plugin', UNLINK => 1 );
215     print $fh $_[0]//'';
216     close $fh;
217     return $fn;
218 }
219
220 sub old01 {
221 # simple old style item plugin: note that Focus has two pars
222 # includes a typical empty Clic function and plugin subroutine
223     return mytempfile( <<'HERE'
224 sub plugin_javascript {
225     my ($dbh,$record,$tagslib,$field_number) = @_;
226     my $function_name = $field_number;
227     my $res = "
228 <script type=\"text/javascript\">
229 //<![CDATA[
230 function Focus$function_name(subfield_managed,id) {
231     document.getElementById(id).value='test';
232     return 0;
233 }
234 function Clic$function_name(subfield_managed) {
235 }
236 //]]>
237 </script>
238 ";
239     return ($function_name,$res);
240 }
241 sub plugin {
242     return "";
243 }
244 HERE
245     );
246 }
247
248 sub good01 { #very simple new style plugin, no launcher
249     return mytempfile( <<'HERE'
250 my $builder = sub {
251     my $params = shift;
252     return qq|
253 <script type="text/javascript">
254     function Focus$params->{id}(event) {
255         if( document.getElementById(event.data.id).value == '' ) {
256             document.getElementById(event.data.id).value='EXAMPLE: ';
257         }
258     }
259 </script>|;
260 };
261 return { builder => $builder };
262 HERE
263     );
264 }
265
266 sub bad01 { # this is no plugin
267     return mytempfile( 'Just nonsense' );
268 }
269
270 sub bad02 { # common syntax error: you forgot the semicolon of sub1 declare
271     return mytempfile( <<'HERE'
272 my $sub1= sub {
273     my $params = shift;
274     return qq|<script type="text/javascript">function Change$params->{id}(event) { alert("Changed"); }</script>|;
275 }
276 return { builder => $sub1 };
277 HERE
278     );
279 }
280
281 sub bad03 { # badscript tag should trigger an error
282     return mytempfile( <<'HERE'
283 my $sub1= sub {
284     my $params = shift;
285     return qq|<badscript type="text/javascript">function Click$params->{id} (event) { alert("Hi there"); return false; }</badscript>|;
286 };
287 return { builder => $sub1 };
288 HERE
289     );
290 }
291
292 sub ugly01 { #works, but not very readable..
293     return mytempfile( <<'HERE'
294 return {builder=>sub{return qq|<script type="text/javascript">function Blur$_[0]->{id}(event){alert('Bye');}</script>|;},launcher=>sub{'abc'}};
295 HERE
296     );
297 }