Bug 33159: Unit tests
[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 => 6;
69
70     t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
71     t::lib::Mocks::mock_preference('SearchEngine', 'Elasticsearch');
72     my $search = Test::MockModule->new('Koha::SearchEngine::Elasticsearch::Search');
73
74     $search->mock('search_auth_compat', sub {
75         my $self = shift;
76         my $search_query = shift;
77         return $search_query;
78     });
79
80
81     my $field = MARC::Field->new( '650', ' ', '0', a => 'Uncles', x => 'Fiction' );
82     my $heading = C4::Heading->new_from_field($field);
83     my $search_query = $heading->_search( 'match-heading' );
84     my $terms = $search_query->{query}->{bool}->{must};
85     my $expected_terms = [
86         { term => { 'match-heading.ci_raw' => 'Uncles generalsubdiv Fiction' } },
87         { term => { 'subject-heading-thesaurus.ci_raw' => 'a' } },
88     ];
89     is_deeply( $terms, $expected_terms, "Search formed as expected for a subject with second indicator 0");
90
91     $field = MARC::Field->new( '650', ' ', '3', a => 'Uncles', x => 'Fiction' );
92     $heading = C4::Heading->new_from_field($field);
93     $search_query = $heading->_search( 'match-heading' );
94     $terms = $search_query->{query}->{bool}->{must};
95     $expected_terms = [
96         { term => { 'match-heading.ci_raw' => 'Uncles generalsubdiv Fiction' } },
97         { term => { 'subject-heading-thesaurus.ci_raw' => 'd' } },
98     ];
99     is_deeply( $terms, $expected_terms, "Search formed as expected with second indicator 3");
100
101     $field = MARC::Field->new( '650', ' ', '7', a => 'Uncles', x => 'Fiction', 2 => 'special_sauce' );
102     $heading = C4::Heading->new_from_field($field);
103     $search_query = $heading->_search( 'match-heading' );
104     $terms = $search_query->{query}->{bool}->{must};
105     $expected_terms = [
106         { term => { 'match-heading.ci_raw' => 'Uncles generalsubdiv Fiction' } },
107         { term => { 'subject-heading-thesaurus-conventions.ci_raw' => 'special_sauce' } },
108         { term => { 'subject-heading-thesaurus.ci_raw' => 'z' } },
109     ];
110     is_deeply( $terms, $expected_terms, "Search formed as expected with second indicator 7 and subfield 2");
111
112     $field = MARC::Field->new( '100', ' ', '', a => 'Yankovic, Al', d => '1959-,' );
113     $heading = C4::Heading->new_from_field($field);
114     $search_query = $heading->_search( 'match-heading' );
115     $terms = $search_query->{query}->{bool}->{must};
116     $expected_terms = [
117         { term => { 'match-heading.ci_raw' => 'Yankovic, Al 1959' } },
118         { term => { 'subject-heading-thesaurus.ci_raw' => 'a' } },
119     ];
120     is_deeply( $terms, $expected_terms, "Search formed as expected for a non-subject field with single punctuation mark");
121
122     $field = MARC::Field->new( '100', ' ', '', a => 'Yankovic, Al', d => '1959-,', e => '[author]' );
123     $heading = C4::Heading->new_from_field($field);
124     $search_query = $heading->_search( 'match-heading' );
125     $terms = $search_query->{query}->{bool}->{must};
126     $expected_terms = [
127         { term => { 'match-heading.ci_raw' => 'Yankovic, Al 1959' } },
128         { term => { 'subject-heading-thesaurus.ci_raw' => 'a' } },
129     ];
130     is_deeply( $terms, $expected_terms, "Search formed as expected for a non-subject field with double punctuation, hyphen+comma");
131
132     $field = MARC::Field->new( '100', ' ', '', a => 'Tolkien, J.R.R.,', e => '[author]' );
133     $heading = C4::Heading->new_from_field($field);
134     $search_query = $heading->_search( 'match-heading' );
135     $terms = $search_query->{query}->{bool}->{must};
136     $expected_terms = [
137         { term => { 'match-heading.ci_raw' => 'Tolkien, J.R.R' } },
138         { term => { 'subject-heading-thesaurus.ci_raw' => 'a' } },
139     ];
140     is_deeply( $terms, $expected_terms, "Search formed as expected for a non-subject field with double punctuation, period+comma ");
141
142 };