3 # This file is part of Koha.
5 # Copyright 2014 Biblibre SARL
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>.
24 use C4::Members::AttributeTypes;
26 use t::lib::TestBuilder;
28 use Test::More tests => 57;
30 use t::lib::TestBuilder;
32 use_ok('C4::Members::Attributes');
34 my $schema = Koha::Database->schema;
35 $schema->storage->txn_begin;
36 my $builder = t::lib::TestBuilder->new;
37 my $dbh = C4::Context->dbh;
38 $dbh->{RaiseError} = 1;
40 $dbh->do(q|DELETE FROM issues|);
41 $dbh->do(q|DELETE FROM borrowers|);
42 $dbh->do(q|DELETE FROM borrower_attributes|);
43 $dbh->do(q|DELETE FROM borrower_attribute_types|);
44 my $library = $builder->build({
48 my $patron = $builder->build(
49 { source => 'Borrower',
51 firstname => 'my firstname',
52 surname => 'my surname',
54 branchcode => $library->{branchcode},
58 C4::Context->_new_userenv('DUMMY SESSION');
59 C4::Context->set_userenv(123, 'userid', 'usercnum', 'First name', 'Surname', $library->{branchcode}, 'My Library', 0);
60 my $borrowernumber = $patron->{borrowernumber};
62 my $attribute_type1 = C4::Members::AttributeTypes->new('my code1', 'my description1');
63 $attribute_type1->unique_id(1);
64 my $attribute_types = C4::Members::Attributes::GetAttributes();
65 is( @$attribute_types, 0, 'GetAttributes returns the correct number of attribute types' );
66 $attribute_type1->store();
67 $attribute_types = C4::Members::Attributes::GetAttributes();
68 is( @$attribute_types, 1, 'GetAttributes returns the correct number of attribute types' );
69 is( $attribute_types->[0], $attribute_type1->code(), 'GetAttributes returns the correct value for code' );
70 $attribute_types = C4::Members::Attributes::GetAttributes(1);
71 is( @$attribute_types, 0, 'GetAttributes returns the correct number of attribute types with the filter opac_only' );
73 my $attribute_type2 = C4::Members::AttributeTypes->new('my code2', 'my description2');
74 $attribute_type2->opac_display(1);
75 $attribute_type2->staff_searchable(1);
76 $attribute_type2->store();
77 $attribute_types = C4::Members::Attributes::GetAttributes();
78 is( @$attribute_types, 2, 'GetAttributes returns the correct number of attribute types' );
79 is( $attribute_types->[1], $attribute_type2->code(), 'GetAttributes returns the correct value for code' );
80 $attribute_types = C4::Members::Attributes::GetAttributes(1);
81 is( @$attribute_types, 1, 'GetAttributes returns the correct number of attribute types with the filter opac_only' );
83 my $new_library = $builder->build( { source => 'Branch' } );
84 my $attribute_type_limited = C4::Members::AttributeTypes->new('my code3', 'my description3');
85 $attribute_type_limited->branches([ $new_library->{branchcode} ]);
86 $attribute_type_limited->store;
88 my $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes();
89 is( @$borrower_attributes, 0, 'GetBorrowerAttributes without the borrower number returns an empty array' );
90 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
91 is( @$borrower_attributes, 0, 'GetBorrowerAttributes returns the correct number of borrower attributes' );
95 value => 'my attribute1',
96 code => $attribute_type1->code(),
97 password => 'my password1',
100 value => 'my attribute2',
101 code => $attribute_type2->code(),
102 password => 'my password2',
105 value => 'my attribute limited',
106 code => $attribute_type_limited->code(),
110 my $set_borrower_attributes = C4::Members::Attributes::SetBorrowerAttributes();
111 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes();
112 is( @$borrower_attributes, 0, 'SetBorrowerAttributes without arguments does not add borrower attributes' );
114 $set_borrower_attributes = C4::Members::Attributes::SetBorrowerAttributes($borrowernumber);
115 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes();
116 is( @$borrower_attributes, 0, 'SetBorrowerAttributes without the attributes does not add borrower attributes' );
118 $set_borrower_attributes = C4::Members::Attributes::SetBorrowerAttributes($borrowernumber, $attributes);
119 is( $set_borrower_attributes, 1, 'SetBorrowerAttributes returns the success code' );
120 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes();
121 is( @$borrower_attributes, 0, 'GetBorrowerAttributes without the borrower number returns an empty array' );
122 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
123 is( @$borrower_attributes, 3, 'GetBorrowerAttributes returns the correct number of borrower attributes' );
124 is( $borrower_attributes->[0]->{code}, $attributes->[0]->{code}, 'SetBorrowerAttributes stores the correct code correctly' );
125 is( $borrower_attributes->[0]->{description}, $attribute_type1->description(), 'SetBorrowerAttributes stores the field description correctly' );
126 is( $borrower_attributes->[0]->{value}, $attributes->[0]->{value}, 'SetBorrowerAttributes stores the field value correctly' );
127 is( $borrower_attributes->[0]->{password}, $attributes->[0]->{password}, 'SetBorrowerAttributes stores the field password correctly' );
128 is( $borrower_attributes->[1]->{code}, $attributes->[1]->{code}, 'SetBorrowerAttributes stores the field code correctly' );
129 is( $borrower_attributes->[1]->{description}, $attribute_type2->description(), 'SetBorrowerAttributes stores the field description correctly' );
130 is( $borrower_attributes->[1]->{value}, $attributes->[1]->{value}, 'SetBorrowerAttributes stores the field value correctly' );
131 is( $borrower_attributes->[1]->{password}, $attributes->[1]->{password}, 'SetBorrowerAttributes stores the field password correctly' );
132 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
133 is( @$borrower_attributes, 3, 'GetBorrowerAttributes returns the correct number of borrower attributes' );
137 value => 'my attribute1',
138 code => $attribute_type1->code(),
139 password => 'my password1',
142 value => 'my attribute2',
143 code => $attribute_type2->code(),
144 password => 'my password2',
147 C4::Members::Attributes::SetBorrowerAttributes($borrowernumber, $attributes);
148 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
149 is( @$borrower_attributes, 3, 'SetBorrowerAttributes should not have removed the attributes limited to another branch' );
151 # TODO This is not implemented yet
152 #$borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber, undef, 'branch_limited');
153 #is( @$borrower_attributes, 2, 'GetBorrowerAttributes returns the correct number of borrower attributes filtered on library' );
155 my $attribute_value = C4::Members::Attributes::GetBorrowerAttributeValue();
156 is( $attribute_value, undef, 'GetBorrowerAttributeValue without arguments returns undef' );
157 $attribute_value = C4::Members::Attributes::GetBorrowerAttributeValue($borrowernumber);
158 is( $attribute_value, undef, 'GetBorrowerAttributeValue without the attribute code returns undef' );
159 $attribute_value = C4::Members::Attributes::GetBorrowerAttributeValue(undef, $attributes->[0]->{code});
160 is( $attribute_value, undef, 'GetBorrowerAttributeValue with a undef borrower number returns undef' );
161 $attribute_value = C4::Members::Attributes::GetBorrowerAttributeValue($borrowernumber, 'my invalid code');
162 is( $attribute_value, undef, 'GetBorrowerAttributeValue with an invalid code retuns undef' );
164 $attribute_value = C4::Members::Attributes::GetBorrowerAttributeValue($borrowernumber, $attributes->[0]->{code});
165 is( $attribute_value, $attributes->[0]->{value}, 'GetBorrowerAttributeValue returns the correct attribute value' );
166 $attribute_value = C4::Members::Attributes::GetBorrowerAttributeValue($borrowernumber, $attributes->[1]->{code});
167 is( $attribute_value, $attributes->[1]->{value}, 'GetBorrowerAttributeValue returns the correct attribute value' );
171 attribute => 'my attribute3',
172 code => $attribute_type1->code(),
173 password => 'my password3',
175 C4::Members::Attributes::UpdateBorrowerAttribute($borrowernumber, $attribute);
176 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
177 is( @$borrower_attributes, 3, 'UpdateBorrowerAttribute does not change the number of borrower attributes' );
178 is( $borrower_attributes->[0]->{code}, $attribute->{code}, 'UpdateBorrowerAttribute updates the field code correctly' );
179 is( $borrower_attributes->[0]->{description}, $attribute_type1->description(), 'UpdateBorrowerAttribute updates the field description correctly' );
180 is( $borrower_attributes->[0]->{value}, $attribute->{attribute}, 'UpdateBorrowerAttribute updates the field value correctly' );
181 is( $borrower_attributes->[0]->{password}, $attribute->{password}, 'UpdateBorrowerAttributes updates the field password correctly' );
184 my $check_uniqueness = C4::Members::Attributes::CheckUniqueness();
185 is( $check_uniqueness, 0, 'CheckUniqueness without arguments returns false' );
186 $check_uniqueness = C4::Members::Attributes::CheckUniqueness($attribute->{code});
187 is( $check_uniqueness, 1, 'CheckUniqueness with a valid argument code returns true' );
188 $check_uniqueness = C4::Members::Attributes::CheckUniqueness(undef, $attribute->{attribute});
189 is( $check_uniqueness, 0, 'CheckUniqueness without the argument code returns false' );
190 $check_uniqueness = C4::Members::Attributes::CheckUniqueness('my invalid code');
191 is( $check_uniqueness, 0, 'CheckUniqueness with an invalid argument code returns false' );
192 $attribute_value = C4::Members::Attributes::GetBorrowerAttributeValue($borrowernumber, $attributes->[1]->{code});
193 $check_uniqueness = C4::Members::Attributes::CheckUniqueness('my invalid code', $attribute->{attribute});
194 is( $check_uniqueness, 0, 'CheckUniqueness with an invalid argument code returns fale' );
195 $check_uniqueness = C4::Members::Attributes::CheckUniqueness($attribute->{code}, 'new value');
196 is( $check_uniqueness, 1, 'CheckUniqueness with a new value returns true' );
197 $check_uniqueness = C4::Members::Attributes::CheckUniqueness('my invalid code', 'new value');
198 is( $check_uniqueness, 0, 'CheckUniqueness with an invalid argument code and a new value returns false' );
199 $check_uniqueness = C4::Members::Attributes::CheckUniqueness($attributes->[1]->{code}, $attributes->[1]->{value});
200 is( $check_uniqueness, 1, 'CheckUniqueness with an attribute unique_id=0 returns true' );
201 $check_uniqueness = C4::Members::Attributes::CheckUniqueness($attribute->{code}, $attribute->{attribute});
202 is( $check_uniqueness, '', 'CheckUniqueness returns false' );
205 my $borrower_numbers = C4::Members::Attributes::SearchIdMatchingAttribute('attribute1');
206 is( @$borrower_numbers, 0, 'SearchIdMatchingAttribute searchs only in attributes with staff_searchable=1' );
207 for my $attr( split(' ', $attributes->[1]->{value}) ) {
208 $borrower_numbers = C4::Members::Attributes::SearchIdMatchingAttribute($attr);
209 is( $borrower_numbers->[0], $borrowernumber, 'SearchIdMatchingAttribute returns the borrower numbers matching' );
213 C4::Members::Attributes::DeleteBorrowerAttribute();
214 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
215 is( @$borrower_attributes, 3, 'DeleteBorrowerAttribute without arguments deletes nothing' );
216 C4::Members::Attributes::DeleteBorrowerAttribute($borrowernumber);
217 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
218 is( @$borrower_attributes, 3, 'DeleteBorrowerAttribute without the attribute deletes nothing' );
219 C4::Members::Attributes::DeleteBorrowerAttribute(undef, $attribute);
220 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
221 is( @$borrower_attributes, 3, 'DeleteBorrowerAttribute with a undef borrower number deletes nothing' );
223 C4::Members::Attributes::DeleteBorrowerAttribute($borrowernumber, $attribute);
224 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
225 is( @$borrower_attributes, 2, 'DeleteBorrowerAttribute deletes a borrower attribute' );
226 is( $borrower_attributes->[0]->{code}, $attributes->[1]->{code}, 'DeleteBorrowerAttribute deletes the correct entry');
227 is( $borrower_attributes->[0]->{description}, $attribute_type2->description(), 'DeleteBorrowerAttribute deletes the correct entry');
228 is( $borrower_attributes->[0]->{value}, $attributes->[1]->{value}, 'DeleteBorrowerAttribute deletes the correct entry');
229 is( $borrower_attributes->[0]->{password}, $attributes->[1]->{password}, 'DeleteBorrowerAttribute deletes the correct entry');
231 C4::Members::Attributes::DeleteBorrowerAttribute($borrowernumber, $attributes->[1]);
232 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
233 is( @$borrower_attributes, 1, 'DeleteBorrowerAttribute deletes a borrower attribute' );