Line 0
Link Here
|
0 |
- |
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 |
|
26 |
use Test::More tests => 60; |
27 |
|
28 |
use_ok('C4::Members::Attributes'); |
29 |
|
30 |
my $dbh = C4::Context->dbh; |
31 |
$dbh->{AutoCommit} = 0; |
32 |
$dbh->{RaiseError} = 1; |
33 |
|
34 |
$dbh->do(q|DELETE FROM borrowers|); |
35 |
$dbh->do(q|DELETE FROM borrower_attributes|); |
36 |
$dbh->do(q|DELETE FROM borrower_attribute_types|); |
37 |
|
38 |
my $borrowernumber = AddMember( |
39 |
firstname => 'my firstname', |
40 |
surname => 'my surname', |
41 |
categorycode => 'S', |
42 |
branchcode => 'CPL', |
43 |
); |
44 |
|
45 |
|
46 |
my $attribute_type1 = C4::Members::AttributeTypes->new('my code1', 'my description1'); |
47 |
$attribute_type1->unique_id(1); |
48 |
my $attribute_type2 = C4::Members::AttributeTypes->new('my code2', 'my description2'); |
49 |
$attribute_type2->opac_display(1); |
50 |
$attribute_type2->staff_searchable(1); |
51 |
|
52 |
my $attribute_types = C4::Members::Attributes::GetAttributes(); |
53 |
is( @$attribute_types, 0, 'GetAttributes returns the correct number of attribute types' ); |
54 |
|
55 |
$attribute_type1->store(); |
56 |
$attribute_types = C4::Members::Attributes::GetAttributes(); |
57 |
is( @$attribute_types, 1, 'GetAttributes returns the correct number of attribute types' ); |
58 |
is( $attribute_types->[0], $attribute_type1->code(), 'GetAttributes returns the correct value for code' ); |
59 |
$attribute_types = C4::Members::Attributes::GetAttributes(1); |
60 |
is( @$attribute_types, 0, 'GetAttributes returns the correct number of attribute types with the filter opac_only' ); |
61 |
|
62 |
$attribute_type2->store(); |
63 |
$attribute_types = C4::Members::Attributes::GetAttributes(); |
64 |
is( @$attribute_types, 2, 'GetAttributes returns the correct number of attribute types' ); |
65 |
is( $attribute_types->[1], $attribute_type2->code(), 'GetAttributes returns the correct value for code' ); |
66 |
$attribute_types = C4::Members::Attributes::GetAttributes(1); |
67 |
is( @$attribute_types, 1, 'GetAttributes returns the correct number of attribute types with the filter opac_only' ); |
68 |
|
69 |
|
70 |
my $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes(); |
71 |
is( @$borrower_attributes, 0, 'GetBorrowerAttributes without the borrower number returns an empty array' ); |
72 |
$borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber); |
73 |
is( @$borrower_attributes, 0, 'GetBorrowerAttributes returns the correct number of borrower attributes' ); |
74 |
|
75 |
my $attributes = [ |
76 |
{ |
77 |
value => 'my attribute1', |
78 |
code => $attribute_type1->code(), |
79 |
password => 'my password1', |
80 |
}, |
81 |
{ |
82 |
value => 'my attribute2', |
83 |
code => $attribute_type2->code(), |
84 |
password => 'my password2', |
85 |
} |
86 |
]; |
87 |
|
88 |
my $set_borrower_attributes = C4::Members::Attributes::SetBorrowerAttributes(); |
89 |
$borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes(); |
90 |
is( @$borrower_attributes, 0, 'SetBorrowerAttributes without arguments does not add borrower attributes' ); |
91 |
|
92 |
$set_borrower_attributes = C4::Members::Attributes::SetBorrowerAttributes($borrowernumber); |
93 |
$borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes(); |
94 |
is( @$borrower_attributes, 0, 'SetBorrowerAttributes without the attributes does not add borrower attributes' ); |
95 |
|
96 |
$set_borrower_attributes = C4::Members::Attributes::SetBorrowerAttributes($borrowernumber, $attributes); |
97 |
is( $set_borrower_attributes, 1, 'SetBorrowerAttributes returns the success code' ); |
98 |
$borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes(); |
99 |
is( @$borrower_attributes, 0, 'GetBorrowerAttributes without the borrower number returns an empty array' ); |
100 |
$borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber); |
101 |
is( @$borrower_attributes, 2, 'GetBorrowerAttributes returns the correct number of borrower attributes' ); |
102 |
is( $borrower_attributes->[0]->{code}, $attributes->[0]->{code}, 'SetBorrowerAttributes stores the correct code correctly' ); |
103 |
is( $borrower_attributes->[0]->{description}, $attribute_type1->description(), 'SetBorrowerAttributes stores the field description correctly' ); |
104 |
is( $borrower_attributes->[0]->{value}, $attributes->[0]->{value}, 'SetBorrowerAttributes stores the field value correctly' ); |
105 |
is( $borrower_attributes->[0]->{password}, $attributes->[0]->{password}, 'SetBorrowerAttributes stores the field password correctly' ); |
106 |
is( $borrower_attributes->[1]->{code}, $attributes->[1]->{code}, 'SetBorrowerAttributes stores the field code correctly' ); |
107 |
is( $borrower_attributes->[1]->{description}, $attribute_type2->description(), 'SetBorrowerAttributes stores the field description correctly' ); |
108 |
is( $borrower_attributes->[1]->{value}, $attributes->[1]->{value}, 'SetBorrowerAttributes stores the field value correctly' ); |
109 |
is( $borrower_attributes->[1]->{password}, $attributes->[1]->{password}, 'SetBorrowerAttributes stores the field password correctly' ); |
110 |
|
111 |
$borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber, 1); |
112 |
is( @$borrower_attributes, 1, 'GetBorrowerAttributes returns the correct number of borrower attributes with the filter opac_only' ); |
113 |
is( $borrower_attributes->[0]->{code}, $attributes->[1]->{code}, 'GetBorrowerAttributes returns the correct code' ); |
114 |
is( $borrower_attributes->[0]->{description}, $attribute_type2->description(), 'GetBorrowerAttributes returns the correct description' ); |
115 |
is( $borrower_attributes->[0]->{value}, $attributes->[1]->{value}, 'GetBorrowerAttributes returns the correct value' ); |
116 |
is( $borrower_attributes->[0]->{password}, $attributes->[1]->{password}, 'GetBorrowerAttributes returns the correct password' ); |
117 |
|
118 |
|
119 |
my $attribute_value = C4::Members::Attributes::GetBorrowerAttributeValue(); |
120 |
is( $attribute_value, undef, 'GetBorrowerAttributeValue without arguments returns undef' ); |
121 |
$attribute_value = C4::Members::Attributes::GetBorrowerAttributeValue($borrowernumber); |
122 |
is( $attribute_value, undef, 'GetBorrowerAttributeValue without the attribute code returns undef' ); |
123 |
$attribute_value = C4::Members::Attributes::GetBorrowerAttributeValue(undef, $attributes->[0]->{code}); |
124 |
is( $attribute_value, undef, 'GetBorrowerAttributeValue with a undef borrower number returns undef' ); |
125 |
$attribute_value = C4::Members::Attributes::GetBorrowerAttributeValue($borrowernumber, 'my invalid code'); |
126 |
is( $attribute_value, undef, 'GetBorrowerAttributeValue with an invalid code retuns undef' ); |
127 |
|
128 |
$attribute_value = C4::Members::Attributes::GetBorrowerAttributeValue($borrowernumber, $attributes->[0]->{code}); |
129 |
is( $attribute_value, $attributes->[0]->{value}, 'GetBorrowerAttributeValue returns the correct attribute value' ); |
130 |
$attribute_value = C4::Members::Attributes::GetBorrowerAttributeValue($borrowernumber, $attributes->[1]->{code}); |
131 |
is( $attribute_value, $attributes->[1]->{value}, 'GetBorrowerAttributeValue returns the correct attribute value' ); |
132 |
|
133 |
|
134 |
my $attribute = { |
135 |
attribute => 'my attribute3', |
136 |
code => $attribute_type1->code(), |
137 |
password => 'my password3', |
138 |
}; |
139 |
C4::Members::Attributes::UpdateBorrowerAttribute($borrowernumber, $attribute); |
140 |
$borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber); |
141 |
is( @$borrower_attributes, 2, 'UpdateBorrowerAttribute does not change the number of borrower attributes' ); |
142 |
is( $borrower_attributes->[0]->{code}, $attribute->{code}, 'UpdateBorrowerAttribute updates the field code correctly' ); |
143 |
is( $borrower_attributes->[0]->{description}, $attribute_type1->description(), 'UpdateBorrowerAttribute updates the field description correctly' ); |
144 |
is( $borrower_attributes->[0]->{value}, $attribute->{attribute}, 'UpdateBorrowerAttribute updates the field value correctly' ); |
145 |
is( $borrower_attributes->[0]->{password}, $attribute->{password}, 'UpdateBorrowerAttributes updates the field password correctly' ); |
146 |
|
147 |
|
148 |
my $check_uniqueness = C4::Members::Attributes::CheckUniqueness(); |
149 |
is( $check_uniqueness, 0, 'CheckUniqueness without arguments returns false' ); |
150 |
$check_uniqueness = C4::Members::Attributes::CheckUniqueness($attribute->{code}); |
151 |
is( $check_uniqueness, 1, 'CheckUniqueness with a valid argument code returns true' ); |
152 |
$check_uniqueness = C4::Members::Attributes::CheckUniqueness(undef, $attribute->{attribute}); |
153 |
is( $check_uniqueness, 0, 'CheckUniqueness without the argument code returns false' ); |
154 |
$check_uniqueness = C4::Members::Attributes::CheckUniqueness('my invalid code'); |
155 |
is( $check_uniqueness, 0, 'CheckUniqueness with an invalid argument code returns false' ); |
156 |
$attribute_value = C4::Members::Attributes::GetBorrowerAttributeValue($borrowernumber, $attributes->[1]->{code}); |
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' ); |
167 |
|
168 |
|
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' ); |
174 |
} |
175 |
|
176 |
|
177 |
C4::Members::Attributes::DeleteBorrowerAttribute(); |
178 |
$borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber); |
179 |
is( @$borrower_attributes, 2, 'DeleteBorrowerAttribute without arguments deletes nothing' ); |
180 |
C4::Members::Attributes::DeleteBorrowerAttribute($borrowernumber); |
181 |
$borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber); |
182 |
is( @$borrower_attributes, 2, 'DeleteBorrowerAttribute without the attribute deletes nothing' ); |
183 |
C4::Members::Attributes::DeleteBorrowerAttribute(undef, $attribute); |
184 |
$borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber); |
185 |
is( @$borrower_attributes, 2, 'DeleteBorrowerAttribute with a undef borrower number deletes nothing' ); |
186 |
|
187 |
C4::Members::Attributes::DeleteBorrowerAttribute($borrowernumber, $attribute); |
188 |
$borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber); |
189 |
is( @$borrower_attributes, 1, 'DeleteBorrowerAttribute deletes a borrower attribute' ); |
190 |
is( $borrower_attributes->[0]->{code}, $attributes->[1]->{code}, 'DeleteBorrowerAttribute deletes the correct entry'); |
191 |
is( $borrower_attributes->[0]->{description}, $attribute_type2->description(), 'DeleteBorrowerAttribute deletes the correct entry'); |
192 |
is( $borrower_attributes->[0]->{value}, $attributes->[1]->{value}, 'DeleteBorrowerAttribute deletes the correct entry'); |
193 |
is( $borrower_attributes->[0]->{password}, $attributes->[1]->{password}, 'DeleteBorrowerAttribute deletes the correct entry'); |
194 |
|
195 |
C4::Members::Attributes::DeleteBorrowerAttribute($borrowernumber, $attributes->[1]); |
196 |
$borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber); |
197 |
is( @$borrower_attributes, 0, 'DeleteBorrowerAttribute deletes a borrower attribute' ); |
198 |
|
199 |
$dbh->rollback; |