|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
use Test::MockModule; |
| 6 |
use Test::MockObject; |
| 7 |
use DBD::Mock; |
| 8 |
use Test::More tests => 4; |
| 9 |
use MARC::Record; |
| 10 |
use Data::Dumper; |
| 11 |
|
| 12 |
BEGIN{ |
| 13 |
use_ok('C4::AuthoritiesMarc'); |
| 14 |
use_ok('C4::Biblio'); |
| 15 |
use_ok('ZOOM'); |
| 16 |
} |
| 17 |
# Tests for Bug 11315 |
| 18 |
|
| 19 |
# Setting-up Database and session Mocks |
| 20 |
|
| 21 |
my $ZOOM_mock = Test::MockObject->new(); |
| 22 |
$ZOOM_mock->fake_module('ZOOM::Query::CCL2RPN', new=>sub{return 0}); |
| 23 |
|
| 24 |
my $module_context = new Test::MockModule('C4::Context'); |
| 25 |
$module_context->mock( |
| 26 |
'Zconn', |
| 27 |
sub{ |
| 28 |
return mock_ZOOM_connection(); |
| 29 |
} |
| 30 |
); |
| 31 |
#Mocking so new_record_from_zebra parses the raw data from usmarc. |
| 32 |
$module_context->mock( |
| 33 |
'config', |
| 34 |
sub{ |
| 35 |
return 'grs1'; |
| 36 |
} |
| 37 |
); |
| 38 |
$module_context->mock( |
| 39 |
'_new_dbh', |
| 40 |
sub { |
| 41 |
my $dbh = DBI->connect( 'DBI:Mock:', '', '' ) || die "Cannot create handle: $DBI::errstr\n"; |
| 42 |
return $dbh; |
| 43 |
} |
| 44 |
); |
| 45 |
|
| 46 |
# Mock Database sessions. |
| 47 |
my $mock_session_no_overwrite = DBD::Mock::Session->new('mock-no_overwrite' =>( |
| 48 |
{ |
| 49 |
statement=> "select authtypecode from auth_header where authid=?", |
| 50 |
results => [ |
| 51 |
['authtypecode'], |
| 52 |
['PERSON'] |
| 53 |
] |
| 54 |
}, |
| 55 |
{ |
| 56 |
statement=> "select authtypecode from auth_header where authid=?", |
| 57 |
results => [ |
| 58 |
['authtypecode'], |
| 59 |
['CHRON_TERM'] |
| 60 |
] |
| 61 |
}, |
| 62 |
{ |
| 63 |
statement=> "select auth_tag_to_report from auth_types where authtypecode=?", |
| 64 |
results => [ |
| 65 |
['auth_tag_to_report'], |
| 66 |
[100] |
| 67 |
] |
| 68 |
}, |
| 69 |
{ |
| 70 |
statement=> "select auth_tag_to_report from auth_types where authtypecode=?", |
| 71 |
results => [ |
| 72 |
['auth_tag_to_report'], |
| 73 |
[100] |
| 74 |
] |
| 75 |
}, |
| 76 |
{ |
| 77 |
statement=> "select frameworkcode,kohafield,tagfield,tagsubfield from marc_subfield_structure where kohafield > ''", |
| 78 |
results => [ |
| 79 |
['frameworkcode','kohafield', 'tagfield','tagsubfield'], |
| 80 |
['', 'biblio.biblionumber', 999, 'c'], |
| 81 |
['FA', 'biblio.biblionumber', 999, 'c'] |
| 82 |
] |
| 83 |
}, |
| 84 |
{ |
| 85 |
statement=> "select distinct tagfield from marc_subfield_structure where authtypecode=?", |
| 86 |
results => [ |
| 87 |
['tagfield'], |
| 88 |
[100] |
| 89 |
] |
| 90 |
}, |
| 91 |
{ |
| 92 |
statement=> "select distinct tagfield from marc_subfield_structure where authtypecode=?", |
| 93 |
results => [ |
| 94 |
['tagfield'], |
| 95 |
[100] |
| 96 |
] |
| 97 |
}, |
| 98 |
{ |
| 99 |
statement=> "SELECT frameworkcode FROM biblio WHERE biblionumber=?", |
| 100 |
results => [ |
| 101 |
['frameworkcode'], |
| 102 |
[''] |
| 103 |
] |
| 104 |
}, |
| 105 |
{ |
| 106 |
statement=> q{ |
| 107 |
SELECT value |
| 108 |
FROM systempreferences |
| 109 |
WHERE variable = ? |
| 110 |
LIMIT 1 |
| 111 |
}, |
| 112 |
results => [ |
| 113 |
['CataloguingLog'], |
| 114 |
[0] |
| 115 |
] |
| 116 |
} |
| 117 |
)); |
| 118 |
my $mock_session_overwrite = DBD::Mock::Session->new('mock-overwrite' =>( |
| 119 |
{ |
| 120 |
statement=> "select authtypecode from auth_header where authid=?", |
| 121 |
results => [ |
| 122 |
['authtypecode'], |
| 123 |
['PERSON'] |
| 124 |
] |
| 125 |
}, |
| 126 |
{ |
| 127 |
statement=> "select authtypecode from auth_header where authid=?", |
| 128 |
results => [ |
| 129 |
['authtypecode'], |
| 130 |
['CHRON_TERM'] |
| 131 |
] |
| 132 |
}, |
| 133 |
{ |
| 134 |
statement=> "select auth_tag_to_report from auth_types where authtypecode=?", |
| 135 |
results => [ |
| 136 |
['auth_tag_to_report'], |
| 137 |
[100] |
| 138 |
] |
| 139 |
}, |
| 140 |
{ |
| 141 |
statement=> "select auth_tag_to_report from auth_types where authtypecode=?", |
| 142 |
results => [ |
| 143 |
['auth_tag_to_report'], |
| 144 |
[100] |
| 145 |
] |
| 146 |
}, |
| 147 |
{ |
| 148 |
statement=> "select distinct tagfield from marc_subfield_structure where authtypecode=?", |
| 149 |
results => [ |
| 150 |
['tagfield'], |
| 151 |
[100] |
| 152 |
] |
| 153 |
}, |
| 154 |
{ |
| 155 |
statement=> "select distinct tagfield from marc_subfield_structure where authtypecode=?", |
| 156 |
results => [ |
| 157 |
['tagfield'], |
| 158 |
[100] |
| 159 |
] |
| 160 |
}, |
| 161 |
{ |
| 162 |
statement=> "SELECT frameworkcode FROM biblio WHERE biblionumber=?", |
| 163 |
results => [ |
| 164 |
['frameworkcode'], |
| 165 |
[''] |
| 166 |
] |
| 167 |
}, |
| 168 |
{ |
| 169 |
statement=> "select frameworkcode,kohafield,tagfield,tagsubfield from marc_subfield_structure where kohafield > ''", |
| 170 |
results => [ |
| 171 |
['frameworkcode','kohafield', 'tagfield','tagsubfield'], |
| 172 |
['', 'biblio.biblionumber', 999, 'c'], |
| 173 |
['FA', 'biblio.biblionumber', 999, 'c'] |
| 174 |
] |
| 175 |
}, |
| 176 |
{ |
| 177 |
statement=> q{ |
| 178 |
SELECT value |
| 179 |
FROM systempreferences |
| 180 |
WHERE variable = ? |
| 181 |
LIMIT 1 |
| 182 |
}, |
| 183 |
results => [ |
| 184 |
['CataloguingLog'], |
| 185 |
[0] |
| 186 |
] |
| 187 |
} |
| 188 |
)); |
| 189 |
|
| 190 |
my $from = '1'; |
| 191 |
my $MARCfrom = MARC::Record->new(); |
| 192 |
$MARCfrom->append_fields(MARC::Field->new('245', '', '', 'a'=> 'From the depths')); |
| 193 |
$MARCfrom->append_fields(MARC::Field->new('100', '', '', 'a'=>'Bruce Wayne', 'b'=>'2014', '9' => '1')); |
| 194 |
$MARCfrom->append_fields(MARC::Field->new('999', '', '', 'c'=> '1')); |
| 195 |
my $to = '1'; |
| 196 |
my $MARCto = MARC::Record->new(); |
| 197 |
$MARCto->append_fields(MARC::Field->new('245', '', '', 'a'=> 'All the way to heaven')); |
| 198 |
$MARCto->append_fields(MARC::Field->new('100', '', '', 'a'=>'Batman', '9' => '1')); |
| 199 |
$MARCto->append_fields(MARC::Field->new('999', '', '', 'c'=> '2')); |
| 200 |
my @records = ( $MARCfrom, $MARCto ); |
| 201 |
|
| 202 |
my $module_Auth = new Test::MockModule('C4::AuthoritiesMarc'); |
| 203 |
$module_Auth->mock( |
| 204 |
'GetMarcBiblio', |
| 205 |
sub{ |
| 206 |
return $MARCfrom; |
| 207 |
} |
| 208 |
); |
| 209 |
|
| 210 |
my $modified_record; |
| 211 |
$module_Auth->mock( |
| 212 |
'ModBiblio', |
| 213 |
sub{ |
| 214 |
$modified_record = shift; |
| 215 |
} |
| 216 |
); |
| 217 |
|
| 218 |
my $dbh = C4::Context->dbh(); |
| 219 |
$dbh->{mock_session} = $mock_session_no_overwrite; |
| 220 |
|
| 221 |
print "Testing merge() without overwrite\n"; |
| 222 |
my $countEdited = merge($from, $MARCfrom, $to, $MARCto, 0); |
| 223 |
my $auth_field = $modified_record->field(100)->subfield('a'); |
| 224 |
my $test_field = $modified_record->field(100)->subfield('b'); |
| 225 |
is($countEdited, 1, 'One record was edited by merge()'); |
| 226 |
is($auth_field, $MARCto->field(100)->subfield('a'), 'Record values updated correctly'); |
| 227 |
is($test_field, $MARCfrom->field(100)->subfield('b'), 'Record not overwritten while merging'); |
| 228 |
|
| 229 |
$dbh->{mock_session} = $mock_session_overwrite; |
| 230 |
$modified_record = undef; |
| 231 |
|
| 232 |
print "Testing merge() with overwrite\n"; |
| 233 |
$countEdited = merge($from, $MARCfrom, $to, $MARCto, 1); |
| 234 |
$auth_field = $modified_record->field(100)->subfield('a'); |
| 235 |
$test_field = $modified_record->field(100)->subfield('b'); |
| 236 |
is($countEdited, 1, 'One record was edited by merge()'); |
| 237 |
is($auth_field, $MARCto->field(100)->subfield('a'), 'Record values updated correctly'); |
| 238 |
is($test_field, undef, 'Record overwritten while merging'); |
| 239 |
|
| 240 |
sub mock_ZOOM_connection{ |
| 241 |
my $mocked_connection = Test::MockObject->new(); |
| 242 |
$mocked_connection->mock('search', sub{return mock_ZOOM_results()}); |
| 243 |
$mocked_connection->mock('option', sub{undef}); |
| 244 |
return $mocked_connection; |
| 245 |
} |
| 246 |
sub mock_ZOOM_results{ |
| 247 |
my $mocked_results = Test::MockObject->new(); |
| 248 |
$mocked_results->mock('size', sub{return 1}); |
| 249 |
$mocked_results->mock('record',sub{ shift; return mock_ZOOM_record(@_) }); |
| 250 |
$mocked_results->mock('destroy', sub{undef}); |
| 251 |
return $mocked_results; |
| 252 |
} |
| 253 |
sub mock_ZOOM_record{ |
| 254 |
my $mocked_record = Test::MockObject->new(); |
| 255 |
my $record_index = shift; |
| 256 |
$mocked_record->mock('raw', sub{return $records[$record_index]->as_usmarc()}); |
| 257 |
return $mocked_record; |
| 258 |
} |