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