Bug 25898: Prohibit indirect object notation
[koha.git] / t / db_dependent / AuthoritiesMarc.t
blobe57f6124b177fef781a9cd2a0450b9e284d7726b
1 #!/usr/bin/perl
3 # This Koha test module is a stub!
4 # Add more tests here!!!
6 use Modern::Perl;
8 use Test::More tests => 9;
9 use Test::MockModule;
10 use Test::Warn;
11 use MARC::Record;
13 use t::lib::Mocks;
14 use t::lib::TestBuilder;
15 use Koha::Database;
16 use Koha::Authority::Types;
18 BEGIN {
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 = Test::MockModule->new('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' ]
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' ]
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' ]
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' ]
57 } else {
58 undef $record;
60 return $record;
61 });
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 = [ [ {
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 } ] ];
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 = [ [ {
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");
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')
124 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
125 my $expected_marc21_summary = {
126 'authorized' => [
128 'field' => '151',
129 'heading' => 'New York (State)',
130 'hemain' => 'New York (State)'
133 'authtypecode' => 'GEOGR_NAME',
134 'mainentry' => 'New York (State)',
135 'mainmainentry' => 'New York (State)',
136 'notes' => [],
137 'otherscript' => [],
138 'seealso' => [
140 'authid' => '1',
141 'field' => '551',
142 'heading' => 'United States',
143 'hemain' => 'United States',
144 'search' => 'United States',
145 'type' => 'broader'
148 'seefrom' => [],
149 'label' => 'Geographic Name',
150 'type' => 'Geographic Name'
152 is_deeply(
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');
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)]')
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 = {
178 'authorized' => [
180 'field' => '200',
181 'heading' => 'Fossey Brigitte',
182 'hemain' => 'Fossey'
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'
196 is_deeply(
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 {
203 plan tests => 3;
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;