|
Lines 1-122
Link Here
|
| 1 |
use Modern::Perl; |
|
|
| 2 |
use Test::NoWarnings; |
| 3 |
use Test::More tests => 13; |
| 4 |
use Test::MockModule; |
| 5 |
|
| 6 |
use MARC::Record; |
| 7 |
use MARC::Field; |
| 8 |
|
| 9 |
use C4::Context; |
| 10 |
use C4::Acquisition qw( FillWithDefaultValues ); |
| 11 |
use Koha::Database; |
| 12 |
|
| 13 |
my $schema = Koha::Database->new->schema; |
| 14 |
$schema->storage->txn_begin; |
| 15 |
|
| 16 |
my $biblio_module = Test::MockModule->new('C4::Biblio'); |
| 17 |
my $default_author = 'default author'; |
| 18 |
my $default_x = 'my default value'; |
| 19 |
$biblio_module->mock( |
| 20 |
'GetMarcStructure', |
| 21 |
sub { |
| 22 |
{ |
| 23 |
# default for a control field |
| 24 |
'008' => { |
| 25 |
x => { defaultvalue => $default_x }, |
| 26 |
}, |
| 27 |
|
| 28 |
# default value for an existing field |
| 29 |
'245' => { |
| 30 |
c => { defaultvalue => $default_author, mandatory => 1 }, |
| 31 |
mandatory => 0, |
| 32 |
repeatable => 0, |
| 33 |
tab => 0, |
| 34 |
lib => 'a lib', |
| 35 |
}, |
| 36 |
|
| 37 |
# default for a nonexisting field |
| 38 |
'099' => { |
| 39 |
x => { defaultvalue => $default_x }, |
| 40 |
}, |
| 41 |
'942' => { |
| 42 |
c => { defaultvalue => 'BK', mandatory => 1 }, |
| 43 |
d => { defaultvalue => '942d_val' }, |
| 44 |
f => { defaultvalue => '942f_val' }, |
| 45 |
}, |
| 46 |
}; |
| 47 |
} |
| 48 |
); |
| 49 |
|
| 50 |
my $record = MARC::Record->new; |
| 51 |
$record->leader('03174nam a2200445 a 4500'); |
| 52 |
my @fields = ( |
| 53 |
MARC::Field->new( |
| 54 |
'008', '1', ' ', |
| 55 |
'@' => '120829t20132012nyu bk 001 0ceng', |
| 56 |
), |
| 57 |
MARC::Field->new( |
| 58 |
100, '1', ' ', |
| 59 |
a => 'Knuth, Donald Ervin', |
| 60 |
d => '1938', |
| 61 |
), |
| 62 |
MARC::Field->new( |
| 63 |
245, '1', '4', |
| 64 |
a => 'The art of computer programming', |
| 65 |
c => 'Donald E. Knuth.', |
| 66 |
), |
| 67 |
MARC::Field->new( |
| 68 |
245, '1', '4', a => 'my second title', |
| 69 |
), |
| 70 |
); |
| 71 |
|
| 72 |
$record->append_fields(@fields); |
| 73 |
|
| 74 |
C4::Acquisition::FillWithDefaultValues($record); |
| 75 |
|
| 76 |
my @fields_245 = $record->field(245); |
| 77 |
is( scalar(@fields_245), 2, 'No new 245 field has been created' ); |
| 78 |
my @subfields_245_0 = $fields_245[0]->subfields; |
| 79 |
my @subfields_245_1 = $fields_245[1]->subfields; |
| 80 |
is_deeply( |
| 81 |
\@subfields_245_0, |
| 82 |
[ [ 'a', 'The art of computer programming' ], [ 'c', 'Donald E. Knuth.' ] ], |
| 83 |
'first 245 field has not been updated' |
| 84 |
); |
| 85 |
is_deeply( |
| 86 |
\@subfields_245_1, |
| 87 |
[ [ 'a', 'my second title' ], [ 'c', $default_author ] ], |
| 88 |
'second 245 field has a new subfield c with a default value' |
| 89 |
); |
| 90 |
|
| 91 |
my @fields_099 = $record->field('099'); |
| 92 |
is( scalar(@fields_099), 1, '1 new 099 field has been created' ); |
| 93 |
my @subfields_099 = $fields_099[0]->subfields; |
| 94 |
is_deeply( |
| 95 |
\@subfields_099, |
| 96 |
[ [ 'x', $default_x ] ], |
| 97 |
'099$x contains the default value' |
| 98 |
); |
| 99 |
|
| 100 |
# Test controlfield default |
| 101 |
$record->field('008')->update(undef); |
| 102 |
C4::Acquisition::FillWithDefaultValues($record); |
| 103 |
is( $record->field('008')->data, $default_x, 'Controlfield got default' ); |
| 104 |
|
| 105 |
is( $record->subfield( '942', 'd' ), '942d_val', 'Check 942d' ); |
| 106 |
|
| 107 |
# Now test only_mandatory parameter |
| 108 |
$record->delete_fields( $record->field('245') ); |
| 109 |
$record->delete_fields( $record->field('942') ); |
| 110 |
$record->append_fields( MARC::Field->new( '942', '', '', 'f' => 'f val' ) ); |
| 111 |
|
| 112 |
# We deleted 245 and replaced 942. If we only apply mandatories, we should get |
| 113 |
# back 245c again and 942c but not 942d. 942f should be left alone. |
| 114 |
C4::Acquisition::FillWithDefaultValues( $record, { only_mandatory => 1 } ); |
| 115 |
@fields_245 = $record->field(245); |
| 116 |
is( scalar @fields_245, 1, 'Only one 245 expected' ); |
| 117 |
is( $record->subfield( '245', 'c' ), $default_author, '245c restored' ); |
| 118 |
is( $record->subfield( '942', 'c' ), 'BK', '942c also restored' ); |
| 119 |
is( $record->subfield( '942', 'd' ), undef, '942d should not be there' ); |
| 120 |
is( $record->subfield( '942', 'f' ), 'f val', '942f untouched' ); |
| 121 |
|
| 122 |
$schema->storage->txn_rollback; |
| 123 |
- |