Bug 5404: C4::Koha - remove subfield_is_koha_internal_p
[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                 mandatory  => 0,
26                 repeatable => 0,
27                 tab        => 0,
28                 lib        => 'a lib',
29               },
30
31             # default for a nonexisting field
32             '099' => {
33                 x => { defaultvalue => $default_x },
34             },
35         };
36     }
37 );
38
39 my $record = MARC::Record->new;
40 $record->leader('03174nam a2200445 a 4500');
41 my @fields = (
42     MARC::Field->new(
43         100, '1', ' ',
44         a => 'Knuth, Donald Ervin',
45         d => '1938',
46     ),
47     MARC::Field->new(
48         245, '1', '4',
49         a => 'The art of computer programming',
50         c => 'Donald E. Knuth.',
51     ),
52     MARC::Field->new(
53         245, '1', '4', a => 'my second title',
54     ),
55 );
56
57 $record->append_fields(@fields);
58
59 C4::Acquisition::FillWithDefaultValues($record);
60
61 my @fields_245 = $record->field(245);
62 is( scalar(@fields_245), 2, 'No new 245 field has been created' );
63 my @subfields_245_0 = $fields_245[0]->subfields;
64 my @subfields_245_1 = $fields_245[1]->subfields;
65 is_deeply(
66     \@subfields_245_0,
67     [ [ 'a', 'The art of computer programming' ], [ 'c', 'Donald E. Knuth.' ] ],
68     'first 245 field has not been updated'
69 );
70 is_deeply(
71     \@subfields_245_1,
72     [ [ 'a', 'my second title' ], [ 'c', $default_author ] ],
73     'second 245 field has a new subfield c with a default value'
74 );
75
76 my @fields_099 = $record->field('099');
77 is( scalar(@fields_099), 1, '1 new 099 field has been created' );
78 my @subfields_099 = $fields_099[0]->subfields;
79 is_deeply(
80     \@subfields_099,
81     [ [ 'x', $default_x ] ],
82     '099$x contains the default value'
83 );