Bug 12803 - Add ability to skip closed libraries when generating the holds queue
[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 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
65
66 is(BuildAuthHierarchies(3, 1), '1,2,3', "Built linked authtrees hierarchy string");
67
68 my $expectedhierarchy = [ [ {
69         'authid' => '1',
70         'value' => 'United States',
71         'class' => 'child0',
72         'children' => [ {
73             'authid' => '2',
74             'value' => 'New York (State)',
75             'class' => 'child1',
76             'children' => [ {
77                 'authid' => '3',
78                 'current_value' => 1,
79                 'value' => 'New York (City)',
80                 'class' => 'child2',
81                 'children' => [],
82                 'parents' => [ {
83                     'authid' => '2',
84                     'value' => 'New York (State)'
85                 } ]
86             } ],
87             'parents' => [ {
88                 'authid' => '1',
89                 'value' => 'United States'
90             } ]
91         } ],
92         'parents' => []
93 } ] ];
94
95 is_deeply(GenerateHierarchy(3), $expectedhierarchy, "Generated hierarchy data structure for linked hierarchy");
96
97 is(BuildAuthHierarchies(4, 1), '4', "Built unlinked authtrees hierarchy string");
98 $expectedhierarchy = [ [ {
99     'authid' => '4',
100     'current_value' => 1,
101     'value' => 'New York (City)',
102     'class' => 'child0',
103     'children' => [],
104     'parents' => []
105 } ] ];
106 is_deeply(GenerateHierarchy(4), $expectedhierarchy, "Generated hierarchy data structure for unlinked hierarchy");
107
108 # set up auth_types for next tests
109 $dbh->do('DELETE FROM auth_types');
110 $dbh->do(q{
111     INSERT INTO auth_types (authtypecode, authtypetext, auth_tag_to_report, summary)
112     VALUES ('GEOGR_NAME', 'Geographic Name', '151', 'Geographic Name')
113 });
114
115 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
116 my $expected_marc21_summary = {
117     'authorized' => [
118                       {
119                         'field' => '151',
120                         'heading' => 'New York (State)',
121                         'hemain' => 'New York (State)'
122                       }
123                     ],
124     'authtypecode' => 'GEOGR_NAME',
125     'mainentry' => 'New York (State)',
126     'mainmainentry' => 'New York (State)',
127     'notes' => [],
128     'otherscript' => [],
129     'seealso' => [
130                    {
131                      'authid' => '1',
132                      'field' => '551',
133                      'heading' => 'United States',
134                      'hemain' => 'United States',
135                      'search' => 'United States',
136                      'type' => 'broader'
137                    }
138                  ],
139     'seefrom' => [],
140     'label' => 'Geographic Name',
141     'type' => 'Geographic Name'
142 };
143 is_deeply(
144     BuildSummary(C4::AuthoritiesMarc::GetAuthority(2), 2, 'GEOGR_NAME'),
145     $expected_marc21_summary,
146     'test BuildSummary for MARC21'
147 );
148
149 my $marc21_subdiv = MARC::Record->new();
150 $marc21_subdiv->add_fields(
151     [ '181', ' ', ' ', x => 'Political aspects' ]
152 );
153 warning_is { BuildSummary($marc21_subdiv, 99999, 'GEN_SUBDIV') } [],
154     'BuildSummary does not generate warning if main heading subfield not present';
155
156 t::lib::Mocks::mock_preference('marcflavour', 'UNIMARC');
157 $dbh->do(q{
158     INSERT INTO auth_types (authtypecode, authtypetext, auth_tag_to_report, summary)
159     VALUES ('NP', 'Auteur', '200', '[200a][, 200b][ 200d][ ; 200c][ (200f)]')
160 });
161
162 my $unimarc_name_auth = MARC::Record->new();
163 $unimarc_name_auth->add_fields(
164     ['100', ' ', ' ',  a => '20121025              frey50       '],
165     ['200', ' ', ' ',  a => 'Fossey', b => 'Brigitte' ],
166     ['152', ' ', ' ',  a => 'NP'],
167 );
168 my $expected_unimarc_name_summary = {
169     'authorized' => [
170                       {
171                         'field' => '200',
172                         'heading' => 'Fossey Brigitte',
173                         'hemain' => 'Fossey'
174                       }
175                     ],
176     'authtypecode' => 'NP',
177     'mainentry' => 'Fossey Brigitte',
178     'mainmainentry' => 'Fossey',
179     'notes' => [],
180     'otherscript' => [],
181     'seealso' => [],
182     'seefrom' => [],
183     'summary' => 'Fossey, Brigitte',
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;