3 # This Koha test module is a stub!
4 # Add more tests here!!!
8 use Test::More tests => 11;
14 use t::lib::TestBuilder;
16 use Koha::Authority::Types;
19 use_ok('C4::AuthoritiesMarc');
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' => ''};
28 $module->mock('AddAuthorityTrees', sub {
31 $module->mock('GetAuthority', sub {
33 my $record = MARC::Record->new();
37 [ '151', ' ', ' ', a => 'United States' ]
39 } elsif ($authid eq '2') {
42 [ '151', ' ', ' ', a => 'New York (State)' ],
43 [ '551', ' ', ' ', a => 'United States', w => 'g', 9 => '1' ]
45 } elsif ($authid eq '3') {
48 [ '151', ' ', ' ', a => 'New York (City)' ],
49 [ '551', ' ', ' ', a => 'New York (State)', w => 'g', 9 => '2' ]
51 } elsif ($authid eq '4') {
54 [ '151', ' ', ' ', a => 'New York (City)' ],
55 [ '551', ' ', ' ', a => 'New York (State)', w => 'g' ]
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;
68 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
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' }});
75 is(BuildAuthHierarchies(3, 1), '1,2,3', "Built linked authtrees hierarchy string");
77 my $expectedhierarchy = [ [ {
79 'value' => 'United States',
83 'value' => 'New York (State)',
88 'value' => 'New York (City)',
93 'value' => 'New York (State)'
98 'value' => 'United States'
104 is_deeply(GenerateHierarchy(3), $expectedhierarchy, "Generated hierarchy data structure for linked hierarchy");
106 is(BuildAuthHierarchies(4, 1), '4', "Built unlinked authtrees hierarchy string");
107 $expectedhierarchy = [ [ {
109 'current_value' => 1,
110 'value' => 'New York (City)',
115 is_deeply(GenerateHierarchy(4), $expectedhierarchy, "Generated hierarchy data structure for unlinked hierarchy");
117 # set up auth_types for next tests
118 $dbh->do('DELETE FROM auth_types');
120 INSERT INTO auth_types (authtypecode, authtypetext, auth_tag_to_report, summary)
121 VALUES ('GEOGR_NAME', 'Geographic Name', '151', 'Geographic Name')
124 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
125 my $expected_marc21_summary = {
129 'heading' => 'New York (State)',
130 'hemain' => 'New York (State)'
133 'authtypecode' => 'GEOGR_NAME',
134 'mainentry' => 'New York (State)',
135 'mainmainentry' => 'New York (State)',
142 'heading' => 'United States',
143 'hemain' => 'United States',
144 'search' => 'United States',
149 'label' => 'Geographic Name',
150 'type' => 'Geographic Name'
153 BuildSummary(C4::AuthoritiesMarc::GetAuthority(2), 2, 'GEOGR_NAME'),
154 $expected_marc21_summary,
155 'test BuildSummary for MARC21'
158 my $marc21_subdiv = MARC::Record->new();
159 $marc21_subdiv->add_fields(
160 [ '181', ' ', ' ', x => 'Political aspects' ]
162 warning_is { BuildSummary($marc21_subdiv, 99999, 'GEN_SUBDIV') } [],
163 'BuildSummary does not generate warning if main heading subfield not present';
165 t::lib::Mocks::mock_preference('marcflavour', 'UNIMARC');
167 INSERT INTO auth_types (authtypecode, authtypetext, auth_tag_to_report, summary)
168 VALUES ('NP', 'Auteur', '200', '[200a][, 200b][ 200d][ ; 200c][ (200f)]')
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'],
177 my $expected_unimarc_name_summary = {
181 'heading' => 'Fossey Brigitte',
185 'authtypecode' => 'NP',
186 'mainentry' => 'Fossey Brigitte',
187 'mainmainentry' => 'Fossey',
192 'summary' => 'Fossey, Brigitte',
197 BuildSummary($unimarc_name_auth, 99999, 'NP'),
198 $expected_unimarc_name_summary,
199 'test BuildSummary for UNIMARC'
202 subtest 'AddAuthority should respect AUTO_INCREMENT (BZ 18104)' => sub {
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' );
217 $schema->storage->txn_rollback;
219 $module->unmock('GetAuthority');
221 subtest 'ModAuthority() tests' => sub {
225 $schema->storage->txn_begin;
227 my $auth_type = 'GEOGR_NAME';
228 my $record = MARC::Record->new;
231 [ '151', ' ', ' ', a => 'United States' ]
234 my $auth_id = AddAuthority( $record, undef, $auth_type );
236 my $mocked_authorities_marc = Test::MockModule->new('C4::AuthoritiesMarc');
237 $mocked_authorities_marc->mock( 'merge', sub { warn 'merge called'; } );
240 { ModAuthority( $auth_id, $record, $auth_type ); }
242 'No param, merge called';
245 { ModAuthority( $auth_id, $record, $auth_type, { skip_merge => 1 } ); }
247 'skip_merge passed, merge not called';
249 $schema->storage->txn_rollback;
252 subtest 'DelAuthority() tests' => sub {
256 $schema->storage->txn_begin;
258 my $auth_type = 'GEOGR_NAME';
259 my $record = MARC::Record->new;
262 [ '151', ' ', ' ', a => 'United States' ]
265 my $auth_id = AddAuthority( $record, undef, $auth_type );
267 my $mocked_authorities_marc = Test::MockModule->new('C4::AuthoritiesMarc');
268 $mocked_authorities_marc->mock( 'merge', sub { warn 'merge called'; } );
271 { DelAuthority({ authid => $auth_id }); }
273 'No param, merge called';
275 $auth_id = AddAuthority( $record, undef, $auth_type );
278 { DelAuthority({ authid => $auth_id, skip_merge => 1 }); }
280 'skip_merge passed, merge not called';
282 $schema->storage->txn_rollback;