758fa692e71c33ab901811a513cd359ac6cf1042
[koha.git] / t / db_dependent / Acquisition / FillWithDefaultValues.t
1 use Modern::Perl;
2 use Test::More tests => 5;
3 use Test::MockModule;
4
5 use MARC::Record;
6 use MARC::Field;
7
8 use C4::Context;
9 use C4::Acquisition qw( FillWithDefaultValues );
10
11 my $dbh = C4::Context->dbh;
12 $dbh->{AutoCommit} = 0;
13 $dbh->{RaiseError} = 1;
14
15 my $biblio_module  = Test::MockModule->new('C4::Biblio');
16 my $default_author = 'default author';
17 my $default_x      = 'my default value';
18 $biblio_module->mock(
19     'GetMarcStructure',
20     sub {
21         {
22             # default value for an existing field
23             '245' => {
24                 c => { defaultvalue => $default_author },
25             },
26
27             # default for a nonexisting field
28             '099' => {
29                 x => { defaultvalue => $default_x },
30             },
31         };
32     }
33 );
34
35 my $record = MARC::Record->new;
36 $record->leader('03174nam a2200445 a 4500');
37 my @fields = (
38     MARC::Field->new(
39         100, '1', ' ',
40         a => 'Knuth, Donald Ervin',
41         d => '1938',
42     ),
43     MARC::Field->new(
44         245, '1', '4',
45         a => 'The art of computer programming',
46         c => 'Donald E. Knuth.',
47     ),
48     MARC::Field->new(
49         245, '1', '4', a => 'my second title',
50     ),
51 );
52
53 $record->append_fields(@fields);
54
55 C4::Acquisition::FillWithDefaultValues($record);
56
57 my @fields_245 = $record->field(245);
58 is( scalar(@fields_245), 2, 'No new 245 field has been created' );
59 my @subfields_245_0 = $fields_245[0]->subfields;
60 my @subfields_245_1 = $fields_245[1]->subfields;
61 is_deeply(
62     \@subfields_245_0,
63     [ [ 'a', 'The art of computer programming' ], [ 'c', 'Donald E. Knuth.' ] ],
64     'first 245 field has not been updated'
65 );
66 is_deeply(
67     \@subfields_245_1,
68     [ [ 'a', 'my second title' ], [ 'c', $default_author ] ],
69     'second 245 field has a new subfield c with a default value'
70 );
71
72 my @fields_099 = $record->field('099');
73 is( scalar(@fields_099), 1, '1 new 099 field has been created' );
74 my @subfields_099 = $fields_099[0]->subfields;
75 is_deeply(
76     \@subfields_099,
77     [ [ 'x', $default_x ] ],
78     '099$x contains the default value'
79 );