From a0cf09bbaee78a845ae17774cfd1402ff7b7b646 Mon Sep 17 00:00:00 2001 From: Julian Maurice Date: Wed, 19 Feb 2014 11:31:13 +0100 Subject: [PATCH] Bug 11700: Add unit tests for C4::AuthoritiesMarc::merge Signed-off-by: Bernardo Gonzalez Kriegel Signed-off-by: Kyle M Hall --- t/db_dependent/AuthoritiesMarc.t | 75 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 74 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/AuthoritiesMarc.t b/t/db_dependent/AuthoritiesMarc.t index f432a5d..875e4f2 100755 --- a/t/db_dependent/AuthoritiesMarc.t +++ b/t/db_dependent/AuthoritiesMarc.t @@ -6,7 +6,9 @@ use strict; use warnings; -use Test::More tests => 8; +use C4::Context; +use C4::Biblio; +use Test::More tests => 15; use Test::MockModule; use Test::Warn; use MARC::Record; @@ -190,4 +192,75 @@ is_deeply( 'test BuildSummary for UNIMARC' ); +$module->unmock_all(); + +# Start testing C4::AuthoritiesMarc::merge + +# Create authority type TEST_PERSO +$dbh->do("INSERT INTO auth_types(authtypecode, authtypetext, auth_tag_to_report, summary) VALUES('TEST_PERSO', 'Personal Name', '109', 'Personal Names');"); +$dbh->do("INSERT INTO auth_tag_structure (authtypecode, tagfield, liblibrarian, libopac, repeatable, mandatory, authorised_value) VALUES('TEST_PERSO', '109', 'HEADING--PERSONAL NAME', 'HEADING--PERSONAL NAME', 0, 0, NULL)"); +$dbh->do("INSERT INTO auth_subfield_structure (authtypecode, tagfield, tagsubfield, liblibrarian, libopac, repeatable, mandatory, tab, authorised_value, value_builder, seealso, isurl, hidden, linkid, kohafield, frameworkcode) VALUES ('TEST_PERSO', '109', 'a', 'Personal name', 'Personal name', 0, 0, 1, NULL, NULL, '', 0, 0, '', '', '')"); + +my $auth1 = new MARC::Record; +$auth1->append_fields(new MARC::Field('109', '0', '0', 'a' => 'George Orwell')); +my $authid1 = AddAuthority($auth1, undef, 'TEST_PERSO'); +my $auth2 = new MARC::Record; +$auth2->append_fields(new MARC::Field('109', '0', '0', 'a' => 'G. Orwell')); +my $authid2 = AddAuthority($auth2, undef, 'TEST_PERSO'); + +$dbh->do("INSERT IGNORE INTO marc_subfield_structure(tagfield, tagsubfield, liblibrarian, libopac, repeatable, mandatory, kohafield, tab, authorised_value, authtypecode, value_builder, isurl, hidden, frameworkcode, seealso, link, defaultvalue) VALUES('609', 'a', 'Personal name', 'Personal name', 0, 0, '', 6, '', 'TEST_PERSO', '', NULL, 0, '', '', '', NULL)"); +$dbh->do("UPDATE marc_subfield_structure SET authtypecode = 'TEST_PERSO' WHERE tagfield='609' AND tagsubfield='a' AND frameworkcode='';"); +my $tagfields = $dbh->selectcol_arrayref("select distinct tagfield from marc_subfield_structure where authtypecode='TEST_PERSO'"); +my $biblio1 = new MARC::Record; +$biblio1->append_fields( + new MARC::Field('609', '0', '0', '9' => $authid1, 'a' => 'George Orwell') +); +my $biblionumber1 = AddBiblio($biblio1, ''); +my $biblio2 = new MARC::Record; +$biblio2->append_fields( + new MARC::Field('609', '0', '0', '9' => $authid2, 'a' => 'G. Orwell') +); +my $biblionumber2 = AddBiblio($biblio2, ''); + +# Mock ZOOM objects +my $zoom_connection = new Test::MockModule('ZOOM::Connection', no_auto => 1); +$zoom_connection->mock('search', sub { + return _new ZOOM::ResultSet(shift, shift, [$biblionumber2]); +}); +my $zoom_resultset = new Test::MockModule('ZOOM::ResultSet', no_auto => 1); +$zoom_resultset->mock('size', sub { + my $this = shift; + return scalar @{ $this->{_rs} }; +}); +$zoom_resultset->mock('record', sub { + my ($this, $which) = @_; + my $_rec = { biblionumber => $this->{_rs}->[$which] }; + return ZOOM::Record->_new($this, $which, $_rec); +}); +$zoom_resultset->mock('destroy', undef); +my $zoom_record = new Test::MockModule('ZOOM::Record', no_auto => 1); +$zoom_record->mock('raw', sub { + my $this = shift; + my $index_mode = C4::Context->config('zebra_bib_index_mode') // 'dom'; + my $sqlfield = $index_mode eq 'dom' ? 'marcxml' : 'marc'; + my $marcs = $dbh->selectcol_arrayref( + "SELECT $sqlfield FROM biblioitems WHERE biblionumber = ?", {}, + $this->{_rec}->{biblionumber}); + return $marcs->[0]; +}); + +my @biblios = C4::AuthoritiesMarc::merge($authid2, undef, $authid1, undef); + +is_deeply(\@biblios, [$biblionumber2]); +$biblio1 = GetMarcBiblio($biblionumber1); +is($biblio1->subfield('609', '9'), $authid1); +is($biblio1->subfield('609', 'a'), 'George Orwell'); +$biblio2 = GetMarcBiblio($biblionumber2); +is($biblio2->subfield('609', '9'), $authid1); +is($biblio2->subfield('609', 'a'), 'George Orwell'); + +ok(defined(GetAuthority($authid1))); +ok(!defined(GetAuthority($authid2))); +# End testing C4::AuthoritiesMarc::merge + $dbh->rollback; -- 1.9.1