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 => 38;
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 $patron = Koha::Patrons->find($borrowernumber);
74 my $borrower_attributes = $patron->get_extended_attributes;
75 is( $borrower_attributes->count, 0, 'get_extended_attributes returns the correct number of borrower attributes' );
79 value => 'my attribute1',
80 code => $attribute_type1->code(),
83 value => 'my attribute2',
84 code => $attribute_type2->code(),
87 value => 'my attribute limited',
88 code => $attribute_type_limited->code(),
92 my $set_borrower_attributes = C4::Members::Attributes::SetBorrowerAttributes($borrowernumber, $attributes);
93 is( $set_borrower_attributes, 1, 'SetBorrowerAttributes returns the success code' );
94 $borrower_attributes = $patron->get_extended_attributes;
95 is( $borrower_attributes->count, 3, 'get_extended_attributes returns the correct number of borrower attributes' );
96 my $attr_0 = $borrower_attributes->next;
97 is( $attr_0->code, $attributes->[0]->{code}, 'SetBorrowerAttributes stores the correct code correctly' );
98 is( $attr_0->type->description, $attribute_type1->description(), 'SetBorrowerAttributes stores the field description correctly' );
99 is( $attr_0->attribute, $attributes->[0]->{value}, 'SetBorrowerAttributes stores the field value correctly' );
100 my $attr_1 = $borrower_attributes->next;
101 is( $attr_1->code, $attributes->[1]->{code}, 'SetBorrowerAttributes stores the field code correctly' );
102 is( $attr_1->type->description, $attribute_type2->description(), 'SetBorrowerAttributes stores the field description correctly' );
103 is( $attr_1->attribute, $attributes->[1]->{value}, 'SetBorrowerAttributes stores the field value correctly' );
107 value => 'my attribute1',
108 code => $attribute_type1->code(),
111 value => 'my attribute2',
112 code => $attribute_type2->code(),
115 C4::Members::Attributes::SetBorrowerAttributes($borrowernumber, $attributes);
116 $borrower_attributes = $patron->get_extended_attributes;
117 is( $borrower_attributes->count, 3, 'SetBorrowerAttributes should not have removed the attributes limited to another branch' );
119 # TODO This is not implemented yet
120 #$borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber, undef, 'branch_limited');
121 #is( @$borrower_attributes, 2, 'GetBorrowerAttributes returns the correct number of borrower attributes filtered on library' );
123 $patron = Koha::Patrons->find($borrowernumber);
124 my $extended_attributes = $patron->get_extended_attributes;
125 my $attribute_value = $extended_attributes->search({ code => 'my invalid code' });
126 is( $attribute_value->count, 0, 'non existent attribute should return empty result set');
127 $attribute_value = $patron->get_extended_attribute('my invalid code');
128 is( $attribute_value, undef, 'non existent attribute should undef');
130 $attribute_value = $patron->get_extended_attribute($attributes->[0]->{code});
131 is( $attribute_value->attribute, $attributes->[0]->{value}, 'get_extended_attribute returns the correct attribute value' );
132 $attribute_value = $patron->get_extended_attribute($attributes->[1]->{code});
133 is( $attribute_value->attribute, $attributes->[1]->{value}, 'get_extended_attribute returns the correct attribute value' );
137 attribute => 'my attribute3',
138 code => $attribute_type1->code(),
140 C4::Members::Attributes::UpdateBorrowerAttribute($borrowernumber, $attribute);
141 $borrower_attributes = $patron->get_extended_attributes;
142 is( $borrower_attributes->count, 3, 'UpdateBorrowerAttribute does not change the number of borrower attributes' );
143 $attr_0 = $borrower_attributes->next;
144 is( $attr_0->code, $attribute->{code}, 'UpdateBorrowerAttribute updates the field code correctly' );
145 is( $attr_0->type->description, $attribute_type1->description(), 'UpdateBorrowerAttribute updates the field description correctly' );
146 is( $attr_0->attribute, $attribute->{attribute}, 'UpdateBorrowerAttribute updates the field value correctly' );
149 my $check_uniqueness = C4::Members::Attributes::CheckUniqueness();
150 is( $check_uniqueness, 0, 'CheckUniqueness without arguments returns false' );
151 $check_uniqueness = C4::Members::Attributes::CheckUniqueness($attribute->{code});
152 is( $check_uniqueness, 1, 'CheckUniqueness with a valid argument code returns true' );
153 $check_uniqueness = C4::Members::Attributes::CheckUniqueness(undef, $attribute->{attribute});
154 is( $check_uniqueness, 0, 'CheckUniqueness without the argument code returns false' );
155 $check_uniqueness = C4::Members::Attributes::CheckUniqueness('my invalid code');
156 is( $check_uniqueness, 0, 'CheckUniqueness with an invalid argument code returns false' );
157 $check_uniqueness = C4::Members::Attributes::CheckUniqueness('my invalid code', $attribute->{attribute});
158 is( $check_uniqueness, 0, 'CheckUniqueness with an invalid argument code returns fale' );
159 $check_uniqueness = C4::Members::Attributes::CheckUniqueness($attribute->{code}, 'new value');
160 is( $check_uniqueness, 1, 'CheckUniqueness with a new value returns true' );
161 $check_uniqueness = C4::Members::Attributes::CheckUniqueness('my invalid code', 'new value');
162 is( $check_uniqueness, 0, 'CheckUniqueness with an invalid argument code and a new value returns false' );
163 $check_uniqueness = C4::Members::Attributes::CheckUniqueness($attributes->[1]->{code}, $attributes->[1]->{value});
164 is( $check_uniqueness, 1, 'CheckUniqueness with an attribute unique_id=0 returns true' );
165 $check_uniqueness = C4::Members::Attributes::CheckUniqueness($attribute->{code}, $attribute->{attribute});
166 is( $check_uniqueness, '', 'CheckUniqueness returns false' );
169 my $borrower_numbers = C4::Members::Attributes::SearchIdMatchingAttribute('attribute1');
170 is( @$borrower_numbers, 0, 'SearchIdMatchingAttribute searchs only in attributes with staff_searchable=1' );
171 for my $attr( split(' ', $attributes->[1]->{value}) ) {
172 $borrower_numbers = C4::Members::Attributes::SearchIdMatchingAttribute($attr);
173 is( $borrower_numbers->[0], $borrowernumber, 'SearchIdMatchingAttribute returns the borrower numbers matching' );
177 $patron->get_extended_attribute($attribute->{code})->delete;
178 $borrower_attributes = $patron->get_extended_attributes;
179 is( $borrower_attributes->count, 2, 'delete attribute by code' );
180 $attr_0 = $borrower_attributes->next;
181 is( $attr_0->code, $attributes->[1]->{code}, 'delete attribute by code');
182 is( $attr_0->type->description, $attribute_type2->description(), 'delete attribute by code');
183 is( $attr_0->attribute, $attributes->[1]->{value}, 'delete attribute by code');
185 $patron->get_extended_attribute($attributes->[1]->{code})->delete;
186 $borrower_attributes = $patron->get_extended_attributes;
187 is( $borrower_attributes->count, 1, 'delete attribute by code' );
189 # Regression tests for bug 16504
190 t::lib::Mocks::mock_userenv({ branchcode => $new_library->{branchcode} });
191 my $another_patron = $builder->build_object(
192 { class => 'Koha::Patrons',
194 firstname => 'my another firstname',
195 surname => 'my another surname',
196 branchcode => $new_library->{branchcode},
202 value => 'my attribute1',
203 code => $attribute_type1->code(),
206 value => 'my attribute2',
207 code => $attribute_type2->code(),
210 value => 'my attribute limited',
211 code => $attribute_type_limited->code(),
214 C4::Members::Attributes::SetBorrowerAttributes($another_patron->borrowernumber, $attributes);
215 $borrower_attributes = $another_patron->get_extended_attributes;
216 is( $borrower_attributes->count, 3, 'SetBorrowerAttributes should have added the 3 attributes for another patron');
217 $borrower_attributes = $patron->get_extended_attributes;
218 is( $borrower_attributes->count, 1, 'SetBorrowerAttributes should not have removed the attributes of other patrons' );