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