Bug 36328: Add a separate 'note' profile
[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     $mAuth->mock( 'check_cookie_auth', sub { return ('ok') } );
148     $mOutput->mock('output_html_with_http_headers',  sub { ++$launched; } );
149
150     my $cgi=CGI->new;
151     my ( $plugins, $min ) = selected_plugins( $incl );
152
153     # test building them
154     my $objs;
155     foreach my $f ( @$plugins ) {
156         $objs->{$f} = Koha::FrameworkPlugin->new( $f );
157         my $pars= { dbh => $dbh, id => $f };
158         is( $objs->{$f}->build($pars), 1, "Builded ".$objs->{$f}->name );
159     }
160
161     # test launching them (but we cannot verify returned results here)
162     undef $objs;
163     foreach my $f ( @$plugins ) {
164         $objs->{$f} = Koha::FrameworkPlugin->new( $f );
165         my $pars= { dbh => $dbh, id => $f };
166         $objs->{$f}->launch({ cgi => $cgi });
167             # may generate some uninitialized warnings for missing params
168         is( $objs->{$f}->errstr, undef, "Launched ".$objs->{$f}->name );
169     }
170     is( $launched >= $min, 1,
171             "$launched of ". scalar @$plugins.' plugins generated output ');
172     done_testing();
173 }
174
175 sub selected_plugins {
176     my ( $incl ) = @_;
177     #if you use includes, FIRST assure yourself that you do not
178     #include any destructive perl scripts! You know what you are doing..
179
180     my ( @fi, $min);
181     if( $incl && @$incl ) {
182         @fi = @$incl;
183         $min = 0; #not sure how many will output
184     } else { # some default MARC, UNIMARC and item plugins
185         @fi = qw| barcode.pl dateaccessioned.pl marc21_orgcode.pl
186 marc21_field_005.pl marc21_field_006.pl marc21_field_007.pl marc21_field_008.pl
187 marc21_field_008_authorities.pl marc21_leader.pl marc21_leader_authorities.pl
188 unimarc_leader.pl unimarc_field_100.pl unimarc_field_105.pl
189 unimarc_field_106.pl unimarc_field_110.pl unimarc_field_120.pl
190 unimarc_field_130.pl unimarc_field_140.pl unimarc_field_225a.pl
191 unimarc_field_4XX.pl |;
192         $min = 16; # the first four generate no output
193     }
194     @fi = grep
195         { !/ajax|callnumber(-KU)?\.pl|labs_theses/ } # skip these
196         @fi;
197     return ( \@fi, $min);
198 }
199
200 sub mock_userenv {
201     my $branch = $builder->build({ source => 'Branch' });
202     return { branch => $branch->{branchcode}, flags => 1, id => 1 };
203 }
204
205 sub all_perms {
206     my $p = $dbh->selectcol_arrayref("SELECT flag FROM userflags");
207     my $rv= {};
208     foreach my $module ( @$p ) {
209         $rv->{ $module } = 1;
210     }
211     return $rv;
212 }
213
214 sub mytempfile {
215     my ( $fh, $fn ) = tempfile( SUFFIX => '.plugin', UNLINK => 1 );
216     print $fh $_[0]//'';
217     close $fh;
218     return $fn;
219 }
220
221 sub old01 {
222 # simple old style item plugin: note that Focus has two pars
223 # includes a typical empty Clic function and plugin subroutine
224     return mytempfile( <<'HERE'
225 sub plugin_javascript {
226     my ($dbh,$record,$tagslib,$field_number) = @_;
227     my $function_name = $field_number;
228     my $res = "
229 <script type=\"text/javascript\">
230 //<![CDATA[
231 function Focus$function_name(subfield_managed,id) {
232     document.getElementById(id).value='test';
233     return 0;
234 }
235 function Clic$function_name(subfield_managed) {
236 }
237 //]]>
238 </script>
239 ";
240     return ($function_name,$res);
241 }
242 sub plugin {
243     return "";
244 }
245 HERE
246     );
247 }
248
249 sub good01 { #very simple new style plugin, no launcher
250     return mytempfile( <<'HERE'
251 my $builder = sub {
252     my $params = shift;
253     return qq|
254 <script type="text/javascript">
255     function Focus$params->{id}(event) {
256         if( document.getElementById(event.data.id).value == '' ) {
257             document.getElementById(event.data.id).value='EXAMPLE: ';
258         }
259     }
260 </script>|;
261 };
262 return { builder => $builder };
263 HERE
264     );
265 }
266
267 sub bad01 { # this is no plugin
268     return mytempfile( 'Just nonsense' );
269 }
270
271 sub bad02 { # common syntax error: you forgot the semicolon of sub1 declare
272     return mytempfile( <<'HERE'
273 my $sub1= sub {
274     my $params = shift;
275     return qq|<script type="text/javascript">function Change$params->{id}(event) { alert("Changed"); }</script>|;
276 }
277 return { builder => $sub1 };
278 HERE
279     );
280 }
281
282 sub bad03 { # badscript tag should trigger an error
283     return mytempfile( <<'HERE'
284 my $sub1= sub {
285     my $params = shift;
286     return qq|<badscript type="text/javascript">function Click$params->{id} (event) { alert("Hi there"); return false; }</badscript>|;
287 };
288 return { builder => $sub1 };
289 HERE
290     );
291 }
292
293 sub ugly01 { #works, but not very readable..
294     return mytempfile( <<'HERE'
295 return {builder=>sub{return qq|<script type="text/javascript">function Blur$_[0]->{id}(event){alert('Bye');}</script>|;},launcher=>sub{'abc'}};
296 HERE
297     );
298 }