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