Bug 26384: Fix executable flags
[koha.git] / t / db_dependent / OAI / AndSets.t
1 #!/usr/bin/perl
2
3 # Copyright 2019 BibLibre
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, see <http://www.gnu.org/licenses>.
18
19 use Modern::Perl;
20
21 use Test::More tests => 4;
22 use Test::MockModule;
23 use Test::Warn;
24 use MARC::Record;
25 use Data::Dumper;
26
27 use Koha::Database;
28 use C4::Biblio;
29 use C4::OAI::Sets;
30
31 use t::lib::TestBuilder;
32
33 my $schema  = Koha::Database->new->schema;
34 $schema->storage->txn_begin;
35 my $dbh = C4::Context->dbh;
36
37 $dbh->do('DELETE FROM oai_sets');
38 $dbh->do('DELETE FROM oai_sets_descriptions');
39 $dbh->do('DELETE FROM oai_sets_mappings');
40 $dbh->do('DELETE FROM oai_sets_biblios');
41
42 my $builder = t::lib::TestBuilder->new;
43
44 my $set1 = {
45     'spec' => 'specSet1',
46     'name' => 'nameSet1',
47 };
48 my $set1_id = AddOAISet($set1);
49
50 my $marcflavour = C4::Context->preference('marcflavour');
51 my $mapping1;
52
53 if ($marcflavour eq 'UNIMARC' ){
54     $mapping1 = [
55         {
56             rule_order => 1,
57             marcfield => '200',
58             marcsubfield => 'a',
59             operator => 'equal',
60             marcvalue => 'myTitle'
61         },
62         {
63             rule_order => 2,
64             rule_operator => 'and',
65             marcfield => '200',
66             marcsubfield => 'f',
67             operator => 'equal',
68             marcvalue => 'myAuthor'
69         },
70     ];
71 } else {
72     $mapping1 = [
73         {
74             rule_order => 1,
75             marcfield => '245',
76             marcsubfield => 'a',
77             operator => 'equal',
78             marcvalue => 'myTitle'
79         },
80         {
81             rule_order => 2,
82             rule_operator => 'and',
83             marcfield => '100',
84             marcsubfield => 'a',
85             operator => 'equal',
86             marcvalue => 'myAuthor'
87         },
88     ];
89 }
90
91 #Add 1st mapping for set1
92 ModOAISetMappings($set1_id, $mapping1);
93
94 my $biblio_1 = $builder->build_sample_biblio({ title => 'myTitle' });
95 my $biblio_2 = $builder->build_sample_biblio({ title => 'myTitle', author => 'myAuthor' });
96
97 my $biblionumber1 = $biblio_1->biblionumber;
98 my $biblionumber2 = $biblio_2->biblionumber;
99
100
101 my $record = GetMarcBiblio({ biblionumber => $biblionumber1 });
102 my @setsEq = CalcOAISetsBiblio($record);
103 ok(!@setsEq, 'If only one condition is true, the record does not belong to the set');
104
105 $record = GetMarcBiblio({ biblionumber => $biblionumber2 });
106 @setsEq = CalcOAISetsBiblio($record);
107 is_deeply(@setsEq, $set1_id, 'If all conditions are true, the record belongs to the set');
108
109 if ($marcflavour eq 'UNIMARC' ){
110     $mapping1 = [
111         {
112             rule_order => 1,
113             marcfield => '200',
114             marcsubfield => 'a',
115             operator => 'equal',
116             marcvalue => 'myTitle'
117         },
118         {
119             rule_order => 2,
120             rule_operator => 'or',
121             marcfield => '200',
122             marcsubfield => 'f',
123             operator => 'equal',
124             marcvalue => 'myAuthor'
125         },
126         {
127             rule_order => 3,
128             rule_operator => 'and',
129             marcfield => '995',
130             marcsubfield => 'r',
131             operator => 'equal',
132             marcvalue => 'myItemType'
133         },
134
135     ];
136 } else {
137     $mapping1 = [
138         {
139             rule_order => 1,
140             marcfield => '245',
141             marcsubfield => 'a',
142             operator => 'equal',
143             marcvalue => 'myTitle'
144         },
145         {
146             rule_order => 2,
147             rule_operator => 'or',
148             marcfield => '100',
149             marcsubfield => 'a',
150             operator => 'equal',
151             marcvalue => 'myAuthor'
152         },
153         {
154             rule_order => 3,
155             rule_operator => 'and',
156             marcfield => '942',
157             marcsubfield => 'c',
158             operator => 'equal',
159             marcvalue => 'myItemType'
160         },
161     ];
162 }
163
164 ModOAISetMappings($set1_id, $mapping1);
165
166 $biblio_1 = $builder->build_sample_biblio({ title => 'myTitle' });
167 $biblio_2 = $builder->build_sample_biblio({ author => 'myAuthor', itemtype => 'myItemType' });
168
169 $biblionumber1 = $biblio_1->biblionumber;
170 $biblionumber2 = $biblio_2->biblionumber;
171
172 $record = GetMarcBiblio({ biblionumber => $biblionumber1 });
173 @setsEq = CalcOAISetsBiblio($record);
174
175 is_deeply(@setsEq, $set1_id, 'Boolean operators precedence is respected, the record with only the title belongs to the set');
176
177 $record = GetMarcBiblio({ biblionumber => $biblionumber2 });
178 @setsEq = CalcOAISetsBiblio($record);
179 is_deeply(@setsEq, $set1_id, 'Boolean operators precedence is respected, the record with author and itemtype belongs to the set');
180
181 $schema->storage->txn_rollback;