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