From 454b30a485bad5bccfab4c7ee8d1934bad7d5449 Mon Sep 17 00:00:00 2001 From: mbeaulieu Date: Mon, 18 Aug 2014 10:04:55 -0400 Subject: [PATCH] Bug 11315 - Added unit tests for the modified sub in AuthoritiesMarc.pm new file: t/AuthoritiesMarc.t --- t/AuthoritiesMarc.t | 258 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 258 insertions(+) create mode 100644 t/AuthoritiesMarc.t diff --git a/t/AuthoritiesMarc.t b/t/AuthoritiesMarc.t new file mode 100644 index 0000000..562fd2d --- /dev/null +++ b/t/AuthoritiesMarc.t @@ -0,0 +1,258 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use Test::MockModule; +use Test::MockObject; +use DBD::Mock; +use Test::More tests => 4; +use MARC::Record; +use Data::Dumper; + +BEGIN{ + use_ok('C4::AuthoritiesMarc'); + use_ok('C4::Biblio'); + use_ok('ZOOM'); +} +# Tests for Bug 11315 + +# Setting-up Database and session Mocks + +my $ZOOM_mock = Test::MockObject->new(); +$ZOOM_mock->fake_module('ZOOM::Query::CCL2RPN', new=>sub{return 0}); + +my $module_context = new Test::MockModule('C4::Context'); +$module_context->mock( + 'Zconn', + sub{ + return mock_ZOOM_connection(); + } +); +#Mocking so new_record_from_zebra parses the raw data from usmarc. +$module_context->mock( + 'config', + sub{ + return 'grs1'; + } +); +$module_context->mock( + '_new_dbh', + sub { + my $dbh = DBI->connect( 'DBI:Mock:', '', '' ) || die "Cannot create handle: $DBI::errstr\n"; + return $dbh; + } +); + +# Mock Database sessions. +my $mock_session_no_overwrite = DBD::Mock::Session->new('mock-no_overwrite' =>( + { + statement=> "select authtypecode from auth_header where authid=?", + results => [ + ['authtypecode'], + ['PERSON'] + ] + }, + { + statement=> "select authtypecode from auth_header where authid=?", + results => [ + ['authtypecode'], + ['CHRON_TERM'] + ] + }, + { + statement=> "select auth_tag_to_report from auth_types where authtypecode=?", + results => [ + ['auth_tag_to_report'], + [100] + ] + }, + { + statement=> "select auth_tag_to_report from auth_types where authtypecode=?", + results => [ + ['auth_tag_to_report'], + [100] + ] + }, + { + statement=> "select frameworkcode,kohafield,tagfield,tagsubfield from marc_subfield_structure where kohafield > ''", + results => [ + ['frameworkcode','kohafield', 'tagfield','tagsubfield'], + ['', 'biblio.biblionumber', 999, 'c'], + ['FA', 'biblio.biblionumber', 999, 'c'] + ] + }, + { + statement=> "select distinct tagfield from marc_subfield_structure where authtypecode=?", + results => [ + ['tagfield'], + [100] + ] + }, + { + statement=> "select distinct tagfield from marc_subfield_structure where authtypecode=?", + results => [ + ['tagfield'], + [100] + ] + }, + { + statement=> "SELECT frameworkcode FROM biblio WHERE biblionumber=?", + results => [ + ['frameworkcode'], + [''] + ] + }, + { + statement=> q{ + SELECT value + FROM systempreferences + WHERE variable = ? + LIMIT 1 + }, + results => [ + ['CataloguingLog'], + [0] + ] + } +)); +my $mock_session_overwrite = DBD::Mock::Session->new('mock-overwrite' =>( + { + statement=> "select authtypecode from auth_header where authid=?", + results => [ + ['authtypecode'], + ['PERSON'] + ] + }, + { + statement=> "select authtypecode from auth_header where authid=?", + results => [ + ['authtypecode'], + ['CHRON_TERM'] + ] + }, + { + statement=> "select auth_tag_to_report from auth_types where authtypecode=?", + results => [ + ['auth_tag_to_report'], + [100] + ] + }, + { + statement=> "select auth_tag_to_report from auth_types where authtypecode=?", + results => [ + ['auth_tag_to_report'], + [100] + ] + }, + { + statement=> "select distinct tagfield from marc_subfield_structure where authtypecode=?", + results => [ + ['tagfield'], + [100] + ] + }, + { + statement=> "select distinct tagfield from marc_subfield_structure where authtypecode=?", + results => [ + ['tagfield'], + [100] + ] + }, + { + statement=> "SELECT frameworkcode FROM biblio WHERE biblionumber=?", + results => [ + ['frameworkcode'], + [''] + ] + }, + { + statement=> "select frameworkcode,kohafield,tagfield,tagsubfield from marc_subfield_structure where kohafield > ''", + results => [ + ['frameworkcode','kohafield', 'tagfield','tagsubfield'], + ['', 'biblio.biblionumber', 999, 'c'], + ['FA', 'biblio.biblionumber', 999, 'c'] + ] + }, + { + statement=> q{ + SELECT value + FROM systempreferences + WHERE variable = ? + LIMIT 1 + }, + results => [ + ['CataloguingLog'], + [0] + ] + } +)); + +my $from = '1'; +my $MARCfrom = MARC::Record->new(); +$MARCfrom->append_fields(MARC::Field->new('245', '', '', 'a'=> 'From the depths')); +$MARCfrom->append_fields(MARC::Field->new('100', '', '', 'a'=>'Bruce Wayne', 'b'=>'2014', '9' => '1')); +$MARCfrom->append_fields(MARC::Field->new('999', '', '', 'c'=> '1')); +my $to = '1'; +my $MARCto = MARC::Record->new(); +$MARCto->append_fields(MARC::Field->new('245', '', '', 'a'=> 'All the way to heaven')); +$MARCto->append_fields(MARC::Field->new('100', '', '', 'a'=>'Batman', '9' => '1')); +$MARCto->append_fields(MARC::Field->new('999', '', '', 'c'=> '2')); +my @records = ( $MARCfrom, $MARCto ); + +my $module_Auth = new Test::MockModule('C4::AuthoritiesMarc'); +$module_Auth->mock( + 'GetMarcBiblio', + sub{ + return $MARCfrom; + } +); + +my $modified_record; +$module_Auth->mock( + 'ModBiblio', + sub{ + $modified_record = shift; + } +); + +my $dbh = C4::Context->dbh(); +$dbh->{mock_session} = $mock_session_no_overwrite; + +print "Testing merge() without overwrite\n"; +my $countEdited = merge($from, $MARCfrom, $to, $MARCto, 0); +my $auth_field = $modified_record->field(100)->subfield('a'); +my $test_field = $modified_record->field(100)->subfield('b'); +is($countEdited, 1, 'One record was edited by merge()'); +is($auth_field, $MARCto->field(100)->subfield('a'), 'Record values updated correctly'); +is($test_field, $MARCfrom->field(100)->subfield('b'), 'Record not overwritten while merging'); + +$dbh->{mock_session} = $mock_session_overwrite; +$modified_record = undef; + +print "Testing merge() with overwrite\n"; +$countEdited = merge($from, $MARCfrom, $to, $MARCto, 1); +$auth_field = $modified_record->field(100)->subfield('a'); +$test_field = $modified_record->field(100)->subfield('b'); +is($countEdited, 1, 'One record was edited by merge()'); +is($auth_field, $MARCto->field(100)->subfield('a'), 'Record values updated correctly'); +is($test_field, undef, 'Record overwritten while merging'); + +sub mock_ZOOM_connection{ + my $mocked_connection = Test::MockObject->new(); + $mocked_connection->mock('search', sub{return mock_ZOOM_results()}); + $mocked_connection->mock('option', sub{undef}); + return $mocked_connection; +} +sub mock_ZOOM_results{ + my $mocked_results = Test::MockObject->new(); + $mocked_results->mock('size', sub{return 1}); + $mocked_results->mock('record',sub{ shift; return mock_ZOOM_record(@_) }); + $mocked_results->mock('destroy', sub{undef}); + return $mocked_results; +} +sub mock_ZOOM_record{ + my $mocked_record = Test::MockObject->new(); + my $record_index = shift; + $mocked_record->mock('raw', sub{return $records[$record_index]->as_usmarc()}); + return $mocked_record; +} -- 1.7.9.5