3 # This Koha test module is a stub!
4 # Add more tests here!!!
8 use Test::More tests => 9;
17 use_ok('C4::AuthoritiesMarc');
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' => ''};
26 $module->mock('AddAuthorityTrees', sub {
29 $module->mock('GetAuthority', sub {
31 my $record = MARC::Record->new();
35 [ '151', ' ', ' ', a => 'United States' ]
37 } elsif ($authid eq '2') {
40 [ '151', ' ', ' ', a => 'New York (State)' ],
41 [ '551', ' ', ' ', a => 'United States', w => 'g', 9 => '1' ]
43 } elsif ($authid eq '3') {
46 [ '151', ' ', ' ', a => 'New York (City)' ],
47 [ '551', ' ', ' ', a => 'New York (State)', w => 'g', 9 => '2' ]
49 } elsif ($authid eq '4') {
52 [ '151', ' ', ' ', a => 'New York (City)' ],
53 [ '551', ' ', ' ', a => 'New York (State)', w => 'g' ]
61 my $schema = Koha::Database->new->schema;
62 $schema->storage->txn_begin;
63 my $dbh = C4::Context->dbh;
65 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
67 is(BuildAuthHierarchies(3, 1), '1,2,3', "Built linked authtrees hierarchy string");
69 my $expectedhierarchy = [ [ {
71 'value' => 'United States',
75 'value' => 'New York (State)',
80 'value' => 'New York (City)',
85 'value' => 'New York (State)'
90 'value' => 'United States'
96 is_deeply(GenerateHierarchy(3), $expectedhierarchy, "Generated hierarchy data structure for linked hierarchy");
98 is(BuildAuthHierarchies(4, 1), '4', "Built unlinked authtrees hierarchy string");
99 $expectedhierarchy = [ [ {
101 'current_value' => 1,
102 'value' => 'New York (City)',
107 is_deeply(GenerateHierarchy(4), $expectedhierarchy, "Generated hierarchy data structure for unlinked hierarchy");
109 # set up auth_types for next tests
110 $dbh->do('DELETE FROM auth_types');
112 INSERT INTO auth_types (authtypecode, authtypetext, auth_tag_to_report, summary)
113 VALUES ('GEOGR_NAME', 'Geographic Name', '151', 'Geographic Name')
116 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
117 my $expected_marc21_summary = {
121 'heading' => 'New York (State)',
122 'hemain' => 'New York (State)'
125 'authtypecode' => 'GEOGR_NAME',
126 'mainentry' => 'New York (State)',
127 'mainmainentry' => 'New York (State)',
134 'heading' => 'United States',
135 'hemain' => 'United States',
136 'search' => 'United States',
141 'label' => 'Geographic Name',
142 'type' => 'Geographic Name'
145 BuildSummary(C4::AuthoritiesMarc::GetAuthority(2), 2, 'GEOGR_NAME'),
146 $expected_marc21_summary,
147 'test BuildSummary for MARC21'
150 my $marc21_subdiv = MARC::Record->new();
151 $marc21_subdiv->add_fields(
152 [ '181', ' ', ' ', x => 'Political aspects' ]
154 warning_is { BuildSummary($marc21_subdiv, 99999, 'GEN_SUBDIV') } [],
155 'BuildSummary does not generate warning if main heading subfield not present';
157 t::lib::Mocks::mock_preference('marcflavour', 'UNIMARC');
159 INSERT INTO auth_types (authtypecode, authtypetext, auth_tag_to_report, summary)
160 VALUES ('NP', 'Auteur', '200', '[200a][, 200b][ 200d][ ; 200c][ (200f)]')
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'],
169 my $expected_unimarc_name_summary = {
173 'heading' => 'Fossey Brigitte',
177 'authtypecode' => 'NP',
178 'mainentry' => 'Fossey Brigitte',
179 'mainmainentry' => 'Fossey',
184 'summary' => 'Fossey, Brigitte',
189 BuildSummary($unimarc_name_auth, 99999, 'NP'),
190 $expected_unimarc_name_summary,
191 'test BuildSummary for UNIMARC'
194 subtest 'AddAuthority should respect AUTO_INCREMENT (BZ 18104)' => sub {
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( $id1 );
201 my $id2 = AddAuthority( $record, undef, 'GEOGR_NAME' );
202 is( $id1, $id2, 'FIXME: Got the same id back, let\'s fix that behavior' );
206 $schema->storage->txn_rollback;