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;
29 use Test::More tests => 46;
31 use_ok('C4::Members::Attributes');
33 my $schema = Koha::Database->schema;
34 $schema->storage->txn_begin;
35 my $builder = t::lib::TestBuilder->new;
36 my $dbh = C4::Context->dbh;
37 $dbh->{RaiseError} = 1;
39 $dbh->do(q|DELETE FROM issues|);
40 $dbh->do(q|DELETE FROM borrowers|);
41 $dbh->do(q|DELETE FROM borrower_attributes|);
42 $dbh->do(q|DELETE FROM borrower_attribute_types|);
43 my $library = $builder->build({
47 my $patron = $builder->build(
48 { source => 'Borrower',
50 firstname => 'my firstname',
51 surname => 'my surname',
52 branchcode => $library->{branchcode},
56 t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} });
57 my $borrowernumber = $patron->{borrowernumber};
59 my $attribute_type1 = C4::Members::AttributeTypes->new('my code1', 'my description1');
60 $attribute_type1->unique_id(1);
61 $attribute_type1->store();
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();
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;
73 my $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes();
75 #is( @$borrower_attributes, 0, 'GetBorrowerAttributes without the borrower number returns an empty array' );
76 $patron = Koha::Patrons->find($borrowernumber);
77 $borrower_attributes = $patron->get_extended_attributes;
78 is( $borrower_attributes->count, 0, 'GetBorrowerAttributes returns the correct number of borrower attributes' );
82 value => 'my attribute1',
83 code => $attribute_type1->code(),
86 value => 'my attribute2',
87 code => $attribute_type2->code(),
90 value => 'my attribute limited',
91 code => $attribute_type_limited->code(),
95 my $set_borrower_attributes = C4::Members::Attributes::SetBorrowerAttributes();
96 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes();
97 is( @$borrower_attributes, 0, 'SetBorrowerAttributes without arguments does not add borrower attributes' );
99 $set_borrower_attributes = C4::Members::Attributes::SetBorrowerAttributes($borrowernumber);
100 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes();
101 is( @$borrower_attributes, 0, 'SetBorrowerAttributes without the attributes does not add borrower attributes' );
103 $set_borrower_attributes = C4::Members::Attributes::SetBorrowerAttributes($borrowernumber, $attributes);
104 is( $set_borrower_attributes, 1, 'SetBorrowerAttributes returns the success code' );
105 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes();
106 is( @$borrower_attributes, 0, 'GetBorrowerAttributes without the borrower number returns an empty array' );
107 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
108 is( @$borrower_attributes, 3, 'GetBorrowerAttributes returns the correct number of borrower attributes' );
109 is( $borrower_attributes->[0]->{code}, $attributes->[0]->{code}, 'SetBorrowerAttributes stores the correct code correctly' );
110 is( $borrower_attributes->[0]->{description}, $attribute_type1->description(), 'SetBorrowerAttributes stores the field description correctly' );
111 is( $borrower_attributes->[0]->{value}, $attributes->[0]->{value}, 'SetBorrowerAttributes stores the field value correctly' );
112 is( $borrower_attributes->[1]->{code}, $attributes->[1]->{code}, 'SetBorrowerAttributes stores the field code correctly' );
113 is( $borrower_attributes->[1]->{description}, $attribute_type2->description(), 'SetBorrowerAttributes stores the field description correctly' );
114 is( $borrower_attributes->[1]->{value}, $attributes->[1]->{value}, 'SetBorrowerAttributes stores the field value correctly' );
115 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
116 is( @$borrower_attributes, 3, 'GetBorrowerAttributes returns the correct number of borrower attributes' );
120 value => 'my attribute1',
121 code => $attribute_type1->code(),
124 value => 'my attribute2',
125 code => $attribute_type2->code(),
128 C4::Members::Attributes::SetBorrowerAttributes($borrowernumber, $attributes);
129 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
130 is( @$borrower_attributes, 3, 'SetBorrowerAttributes should not have removed the attributes limited to another branch' );
132 # TODO This is not implemented yet
133 #$borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber, undef, 'branch_limited');
134 #is( @$borrower_attributes, 2, 'GetBorrowerAttributes returns the correct number of borrower attributes filtered on library' );
136 $patron = Koha::Patrons->find($borrowernumber);
137 my $extended_attributes = $patron->get_extended_attributes;
138 my $attribute_value = $extended_attributes->search({ code => 'my invalid code' });
139 is( $attribute_value->count, 0, 'non existent attribute should return empty result set');
140 $attribute_value = $patron->get_extended_attribute_value('my invalid code');
141 is( $attribute_value, undef, 'non existent attribute should undef');
143 $attribute_value = $patron->get_extended_attribute_value($attributes->[0]->{code});
144 is( $attribute_value, $attributes->[0]->{value}, 'get_extended_attribute_value returns the correct attribute value' );
145 $attribute_value = $patron->get_extended_attribute_value($attributes->[1]->{code});
146 is( $attribute_value, $attributes->[1]->{value}, 'get_extended_attribute_value returns the correct attribute value' );
150 attribute => 'my attribute3',
151 code => $attribute_type1->code(),
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' );
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 $check_uniqueness = C4::Members::Attributes::CheckUniqueness('my invalid code', $attribute->{attribute});
170 is( $check_uniqueness, 0, 'CheckUniqueness with an invalid argument code returns fale' );
171 $check_uniqueness = C4::Members::Attributes::CheckUniqueness($attribute->{code}, 'new value');
172 is( $check_uniqueness, 1, 'CheckUniqueness with a new value returns true' );
173 $check_uniqueness = C4::Members::Attributes::CheckUniqueness('my invalid code', 'new value');
174 is( $check_uniqueness, 0, 'CheckUniqueness with an invalid argument code and a new value returns false' );
175 $check_uniqueness = C4::Members::Attributes::CheckUniqueness($attributes->[1]->{code}, $attributes->[1]->{value});
176 is( $check_uniqueness, 1, 'CheckUniqueness with an attribute unique_id=0 returns true' );
177 $check_uniqueness = C4::Members::Attributes::CheckUniqueness($attribute->{code}, $attribute->{attribute});
178 is( $check_uniqueness, '', 'CheckUniqueness returns false' );
181 my $borrower_numbers = C4::Members::Attributes::SearchIdMatchingAttribute('attribute1');
182 is( @$borrower_numbers, 0, 'SearchIdMatchingAttribute searchs only in attributes with staff_searchable=1' );
183 for my $attr( split(' ', $attributes->[1]->{value}) ) {
184 $borrower_numbers = C4::Members::Attributes::SearchIdMatchingAttribute($attr);
185 is( $borrower_numbers->[0], $borrowernumber, 'SearchIdMatchingAttribute returns the borrower numbers matching' );
189 C4::Members::Attributes::DeleteBorrowerAttribute();
190 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
191 is( @$borrower_attributes, 3, 'DeleteBorrowerAttribute without arguments deletes nothing' );
192 C4::Members::Attributes::DeleteBorrowerAttribute($borrowernumber);
193 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
194 is( @$borrower_attributes, 3, 'DeleteBorrowerAttribute without the attribute deletes nothing' );
195 C4::Members::Attributes::DeleteBorrowerAttribute(undef, $attribute);
196 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
197 is( @$borrower_attributes, 3, 'DeleteBorrowerAttribute with a undef borrower number deletes nothing' );
199 C4::Members::Attributes::DeleteBorrowerAttribute($borrowernumber, $attribute);
200 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
201 is( @$borrower_attributes, 2, 'DeleteBorrowerAttribute deletes a borrower attribute' );
202 is( $borrower_attributes->[0]->{code}, $attributes->[1]->{code}, 'DeleteBorrowerAttribute deletes the correct entry');
203 is( $borrower_attributes->[0]->{description}, $attribute_type2->description(), 'DeleteBorrowerAttribute deletes the correct entry');
204 is( $borrower_attributes->[0]->{value}, $attributes->[1]->{value}, 'DeleteBorrowerAttribute deletes the correct entry');
206 C4::Members::Attributes::DeleteBorrowerAttribute($borrowernumber, $attributes->[1]);
207 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
208 is( @$borrower_attributes, 1, 'DeleteBorrowerAttribute deletes a borrower attribute' );
210 # Regression tests for bug 16504
211 t::lib::Mocks::mock_userenv({ branchcode => $new_library->{branchcode} });
212 my $another_patron = $builder->build(
213 { source => 'Borrower',
215 firstname => 'my another firstname',
216 surname => 'my another surname',
217 branchcode => $new_library->{branchcode},
223 value => 'my attribute1',
224 code => $attribute_type1->code(),
227 value => 'my attribute2',
228 code => $attribute_type2->code(),
231 value => 'my attribute limited',
232 code => $attribute_type_limited->code(),
235 C4::Members::Attributes::SetBorrowerAttributes($another_patron->{borrowernumber}, $attributes);
236 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($another_patron->{borrowernumber});
237 is( @$borrower_attributes, 3, 'SetBorrowerAttributes should have added the 3 attributes for another patron');
238 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
239 is( @$borrower_attributes, 1, 'SetBorrowerAttributes should not have removed the attributes of other patrons' );