Bug 18790: Prevent the tests to fail if precision changes
[koha.git] / t / db_dependent / Members / Attributes.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Copyright 2014  Biblibre SARL
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 C4::Context;
23 use C4::Members;
24 use C4::Members::AttributeTypes;
25 use Koha::Database;
26 use t::lib::TestBuilder;
27
28 use Test::More tests => 55;
29
30 use_ok('C4::Members::Attributes');
31
32 my $schema = Koha::Database->schema;
33 $schema->storage->txn_begin;
34 my $builder = t::lib::TestBuilder->new;
35 my $dbh = C4::Context->dbh;
36 $dbh->{RaiseError} = 1;
37
38 $dbh->do(q|DELETE FROM issues|);
39 $dbh->do(q|DELETE FROM borrowers|);
40 $dbh->do(q|DELETE FROM borrower_attributes|);
41 $dbh->do(q|DELETE FROM borrower_attribute_types|);
42 my $library = $builder->build({
43     source => 'Branch',
44 });
45
46 my $patron = $builder->build(
47     {   source => 'Borrower',
48         value  => {
49             firstname    => 'my firstname',
50             surname      => 'my surname',
51             branchcode => $library->{branchcode},
52         }
53     }
54 );
55 C4::Context->_new_userenv('DUMMY SESSION');
56 C4::Context->set_userenv(123, 'userid', 'usercnum', 'First name', 'Surname', $library->{branchcode}, 'My Library', 0);
57 my $borrowernumber = $patron->{borrowernumber};
58
59 my $attribute_type1 = C4::Members::AttributeTypes->new('my code1', 'my description1');
60 $attribute_type1->unique_id(1);
61 my $attribute_types = C4::Members::Attributes::GetAttributes();
62 is( @$attribute_types, 0, 'GetAttributes returns the correct number of attribute types' );
63 $attribute_type1->store();
64 $attribute_types = C4::Members::Attributes::GetAttributes();
65 is( @$attribute_types, 1, 'GetAttributes returns the correct number of attribute types' );
66 is( $attribute_types->[0], $attribute_type1->code(), 'GetAttributes returns the correct value for code' );
67 $attribute_types = C4::Members::Attributes::GetAttributes(1);
68 is( @$attribute_types, 0, 'GetAttributes returns the correct number of attribute types with the filter opac_only' );
69
70 my $attribute_type2 = C4::Members::AttributeTypes->new('my code2', 'my description2');
71 $attribute_type2->opac_display(1);
72 $attribute_type2->staff_searchable(1);
73 $attribute_type2->store();
74 $attribute_types = C4::Members::Attributes::GetAttributes();
75 is( @$attribute_types, 2, 'GetAttributes returns the correct number of attribute types' );
76 is( $attribute_types->[1], $attribute_type2->code(), 'GetAttributes returns the correct value for code' );
77 $attribute_types = C4::Members::Attributes::GetAttributes(1);
78 is( @$attribute_types, 1, 'GetAttributes returns the correct number of attribute types with the filter opac_only' );
79
80 my $new_library = $builder->build( { source => 'Branch' } );
81 my $attribute_type_limited = C4::Members::AttributeTypes->new('my code3', 'my description3');
82 $attribute_type_limited->branches([ $new_library->{branchcode} ]);
83 $attribute_type_limited->store;
84
85 my $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes();
86 is( @$borrower_attributes, 0, 'GetBorrowerAttributes without the borrower number returns an empty array' );
87 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
88 is( @$borrower_attributes, 0, 'GetBorrowerAttributes returns the correct number of borrower attributes' );
89
90 my $attributes = [
91     {
92         value => 'my attribute1',
93         code => $attribute_type1->code(),
94     },
95     {
96         value => 'my attribute2',
97         code => $attribute_type2->code(),
98     },
99     {
100         value => 'my attribute limited',
101         code => $attribute_type_limited->code(),
102     }
103 ];
104
105 my $set_borrower_attributes = C4::Members::Attributes::SetBorrowerAttributes();
106 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes();
107 is( @$borrower_attributes, 0, 'SetBorrowerAttributes without arguments does not add borrower attributes' );
108
109 $set_borrower_attributes = C4::Members::Attributes::SetBorrowerAttributes($borrowernumber);
110 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes();
111 is( @$borrower_attributes, 0, 'SetBorrowerAttributes without the attributes does not add borrower attributes' );
112
113 $set_borrower_attributes = C4::Members::Attributes::SetBorrowerAttributes($borrowernumber, $attributes);
114 is( $set_borrower_attributes, 1, 'SetBorrowerAttributes returns the success code' );
115 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes();
116 is( @$borrower_attributes, 0, 'GetBorrowerAttributes without the borrower number returns an empty array' );
117 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
118 is( @$borrower_attributes, 3, 'GetBorrowerAttributes returns the correct number of borrower attributes' );
119 is( $borrower_attributes->[0]->{code}, $attributes->[0]->{code}, 'SetBorrowerAttributes stores the correct code correctly' );
120 is( $borrower_attributes->[0]->{description}, $attribute_type1->description(), 'SetBorrowerAttributes stores the field description correctly' );
121 is( $borrower_attributes->[0]->{value}, $attributes->[0]->{value}, 'SetBorrowerAttributes stores the field value correctly' );
122 is( $borrower_attributes->[1]->{code}, $attributes->[1]->{code}, 'SetBorrowerAttributes stores the field code correctly' );
123 is( $borrower_attributes->[1]->{description}, $attribute_type2->description(), 'SetBorrowerAttributes stores the field description correctly' );
124 is( $borrower_attributes->[1]->{value}, $attributes->[1]->{value}, 'SetBorrowerAttributes stores the field value correctly' );
125 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
126 is( @$borrower_attributes, 3, 'GetBorrowerAttributes returns the correct number of borrower attributes' );
127
128 $attributes = [
129     {
130         value => 'my attribute1',
131         code => $attribute_type1->code(),
132     },
133     {
134         value => 'my attribute2',
135         code => $attribute_type2->code(),
136     }
137 ];
138 C4::Members::Attributes::SetBorrowerAttributes($borrowernumber, $attributes);
139 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
140 is( @$borrower_attributes, 3, 'SetBorrowerAttributes should not have removed the attributes limited to another branch' );
141
142 # TODO This is not implemented yet
143 #$borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber, undef, 'branch_limited');
144 #is( @$borrower_attributes, 2, 'GetBorrowerAttributes returns the correct number of borrower attributes filtered on library' );
145
146 my $attribute_value = C4::Members::Attributes::GetBorrowerAttributeValue();
147 is( $attribute_value, undef, 'GetBorrowerAttributeValue without arguments returns undef' );
148 $attribute_value = C4::Members::Attributes::GetBorrowerAttributeValue($borrowernumber);
149 is( $attribute_value, undef, 'GetBorrowerAttributeValue without the attribute code returns undef' );
150 $attribute_value = C4::Members::Attributes::GetBorrowerAttributeValue(undef, $attributes->[0]->{code});
151 is( $attribute_value, undef, 'GetBorrowerAttributeValue with a undef borrower number returns undef' );
152 $attribute_value = C4::Members::Attributes::GetBorrowerAttributeValue($borrowernumber, 'my invalid code');
153 is( $attribute_value, undef, 'GetBorrowerAttributeValue with an invalid code retuns undef' );
154
155 $attribute_value = C4::Members::Attributes::GetBorrowerAttributeValue($borrowernumber, $attributes->[0]->{code});
156 is( $attribute_value, $attributes->[0]->{value}, 'GetBorrowerAttributeValue returns the correct attribute value' );
157 $attribute_value = C4::Members::Attributes::GetBorrowerAttributeValue($borrowernumber, $attributes->[1]->{code});
158 is( $attribute_value, $attributes->[1]->{value}, 'GetBorrowerAttributeValue returns the correct attribute value' );
159
160
161 my $attribute = {
162     attribute => 'my attribute3',
163     code => $attribute_type1->code(),
164 };
165 C4::Members::Attributes::UpdateBorrowerAttribute($borrowernumber, $attribute);
166 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
167 is( @$borrower_attributes, 3, 'UpdateBorrowerAttribute does not change the number of borrower attributes' );
168 is( $borrower_attributes->[0]->{code}, $attribute->{code}, 'UpdateBorrowerAttribute updates the field code correctly' );
169 is( $borrower_attributes->[0]->{description}, $attribute_type1->description(), 'UpdateBorrowerAttribute updates the field description correctly' );
170 is( $borrower_attributes->[0]->{value}, $attribute->{attribute}, 'UpdateBorrowerAttribute updates the field value correctly' );
171
172
173 my $check_uniqueness = C4::Members::Attributes::CheckUniqueness();
174 is( $check_uniqueness, 0, 'CheckUniqueness without arguments returns false' );
175 $check_uniqueness = C4::Members::Attributes::CheckUniqueness($attribute->{code});
176 is( $check_uniqueness, 1, 'CheckUniqueness with a valid argument code returns true' );
177 $check_uniqueness = C4::Members::Attributes::CheckUniqueness(undef, $attribute->{attribute});
178 is( $check_uniqueness, 0, 'CheckUniqueness without the argument code returns false' );
179 $check_uniqueness = C4::Members::Attributes::CheckUniqueness('my invalid code');
180 is( $check_uniqueness, 0, 'CheckUniqueness with an invalid argument code returns false' );
181 $attribute_value = C4::Members::Attributes::GetBorrowerAttributeValue($borrowernumber, $attributes->[1]->{code});
182 $check_uniqueness = C4::Members::Attributes::CheckUniqueness('my invalid code', $attribute->{attribute});
183 is( $check_uniqueness, 0, 'CheckUniqueness with an invalid argument code returns fale' );
184 $check_uniqueness = C4::Members::Attributes::CheckUniqueness($attribute->{code}, 'new value');
185 is( $check_uniqueness, 1, 'CheckUniqueness with a new value returns true' );
186 $check_uniqueness = C4::Members::Attributes::CheckUniqueness('my invalid code', 'new value');
187 is( $check_uniqueness, 0, 'CheckUniqueness with an invalid argument code and a new value returns false' );
188 $check_uniqueness = C4::Members::Attributes::CheckUniqueness($attributes->[1]->{code}, $attributes->[1]->{value});
189 is( $check_uniqueness, 1, 'CheckUniqueness with an attribute unique_id=0 returns true' );
190 $check_uniqueness = C4::Members::Attributes::CheckUniqueness($attribute->{code}, $attribute->{attribute});
191 is( $check_uniqueness, '', 'CheckUniqueness returns false' );
192
193
194 my $borrower_numbers = C4::Members::Attributes::SearchIdMatchingAttribute('attribute1');
195 is( @$borrower_numbers, 0, 'SearchIdMatchingAttribute searchs only in attributes with staff_searchable=1' );
196 for my $attr( split(' ', $attributes->[1]->{value}) ) {
197     $borrower_numbers = C4::Members::Attributes::SearchIdMatchingAttribute($attr);
198     is( $borrower_numbers->[0], $borrowernumber, 'SearchIdMatchingAttribute returns the borrower numbers matching' );
199 }
200
201
202 C4::Members::Attributes::DeleteBorrowerAttribute();
203 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
204 is( @$borrower_attributes, 3, 'DeleteBorrowerAttribute without arguments deletes nothing' );
205 C4::Members::Attributes::DeleteBorrowerAttribute($borrowernumber);
206 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
207 is( @$borrower_attributes, 3, 'DeleteBorrowerAttribute without the attribute deletes nothing' );
208 C4::Members::Attributes::DeleteBorrowerAttribute(undef, $attribute);
209 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
210 is( @$borrower_attributes, 3, 'DeleteBorrowerAttribute with a undef borrower number deletes nothing' );
211
212 C4::Members::Attributes::DeleteBorrowerAttribute($borrowernumber, $attribute);
213 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
214 is( @$borrower_attributes, 2, 'DeleteBorrowerAttribute deletes a borrower attribute' );
215 is( $borrower_attributes->[0]->{code}, $attributes->[1]->{code}, 'DeleteBorrowerAttribute deletes the correct entry');
216 is( $borrower_attributes->[0]->{description}, $attribute_type2->description(), 'DeleteBorrowerAttribute deletes the correct entry');
217 is( $borrower_attributes->[0]->{value}, $attributes->[1]->{value}, 'DeleteBorrowerAttribute deletes the correct entry');
218
219 C4::Members::Attributes::DeleteBorrowerAttribute($borrowernumber, $attributes->[1]);
220 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
221 is( @$borrower_attributes, 1, 'DeleteBorrowerAttribute deletes a borrower attribute' );
222
223 # Regression tests for bug 16504
224 C4::Context->set_userenv(123, 'userid', 'usercnum', 'First name', 'Surname', $new_library->{branchcode}, 'My Library', 0);
225 my $another_patron = $builder->build(
226     {   source => 'Borrower',
227         value  => {
228             firstname    => 'my another firstname',
229             surname      => 'my another surname',
230             branchcode => $new_library->{branchcode},
231         }
232     }
233 );
234 $attributes = [
235     {
236         value => 'my attribute1',
237         code => $attribute_type1->code(),
238     },
239     {
240         value => 'my attribute2',
241         code => $attribute_type2->code(),
242     },
243     {
244         value => 'my attribute limited',
245         code => $attribute_type_limited->code(),
246     }
247 ];
248 C4::Members::Attributes::SetBorrowerAttributes($another_patron->{borrowernumber}, $attributes);
249 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($another_patron->{borrowernumber});
250 is( @$borrower_attributes, 3, 'SetBorrowerAttributes should have added the 3 attributes for another patron');
251 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
252 is( @$borrower_attributes, 1, 'SetBorrowerAttributes should not have removed the attributes of other patrons' );