4 use File::Temp qw/tempfile/;
7 use Test::More tests => 6;
10 use t::lib::TestBuilder;
12 use C4::Auth qw( checkauth );
13 use C4::Output qw( output_html_with_http_headers );
15 use Koha::FrameworkPlugin;
16 use Koha::Util::FrameworkPlugin qw( biblio_008 );
19 GetOptions( 'include=s{,}' => \@includes ); #not used by default !
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;
26 subtest 'Test01 -- Simple tests for new and name' => sub {
30 subtest 'Test02 -- test build with old styler and marc21_leader' => sub {
34 subtest 'Test03 -- tests with bad plugins' => sub {
37 subtest 'Test04 -- tests with new style plugin' => sub {
41 subtest 'Test05 -- tests with build and launch for default plugins' => sub {
45 subtest 'Test06 -- test biblio_008' => sub {
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' );
61 $schema->storage->txn_rollback;
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');
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' );
82 # first test an old style item plugin
83 my $old = old01(); # plugin filename
85 if( $old =~ /^(.*)\/([^\/]+)$/ ) { # extract path
89 my $plugin= Koha::FrameworkPlugin->new({
90 name => $old, path => $path, item_style => 1,
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' );
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');
108 my $plugin= Koha::FrameworkPlugin->new('file_does_not_exist');
110 is( $plugin->errstr =~ /not found/i, 1, 'File not found-message');
112 #three bad ones: no perl, syntax error, bad return value
113 foreach my $f ( bad01(), bad02(), bad03() ) {
115 $plugin= Koha::FrameworkPlugin->new( $f );
116 $plugin->build({ id => '998877' });
117 is( defined($plugin->errstr), 1,
118 "Saw: ". ( $plugin->errstr//'no error??' ));
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' );
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
140 #mock to simulate some authorization and eliminate lots of output
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; } );
150 my ( $plugins, $min ) = selected_plugins( $incl );
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 );
160 # test launching them (but we cannot verify returned results here)
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 );
169 is( $launched >= $min, 1,
170 "$launched of ". scalar @$plugins.' plugins generated output ');
174 sub selected_plugins {
176 #if you use includes, FIRST assure yourself that you do not
177 #include any destructive perl scripts! You know what you are doing..
180 if( $incl && @$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
194 { !/ajax|callnumber(-KU)?\.pl|labs_theses/ } # skip these
196 return ( \@fi, $min);
200 my $branch = $builder->build({ source => 'Branch' });
201 return { branch => $branch->{branchcode}, flags => 1, id => 1 };
205 my $p = $dbh->selectcol_arrayref("SELECT flag FROM userflags");
207 foreach my $module ( @$p ) {
208 $rv->{ $module } = 1;
214 my ( $fh, $fn ) = tempfile( SUFFIX => '.plugin', UNLINK => 1 );
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;
228 <script type=\"text/javascript\">
230 function Focus$function_name(subfield_managed,id) {
231 document.getElementById(id).value='test';
234 function Clic$function_name(subfield_managed) {
239 return ($function_name,$res);
248 sub good01 { #very simple new style plugin, no launcher
249 return mytempfile( <<'HERE'
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: ';
261 return { builder => $builder };
266 sub bad01 { # this is no plugin
267 return mytempfile( 'Just nonsense' );
270 sub bad02 { # common syntax error: you forgot the semicolon of sub1 declare
271 return mytempfile( <<'HERE'
274 return qq|<script type="text/javascript">function Change$params->{id}(event) { alert("Changed"); }</script>|;
276 return { builder => $sub1 };
281 sub bad03 { # badscript tag should trigger an error
282 return mytempfile( <<'HERE'
285 return qq|<badscript type="text/javascript">function Click$params->{id} (event) { alert("Hi there"); return false; }</badscript>|;
287 return { builder => $sub1 };
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'}};