Bug 9988: Merge should have a parameter hash
[koha.git] / t / db_dependent / AuthoritiesMarc.t
1 #!/usr/bin/perl
2 #
3 # This Koha test module is a stub!  
4 # Add more tests here!!!
5
6 use Modern::Perl;
7
8 use Test::More tests => 9;
9 use Test::MockModule;
10 use Test::Warn;
11 use MARC::Record;
12
13 use t::lib::Mocks;
14 use Koha::Database;
15
16 BEGIN {
17         use_ok('C4::AuthoritiesMarc');
18 }
19
20 # We are now going to be testing the authorities hierarchy code, and
21 # therefore need to pretend that we have consistent data in our database
22 my $module = new Test::MockModule('C4::AuthoritiesMarc');
23 $module->mock('GetHeaderAuthority', sub {
24     return {'authtrees' => ''};
25 });
26 $module->mock('AddAuthorityTrees', sub {
27     return;
28 });
29 $module->mock('GetAuthority', sub {
30     my ($authid) = @_;
31     my $record = MARC::Record->new();
32     if ($authid eq '1') {
33         $record->add_fields(
34             [ '001', '1' ],
35             [ '151', ' ', ' ', a => 'United States' ]
36             );
37     } elsif ($authid eq '2') {
38         $record->add_fields(
39             [ '001', '2' ],
40             [ '151', ' ', ' ', a => 'New York (State)' ],
41             [ '551', ' ', ' ', a => 'United States', w => 'g', 9 => '1' ]
42             );
43     } elsif ($authid eq '3') {
44         $record->add_fields(
45             [ '001', '3' ],
46             [ '151', ' ', ' ', a => 'New York (City)' ],
47             [ '551', ' ', ' ', a => 'New York (State)', w => 'g', 9 => '2' ]
48             );
49     } elsif ($authid eq '4') {
50         $record->add_fields(
51             [ '001', '4' ],
52             [ '151', ' ', ' ', a => 'New York (City)' ],
53             [ '551', ' ', ' ', a => 'New York (State)', w => 'g' ]
54             );
55     } else {
56         undef $record;
57     }
58     return $record;
59 });
60
61 my $schema  = Koha::Database->new->schema;
62 $schema->storage->txn_begin;
63 my $dbh = C4::Context->dbh;
64
65 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
66
67 is(BuildAuthHierarchies(3, 1), '1,2,3', "Built linked authtrees hierarchy string");
68
69 my $expectedhierarchy = [ [ {
70         'authid' => '1',
71         'value' => 'United States',
72         'class' => 'child0',
73         'children' => [ {
74             'authid' => '2',
75             'value' => 'New York (State)',
76             'class' => 'child1',
77             'children' => [ {
78                 'authid' => '3',
79                 'current_value' => 1,
80                 'value' => 'New York (City)',
81                 'class' => 'child2',
82                 'children' => [],
83                 'parents' => [ {
84                     'authid' => '2',
85                     'value' => 'New York (State)'
86                 } ]
87             } ],
88             'parents' => [ {
89                 'authid' => '1',
90                 'value' => 'United States'
91             } ]
92         } ],
93         'parents' => []
94 } ] ];
95
96 is_deeply(GenerateHierarchy(3), $expectedhierarchy, "Generated hierarchy data structure for linked hierarchy");
97
98 is(BuildAuthHierarchies(4, 1), '4', "Built unlinked authtrees hierarchy string");
99 $expectedhierarchy = [ [ {
100     'authid' => '4',
101     'current_value' => 1,
102     'value' => 'New York (City)',
103     'class' => 'child0',
104     'children' => [],
105     'parents' => []
106 } ] ];
107 is_deeply(GenerateHierarchy(4), $expectedhierarchy, "Generated hierarchy data structure for unlinked hierarchy");
108
109 # set up auth_types for next tests
110 $dbh->do('DELETE FROM auth_types');
111 $dbh->do(q{
112     INSERT INTO auth_types (authtypecode, authtypetext, auth_tag_to_report, summary)
113     VALUES ('GEOGR_NAME', 'Geographic Name', '151', 'Geographic Name')
114 });
115
116 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
117 my $expected_marc21_summary = {
118     'authorized' => [
119                       {
120                         'field' => '151',
121                         'heading' => 'New York (State)',
122                         'hemain' => 'New York (State)'
123                       }
124                     ],
125     'authtypecode' => 'GEOGR_NAME',
126     'mainentry' => 'New York (State)',
127     'mainmainentry' => 'New York (State)',
128     'notes' => [],
129     'otherscript' => [],
130     'seealso' => [
131                    {
132                      'authid' => '1',
133                      'field' => '551',
134                      'heading' => 'United States',
135                      'hemain' => 'United States',
136                      'search' => 'United States',
137                      'type' => 'broader'
138                    }
139                  ],
140     'seefrom' => [],
141     'label' => 'Geographic Name',
142     'type' => 'Geographic Name'
143 };
144 is_deeply(
145     BuildSummary(C4::AuthoritiesMarc::GetAuthority(2), 2, 'GEOGR_NAME'),
146     $expected_marc21_summary,
147     'test BuildSummary for MARC21'
148 );
149
150 my $marc21_subdiv = MARC::Record->new();
151 $marc21_subdiv->add_fields(
152     [ '181', ' ', ' ', x => 'Political aspects' ]
153 );
154 warning_is { BuildSummary($marc21_subdiv, 99999, 'GEN_SUBDIV') } [],
155     'BuildSummary does not generate warning if main heading subfield not present';
156
157 t::lib::Mocks::mock_preference('marcflavour', 'UNIMARC');
158 $dbh->do(q{
159     INSERT INTO auth_types (authtypecode, authtypetext, auth_tag_to_report, summary)
160     VALUES ('NP', 'Auteur', '200', '[200a][, 200b][ 200d][ ; 200c][ (200f)]')
161 });
162
163 my $unimarc_name_auth = MARC::Record->new();
164 $unimarc_name_auth->add_fields(
165     ['100', ' ', ' ',  a => '20121025              frey50       '],
166     ['200', ' ', ' ',  a => 'Fossey', b => 'Brigitte' ],
167     ['152', ' ', ' ',  a => 'NP'],
168 );
169 my $expected_unimarc_name_summary = {
170     'authorized' => [
171                       {
172                         'field' => '200',
173                         'heading' => 'Fossey Brigitte',
174                         'hemain' => 'Fossey'
175                       }
176                     ],
177     'authtypecode' => 'NP',
178     'mainentry' => 'Fossey Brigitte',
179     'mainmainentry' => 'Fossey',
180     'notes' => [],
181     'otherscript' => [],
182     'seealso' => [],
183     'seefrom' => [],
184     'summary' => 'Fossey, Brigitte',
185     'type' => 'Auteur'
186 };
187
188 is_deeply(
189     BuildSummary($unimarc_name_auth, 99999, 'NP'),
190     $expected_unimarc_name_summary,
191     'test BuildSummary for UNIMARC'
192 );
193
194 subtest 'AddAuthority should respect AUTO_INCREMENT (BZ 18104)' => sub {
195     plan tests => 3;
196
197     t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' );
198     my $record = C4::AuthoritiesMarc::GetAuthority(1);
199     my $id1 = AddAuthority( $record, undef, 'GEOGR_NAME' );
200     DelAuthority({ authid => $id1 });
201     my $id2 = AddAuthority( $record, undef, 'GEOGR_NAME' );
202     isnt( $id1, $id2, 'Do not return the same id again' );
203     t::lib::Mocks::mock_preference( 'marcflavour', 'UNIMARC' );
204     my $id3 = AddAuthority( $record, undef, 'GEOGR_NAME' );
205     ok( $id3 > 0, 'Tested AddAuthority with UNIMARC' );
206     is( $record->field('001')->data, $id3, 'Check updated 001' );
207 };
208
209 $schema->storage->txn_rollback;