Bug 19532: (follow-up) aria-hidden attr on OPAC, and more
[koha.git] / t / db_dependent / Koha / SearchField.t
1 #!/usr/bin/perl
2
3 # Copyright 2018 Biblibre
4 #
5 # This file is part of Koha
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use Test::More tests => 10;
23
24 use Koha::Database;
25 use Koha::SearchFields;
26
27 use t::lib::Mocks;
28 use t::lib::TestBuilder;
29
30 my $schema = Koha::Database->new->schema;
31 $schema->storage->txn_begin;
32
33 my $builder = t::lib::TestBuilder->new;
34
35 t::lib::Mocks::mock_preference('marcflavour', 'marc21');
36
37 my $sf = $builder->build({
38     source => 'SearchField',
39 });
40
41 my $smm = $builder->build({
42     source => 'SearchMarcMap',
43     value => {
44         index_name  => 'biblios',
45         marc_type   => 'marc21',
46         marc_field  => '410abcdef'
47     }
48 });
49
50 my $smtf = $builder->build({
51     source => 'SearchMarcToField',
52     value => {
53         search_marc_map_id  => $smm->{id},
54         search_field_id     => $sf->{id}
55     }
56 });
57
58 my $smm2 = $builder->build({
59     source => 'SearchMarcMap',
60     value => {
61         index_name  => 'biblios',
62         marc_type   => 'unimarc',
63         marc_field  => '410abcdef'
64     }
65 });
66
67 my $smtf2 = $builder->build({
68     source => 'SearchMarcToField',
69     value => {
70         search_marc_map_id  => $smm2->{id},
71         search_field_id     => $sf->{id}
72     }
73 });
74
75 my $search_field = Koha::SearchFields->find($sf->{id});
76 my $marc_maps = $search_field->search_marc_maps;
77 my $marc_map = $marc_maps->next;
78 is($marc_maps->count, 1, 'search_marc_maps should return 1 marc map');
79 is($marc_map->get_column('index_name'), 'biblios', 'Marc map index name is biblios');
80 is($marc_map->get_column('marc_type'), 'marc21', 'Marc map type is marc21');
81 is($marc_map->get_column('marc_field'), '410abcdef', 'Marc map field is 410abcdef');
82
83 ok($search_field->is_mapped_biblios, 'Search field 1 is mapped with biblios');
84
85 my $sf2 = $builder->build({
86     source => 'SearchField',
87 });
88
89 my $smm3 = $builder->build({
90     source => 'SearchMarcMap',
91     value => {
92         index_name  => 'authorities',
93         marc_type   => 'marc21',
94         marc_field  => '700a'
95     }
96 });
97
98 my $smtf3 = $builder->build({
99     source => 'SearchMarcToField',
100     value => {
101         search_marc_map_id  => $smm3->{id},
102         search_field_id     => $sf2->{id}
103     }
104 });
105
106 $search_field = Koha::SearchFields->find($sf2->{id});
107 ok(!$search_field->is_mapped_biblios, 'Search field is mapped with authorities only');
108
109 my $smm4 = $builder->build({
110     source => 'SearchMarcMap',
111     value => {
112         index_name  => 'biblios',
113         marc_type   => 'marc21',
114         marc_field  => '200a'
115     }
116 });
117
118 my $smtf4 = $builder->build({
119     source => 'SearchMarcToField',
120     value => {
121         search_marc_map_id  => $smm4->{id},
122         search_field_id     => $sf2->{id}
123     }
124 });
125
126 $search_field = Koha::SearchFields->find($sf2->{id});
127 ok($search_field->is_mapped_biblios, 'Search field is mapped with authorities and biblios');
128
129 my $sf3 = $builder->build({
130     source => 'SearchField',
131     value => {
132         mandatory => 1,
133     }
134 });
135
136 $search_field = Koha::SearchFields->find($sf3->{id});
137 ok(!$search_field->is_mapped_biblios, 'Search field is not mapped');
138
139 ok($search_field->mandatory, 'Search field can be marked mandatory');
140 $search_field->mandatory(0)->store;
141 ok(!$search_field->mandatory, 'Search field can be marked not mandatory');
142
143 Koha::SearchFields->search({})->delete;
144
145 $schema->storage->txn_rollback;