Bug 14334: Remove AutoCommit from tests
[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 => 15;
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 });
132
133 $search_field = Koha::SearchFields->find($sf3->{id});
134 ok(!$search_field->is_mapped_biblios, 'Search field is not mapped');
135
136 Koha::SearchFields->search({})->delete;
137
138 $builder->build({
139     source => 'SearchField',
140     value => {
141         name    => 'acqdate',
142         label   => 'acqdate',
143         weight  => undef
144     }
145 });
146
147 $builder->build({
148     source => 'SearchField',
149     value => {
150         name    => 'copydate',
151         label   => 'copydate',
152         weight  => undef
153     }
154 });
155
156 $builder->build({
157     source => 'SearchField',
158     value => {
159         name    => 'ccode',
160         label   => 'ccode',
161         weight  => 0
162     }
163 });
164
165 $builder->build({
166     source => 'SearchField',
167     value => {
168         name    => 'title',
169         label   => 'title',
170         weight  => 25
171     }
172 });
173
174 $builder->build({
175     source => 'SearchField',
176     value => {
177         name    => 'subject',
178         label   => 'subject',
179         weight  => 15
180     }
181 });
182
183 $builder->build({
184     source => 'SearchField',
185     value => {
186         name    => 'author',
187         label   => 'author',
188         weight  => 5
189     }
190 });
191
192 my @w_fields = Koha::SearchFields->weighted_fields();
193 is(scalar(@w_fields), 3, 'weighted_fields should return 3 weighted fields.');
194
195 is($w_fields[0]->name, 'title', 'First field is title.');
196 is($w_fields[0]->weight+0, 25, 'Title weight is 25.');
197 is($w_fields[1]->name, 'subject', 'Second field is subject.');
198 is($w_fields[1]->weight+0, 15, 'Subject weight is 15.');
199 is($w_fields[2]->name, 'author', 'Third field is author.');
200 is($w_fields[2]->weight+0, 5, 'Author weight is 5.');
201
202 $schema->storage->txn_rollback;