3 # Copyright 2018 Biblibre
5 # This file is part of Koha
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.
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.
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>.
22 use Test::More tests => 10;
25 use Koha::SearchFields;
28 use t::lib::TestBuilder;
30 my $schema = Koha::Database->new->schema;
31 $schema->storage->txn_begin;
33 my $builder = t::lib::TestBuilder->new;
35 t::lib::Mocks::mock_preference('marcflavour', 'marc21');
37 my $sf = $builder->build({
38 source => 'SearchField',
41 my $smm = $builder->build({
42 source => 'SearchMarcMap',
44 index_name => 'biblios',
45 marc_type => 'marc21',
46 marc_field => '410abcdef'
50 my $smtf = $builder->build({
51 source => 'SearchMarcToField',
53 search_marc_map_id => $smm->{id},
54 search_field_id => $sf->{id}
58 my $smm2 = $builder->build({
59 source => 'SearchMarcMap',
61 index_name => 'biblios',
62 marc_type => 'unimarc',
63 marc_field => '410abcdef'
67 my $smtf2 = $builder->build({
68 source => 'SearchMarcToField',
70 search_marc_map_id => $smm2->{id},
71 search_field_id => $sf->{id}
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');
83 ok($search_field->is_mapped_biblios, 'Search field 1 is mapped with biblios');
85 my $sf2 = $builder->build({
86 source => 'SearchField',
89 my $smm3 = $builder->build({
90 source => 'SearchMarcMap',
92 index_name => 'authorities',
93 marc_type => 'marc21',
98 my $smtf3 = $builder->build({
99 source => 'SearchMarcToField',
101 search_marc_map_id => $smm3->{id},
102 search_field_id => $sf2->{id}
106 $search_field = Koha::SearchFields->find($sf2->{id});
107 ok(!$search_field->is_mapped_biblios, 'Search field is mapped with authorities only');
109 my $smm4 = $builder->build({
110 source => 'SearchMarcMap',
112 index_name => 'biblios',
113 marc_type => 'marc21',
118 my $smtf4 = $builder->build({
119 source => 'SearchMarcToField',
121 search_marc_map_id => $smm4->{id},
122 search_field_id => $sf2->{id}
126 $search_field = Koha::SearchFields->find($sf2->{id});
127 ok($search_field->is_mapped_biblios, 'Search field is mapped with authorities and biblios');
129 my $sf3 = $builder->build({
130 source => 'SearchField',
136 $search_field = Koha::SearchFields->find($sf3->{id});
137 ok(!$search_field->is_mapped_biblios, 'Search field is not mapped');
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');
143 Koha::SearchFields->search({})->delete;
145 $schema->storage->txn_rollback;