|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
| 3 |
# This file is part of Koha. |
| 4 |
# |
| 5 |
# Copyright (C) 2020 Rijksmuseum |
| 6 |
# |
| 7 |
# Koha is free software; you can redistribute it and/or modify it |
| 8 |
# under the terms of the GNU General Public License as published by |
| 9 |
# the Free Software Foundation; either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# Koha is distributed in the hope that it will be useful, but |
| 13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
| 19 |
|
| 20 |
use Modern::Perl; |
| 21 |
#use Data::Dumper qw/Dumper/; |
| 22 |
use MARC::Record; |
| 23 |
use Test::More tests => 4; |
| 24 |
use Test::MockModule; |
| 25 |
use Test::MockObject; |
| 26 |
use Test::Warn; |
| 27 |
|
| 28 |
use t::lib::Mocks; |
| 29 |
use t::lib::TestBuilder; |
| 30 |
|
| 31 |
use C4::AuthoritiesMarc qw//; |
| 32 |
use C4::Biblio qw//; |
| 33 |
use C4::Context; |
| 34 |
use C4::Items; |
| 35 |
use Koha::Database; |
| 36 |
use Koha::PID::Controller; |
| 37 |
|
| 38 |
our $schema = Koha::Database->new->schema; |
| 39 |
our $builder = t::lib::TestBuilder->new; |
| 40 |
|
| 41 |
our @plugins; |
| 42 |
my $plugin_module = Test::MockModule->new( 'Koha::Plugins' ); |
| 43 |
$plugin_module->mock( 'GetPlugins', sub { return @plugins; }); |
| 44 |
|
| 45 |
$schema->storage->txn_begin; |
| 46 |
|
| 47 |
subtest 'Test get method (biblio)' => sub { |
| 48 |
plan tests => 6; |
| 49 |
|
| 50 |
my $plugin = Test::MockObject->new; |
| 51 |
|
| 52 |
my $record = MARC::Record->new; |
| 53 |
$record->append_fields( |
| 54 |
MARC::Field->new( '003', 'http://unimarc.fr/U-003' ), |
| 55 |
MARC::Field->new( '024', '', '', 1 => 'http://donotgohere.com/M24a', q => 'qual_a' ), |
| 56 |
MARC::Field->new( '024', '', '', 1 => 'http://bestpersistentid.nl/M24b' ), |
| 57 |
MARC::Field->new( '024', '', '', 1 => 'http://bestpersistentid.nl/M24c', q => 'qual_c' ), |
| 58 |
MARC::Field->new( '035', '', '', a => '(RMA) http://mysite01.org/M35a' ), # deliberate spaces |
| 59 |
MARC::Field->new( '035', '', '', a => '(RMAxx)http://mysite02.org/M35b' ), |
| 60 |
MARC::Field->new( '035', '', '', a => '(RMAxx)http://bestpersistentid.nl/M35c' ), |
| 61 |
MARC::Field->new( '035', '', '', a => '(RMAx) http://mysite03.org/M35d' ), |
| 62 |
); |
| 63 |
my ($biblionumber) = C4::Biblio::AddBiblio( $record, q{} ); |
| 64 |
|
| 65 |
t::lib::Mocks::mock_preference('PID_Field', 'biblio:003'); |
| 66 |
t::lib::Mocks::mock_preference('PID_Domain', 'http://bestpersistentid.nl'); |
| 67 |
my $ctrl = Koha::PID::Controller->new; |
| 68 |
# UNIMARC 003, no domain control |
| 69 |
like( $ctrl->get({ category => 'biblio', kohaIdentifier => $biblionumber }), qr/^http.*U-003$/, 'Test get for UNIMARC biblio' ); |
| 70 |
|
| 71 |
t::lib::Mocks::mock_preference('PID_Field', 'biblio:024$1'); |
| 72 |
$ctrl = Koha::PID::Controller->new; |
| 73 |
# MARC21 024 $1, domain controlled |
| 74 |
like( $ctrl->get({ category => 'biblio', kohaIdentifier => $biblionumber }), qr/^http.*M24b$/, 'Test get b for MARC21 biblio (024)' ); |
| 75 |
|
| 76 |
$plugin->mock( 'pid_get_hook', \&get_hook_24 ); |
| 77 |
push @plugins, $plugin; |
| 78 |
t::lib::Mocks::mock_preference('PID_Domain', q{}); |
| 79 |
$ctrl = Koha::PID::Controller->new; |
| 80 |
# MARC21 024 $1, no domain check, plugin hook (qualifier) |
| 81 |
like( $ctrl->get({ category => 'biblio', kohaIdentifier => $biblionumber }), qr/^http.*M24c$/, 'Test get c for MARC21 biblio (024)' ); |
| 82 |
pop @plugins; |
| 83 |
|
| 84 |
t::lib::Mocks::mock_preference('PID_Field', 'biblio:035'); # NOTE: we omit subfield |
| 85 |
t::lib::Mocks::mock_preference('PID_Domain', 'http://bestpersistentid.nl'); |
| 86 |
$ctrl = Koha::PID::Controller->new; |
| 87 |
# MARC21 035 $a, domain controlled |
| 88 |
like( $ctrl->get({ category => 'biblio', kohaIdentifier => $biblionumber }), qr/^http.*M35c$/, 'Test get c for MARC21 biblio (035)' ); |
| 89 |
|
| 90 |
$ctrl = Koha::PID::Controller->new({ domain => 'site' }); |
| 91 |
# MARC21 035 $a, incomplete domain check |
| 92 |
like( $ctrl->get({ category => 'biblio', kohaIdentifier => $biblionumber }), qr/^http.*M35a$/, 'Test get a for MARC21 biblio (035)' ); |
| 93 |
|
| 94 |
$plugin->mock( 'pid_get_hook', \&get_hook_35 ); |
| 95 |
push @plugins, $plugin; |
| 96 |
t::lib::Mocks::mock_preference('MARCOrgCode', 'RMAx'); |
| 97 |
t::lib::Mocks::mock_preference('PID_Domain', q{}); |
| 98 |
$ctrl = Koha::PID::Controller->new; |
| 99 |
# MARC21 035 $a, no domain check, plugin hook (orgcode) |
| 100 |
like( $ctrl->get({ category => 'biblio', kohaIdentifier => $biblionumber }), qr/^http.*M35d$/, 'Test get d for MARC21 biblio (035)' ); |
| 101 |
pop @plugins; |
| 102 |
}; |
| 103 |
|
| 104 |
sub get_hook_24 { # Mimics plugin that makes get return 024 with condition on $q |
| 105 |
my ($self, $params) = @_; |
| 106 |
my $record = $params->{record}; |
| 107 |
my $qual = 'qual_c'; |
| 108 |
foreach my $fld ( $record->field('024') ) { |
| 109 |
if( !$fld->subfield('q') || $fld->subfield('q') !~ /^\Q$qual\E/ ) { |
| 110 |
$record->delete_fields( $fld ); |
| 111 |
} |
| 112 |
} |
| 113 |
return 1; |
| 114 |
} |
| 115 |
|
| 116 |
sub get_hook_35 { # Mimics plugin that makes get return 035 with orgcode |
| 117 |
my ($self, $params) = @_; |
| 118 |
my $record = $params->{record}; |
| 119 |
my $orgcode = C4::Context->preference('MARCOrgCode'); |
| 120 |
foreach my $fld ( $record->field('035') ) { |
| 121 |
if( !$fld->subfield('a') || $fld->subfield('a') !~ /^\(\Q$orgcode\E\)/ ) { |
| 122 |
$record->delete_fields( $fld ); |
| 123 |
} |
| 124 |
} |
| 125 |
return 1; |
| 126 |
} |
| 127 |
|
| 128 |
subtest 'Test get method (item)' => sub { |
| 129 |
plan tests => 4; |
| 130 |
|
| 131 |
# Test item with object |
| 132 |
t::lib::Mocks::mock_preference('PID_Field', 'item'); |
| 133 |
t::lib::Mocks::mock_preference('PID_Domain', 'my'); # do not use this value normally |
| 134 |
my $ctrl = Koha::PID::Controller->new; |
| 135 |
my $item = $builder->build_object({ class => 'Koha::Items', value => { uri => 'uri01 | my02 | uri03 | http://site04.org' } }); |
| 136 |
is( $ctrl->get({ category => 'item', object => $item }), 'my02', 'Test get my02 for item' ); |
| 137 |
# Test fallback to http |
| 138 |
t::lib::Mocks::mock_preference('PID_Domain', q{}); |
| 139 |
$ctrl = Koha::PID::Controller->new; |
| 140 |
like( $ctrl->get({ category => 'item', kohaIdentifier => $item->itemnumber }), qr/04\.org$/, 'Test get http item' ); |
| 141 |
# Item hook |
| 142 |
my $plugin = Test::MockObject->new; |
| 143 |
$plugin->mock( 'pid_get_hook', \&get_hook_item ); |
| 144 |
push @plugins, $plugin; |
| 145 |
$ctrl->get({ category => 'item', object => $item }); |
| 146 |
is( $item->itemnotes, 'Reached', 'Item hook executed' ); |
| 147 |
pop @plugins; |
| 148 |
# Test clearing PID_Field |
| 149 |
t::lib::Mocks::mock_preference('PID_Field', q{}); |
| 150 |
$ctrl = Koha::PID::Controller->new; |
| 151 |
is( $ctrl->get({ category => 'item', kohaIdentifier => $item->itemnumber }), undef, 'Test no item category' ); |
| 152 |
}; |
| 153 |
|
| 154 |
sub get_hook_item { # Simple hack to test execution |
| 155 |
my ($self, $params) = @_; |
| 156 |
my $record = $params->{record}; # Koha::Item |
| 157 |
$record->itemnotes('Reached'); |
| 158 |
return 1; |
| 159 |
} |
| 160 |
|
| 161 |
subtest 'Test create' => sub { |
| 162 |
plan tests => 4; |
| 163 |
|
| 164 |
my $plugin = Test::MockObject->new; |
| 165 |
$plugin->mock( 'pid_create_hook', \&create_hook ); |
| 166 |
push @plugins, $plugin; |
| 167 |
my $ctrl = Koha::PID::Controller->new({ domain => 'https://id.rijksmuseum.nl' }); |
| 168 |
is( $ctrl->create({ category => 'biblio', kohaIdentifier => 100000 })->{uri}, 'https://id.rijksmuseum.nl/biblio/100000', "Create biblio pid" ); |
| 169 |
# Category is not checked |
| 170 |
is( $ctrl->create({ category => 'unchecked', kohaIdentifier => 999 })->{uri}, 'https://id.rijksmuseum.nl/unchecked/999', "Create unchecked pid" ); |
| 171 |
# Two plugins, 'whatever' comes first |
| 172 |
unshift @plugins, Test::MockObject->new->mock( 'pid_create_hook', sub { 'whatever' } ); |
| 173 |
my $rv = $ctrl->create({ category => 'auth', kohaIdentifier => 1234 }); |
| 174 |
is( $rv->{uri}, 'whatever', "Result of first plugin returning true value" ); |
| 175 |
# No plugins |
| 176 |
@plugins = (); |
| 177 |
is( $ctrl->create({ category => 'item', kohaIdentifier => 654321 })->{success}, 0, "Test without plugin" ); |
| 178 |
}; |
| 179 |
|
| 180 |
sub create_hook { # Super trivial return |
| 181 |
my ($self, $params) = @_; |
| 182 |
return $params->{domain}. '/'. $params->{category}. '/'. $params->{identifier}; |
| 183 |
} |
| 184 |
|
| 185 |
subtest 'Test insert' => sub { |
| 186 |
plan tests => 19; |
| 187 |
|
| 188 |
t::lib::Mocks::mock_preference('PID_Field', 'biblio:024$1,item'); |
| 189 |
my $ctrl = Koha::PID::Controller->new; |
| 190 |
|
| 191 |
# category undefined |
| 192 |
is( $ctrl->insert({ pid => '1', category => 'auth', kohaIdentifier => 1 })->{errcode}, 'INV_CAT', 'Undefined category' ); |
| 193 |
|
| 194 |
# hook |
| 195 |
my $plugin = Test::MockObject->new; |
| 196 |
$plugin->mock( 'pid_insert_hook', \&insert_hook ); |
| 197 |
push @plugins, $plugin; |
| 198 |
my $item = $builder->build_object({ class => 'Koha::Items', value => { uri => 'A | B' } }); |
| 199 |
$ctrl->insert({ pid => '1', category => 'item', kohaIdentifier => $item->itemnumber }); # 1 | A | B |
| 200 |
is( @plugins, 0, 'Hook got executed' ); |
| 201 |
# item |
| 202 |
$item->discard_changes; |
| 203 |
is( $item->uri, '1 | A | B', 'Check result of item insert' ); |
| 204 |
# reinsert the same value |
| 205 |
is( $ctrl->insert({ pid => '1', category => 'item', kohaIdentifier => $item->itemnumber })->{errcode}, 'EXIST', 'Check reinsert' ); |
| 206 |
# Test do_not_save |
| 207 |
my $result = $ctrl->insert({ pid => '2', category => 'item', kohaIdentifier => $item->itemnumber, do_not_save => 1 }); |
| 208 |
like( $result->{record}->uri, qr/^2/, 'New uri in response' ); |
| 209 |
$item->discard_changes; |
| 210 |
is( $item->uri, '1 | A | B', 'But not saved as expected' ); |
| 211 |
|
| 212 |
# marc record changes (biblio or auth) |
| 213 |
my $biblio = $builder->build_sample_biblio; |
| 214 |
my $record; |
| 215 |
$result = $ctrl->insert({ pid => '123', category => 'biblio', kohaIdentifier => $biblio->biblionumber }); |
| 216 |
$record = C4::Biblio::GetMarcBiblio({ biblionumber => $biblio->biblionumber }); |
| 217 |
is( $record->subfield('024', '1'), '123', 'Check biblio pid 123' ); |
| 218 |
is( $record->field('024')->indicator(1), '7', 'Check indicator1 from default hook' ); |
| 219 |
is( $record->subfield('024', '2'), 'uri', 'Check $2 from default hook' ); |
| 220 |
# reinsert (without hook) |
| 221 |
$result = $ctrl->insert({ pid => '123', category => 'biblio', kohaIdentifier => $biblio->biblionumber }); |
| 222 |
is( $result->{errcode}, 'EXIST', 'Check reinsert 123' ); |
| 223 |
# empty hook |
| 224 |
$plugin->mock('after_biblio_action', sub {} ); # prevent warn |
| 225 |
$plugin->mock('pid_insert_hook', sub { 1; } ); |
| 226 |
push @plugins, $plugin; |
| 227 |
$result = $ctrl->insert({ pid => '456', category => 'biblio', kohaIdentifier => $biblio->biblionumber }); |
| 228 |
$record = C4::Biblio::GetMarcBiblio({ biblionumber => $biblio->biblionumber }); |
| 229 |
is( $record->subfield('024', '1'), '456', 'Check biblio pid 456' ); |
| 230 |
is( $record->field('024')->indicator(1), q{ }, 'Check if indicator1 blank' ); |
| 231 |
is( $record->subfield('024', '2'), undef, 'Check if $2 empty' ); |
| 232 |
# reinsert (with hook) |
| 233 |
$result = $ctrl->insert({ pid => '123', category => 'biblio', kohaIdentifier => $biblio->biblionumber }); |
| 234 |
is( $result->{success}, 1, 'Reinsert with hook returns success' ); # since the hook 'fixes everything' |
| 235 |
my @fields = $result->{record}->field('024'); |
| 236 |
is( @fields, 2, 'Still expecting to find two 024s' ); |
| 237 |
# hook returns false |
| 238 |
my $plugin2 = Test::MockObject->new; |
| 239 |
$plugin2->mock('pid_insert_hook', sub {} ); |
| 240 |
push @plugins, $plugin2; |
| 241 |
$result = $ctrl->insert({ pid => '567', category => 'biblio', kohaIdentifier => $biblio->biblionumber }); |
| 242 |
is( $result->{errcode}, 'HOOK', 'Hook returned false' ); |
| 243 |
@plugins = (); |
| 244 |
|
| 245 |
# PERSO_NAME authority |
| 246 |
t::lib::Mocks::mock_preference('PID_Field', 'auth:024'); |
| 247 |
$ctrl = Koha::PID::Controller->new; |
| 248 |
$record = MARC::Record->new; |
| 249 |
my $authid = C4::AuthoritiesMarc::AddAuthority( $record, undef, 'PERSO_NAME' ); |
| 250 |
$result = $ctrl->insert({ pid => '234', category => 'auth', kohaIdentifier => $authid }); |
| 251 |
$record = $result->{record}; |
| 252 |
is( $record->subfield('024', 'a'), '234', 'Check auth pid 234' ); |
| 253 |
# control field |
| 254 |
t::lib::Mocks::mock_preference('PID_Field', 'auth:003'); |
| 255 |
$ctrl = Koha::PID::Controller->new; |
| 256 |
$result = $ctrl->insert({ pid => '345', category => 'auth', kohaIdentifier => $authid }); |
| 257 |
$record = $result->{record}; |
| 258 |
is( $record->field('003')->data, '345', 'Check auth pid 345' ); |
| 259 |
# reinsert |
| 260 |
$result = $ctrl->insert({ pid => '345', category => 'auth', kohaIdentifier => $authid }); |
| 261 |
is( $result->{errcode}, 'EXIST', 'Check reinsert 345' ); |
| 262 |
}; |
| 263 |
|
| 264 |
sub insert_hook { |
| 265 |
my ($self, $params) = @_; |
| 266 |
pop @plugins; # self destructive ;) |
| 267 |
return 1; |
| 268 |
} |
| 269 |
|
| 270 |
$schema->storage->txn_rollback; |