775a28d71d716982981fefcf46387e0a1f2de93e
[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 use t::lib::Mocks;
28
29 use Test::More tests => 46;
30
31 use_ok('C4::Members::Attributes');
32
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;
38
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({
44     source => 'Branch',
45 });
46
47 my $patron = $builder->build(
48     {   source => 'Borrower',
49         value  => {
50             firstname    => 'my firstname',
51             surname      => 'my surname',
52             branchcode => $library->{branchcode},
53         }
54     }
55 );
56 t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} });
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 ok(1);
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' );
79
80 my $attributes = [
81     {
82         value => 'my attribute1',
83         code => $attribute_type1->code(),
84     },
85     {
86         value => 'my attribute2',
87         code => $attribute_type2->code(),
88     },
89     {
90         value => 'my attribute limited',
91         code => $attribute_type_limited->code(),
92     }
93 ];
94
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' );
98
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' );
102
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' );
117
118 $attributes = [
119     {
120         value => 'my attribute1',
121         code => $attribute_type1->code(),
122     },
123     {
124         value => 'my attribute2',
125         code => $attribute_type2->code(),
126     }
127 ];
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' );
131
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' );
135
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');
142
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' );
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 $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' );
179
180
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' );
186 }
187
188
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' );
198
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');
205
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' );
209
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',
214         value  => {
215             firstname    => 'my another firstname',
216             surname      => 'my another surname',
217             branchcode => $new_library->{branchcode},
218         }
219     }
220 );
221 $attributes = [
222     {
223         value => 'my attribute1',
224         code => $attribute_type1->code(),
225     },
226     {
227         value => 'my attribute2',
228         code => $attribute_type2->code(),
229     },
230     {
231         value => 'my attribute limited',
232         code => $attribute_type_limited->code(),
233     }
234 ];
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' );