From 1671e85fe99249f880f93859064e71282455cc8b Mon Sep 17 00:00:00 2001 From: Ere Maijala Date: Mon, 19 Nov 2018 14:26:00 +0200 Subject: [PATCH] Bug 21826: Add tests Adds tests for new Heading::valid_bib_heading_subfield and ModBiblio with auto-created authorities. Signed-off-by: Martin Renvoize --- t/Heading.t | 55 ++++++++++++++++++++++++++-- t/db_dependent/Biblio.t | 81 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 129 insertions(+), 7 deletions(-) diff --git a/t/Heading.t b/t/Heading.t index d7809443c4..bcbbe515ec 100755 --- a/t/Heading.t +++ b/t/Heading.t @@ -1,14 +1,61 @@ #!/usr/bin/perl # -# This Koha test module is a stub! -# Add more tests here!!! +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . use strict; use warnings; -use Test::More tests => 1; +use Test::More tests => 3; + +use t::lib::Mocks; BEGIN { - use_ok('C4::Heading'); + use_ok('C4::Heading'); } +subtest "MARC21 tests" => sub { + plan tests => 7; + + t::lib::Mocks::mock_preference('marcflavour', 'MARC21'); + + ok(C4::Heading::valid_bib_heading_subfield('100', 'a'), '100a valid'); + ok(!C4::Heading::valid_bib_heading_subfield('100', 'e'), '100e not valid'); + + ok(C4::Heading::valid_bib_heading_subfield('110', 'a'), '110a valid'); + ok(!C4::Heading::valid_bib_heading_subfield('110', 'e'), '110e not valid'); + + ok(C4::Heading::valid_bib_heading_subfield('600', 'a'), '600a valid'); + ok(!C4::Heading::valid_bib_heading_subfield('600', 'e'), '600e not valid'); + + ok(!C4::Heading::valid_bib_heading_subfield('012', 'a'), '012a invalid field'); +}; + +subtest "UNIMARC tests" => sub { + plan tests => 7; + + t::lib::Mocks::mock_preference('marcflavour', 'UNIMARC'); + + ok(C4::Heading::valid_bib_heading_subfield('100', 'a'), '100a valid'); + ok(!C4::Heading::valid_bib_heading_subfield('100', 'i'), '100i not valid'); + + ok(C4::Heading::valid_bib_heading_subfield('110', 'a'), '110a valid'); + ok(!C4::Heading::valid_bib_heading_subfield('110', 'i'), '110i not valid'); + + ok(C4::Heading::valid_bib_heading_subfield('600', 'a'), '600a valid'); + ok(!C4::Heading::valid_bib_heading_subfield('600', 'i'), '600i not valid'); + + ok(!C4::Heading::valid_bib_heading_subfield('012', 'a'), '012a invalid field'); +} diff --git a/t/db_dependent/Biblio.t b/t/db_dependent/Biblio.t index 1f954dcca0..60ccde3a18 100755 --- a/t/db_dependent/Biblio.t +++ b/t/db_dependent/Biblio.t @@ -348,6 +348,54 @@ sub run_tests { like( $marcurl->[0]->{MARCURL}, qr/^https/, 'GetMarcUrls did not stumble over a preceding space' ); ok( $marcflavour ne 'MARC21' || $marcurl->[1]->{MARCURL} =~ /^http:\/\//, 'GetMarcUrls prefixed a MARC21 URL with http://' ); + + # Automatic authority creation + t::lib::Mocks::mock_preference('BiblioAddsAuthorities', 1); + t::lib::Mocks::mock_preference('AutoCreateAuthorities', 1); + my $authorities_mod = Test::MockModule->new( 'C4::AuthoritiesMarc' ); + $authorities_mod->mock( + 'SearchAuthorities', + sub { + my @results; + return \@results, 0; + } + ); + $success = 0; + $field = create_author_field('Author Name'); + eval { + $marc_record->append_fields($field); + $success = ModBiblio($marc_record,$biblionumber,''); + } or do { + diag($@); + $success = 0; + }; + ok($success, "ModBiblio handles authority addition for author"); + + my ($author_field, $author_subfield, $author_relator_subfield) = get_author_field(); + $field = $marc_record->field($author_field); + ok($field->subfield($author_subfield), "ModBiblio keeps $author_field$author_subfield intact"); + + my $authid = $field->subfield('9'); + ok($authid, 'ModBiblio adds authority id'); + + use_ok('C4::AuthoritiesMarc'); + my $auth_record = C4::AuthoritiesMarc::GetAuthority($authid); + ok($auth_record, 'Authority record successfully retrieved'); + + + my ($auth_author_field, $auth_author_subfield) = get_auth_author_field(); + $field = $auth_record->field($auth_author_field); + ok($field, "Authority record contains field $auth_author_field"); + is( + $field->subfield($auth_author_subfield), + 'Author Name', + 'Authority $auth_author_field$auth_author_subfield contains author name' + ); + is($field->subfield($author_relator_subfield), undef, 'Authority does not contain relator subfield'); + + # Reset settings + t::lib::Mocks::mock_preference('BiblioAddsAuthorities', 0); + t::lib::Mocks::mock_preference('AutoCreateAuthorities', 0); } sub get_title_field { @@ -370,6 +418,16 @@ sub get_itemnumber_field { return ( $marc_flavour eq 'UNIMARC' ) ? ( '995', '9' ) : ( '952', '9' ); } +sub get_author_field { + my $marc_flavour = C4::Context->preference('marcflavour'); + return ( $marc_flavour eq 'UNIMARC' ) ? ( '700', 'a', '4' ) : ( '100', 'a', 'e' ); +} + +sub get_auth_author_field { + my $marc_flavour = C4::Context->preference('marcflavour'); + return ( $marc_flavour eq 'UNIMARC' ) ? ( '106', 'a' ) : ( '100', 'a' ); +} + sub create_title_field { my ( $title, $marcflavour ) = @_; @@ -401,22 +459,39 @@ sub create_issn_field { return $field; } +sub create_author_field { + my ( $author ) = @_; + + my ( $author_field, $author_subfield, $author_relator_subfield ) = get_author_field(); + my $field = MARC::Field->new( + $author_field, '', '', + $author_subfield => $author, + $author_relator_subfield => 'aut' + ); + + return $field; +} + subtest 'MARC21' => sub { - plan tests => 34; + plan tests => 42; run_tests('MARC21'); $schema->storage->txn_rollback; $schema->storage->txn_begin; }; subtest 'UNIMARC' => sub { - plan tests => 34; + plan tests => 42; + + # Mock the auth type data for UNIMARC + $dbh->do("UPDATE auth_types SET auth_tag_to_report = '106' WHERE auth_tag_to_report = '100'") or die $dbh->errstr; + run_tests('UNIMARC'); $schema->storage->txn_rollback; $schema->storage->txn_begin; }; subtest 'NORMARC' => sub { - plan tests => 34; + plan tests => 42; run_tests('NORMARC'); $schema->storage->txn_rollback; $schema->storage->txn_begin; -- 2.19.1