Lines 1-228
Link Here
|
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 Koha::Database; |
25 |
use t::lib::TestBuilder; |
26 |
use t::lib::Mocks; |
27 |
|
28 |
use Test::Exception; |
29 |
use Test::More tests => 33; |
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 = Koha::Patron::Attribute::Type->new( |
60 |
{ |
61 |
code => 'my code1', |
62 |
description => 'my description1', |
63 |
unique_id => 1 |
64 |
} |
65 |
)->store; |
66 |
my $attribute_type2 = Koha::Patron::Attribute::Type->new( |
67 |
{ |
68 |
code => 'my code2', |
69 |
description => 'my description2', |
70 |
opac_display => 1, |
71 |
staff_searchable => 1 |
72 |
} |
73 |
)->store; |
74 |
|
75 |
my $new_library = $builder->build( { source => 'Branch' } ); |
76 |
my $attribute_type_limited = Koha::Patron::Attribute::Type->new( |
77 |
{ code => 'my code3', description => 'my description3' } )->store; |
78 |
$attribute_type_limited->library_limits( [ $new_library->{branchcode} ] ); |
79 |
|
80 |
$patron = Koha::Patrons->find($borrowernumber); |
81 |
my $borrower_attributes = $patron->extended_attributes; |
82 |
is( $borrower_attributes->count, 0, 'extended_attributes returns the correct number of borrower attributes' ); |
83 |
|
84 |
my $attributes = [ |
85 |
{ |
86 |
attribute => 'my attribute1', |
87 |
code => $attribute_type1->code(), |
88 |
}, |
89 |
{ |
90 |
attribute => 'my attribute2', |
91 |
code => $attribute_type2->code(), |
92 |
}, |
93 |
{ |
94 |
attribute => 'my attribute limited', |
95 |
code => $attribute_type_limited->code(), |
96 |
} |
97 |
]; |
98 |
|
99 |
$patron->extended_attributes->filter_by_branch_limitations->delete; |
100 |
my $set_borrower_attributes = $patron->extended_attributes($attributes); |
101 |
is( ref($set_borrower_attributes), 'Koha::Patron::Attributes', ''); |
102 |
is( $set_borrower_attributes->count, 3, ''); |
103 |
$borrower_attributes = $patron->extended_attributes; |
104 |
is( $borrower_attributes->count, 3, 'extended_attributes returns the correct number of borrower attributes' ); |
105 |
my $attr_0 = $borrower_attributes->next; |
106 |
is( $attr_0->code, $attributes->[0]->{code}, 'setter for extended_attributes sestores the correct code correctly' ); |
107 |
is( $attr_0->type->description, $attribute_type1->description(), 'setter for extended_attributes stores the field description correctly' ); |
108 |
is( $attr_0->attribute, $attributes->[0]->{attribute}, 'setter for extended_attributes stores the field value correctly' ); |
109 |
my $attr_1 = $borrower_attributes->next; |
110 |
is( $attr_1->code, $attributes->[1]->{code}, 'setter for extended_attributes stores the field code correctly' ); |
111 |
is( $attr_1->type->description, $attribute_type2->description(), 'setter for extended_attributes stores the field description correctly' ); |
112 |
is( $attr_1->attribute, $attributes->[1]->{attribute}, 'setter for extended_attributes stores the field value correctly' ); |
113 |
|
114 |
$attributes = [ |
115 |
{ |
116 |
attribute => 'my attribute1', |
117 |
code => $attribute_type1->code(), |
118 |
}, |
119 |
{ |
120 |
attribute => 'my attribute2', |
121 |
code => $attribute_type2->code(), |
122 |
} |
123 |
]; |
124 |
$patron->extended_attributes->filter_by_branch_limitations->delete; |
125 |
$patron->extended_attributes($attributes); |
126 |
$borrower_attributes = $patron->extended_attributes; |
127 |
is( $borrower_attributes->count, 3, 'setter for extended_attributes should not have removed the attributes limited to another branch' ); |
128 |
|
129 |
# TODO This is not implemented yet |
130 |
#$borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber, undef, 'branch_limited'); |
131 |
#is( @$borrower_attributes, 2, 'GetBorrowerAttributes returns the correct number of borrower attributes filtered on library' ); |
132 |
|
133 |
$patron = Koha::Patrons->find($borrowernumber); |
134 |
my $extended_attributes = $patron->extended_attributes; |
135 |
my $attribute_value = $extended_attributes->search({ code => 'my invalid code' }); |
136 |
is( $attribute_value->count, 0, 'non existent attribute should return empty result set'); |
137 |
$attribute_value = $patron->get_extended_attribute('my invalid code'); |
138 |
is( $attribute_value, undef, 'non existent attribute should undef'); |
139 |
|
140 |
$attribute_value = $patron->get_extended_attribute($attributes->[0]->{code}); |
141 |
is( $attribute_value->attribute, $attributes->[0]->{attribute}, 'get_extended_attribute returns the correct attribute value' ); |
142 |
$attribute_value = $patron->get_extended_attribute($attributes->[1]->{code}); |
143 |
is( $attribute_value->attribute, $attributes->[1]->{attribute}, 'get_extended_attribute returns the correct attribute value' ); |
144 |
|
145 |
|
146 |
my $attribute = { |
147 |
attribute => 'my attribute3', |
148 |
code => $attribute_type1->code(), |
149 |
}; |
150 |
$patron->extended_attributes->search({'me.code' => $attribute->{code}})->filter_by_branch_limitations->delete; |
151 |
$patron->add_extended_attribute($attribute); |
152 |
$borrower_attributes = $patron->extended_attributes; |
153 |
is( $borrower_attributes->count, 3, 'delete then add a new attribute does not change the number of borrower attributes' ); |
154 |
$attr_0 = $borrower_attributes->next; |
155 |
is( $attr_0->code, $attribute->{code}, 'delete then add a new attribute updates the field code correctly' ); |
156 |
is( $attr_0->type->description, $attribute_type1->description(), 'delete then add a new attribute updates the field description correctly' ); |
157 |
is( $attr_0->attribute, $attribute->{attribute}, 'delete then add a new attribute updates the field value correctly' ); |
158 |
|
159 |
|
160 |
lives_ok { # Editing, new value, same patron |
161 |
Koha::Patron::Attribute->new( |
162 |
{ |
163 |
code => $attribute->{code}, |
164 |
attribute => 'new value', |
165 |
borrowernumber => $patron->borrowernumber |
166 |
} |
167 |
)->check_unique_id; |
168 |
} 'no exception raised'; |
169 |
lives_ok { # Editing, same value, same patron |
170 |
Koha::Patron::Attribute->new( |
171 |
{ |
172 |
code => $attributes->[1]->{code}, |
173 |
attribute => $attributes->[1]->{attribute}, |
174 |
borrowernumber => $patron->borrowernumber |
175 |
} |
176 |
)->check_unique_id; |
177 |
} 'no exception raised'; |
178 |
throws_ok { # Creating a new one, but already exists! |
179 |
Koha::Patron::Attribute->new( |
180 |
{ code => $attribute->{code}, attribute => $attribute->{attribute} } ) |
181 |
->check_unique_id; |
182 |
} 'Koha::Exceptions::Patron::Attribute::UniqueIDConstraint'; |
183 |
|
184 |
|
185 |
$patron->get_extended_attribute($attribute->{code})->delete; |
186 |
$borrower_attributes = $patron->extended_attributes; |
187 |
is( $borrower_attributes->count, 2, 'delete attribute by code' ); |
188 |
$attr_0 = $borrower_attributes->next; |
189 |
is( $attr_0->code, $attributes->[1]->{code}, 'delete attribute by code'); |
190 |
is( $attr_0->type->description, $attribute_type2->description(), 'delete attribute by code'); |
191 |
is( $attr_0->attribute, $attributes->[1]->{attribute}, 'delete attribute by code'); |
192 |
|
193 |
$patron->get_extended_attribute($attributes->[1]->{code})->delete; |
194 |
$borrower_attributes = $patron->extended_attributes; |
195 |
is( $borrower_attributes->count, 1, 'delete attribute by code' ); |
196 |
|
197 |
# Regression tests for bug 16504 |
198 |
t::lib::Mocks::mock_userenv({ branchcode => $new_library->{branchcode} }); |
199 |
my $another_patron = $builder->build_object( |
200 |
{ class => 'Koha::Patrons', |
201 |
value => { |
202 |
firstname => 'my another firstname', |
203 |
surname => 'my another surname', |
204 |
branchcode => $new_library->{branchcode}, |
205 |
} |
206 |
} |
207 |
); |
208 |
$attributes = [ |
209 |
{ |
210 |
attribute => 'my attribute1', |
211 |
code => $attribute_type1->code(), |
212 |
}, |
213 |
{ |
214 |
attribute => 'my attribute2', |
215 |
code => $attribute_type2->code(), |
216 |
}, |
217 |
{ |
218 |
attribute => 'my attribute limited', |
219 |
code => $attribute_type_limited->code(), |
220 |
} |
221 |
]; |
222 |
|
223 |
$another_patron->extended_attributes->filter_by_branch_limitations->delete; |
224 |
$another_patron->extended_attributes($attributes); |
225 |
$borrower_attributes = $another_patron->extended_attributes; |
226 |
is( $borrower_attributes->count, 3, 'setter for extended_attributes should have added the 3 attributes for another patron'); |
227 |
$borrower_attributes = $patron->extended_attributes; |
228 |
is( $borrower_attributes->count, 1, 'setter for extended_attributes should not have removed the attributes of other patrons' ); |