use Modern::Perl; use CGI; use File::Temp qw/tempfile/; use Getopt::Long; use Test::NoWarnings; use Test::More tests => 8; use Test::MockModule; use Test::Warn; use t::lib::Mocks; use t::lib::TestBuilder; use C4::Auth qw( checkauth ); use C4::Output qw( output_html_with_http_headers ); use Koha::Database; use Koha::FrameworkPlugin; use Koha::Util::FrameworkPlugin qw( biblio_008 ); use Koha::ContentSecurityPolicy; our @includes; GetOptions( 'include=s{,}' => \@includes ); #not used by default ! # Because of CGI::Session being buggy (see bug 17427) and a use of get_session somewhere here t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' ); Koha::ContentSecurityPolicy->new->set_nonce; my $schema = Koha::Database->new->schema; $schema->storage->txn_begin; our $dbh = C4::Context->dbh; our $builder = t::lib::TestBuilder->new; subtest 'Test01 -- Simple tests for new and name' => sub { plan tests => 7; test01(); }; subtest 'Test02 -- test build with old styler and marc21_leader' => sub { plan tests => 5; test02(); }; subtest 'Test03 -- tests with bad plugins' => sub { test03(); }; subtest 'Test04 -- tests with new style plugin' => sub { plan tests => 5; test04(); }; subtest 'Test05 -- tests with build and launch for default plugins' => sub { test05( \@includes ); }; subtest 'Test06 -- test biblio_008' => sub { plan tests => 5; t::lib::Mocks::mock_preference( 'DefaultCountryField008', 'nl' ); # deliberately shorter than 3 pos t::lib::Mocks::mock_preference( 'DefaultLanguageField008', 'dutch' ); # deliberately too long my $field = biblio_008(); is( length($field), 40, 'Check length' ); is( substr( $field, 15, 3 ), 'nl ', 'Check country right padded' ); is( substr( $field, 35, 3 ), 'dut', 'Check language' ); t::lib::Mocks::mock_preference( 'DefaultCountryField008', '' ); $field = biblio_008(); is( substr( $field, 15, 3 ), '|||', 'Check country fallback for empty string' ); t::lib::Mocks::mock_preference( 'DefaultCountryField008', undef ); $field = biblio_008(); is( substr( $field, 15, 3 ), '|||', 'Check country fallback for undefined' ); }; subtest 'Test07 -- validate input' => sub { plan tests => 4; my $plugin = Koha::FrameworkPlugin->new( { name => '../../misc/maintenance/touch_all_biblios.pl' } ); warning_like { $plugin->launch } qr{Attempt to load}; like( $plugin->errstr, qr{File not found} ); $plugin = Koha::FrameworkPlugin->new( { name => 'non-existent-plugin.pl' } ); warning_like { $plugin->launch } q{}; like( $plugin->errstr, qr{File not found} ); }; $schema->storage->txn_rollback; sub test01 { #empty plugin my $plugin = Koha::FrameworkPlugin->new; is( ref($plugin), 'Koha::FrameworkPlugin', 'Got an object' ); isnt( $plugin->errstr, undef, 'We should have an error for missing name' ); is( $plugin->build, undef, 'Build returns undef' ); #tests for name and path, with/without hashref $plugin = Koha::FrameworkPlugin->new( { name => 'marc21_leader.pl' } ); is( $plugin->name, 'marc21_leader.pl', 'Check name without path in hash' ); $plugin = Koha::FrameworkPlugin->new('marc21_leader.pl'); is( $plugin->name, 'marc21_leader.pl', 'Check name without path' ); $plugin = Koha::FrameworkPlugin->new('cataloguing/value_builder/marc21_leader.pl'); is( $plugin->name, 'marc21_leader.pl', 'Check name with path' ); $plugin = Koha::FrameworkPlugin->new( { path => 'cataloguing/value_builder', name => 'marc21_leader.pl' } ); is( $plugin->name, 'marc21_leader.pl', 'Check name and path in hash' ); } sub test02 { # first test an old style item plugin my $old = old01(); # plugin filename my $path; if ( $old =~ /^(.*)\/([^\/]+)$/ ) { # extract path $path = $1; $old = $2; } my $plugin = Koha::FrameworkPlugin->new( { name => $old, path => $path, item_style => 1, } ); my $pars = { id => '234567' }; is( $plugin->build($pars), 1, 'Build oldstyler successful' ); is( length( $plugin->javascript ) > 0 && !$plugin->noclick, 1, 'Checked javascript and noclick' ); # now test marc21_leader $plugin = Koha::FrameworkPlugin->new( { name => 'marc21_leader.pl' } ); $pars = { dbh => $dbh, id => '123456' }; my $nonce = Koha::ContentSecurityPolicy->new->get_nonce; is( $plugin->build($pars), 1, 'Build marc21_leader successful' ); is( $plugin->javascript =~ / "; return ($function_name,$res); } sub plugin { return ""; } HERE ); } sub good01 { #very simple new style plugin, no launcher return mytempfile( <<'HERE' my $builder = sub { my $params = shift; return qq| |; }; return { builder => $builder }; HERE ); } sub bad01 { # this is no plugin return mytempfile('Just nonsense'); } sub bad02 { # common syntax error: you forgot the semicolon of sub1 declare return mytempfile( <<'HERE' my $sub1= sub { my $params = shift; return qq||; } return { builder => $sub1 }; HERE ); } sub bad03 { # badscript tag should trigger an error return mytempfile( <<'HERE' my $sub1= sub { my $params = shift; return qq|function Click$params->{id} (event) { alert("Hi there"); return false; }|; }; return { builder => $sub1 }; HERE ); } sub ugly01 { #works, but not very readable.. return mytempfile( <<'HERE' return {builder=>sub{return qq||;},launcher=>sub{'abc'}}; HERE ); }