3 # This Koha test module is a stub!
4 # Add more tests here!!!
8 use Test::More tests => 12;
15 use t::lib::TestBuilder;
17 use Koha::Authority::Types;
20 use_ok('C4::AuthoritiesMarc', qw( GetHeaderAuthority AddAuthority AddAuthorityTrees GetAuthority BuildAuthHierarchies GenerateHierarchy BuildSummary DelAuthority CompareFieldWithAuthority ModAuthority merge ));
23 # We are now going to be testing the authorities hierarchy code, and
24 # therefore need to pretend that we have consistent data in our database
25 my $module = Test::MockModule->new('C4::AuthoritiesMarc');
26 $module->mock('GetHeaderAuthority', sub {
27 return {'authtrees' => ''};
29 $module->mock('AddAuthorityTrees', sub {
32 $module->mock('GetAuthority', sub {
34 my $record = MARC::Record->new();
38 [ '151', ' ', ' ', a => 'United States' ]
40 } elsif ($authid eq '2') {
43 [ '151', ' ', ' ', a => 'New York (State)' ],
44 [ '551', ' ', ' ', a => 'United States', w => 'g', 9 => '1' ]
46 } elsif ($authid eq '3') {
49 [ '151', ' ', ' ', a => 'New York (City)' ],
50 [ '551', ' ', ' ', a => 'New York (State)', w => 'g', 9 => '2' ]
52 } elsif ($authid eq '4') {
55 [ '151', ' ', ' ', a => 'New York (City)' ],
56 [ '551', ' ', ' ', a => 'New York (State)', w => 'g' ]
58 } elsif ($authid eq '5') {
61 [ '100', ' ', ' ', a => 'Lastname, Firstname', b => 'b', c => 'c', i => 'i' ]
69 my $schema = Koha::Database->new->schema;
70 $schema->storage->txn_begin;
71 my $dbh = C4::Context->dbh;
72 my $builder = t::lib::TestBuilder->new;
74 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
76 # Authority type GEOGR_NAME is hardcoded here
77 if( ! Koha::Authority::Types->find('GEOGR_NAME') ) {
78 $builder->build({ source => 'AuthType', value => { authtypecode => 'GEOGR_NAME' }});
81 is(BuildAuthHierarchies(3, 1), '1,2,3', "Built linked authtrees hierarchy string");
83 my $expectedhierarchy = [ [ {
85 'value' => 'United States',
89 'value' => 'New York (State)',
94 'value' => 'New York (City)',
99 'value' => 'New York (State)'
104 'value' => 'United States'
110 is_deeply(GenerateHierarchy(3), $expectedhierarchy, "Generated hierarchy data structure for linked hierarchy");
112 is(BuildAuthHierarchies(4, 1), '4', "Built unlinked authtrees hierarchy string");
113 $expectedhierarchy = [ [ {
115 'current_value' => 1,
116 'value' => 'New York (City)',
121 is_deeply(GenerateHierarchy(4), $expectedhierarchy, "Generated hierarchy data structure for unlinked hierarchy");
123 # set up auth_types for next tests
124 $dbh->do('DELETE FROM auth_types');
126 INSERT INTO auth_types (authtypecode, authtypetext, auth_tag_to_report, summary)
127 VALUES ('GEOGR_NAME', 'Geographic Name', '151', 'Geographic Name')
130 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
131 my $expected_marc21_summary = {
135 'heading' => 'New York (State)',
136 'hemain' => 'New York (State)'
139 'authtypecode' => 'GEOGR_NAME',
140 'mainentry' => 'New York (State)',
141 'mainmainentry' => 'New York (State)',
148 'heading' => 'United States',
149 'hemain' => 'United States',
150 'search' => 'United States',
155 'label' => 'Geographic Name',
156 'type' => 'Geographic Name',
161 BuildSummary(C4::AuthoritiesMarc::GetAuthority(2), 2, 'GEOGR_NAME'),
162 $expected_marc21_summary,
163 'test BuildSummary for MARC21'
166 my $marc21_subdiv = MARC::Record->new();
167 $marc21_subdiv->add_fields(
168 [ '181', ' ', ' ', x => 'Political aspects' ]
170 warning_is { BuildSummary($marc21_subdiv, 99999, 'GEN_SUBDIV') } [],
171 'BuildSummary does not generate warning if main heading subfield not present';
173 t::lib::Mocks::mock_preference('marcflavour', 'UNIMARC');
175 INSERT INTO auth_types (authtypecode, authtypetext, auth_tag_to_report, summary)
176 VALUES ('NP', 'Auteur', '200', '[200a][, 200b][ 200d][ ; 200c][ (200f)]')
179 my $unimarc_name_auth = MARC::Record->new();
180 $unimarc_name_auth->add_fields(
181 ['100', ' ', ' ', a => '20121025 frey50 '],
182 ['200', ' ', ' ', a => 'Fossey', b => 'Brigitte' ],
183 ['152', ' ', ' ', a => 'NP'],
185 my $expected_unimarc_name_summary = {
189 'heading' => 'Fossey Brigitte',
193 'authtypecode' => 'NP',
194 'mainentry' => 'Fossey Brigitte',
195 'mainmainentry' => 'Fossey',
200 'summary' => 'Fossey, Brigitte',
206 BuildSummary($unimarc_name_auth, 99999, 'NP'),
207 $expected_unimarc_name_summary,
208 'test BuildSummary for UNIMARC'
211 subtest 'AddAuthority should respect AUTO_INCREMENT (BZ 18104)' => sub {
214 t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' );
215 my $record = C4::AuthoritiesMarc::GetAuthority(1);
216 my $id1 = AddAuthority( $record, undef, 'GEOGR_NAME' );
217 DelAuthority({ authid => $id1 });
218 my $id2 = AddAuthority( $record, undef, 'GEOGR_NAME' );
219 isnt( $id1, $id2, 'Do not return the same id again' );
220 t::lib::Mocks::mock_preference( 'marcflavour', 'UNIMARC' );
221 my $id3 = AddAuthority( $record, undef, 'GEOGR_NAME' );
222 ok( $id3 > 0, 'Tested AddAuthority with UNIMARC' );
223 is( $record->field('001')->data, $id3, 'Check updated 001' );
226 subtest 'CompareFieldWithAuthority tests' => sub {
229 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
231 $builder->build({ source => 'AuthType', value => { authtypecode => 'PERSO_NAME' }});
233 my $field = MARC::Field->new('100', 0, 0, a => 'Lastname, Firstname', b => 'b', c => 'c');
235 ok(C4::AuthoritiesMarc::CompareFieldWithAuthority({'field' => $field, 'authid' => 5}), 'Authority matches');
237 $field->add_subfields(i => 'X');
239 ok(C4::AuthoritiesMarc::CompareFieldWithAuthority({'field' => $field, 'authid' => 5}), 'Compare ignores unlisted subfields');
241 $field->add_subfields(d => 'd');
243 ok(!C4::AuthoritiesMarc::CompareFieldWithAuthority({'field' => $field, 'authid' => 5}), 'Authority does not match');
246 $schema->storage->txn_rollback;
248 $module->unmock('GetAuthority');
250 subtest 'ModAuthority() tests' => sub {
254 $schema->storage->txn_begin;
256 my $auth_type = 'GEOGR_NAME';
257 my $record = MARC::Record->new;
260 [ '151', ' ', ' ', a => 'United States' ]
263 my $auth_id = AddAuthority( $record, undef, $auth_type );
265 my $mocked_authorities_marc = Test::MockModule->new('C4::AuthoritiesMarc');
266 $mocked_authorities_marc->mock( 'merge', sub { warn 'merge called'; } );
269 { ModAuthority( $auth_id, $record, $auth_type ); }
271 'No param, merge called';
274 { ModAuthority( $auth_id, $record, $auth_type, { skip_merge => 1 } ); }
276 'skip_merge passed, merge not called';
278 $schema->storage->txn_rollback;
281 subtest 'DelAuthority() tests' => sub {
285 $schema->storage->txn_begin;
287 my $auth_type = 'GEOGR_NAME';
288 my $record = MARC::Record->new;
291 [ '151', ' ', ' ', a => 'United States' ]
294 my $auth_id = AddAuthority( $record, undef, $auth_type );
296 my $mocked_authorities_marc = Test::MockModule->new('C4::AuthoritiesMarc');
297 $mocked_authorities_marc->mock( 'merge', sub { warn 'merge called'; } );
300 { DelAuthority({ authid => $auth_id }); }
302 'No param, merge called';
304 $auth_id = AddAuthority( $record, undef, $auth_type );
307 { DelAuthority({ authid => $auth_id, skip_merge => 1 }); }
309 'skip_merge passed, merge not called';
311 $schema->storage->txn_rollback;