Bug 32121: (QA follow-up): Fix unit tests count
[koha-ffzg.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
25 BEGIN {
26     use_ok('C4::Heading', qw( field valid_heading_subfield ));
27 }
28
29 subtest "MARC21 tests" => sub {
30     plan tests => 8;
31
32     t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
33
34     ok(C4::Heading::valid_heading_subfield('100', 'a'), '100a valid for bib');
35     ok(!C4::Heading::valid_heading_subfield('100', 'e'), '100e not valid for bib');
36
37     ok(C4::Heading::valid_heading_subfield('100', 'a', 1), '100a valid for authority');
38
39     ok(C4::Heading::valid_heading_subfield('110', 'a'), '110a valid for bib');
40     ok(!C4::Heading::valid_heading_subfield('110', 'e'), '110e not valid for bib');
41
42     ok(C4::Heading::valid_heading_subfield('600', 'a'), '600a valid for bib');
43     ok(!C4::Heading::valid_heading_subfield('600', 'e'), '600e not valid for bib');
44
45     ok(!C4::Heading::valid_heading_subfield('012', 'a'), '012a invalid field for bib');
46 };
47
48 subtest "UNIMARC tests" => sub {
49     plan tests => 7;
50
51     t::lib::Mocks::mock_preference('marcflavour', 'UNIMARC');
52
53     ok(C4::Heading::valid_heading_subfield('100', 'a'), '100a valid for bib');
54     ok(!C4::Heading::valid_heading_subfield('100', 'i'), '100i not valid fir bib');
55
56     ok(C4::Heading::valid_heading_subfield('110', 'a'), '110a valid for bib');
57     ok(!C4::Heading::valid_heading_subfield('110', 'i'), '110i not valid for bib');
58
59     ok(C4::Heading::valid_heading_subfield('600', 'a'), '600a valid for bib');
60     ok(!C4::Heading::valid_heading_subfield('600', 'i'), '600i not valid for bib');
61
62     ok(!C4::Heading::valid_heading_subfield('012', 'a'), '012a invalid field for bib');
63 };
64
65 subtest "_search tests" => sub {
66     plan tests => 3;
67
68     t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
69     t::lib::Mocks::mock_preference('SearchEngine', 'Elasticsearch');
70     my $search = Test::MockModule->new('Koha::SearchEngine::Elasticsearch::Search');
71
72     $search->mock(
73         'search_auth_compat',
74         sub {
75             my $self         = shift;
76             my $search_query = shift;
77             return $search_query;
78         }
79     );
80
81     my ( $field, $heading, $search_query, $terms );
82
83     $field = MARC::Field->new( '100', ' ', '', a => 'Yankovic, Al', d => '1959-,' );
84     $heading = C4::Heading->new_from_field($field);
85     $search_query = $heading->_search( 'match-heading' );
86     $terms = $search_query->{query}->{bool}->{must};
87     is_deeply( $terms->[0], { term => { 'match-heading.ci_raw' => 'Yankovic, Al 1959' } }, "Search formed as expected for a non-subject field with single punctuation mark");
88
89
90     $field = MARC::Field->new( '100', ' ', '', a => 'Yankovic, Al', d => '1959-,', e => '[author]' );
91     $heading = C4::Heading->new_from_field($field);
92     $search_query = $heading->_search( 'match-heading' );
93     $terms = $search_query->{query}->{bool}->{must};
94     is_deeply( $terms->[0], { term => { 'match-heading.ci_raw' => 'Yankovic, Al 1959' } }, "Search formed as expected for a non-subject field with double punctuation, hyphen+comma");
95
96     $field = MARC::Field->new( '100', ' ', '', a => 'Tolkien, J.R.R.,', e => '[author]' );
97     $heading = C4::Heading->new_from_field($field);
98     $search_query = $heading->_search( 'match-heading' );
99     $terms = $search_query->{query}->{bool}->{must};
100     is_deeply( $terms->[0], { term => { 'match-heading.ci_raw' => 'Tolkien, J.R.R' } }, "Search formed as expected for a non-subject field with double punctuation, period+comma ");
101
102 };