From 4c1c66e806fff23385fc6dd609cfd0b81f8460b5 Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Mon, 21 Jun 2021 08:06:38 +0000 Subject: [PATCH] Bug 28491: Add unit test Test plan: Run t/db_dependent/AuthoritiesMarc.t Signed-off-by: Marcel de Rooy Signed-off-by: hakam --- t/db_dependent/AuthoritiesMarc.t | 45 +++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/t/db_dependent/AuthoritiesMarc.t b/t/db_dependent/AuthoritiesMarc.t index 8e626f3f5c..efa1b4bd4a 100755 --- a/t/db_dependent/AuthoritiesMarc.t +++ b/t/db_dependent/AuthoritiesMarc.t @@ -5,7 +5,7 @@ use Modern::Perl; -use Test::More tests => 12; +use Test::More tests => 13; use Test::MockModule; use Test::Warn; use MARC::Field; @@ -15,6 +15,7 @@ use t::lib::Mocks; use t::lib::TestBuilder; use Koha::Database; use Koha::Authority::Types; +use Koha::Libraries; BEGIN { use_ok('C4::AuthoritiesMarc'); @@ -29,7 +30,7 @@ $module->mock('GetHeaderAuthority', sub { $module->mock('AddAuthorityTrees', sub { return; }); -$module->mock('GetAuthority', sub { +my $sub_get_authority = sub { my ($authid) = @_; my $record = MARC::Record->new(); if ($authid eq '1') { @@ -64,7 +65,8 @@ $module->mock('GetAuthority', sub { undef $record; } return $record; -}); +}; +$module->mock('GetAuthority', $sub_get_authority); my $schema = Koha::Database->new->schema; $schema->storage->txn_begin; @@ -220,6 +222,43 @@ subtest 'AddAuthority should respect AUTO_INCREMENT (BZ 18104)' => sub { is( $record->field('001')->data, $id3, 'Check updated 001' ); }; +subtest 'AddAuthority always updates 003' => sub { + plan tests => 4; + + $module->unmock('GetAuthority'); + + t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' ); + + my $library = $builder->build_object( { class => 'Koha::Libraries' } ); + my $library_branchcode = $library->branchcode; + my $library_orgcode = $library->marcorgcode; + + my $record = MARC::Record->new; + $record->append_fields( + MARC::Field->new( '001', 999 ), + MARC::Field->new( '003', 'ORG_01' ), + MARC::Field->new( '100', '', '', a => 'Marcel' ), + MARC::Field->new( '942', '', '', a => 'PERSO_NAME' ), + ); + + t::lib::Mocks::mock_preference( 'MARCOrgCode', 'ORG_02' ); + t::lib::Mocks::mock_userenv({ branchcode => q{} }); + my $id = AddAuthority( $record, undef, 'PERSO_NAME' ); + is( C4::AuthoritiesMarc::GetAuthority($id)->field('003')->data, 'ORG_02', 'New record: Updated to pref value' ); + t::lib::Mocks::mock_userenv({ branchcode => $library_branchcode }); + $id = AddAuthority( $record, undef, 'PERSO_NAME' ); + is( C4::AuthoritiesMarc::GetAuthority($id)->field('003')->data, $library_orgcode, 'New record: Updated to library value' ); + + t::lib::Mocks::mock_userenv({ branchcode => $library_branchcode }); + ModAuthority( $id, $record, 'PERSO_NAME' ); + is( C4::AuthoritiesMarc::GetAuthority($id)->field('003')->data, $library_orgcode, 'Changed record: Updated to library value' ); + t::lib::Mocks::mock_userenv({ branchcode => q{} }); + ModAuthority( $id, $record, 'PERSO_NAME' ); + is( C4::AuthoritiesMarc::GetAuthority($id)->field('003')->data, 'ORG_02', 'Changed record: Updated to pref value' ); + + $module->mock('GetAuthority', $sub_get_authority); +}; + subtest 'CompareFieldWithAuthority tests' => sub { plan tests => 3; -- 2.25.1