Bug 34912: Add 14 hours to 1970 date in Account(s).t
[koha.git] / t / db_dependent / Heading.t
1 #!/usr/bin/perl
2 #
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use strict;
19 use warnings;
20
21 use Test::More tests => 4;
22
23 use t::lib::Mocks;
24 use Test::MockModule;
25
26 BEGIN {
27     use_ok('C4::Heading', qw( field valid_heading_subfield ));
28 }
29
30 subtest "MARC21 tests" => sub {
31     plan tests => 8;
32
33     t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
34
35     ok(C4::Heading::valid_heading_subfield('100', 'a'), '100a valid for bib');
36     ok(!C4::Heading::valid_heading_subfield('100', 'e'), '100e not valid for bib');
37
38     ok(C4::Heading::valid_heading_subfield('100', 'a', 1), '100a valid for authority');
39
40     ok(C4::Heading::valid_heading_subfield('110', 'a'), '110a valid for bib');
41     ok(!C4::Heading::valid_heading_subfield('110', 'e'), '110e not valid for bib');
42
43     ok(C4::Heading::valid_heading_subfield('600', 'a'), '600a valid for bib');
44     ok(!C4::Heading::valid_heading_subfield('600', 'e'), '600e not valid for bib');
45
46     ok(!C4::Heading::valid_heading_subfield('012', 'a'), '012a invalid field for bib');
47 };
48
49 subtest "UNIMARC tests" => sub {
50     plan tests => 7;
51
52     t::lib::Mocks::mock_preference('marcflavour', 'UNIMARC');
53
54     ok(C4::Heading::valid_heading_subfield('100', 'a'), '100a valid for bib');
55     ok(!C4::Heading::valid_heading_subfield('100', 'i'), '100i not valid fir bib');
56
57     ok(C4::Heading::valid_heading_subfield('110', 'a'), '110a valid for bib');
58     ok(!C4::Heading::valid_heading_subfield('110', 'i'), '110i not valid for bib');
59
60     ok(C4::Heading::valid_heading_subfield('600', 'a'), '600a valid for bib');
61     ok(!C4::Heading::valid_heading_subfield('600', 'i'), '600i not valid for bib');
62
63     ok(!C4::Heading::valid_heading_subfield('012', 'a'), '012a invalid field for bib');
64 };
65
66 subtest "_search tests" => sub {
67
68     plan tests => 10;
69
70     t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
71     t::lib::Mocks::mock_preference('SearchEngine', 'Elasticsearch');
72     # NOTE: We are testing solely against ES here to make the assumptions simpler while testing
73     # C4/Headings code specifically. The actual query building and searching code should
74     # be covered in other test files
75     my $search = Test::MockModule->new('Koha::SearchEngine::Elasticsearch::Search');
76
77     $search->mock('search_auth_compat', sub {
78         my $self = shift;
79         my $search_query = shift;
80         return ($search_query, 1 );
81     });
82
83     t::lib::Mocks::mock_preference('LinkerConsiderThesaurus', '0');
84
85     my $field = MARC::Field->new( '650', ' ', '0', a => 'Uncles', x => 'Fiction' );
86     my $heading = C4::Heading->new_from_field($field);
87     my ($search_query) = $heading->_search( 'match-heading' );
88     my $terms = $search_query->{query}->{bool}->{must};
89     my $expected_terms = [
90         { term => { 'match-heading.ci_raw' => 'Uncles generalsubdiv Fiction' } },
91     ];
92     is_deeply( $terms, $expected_terms, "Search formed only using heading content, not thesaurus, when LinkerConsiderThesaurus disabled");
93
94
95     t::lib::Mocks::mock_preference('LinkerConsiderThesaurus', '1');
96
97     $field = MARC::Field->new( '650', ' ', '0', a => 'Uncles', x => 'Fiction' );
98     $heading = C4::Heading->new_from_field($field);
99     ($search_query) = $heading->_search( 'match-heading' );
100     $terms = $search_query->{query}->{bool}->{must};
101     $expected_terms = [
102         { term => { 'match-heading.ci_raw' => 'Uncles generalsubdiv Fiction' } },
103         { term => { 'subject-heading-thesaurus.ci_raw' => 'a' } },
104     ];
105     is_deeply( $terms, $expected_terms, "Search formed as expected for a subject with second indicator 0");
106
107     $field = MARC::Field->new( '650', ' ', '3', a => 'Uncles', x => 'Fiction' );
108     $heading = C4::Heading->new_from_field($field);
109     ($search_query) = $heading->_search( 'match-heading' );
110     $terms = $search_query->{query}->{bool}->{must};
111     $expected_terms = [
112         { term => { 'match-heading.ci_raw' => 'Uncles generalsubdiv Fiction' } },
113         { term => { 'subject-heading-thesaurus.ci_raw' => 'd' } },
114     ];
115     is_deeply( $terms, $expected_terms, "Search formed as expected with second indicator 3");
116
117     $field = MARC::Field->new( '650', ' ', '7', a => 'Uncles', x => 'Fiction', 2 => 'special_sauce' );
118     $heading = C4::Heading->new_from_field($field);
119     ($search_query) = $heading->_search( 'match-heading' );
120     $terms = $search_query->{query}->{bool}->{must};
121     $expected_terms = [
122         { term => { 'match-heading.ci_raw' => 'Uncles generalsubdiv Fiction' } },
123         { term => { 'subject-heading-thesaurus.ci_raw' => 'special_sauce' } },
124     ];
125     is_deeply( $terms, $expected_terms, "Search formed as expected with second indicator 7 and subfield 2");
126
127     $field = MARC::Field->new( '650', ' ', '4', a => 'Uncles', x => 'Fiction' );
128     $heading = C4::Heading->new_from_field($field);
129     ($search_query) = $heading->_search( 'match-heading' );
130     $terms = $search_query->{query}->{bool}->{must};
131     $expected_terms = [
132         { term => { 'match-heading.ci_raw' => 'Uncles generalsubdiv Fiction' } },
133         { term => { 'subject-heading-thesaurus.ci_raw' => '|' } },
134     ];
135     is_deeply( $terms, $expected_terms, "Search looks for thesaurus '|' when second indicator 4");
136
137     $field = MARC::Field->new( '100', ' ', '', a => 'Yankovic, Al', d => '1959-,' );
138     $heading = C4::Heading->new_from_field($field);
139     ($search_query) = $heading->_search( 'match-heading' );
140     $terms = $search_query->{query}->{bool}->{must};
141     $expected_terms = [
142         { term => { 'match-heading.ci_raw' => 'Yankovic, Al 1959' } },
143     ];
144     is_deeply( $terms, $expected_terms, "Search formed as expected for a non-subject field with single punctuation mark");
145
146     $field = MARC::Field->new( '100', ' ', '', a => 'Yankovic, Al', d => '1959-,', e => '[author]' );
147     $heading = C4::Heading->new_from_field($field);
148     ($search_query) = $heading->_search( 'match-heading' );
149     $terms = $search_query->{query}->{bool}->{must};
150     $expected_terms = [
151         { term => { 'match-heading.ci_raw' => 'Yankovic, Al 1959' } },
152     ];
153     is_deeply( $terms, $expected_terms, "Search formed as expected for a non-subject field with double punctuation, hyphen+comma");
154
155     $field = MARC::Field->new( '100', ' ', '', a => 'Tolkien, J.R.R.,', e => '[author]' );
156     $heading = C4::Heading->new_from_field($field);
157     ($search_query) = $heading->_search( 'match-heading' );
158     $terms = $search_query->{query}->{bool}->{must};
159     $expected_terms = [
160         { term => { 'match-heading.ci_raw' => 'Tolkien, J.R.R' } },
161     ];
162     is_deeply( $terms, $expected_terms, "Search formed as expected for a non-subject field with double punctuation, period+comma ");
163
164     $search->mock('search_auth_compat', sub {
165         my $self = shift;
166         my $search_query = shift;
167         if(
168             scalar @{$search_query->{query}->{bool}->{must}} == 2 &&
169             $search_query->{query}->{bool}->{must}[1]->{term}->{'subject-heading-thesaurus.ci_raw'} eq 'special_sauce'
170         ){
171             return;
172         }
173         return ($search_query, 1);
174     });
175
176     # Special case where thesaurus defined in subfield 2 should also match record with no thesaurus
177     $field = MARC::Field->new( '650', ' ', '7', a => 'Uncles', x => 'Fiction', 2 => 'special_sauce' );
178     $heading = C4::Heading->new_from_field($field);
179     ($search_query) = $heading->_search( 'match-heading' );
180     $terms = $search_query->{query}->{bool}->{must};
181     $expected_terms = [
182         { term => { 'match-heading.ci_raw' => 'Uncles generalsubdiv Fiction' } },
183         { term => { 'subject-heading-thesaurus.ci_raw' => 'z' } },
184     ];
185     is_deeply( $terms, $expected_terms, "When thesaurus in subfield 2, and nothing is found, we should search again for notdefined (008_11 = z) ");
186
187     t::lib::Mocks::mock_preference('LinkerConsiderThesaurus', '0');
188
189     $search_query = undef;
190     ($search_query) = $heading->_search( 'match-heading' );
191     $terms = $search_query->{query}->{bool}->{must};
192     $expected_terms = [
193         { term => { 'match-heading.ci_raw' => 'Uncles generalsubdiv Fiction' } },
194     ];
195     is_deeply( $terms, $expected_terms, "When thesaurus in subfield 2, and nothing is found, we don't search again if LinkerConsiderThesaurusDisabled");
196
197 };