|
Lines 26-32
use Koha::Database;
Link Here
|
| 26 |
use t::lib::TestBuilder; |
26 |
use t::lib::TestBuilder; |
| 27 |
use t::lib::Mocks; |
27 |
use t::lib::Mocks; |
| 28 |
|
28 |
|
| 29 |
use Test::More tests => 38; |
29 |
use Test::More tests => 39; |
| 30 |
|
30 |
|
| 31 |
use_ok('C4::Members::Attributes'); |
31 |
use_ok('C4::Members::Attributes'); |
| 32 |
|
32 |
|
|
Lines 71-149
$attribute_type_limited->branches([ $new_library->{branchcode} ]);
Link Here
|
| 71 |
$attribute_type_limited->store; |
71 |
$attribute_type_limited->store; |
| 72 |
|
72 |
|
| 73 |
$patron = Koha::Patrons->find($borrowernumber); |
73 |
$patron = Koha::Patrons->find($borrowernumber); |
| 74 |
my $borrower_attributes = $patron->get_extended_attributes; |
74 |
my $borrower_attributes = $patron->extended_attributes; |
| 75 |
is( $borrower_attributes->count, 0, 'get_extended_attributes returns the correct number of borrower attributes' ); |
75 |
is( $borrower_attributes->count, 0, 'extended_attributes returns the correct number of borrower attributes' ); |
| 76 |
|
76 |
|
| 77 |
my $attributes = [ |
77 |
my $attributes = [ |
| 78 |
{ |
78 |
{ |
| 79 |
value => 'my attribute1', |
79 |
attribute => 'my attribute1', |
| 80 |
code => $attribute_type1->code(), |
80 |
code => $attribute_type1->code(), |
| 81 |
}, |
81 |
}, |
| 82 |
{ |
82 |
{ |
| 83 |
value => 'my attribute2', |
83 |
attribute => 'my attribute2', |
| 84 |
code => $attribute_type2->code(), |
84 |
code => $attribute_type2->code(), |
| 85 |
}, |
85 |
}, |
| 86 |
{ |
86 |
{ |
| 87 |
value => 'my attribute limited', |
87 |
attribute => 'my attribute limited', |
| 88 |
code => $attribute_type_limited->code(), |
88 |
code => $attribute_type_limited->code(), |
| 89 |
} |
89 |
} |
| 90 |
]; |
90 |
]; |
| 91 |
|
91 |
|
| 92 |
my $set_borrower_attributes = C4::Members::Attributes::SetBorrowerAttributes($borrowernumber, $attributes); |
92 |
$patron->extended_attributes->filter_by_branch_limitations->delete; |
| 93 |
is( $set_borrower_attributes, 1, 'SetBorrowerAttributes returns the success code' ); |
93 |
my $set_borrower_attributes = $patron->extended_attributes($attributes); |
| 94 |
$borrower_attributes = $patron->get_extended_attributes; |
94 |
is( ref($set_borrower_attributes), 'Koha::Patron::Attributes', ''); |
| 95 |
is( $borrower_attributes->count, 3, 'get_extended_attributes returns the correct number of borrower 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' ); |
| 96 |
my $attr_0 = $borrower_attributes->next; |
98 |
my $attr_0 = $borrower_attributes->next; |
| 97 |
is( $attr_0->code, $attributes->[0]->{code}, 'SetBorrowerAttributes stores the correct code correctly' ); |
99 |
is( $attr_0->code, $attributes->[0]->{code}, 'setter for extended_attributes sestores the correct code correctly' ); |
| 98 |
is( $attr_0->type->description, $attribute_type1->description(), 'SetBorrowerAttributes stores the field description correctly' ); |
100 |
is( $attr_0->type->description, $attribute_type1->description(), 'setter for extended_attributes stores the field description correctly' ); |
| 99 |
is( $attr_0->attribute, $attributes->[0]->{value}, 'SetBorrowerAttributes stores the field value correctly' ); |
101 |
is( $attr_0->attribute, $attributes->[0]->{attribute}, 'setter for extended_attributes stores the field value correctly' ); |
| 100 |
my $attr_1 = $borrower_attributes->next; |
102 |
my $attr_1 = $borrower_attributes->next; |
| 101 |
is( $attr_1->code, $attributes->[1]->{code}, 'SetBorrowerAttributes stores the field code correctly' ); |
103 |
is( $attr_1->code, $attributes->[1]->{code}, 'setter for extended_attributes stores the field code correctly' ); |
| 102 |
is( $attr_1->type->description, $attribute_type2->description(), 'SetBorrowerAttributes stores the field description correctly' ); |
104 |
is( $attr_1->type->description, $attribute_type2->description(), 'setter for extended_attributes stores the field description correctly' ); |
| 103 |
is( $attr_1->attribute, $attributes->[1]->{value}, 'SetBorrowerAttributes stores the field value correctly' ); |
105 |
is( $attr_1->attribute, $attributes->[1]->{attribute}, 'setter for extended_attributes stores the field value correctly' ); |
| 104 |
|
106 |
|
| 105 |
$attributes = [ |
107 |
$attributes = [ |
| 106 |
{ |
108 |
{ |
| 107 |
value => 'my attribute1', |
109 |
attribute => 'my attribute1', |
| 108 |
code => $attribute_type1->code(), |
110 |
code => $attribute_type1->code(), |
| 109 |
}, |
111 |
}, |
| 110 |
{ |
112 |
{ |
| 111 |
value => 'my attribute2', |
113 |
attribute => 'my attribute2', |
| 112 |
code => $attribute_type2->code(), |
114 |
code => $attribute_type2->code(), |
| 113 |
} |
115 |
} |
| 114 |
]; |
116 |
]; |
| 115 |
C4::Members::Attributes::SetBorrowerAttributes($borrowernumber, $attributes); |
117 |
$patron->extended_attributes->filter_by_branch_limitations->delete; |
| 116 |
$borrower_attributes = $patron->get_extended_attributes; |
118 |
$patron->extended_attributes($attributes); |
| 117 |
is( $borrower_attributes->count, 3, 'SetBorrowerAttributes should not have removed the attributes limited to another branch' ); |
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' ); |
| 118 |
|
121 |
|
| 119 |
# TODO This is not implemented yet |
122 |
# TODO This is not implemented yet |
| 120 |
#$borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber, undef, 'branch_limited'); |
123 |
#$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' ); |
124 |
#is( @$borrower_attributes, 2, 'GetBorrowerAttributes returns the correct number of borrower attributes filtered on library' ); |
| 122 |
|
125 |
|
| 123 |
$patron = Koha::Patrons->find($borrowernumber); |
126 |
$patron = Koha::Patrons->find($borrowernumber); |
| 124 |
my $extended_attributes = $patron->get_extended_attributes; |
127 |
my $extended_attributes = $patron->extended_attributes; |
| 125 |
my $attribute_value = $extended_attributes->search({ code => 'my invalid code' }); |
128 |
my $attribute_value = $extended_attributes->search({ code => 'my invalid code' }); |
| 126 |
is( $attribute_value->count, 0, 'non existent attribute should return empty result set'); |
129 |
is( $attribute_value->count, 0, 'non existent attribute should return empty result set'); |
| 127 |
$attribute_value = $patron->get_extended_attribute('my invalid code'); |
130 |
$attribute_value = $patron->get_extended_attribute('my invalid code'); |
| 128 |
is( $attribute_value, undef, 'non existent attribute should undef'); |
131 |
is( $attribute_value, undef, 'non existent attribute should undef'); |
| 129 |
|
132 |
|
| 130 |
$attribute_value = $patron->get_extended_attribute($attributes->[0]->{code}); |
133 |
$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' ); |
134 |
is( $attribute_value->attribute, $attributes->[0]->{attribute}, 'get_extended_attribute returns the correct attribute value' ); |
| 132 |
$attribute_value = $patron->get_extended_attribute($attributes->[1]->{code}); |
135 |
$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' ); |
136 |
is( $attribute_value->attribute, $attributes->[1]->{attribute}, 'get_extended_attribute returns the correct attribute value' ); |
| 134 |
|
137 |
|
| 135 |
|
138 |
|
| 136 |
my $attribute = { |
139 |
my $attribute = { |
| 137 |
attribute => 'my attribute3', |
140 |
attribute => 'my attribute3', |
| 138 |
code => $attribute_type1->code(), |
141 |
code => $attribute_type1->code(), |
| 139 |
}; |
142 |
}; |
| 140 |
C4::Members::Attributes::UpdateBorrowerAttribute($borrowernumber, $attribute); |
143 |
$patron->extended_attributes->search({'me.code' => $attribute->{code}})->filter_by_branch_limitations->delete; |
| 141 |
$borrower_attributes = $patron->get_extended_attributes; |
144 |
$patron->add_extended_attribute($attribute); |
| 142 |
is( $borrower_attributes->count, 3, 'UpdateBorrowerAttribute does not change the number of borrower attributes' ); |
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' ); |
| 143 |
$attr_0 = $borrower_attributes->next; |
147 |
$attr_0 = $borrower_attributes->next; |
| 144 |
is( $attr_0->code, $attribute->{code}, 'UpdateBorrowerAttribute updates the field code correctly' ); |
148 |
is( $attr_0->code, $attribute->{code}, 'delete then add a new attribute updates the field code correctly' ); |
| 145 |
is( $attr_0->type->description, $attribute_type1->description(), 'UpdateBorrowerAttribute updates the field description correctly' ); |
149 |
is( $attr_0->type->description, $attribute_type1->description(), 'delete then add a new attribute updates the field description correctly' ); |
| 146 |
is( $attr_0->attribute, $attribute->{attribute}, 'UpdateBorrowerAttribute updates the field value correctly' ); |
150 |
is( $attr_0->attribute, $attribute->{attribute}, 'delete then add a new attribute updates the field value correctly' ); |
| 147 |
|
151 |
|
| 148 |
|
152 |
|
| 149 |
my $check_uniqueness = C4::Members::Attributes::CheckUniqueness(); |
153 |
my $check_uniqueness = C4::Members::Attributes::CheckUniqueness(); |
|
Lines 160-166
$check_uniqueness = C4::Members::Attributes::CheckUniqueness($attribute->{code},
Link Here
|
| 160 |
is( $check_uniqueness, 1, 'CheckUniqueness with a new value returns true' ); |
164 |
is( $check_uniqueness, 1, 'CheckUniqueness with a new value returns true' ); |
| 161 |
$check_uniqueness = C4::Members::Attributes::CheckUniqueness('my invalid code', 'new value'); |
165 |
$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' ); |
166 |
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}); |
167 |
$check_uniqueness = C4::Members::Attributes::CheckUniqueness($attributes->[1]->{code}, $attributes->[1]->{attribute}); |
| 164 |
is( $check_uniqueness, 1, 'CheckUniqueness with an attribute unique_id=0 returns true' ); |
168 |
is( $check_uniqueness, 1, 'CheckUniqueness with an attribute unique_id=0 returns true' ); |
| 165 |
$check_uniqueness = C4::Members::Attributes::CheckUniqueness($attribute->{code}, $attribute->{attribute}); |
169 |
$check_uniqueness = C4::Members::Attributes::CheckUniqueness($attribute->{code}, $attribute->{attribute}); |
| 166 |
is( $check_uniqueness, '', 'CheckUniqueness returns false' ); |
170 |
is( $check_uniqueness, '', 'CheckUniqueness returns false' ); |
|
Lines 168-189
is( $check_uniqueness, '', 'CheckUniqueness returns false' );
Link Here
|
| 168 |
|
172 |
|
| 169 |
my $borrower_numbers = C4::Members::Attributes::SearchIdMatchingAttribute('attribute1'); |
173 |
my $borrower_numbers = C4::Members::Attributes::SearchIdMatchingAttribute('attribute1'); |
| 170 |
is( @$borrower_numbers, 0, 'SearchIdMatchingAttribute searchs only in attributes with staff_searchable=1' ); |
174 |
is( @$borrower_numbers, 0, 'SearchIdMatchingAttribute searchs only in attributes with staff_searchable=1' ); |
| 171 |
for my $attr( split(' ', $attributes->[1]->{value}) ) { |
175 |
for my $attr( split(' ', $attributes->[1]->{attribute}) ) { |
| 172 |
$borrower_numbers = C4::Members::Attributes::SearchIdMatchingAttribute($attr); |
176 |
$borrower_numbers = C4::Members::Attributes::SearchIdMatchingAttribute($attr); |
| 173 |
is( $borrower_numbers->[0], $borrowernumber, 'SearchIdMatchingAttribute returns the borrower numbers matching' ); |
177 |
is( $borrower_numbers->[0], $borrowernumber, 'SearchIdMatchingAttribute returns the borrower numbers matching' ); |
| 174 |
} |
178 |
} |
| 175 |
|
179 |
|
| 176 |
|
180 |
|
| 177 |
$patron->get_extended_attribute($attribute->{code})->delete; |
181 |
$patron->get_extended_attribute($attribute->{code})->delete; |
| 178 |
$borrower_attributes = $patron->get_extended_attributes; |
182 |
$borrower_attributes = $patron->extended_attributes; |
| 179 |
is( $borrower_attributes->count, 2, 'delete attribute by code' ); |
183 |
is( $borrower_attributes->count, 2, 'delete attribute by code' ); |
| 180 |
$attr_0 = $borrower_attributes->next; |
184 |
$attr_0 = $borrower_attributes->next; |
| 181 |
is( $attr_0->code, $attributes->[1]->{code}, 'delete attribute by code'); |
185 |
is( $attr_0->code, $attributes->[1]->{code}, 'delete attribute by code'); |
| 182 |
is( $attr_0->type->description, $attribute_type2->description(), 'delete attribute by code'); |
186 |
is( $attr_0->type->description, $attribute_type2->description(), 'delete attribute by code'); |
| 183 |
is( $attr_0->attribute, $attributes->[1]->{value}, 'delete attribute by code'); |
187 |
is( $attr_0->attribute, $attributes->[1]->{attribute}, 'delete attribute by code'); |
| 184 |
|
188 |
|
| 185 |
$patron->get_extended_attribute($attributes->[1]->{code})->delete; |
189 |
$patron->get_extended_attribute($attributes->[1]->{code})->delete; |
| 186 |
$borrower_attributes = $patron->get_extended_attributes; |
190 |
$borrower_attributes = $patron->extended_attributes; |
| 187 |
is( $borrower_attributes->count, 1, 'delete attribute by code' ); |
191 |
is( $borrower_attributes->count, 1, 'delete attribute by code' ); |
| 188 |
|
192 |
|
| 189 |
# Regression tests for bug 16504 |
193 |
# Regression tests for bug 16504 |
|
Lines 199-218
my $another_patron = $builder->build_object(
Link Here
|
| 199 |
); |
203 |
); |
| 200 |
$attributes = [ |
204 |
$attributes = [ |
| 201 |
{ |
205 |
{ |
| 202 |
value => 'my attribute1', |
206 |
attribute => 'my attribute1', |
| 203 |
code => $attribute_type1->code(), |
207 |
code => $attribute_type1->code(), |
| 204 |
}, |
208 |
}, |
| 205 |
{ |
209 |
{ |
| 206 |
value => 'my attribute2', |
210 |
attribute => 'my attribute2', |
| 207 |
code => $attribute_type2->code(), |
211 |
code => $attribute_type2->code(), |
| 208 |
}, |
212 |
}, |
| 209 |
{ |
213 |
{ |
| 210 |
value => 'my attribute limited', |
214 |
attribute => 'my attribute limited', |
| 211 |
code => $attribute_type_limited->code(), |
215 |
code => $attribute_type_limited->code(), |
| 212 |
} |
216 |
} |
| 213 |
]; |
217 |
]; |
| 214 |
C4::Members::Attributes::SetBorrowerAttributes($another_patron->borrowernumber, $attributes); |
218 |
|
| 215 |
$borrower_attributes = $another_patron->get_extended_attributes; |
219 |
$another_patron->extended_attributes->filter_by_branch_limitations->delete; |
| 216 |
is( $borrower_attributes->count, 3, 'SetBorrowerAttributes should have added the 3 attributes for another patron'); |
220 |
$another_patron->extended_attributes($attributes); |
| 217 |
$borrower_attributes = $patron->get_extended_attributes; |
221 |
$borrower_attributes = $another_patron->extended_attributes; |
| 218 |
is( $borrower_attributes->count, 1, 'SetBorrowerAttributes should not have removed the attributes of other patrons' ); |
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' ); |