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 => 8;
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_map = $search_field->search_marc_maps;
77 is(scalar(@marc_map), 1, 'search_marc_maps should return 1 marc map');
78 is($marc_map[0]->index_name, 'biblios', 'Marc map index name is biblios');
79 is($marc_map[0]->marc_type, 'marc21', 'Marc map type is marc21');
80 is($marc_map[0]->marc_field, '410abcdef', 'Marc map field is 410abcdef');
82 ok($search_field->is_mapped_biblios, 'Search field 1 is mapped with biblios');
84 my $sf2 = $builder->build({
85 source => 'SearchField',
88 my $smm3 = $builder->build({
89 source => 'SearchMarcMap',
91 index_name => 'authorities',
92 marc_type => 'marc21',
97 my $smtf3 = $builder->build({
98 source => 'SearchMarcToField',
100 search_marc_map_id => $smm3->{id},
101 search_field_id => $sf2->{id}
105 $search_field = Koha::SearchFields->find($sf2->{id});
106 ok(!$search_field->is_mapped_biblios, 'Search field is mapped with authorities only');
108 my $smm4 = $builder->build({
109 source => 'SearchMarcMap',
111 index_name => 'biblios',
112 marc_type => 'marc21',
117 my $smtf4 = $builder->build({
118 source => 'SearchMarcToField',
120 search_marc_map_id => $smm4->{id},
121 search_field_id => $sf2->{id}
125 $search_field = Koha::SearchFields->find($sf2->{id});
126 ok($search_field->is_mapped_biblios, 'Search field is mapped with authorities and biblios');
128 my $sf3 = $builder->build({
129 source => 'SearchField',
132 $search_field = Koha::SearchFields->find($sf3->{id});
133 ok(!$search_field->is_mapped_biblios, 'Search field is not mapped');
135 $schema->storage->txn_rollback;