From e440c144518a9038a8af50337702cda1e80b80a7 Mon Sep 17 00:00:00 2001 From: Olli-Antti Kivilahti Date: Tue, 1 Oct 2013 16:30:14 +0300 Subject: [PATCH] Bug 6113 [ENH] - enhancement to keep previous ids Created the test cases. --- t/db_dependent/PreserveLegacyId.t | 150 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100755 t/db_dependent/PreserveLegacyId.t diff --git a/t/db_dependent/PreserveLegacyId.t b/t/db_dependent/PreserveLegacyId.t new file mode 100755 index 0000000..56dc7de --- /dev/null +++ b/t/db_dependent/PreserveLegacyId.t @@ -0,0 +1,150 @@ +#!/usr/bin/perl +# +# This Koha test module is a stub! +# Add more tests here!!! + +use Modern::Perl; +use utf8; + +use Test::More tests => 3; +use MARC::Record; + +BEGIN { + use_ok('C4::Record'); + use_ok('C4::Biblio'); +} + +### Preparing our testing data ### +my $bibFramework = ''; #Using the default bibliographic framework. +my $marcxml=qq( + + 00000cim a22000004a 4500 + 1001 + 2013-06-03 07:04:07+02 + ss||||j||||||| + uuuu xxk|||||||||||||||||eng|c + + 0-00-103147-3 + 14.46 EUR + + + eng + + + 83.5 + ykl + + + SHAKESPEARE, WILLIAM. + + + THE TAMING OF THE SHREW / + WILLIAM SHAKESPEARE + [ÄÄNITE]. + + + LONDON : + COLLINS. + + + 2 ÄÄNIKASETTIA. + + + FI-Jm + 83.5 + + + FI-Konti + 83.5 + + +); + +my $record=marcxml2marc($marcxml); + +my ( $biblionumberTagid, $biblionumberSubfieldid ) = + GetMarcFromKohaField( "biblio.biblionumber", $bibFramework ); +my ( $biblioitemnumberTagid, $biblioitemnumberSubfieldid ) = + GetMarcFromKohaField( "biblioitems.biblioitemnumber", $bibFramework ); + +# Making sure the Koha to MARC mappings stand correct +is ($biblionumberTagid, $biblioitemnumberTagid, 'Testing "Koha to MARC mapping" biblionumber and biblioitemnumber source MARC field sameness'); +if ($biblionumberTagid ne $biblioitemnumberTagid) { + warn "Koha to MARC mapping values biblio.biblionumber and biblioitems.biblioitemnumber must share the same MARC field!"; +} + +#Moving the 001 to the field configured for legacy id +my $legacyIdField = $record->field( '001' ); +my $legacyId = $legacyIdField->data(); +my $biblionumberField = MARC::Field->new( $biblionumberTagid, '', '', + $biblionumberSubfieldid => $legacyId, + $biblioitemnumberSubfieldid => $legacyId, +); +$record->append_fields($biblionumberField); + +#Convenience method to easily change the legacy id. +# Is used to test out database id boundary values. +sub changeLegacyId { + my ($record, $newId) = @_; + + # Because the author of Marc::Record decided it would be nice to replace all subfields with new ones whenever they are updated, + # old references are lost and need to be found again. + my $legacyIdField = $record->field( '001' ); + my $biblionumberField = $record->field( $biblionumberTagid ); + + $legacyIdField->update( $newId ); + $biblionumberField->update( $biblionumberSubfieldid => $newId, + $biblioitemnumberSubfieldid => $newId); + $legacyId = $newId; + + +} + +############################## +### Running the main test! ### +############################## + +## INSERT the record to the DB, SELECT the one we got, +## then find out if the biblionumber and biblioitemnumbers are the original ones. + +my $dbh = C4::Context->dbh; + +$dbh->{AutoCommit} = 0; #We don't want to save anything in DB as this is just a test run! +$dbh->{RaiseError} = 1; + +##Testing just below the critical threshold in _koha_add_item() primary key validators. +## Test should run OK. Also this biblionumber hardly is reserved :) +changeLegacyId($record, 2147483639); + +my ( $newBiblionumber, $newBiblioitemnumber ) = AddBiblio( $record, $bibFramework, { defer_marc_save => 1 } ); +my $selectedBiblio = GetBiblio($legacyId); + +is ($legacyId, $newBiblionumber, 'Biblionumber returned by AddBiblio matches'); +is ($legacyId, $selectedBiblio->{biblionumber}, 'Biblionumber returned by GetBiblio matches the legacy biblionumber'); + + +##Testing primary key exhaustion situation. Validator should prevent biblionumber or biblioitemnumber INSERTion +## if the value is over the safety threshold 2147483639 == LONG_MAX +## So both tests should fail. +changeLegacyId($record, 2147483645); + +( $newBiblionumber, $newBiblioitemnumber ) = AddBiblio( $record, $bibFramework, { defer_marc_save => 1 } ); +$selectedBiblio = GetBiblio($legacyId); + +isnt ($legacyId, $newBiblionumber, 'Testing primary key exhaustion prevention.'); +isnt ($legacyId, $selectedBiblio->{biblionumber}, 'Primary key exhausted biblio matching'); + +##Testing bad primary key situation. Validator should prevent biblionumber or biblioitemnumber INSERTion +## if the value is not a pure integer/long. +## So both tests should fail. +changeLegacyId($record, '12134FAN'); + +( $newBiblionumber, $newBiblioitemnumber ) = AddBiblio( $record, $bibFramework, { defer_marc_save => 1 } ); +$selectedBiblio = GetBiblio($legacyId); + +isnt ($legacyId, $newBiblionumber, 'Testing primary key not-an-integer prevention.'); +isnt ($legacyId, $selectedBiblio->{biblionumber}, 'Primary key not-an-integer biblio matching'); + + +$dbh->rollback(); +$dbh->disconnect(); -- 1.8.1.2