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 => 39;
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->extended_attributes;
75 is( $borrower_attributes->count, 0, 'extended_attributes returns the correct number of borrower attributes' );
79 attribute => 'my attribute1',
80 code => $attribute_type1->code(),
83 attribute => 'my attribute2',
84 code => $attribute_type2->code(),
87 attribute => 'my attribute limited',
88 code => $attribute_type_limited->code(),
92 $patron->extended_attributes->filter_by_branch_limitations->delete;
93 my $set_borrower_attributes = $patron->extended_attributes($attributes);
94 is( ref($set_borrower_attributes), 'Koha::Patron::Attributes', '');
95 is( $set_borrower_attributes->count, 3, '');
96 $borrower_attributes = $patron->extended_attributes;
97 is( $borrower_attributes->count, 3, 'extended_attributes returns the correct number of borrower attributes' );
98 my $attr_0 = $borrower_attributes->next;
99 is( $attr_0->code, $attributes->[0]->{code}, 'setter for extended_attributes sestores the correct code correctly' );
100 is( $attr_0->type->description, $attribute_type1->description(), 'setter for extended_attributes stores the field description correctly' );
101 is( $attr_0->attribute, $attributes->[0]->{attribute}, 'setter for extended_attributes stores the field value correctly' );
102 my $attr_1 = $borrower_attributes->next;
103 is( $attr_1->code, $attributes->[1]->{code}, 'setter for extended_attributes stores the field code correctly' );
104 is( $attr_1->type->description, $attribute_type2->description(), 'setter for extended_attributes stores the field description correctly' );
105 is( $attr_1->attribute, $attributes->[1]->{attribute}, 'setter for extended_attributes stores the field value correctly' );
109 attribute => 'my attribute1',
110 code => $attribute_type1->code(),
113 attribute => 'my attribute2',
114 code => $attribute_type2->code(),
117 $patron->extended_attributes->filter_by_branch_limitations->delete;
118 $patron->extended_attributes($attributes);
119 $borrower_attributes = $patron->extended_attributes;
120 is( $borrower_attributes->count, 3, 'setter for extended_attributes should not have removed the attributes limited to another branch' );
122 # TODO This is not implemented yet
123 #$borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber, undef, 'branch_limited');
124 #is( @$borrower_attributes, 2, 'GetBorrowerAttributes returns the correct number of borrower attributes filtered on library' );
126 $patron = Koha::Patrons->find($borrowernumber);
127 my $extended_attributes = $patron->extended_attributes;
128 my $attribute_value = $extended_attributes->search({ code => 'my invalid code' });
129 is( $attribute_value->count, 0, 'non existent attribute should return empty result set');
130 $attribute_value = $patron->get_extended_attribute('my invalid code');
131 is( $attribute_value, undef, 'non existent attribute should undef');
133 $attribute_value = $patron->get_extended_attribute($attributes->[0]->{code});
134 is( $attribute_value->attribute, $attributes->[0]->{attribute}, 'get_extended_attribute returns the correct attribute value' );
135 $attribute_value = $patron->get_extended_attribute($attributes->[1]->{code});
136 is( $attribute_value->attribute, $attributes->[1]->{attribute}, 'get_extended_attribute returns the correct attribute value' );
140 attribute => 'my attribute3',
141 code => $attribute_type1->code(),
143 $patron->extended_attributes->search({'me.code' => $attribute->{code}})->filter_by_branch_limitations->delete;
144 $patron->add_extended_attribute($attribute);
145 $borrower_attributes = $patron->extended_attributes;
146 is( $borrower_attributes->count, 3, 'delete then add a new attribute does not change the number of borrower attributes' );
147 $attr_0 = $borrower_attributes->next;
148 is( $attr_0->code, $attribute->{code}, 'delete then add a new attribute updates the field code correctly' );
149 is( $attr_0->type->description, $attribute_type1->description(), 'delete then add a new attribute updates the field description correctly' );
150 is( $attr_0->attribute, $attribute->{attribute}, 'delete then add a new attribute updates the field value correctly' );
153 my $check_uniqueness = C4::Members::Attributes::CheckUniqueness();
154 is( $check_uniqueness, 0, 'CheckUniqueness without arguments returns false' );
155 $check_uniqueness = C4::Members::Attributes::CheckUniqueness($attribute->{code});
156 is( $check_uniqueness, 1, 'CheckUniqueness with a valid argument code returns true' );
157 $check_uniqueness = C4::Members::Attributes::CheckUniqueness(undef, $attribute->{attribute});
158 is( $check_uniqueness, 0, 'CheckUniqueness without the argument code returns false' );
159 $check_uniqueness = C4::Members::Attributes::CheckUniqueness('my invalid code');
160 is( $check_uniqueness, 0, 'CheckUniqueness with an invalid argument code returns false' );
161 $check_uniqueness = C4::Members::Attributes::CheckUniqueness('my invalid code', $attribute->{attribute});
162 is( $check_uniqueness, 0, 'CheckUniqueness with an invalid argument code returns fale' );
163 $check_uniqueness = C4::Members::Attributes::CheckUniqueness($attribute->{code}, 'new value');
164 is( $check_uniqueness, 1, 'CheckUniqueness with a new value returns true' );
165 $check_uniqueness = C4::Members::Attributes::CheckUniqueness('my invalid code', 'new value');
166 is( $check_uniqueness, 0, 'CheckUniqueness with an invalid argument code and a new value returns false' );
167 $check_uniqueness = C4::Members::Attributes::CheckUniqueness($attributes->[1]->{code}, $attributes->[1]->{attribute});
168 is( $check_uniqueness, 1, 'CheckUniqueness with an attribute unique_id=0 returns true' );
169 $check_uniqueness = C4::Members::Attributes::CheckUniqueness($attribute->{code}, $attribute->{attribute});
170 is( $check_uniqueness, '', 'CheckUniqueness returns false' );
173 my $borrower_numbers = C4::Members::Attributes::SearchIdMatchingAttribute('attribute1');
174 is( @$borrower_numbers, 0, 'SearchIdMatchingAttribute searchs only in attributes with staff_searchable=1' );
175 for my $attr( split(' ', $attributes->[1]->{attribute}) ) {
176 $borrower_numbers = C4::Members::Attributes::SearchIdMatchingAttribute($attr);
177 is( $borrower_numbers->[0], $borrowernumber, 'SearchIdMatchingAttribute returns the borrower numbers matching' );
181 $patron->get_extended_attribute($attribute->{code})->delete;
182 $borrower_attributes = $patron->extended_attributes;
183 is( $borrower_attributes->count, 2, 'delete attribute by code' );
184 $attr_0 = $borrower_attributes->next;
185 is( $attr_0->code, $attributes->[1]->{code}, 'delete attribute by code');
186 is( $attr_0->type->description, $attribute_type2->description(), 'delete attribute by code');
187 is( $attr_0->attribute, $attributes->[1]->{attribute}, 'delete attribute by code');
189 $patron->get_extended_attribute($attributes->[1]->{code})->delete;
190 $borrower_attributes = $patron->extended_attributes;
191 is( $borrower_attributes->count, 1, 'delete attribute by code' );
193 # Regression tests for bug 16504
194 t::lib::Mocks::mock_userenv({ branchcode => $new_library->{branchcode} });
195 my $another_patron = $builder->build_object(
196 { class => 'Koha::Patrons',
198 firstname => 'my another firstname',
199 surname => 'my another surname',
200 branchcode => $new_library->{branchcode},
206 attribute => 'my attribute1',
207 code => $attribute_type1->code(),
210 attribute => 'my attribute2',
211 code => $attribute_type2->code(),
214 attribute => 'my attribute limited',
215 code => $attribute_type_limited->code(),
219 $another_patron->extended_attributes->filter_by_branch_limitations->delete;
220 $another_patron->extended_attributes($attributes);
221 $borrower_attributes = $another_patron->extended_attributes;
222 is( $borrower_attributes->count, 3, 'setter for extended_attributes should have added the 3 attributes for another patron');
223 $borrower_attributes = $patron->extended_attributes;
224 is( $borrower_attributes->count, 1, 'setter for extended_attributes should not have removed the attributes of other patrons' );