Bug 12803 - Add ability to skip closed libraries when generating the holds queue
[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
27 use Test::More tests => 60;
28
29 use t::lib::TestBuilder;
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
44 my $library = $builder->build({
45     source => 'Branch',
46 });
47 my $borrowernumber = AddMember(
48     firstname =>  'my firstname',
49     surname => 'my surname',
50     categorycode => 'S',
51     branchcode => $library->{branchcode},
52 );
53
54
55 my $attribute_type1 = C4::Members::AttributeTypes->new('my code1', 'my description1');
56 $attribute_type1->unique_id(1);
57 my $attribute_type2 = C4::Members::AttributeTypes->new('my code2', 'my description2');
58 $attribute_type2->opac_display(1);
59 $attribute_type2->staff_searchable(1);
60
61 my $attribute_types = C4::Members::Attributes::GetAttributes();
62 is( @$attribute_types, 0, 'GetAttributes returns the correct number of attribute types' );
63
64 $attribute_type1->store();
65 $attribute_types = C4::Members::Attributes::GetAttributes();
66 is( @$attribute_types, 1, 'GetAttributes returns the correct number of attribute types' );
67 is( $attribute_types->[0], $attribute_type1->code(), 'GetAttributes returns the correct value for code' );
68 $attribute_types = C4::Members::Attributes::GetAttributes(1);
69 is( @$attribute_types, 0, 'GetAttributes returns the correct number of attribute types with the filter opac_only' );
70
71 $attribute_type2->store();
72 $attribute_types = C4::Members::Attributes::GetAttributes();
73 is( @$attribute_types, 2, 'GetAttributes returns the correct number of attribute types' );
74 is( $attribute_types->[1], $attribute_type2->code(), 'GetAttributes returns the correct value for code' );
75 $attribute_types = C4::Members::Attributes::GetAttributes(1);
76 is( @$attribute_types, 1, 'GetAttributes returns the correct number of attribute types with the filter opac_only' );
77
78
79 my $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes();
80 is( @$borrower_attributes, 0, 'GetBorrowerAttributes without the borrower number returns an empty array' );
81 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
82 is( @$borrower_attributes, 0, 'GetBorrowerAttributes returns the correct number of borrower attributes' );
83
84 my $attributes = [
85     {
86         value => 'my attribute1',
87         code => $attribute_type1->code(),
88         password => 'my password1',
89     },
90     {
91         value => 'my attribute2',
92         code => $attribute_type2->code(),
93         password => 'my password2',
94     }
95 ];
96
97 my $set_borrower_attributes = C4::Members::Attributes::SetBorrowerAttributes();
98 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes();
99 is( @$borrower_attributes, 0, 'SetBorrowerAttributes without arguments does not add borrower attributes' );
100
101 $set_borrower_attributes = C4::Members::Attributes::SetBorrowerAttributes($borrowernumber);
102 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes();
103 is( @$borrower_attributes, 0, 'SetBorrowerAttributes without the attributes does not add borrower attributes' );
104
105 $set_borrower_attributes = C4::Members::Attributes::SetBorrowerAttributes($borrowernumber, $attributes);
106 is( $set_borrower_attributes, 1, 'SetBorrowerAttributes returns the success code' );
107 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes();
108 is( @$borrower_attributes, 0, 'GetBorrowerAttributes without the borrower number returns an empty array' );
109 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
110 is( @$borrower_attributes, 2, 'GetBorrowerAttributes returns the correct number of borrower attributes' );
111 is( $borrower_attributes->[0]->{code}, $attributes->[0]->{code}, 'SetBorrowerAttributes stores the correct code correctly' );
112 is( $borrower_attributes->[0]->{description}, $attribute_type1->description(), 'SetBorrowerAttributes stores the field description correctly' );
113 is( $borrower_attributes->[0]->{value}, $attributes->[0]->{value}, 'SetBorrowerAttributes stores the field value correctly' );
114 is( $borrower_attributes->[0]->{password}, $attributes->[0]->{password}, 'SetBorrowerAttributes stores the field password correctly' );
115 is( $borrower_attributes->[1]->{code}, $attributes->[1]->{code}, 'SetBorrowerAttributes stores the field code correctly' );
116 is( $borrower_attributes->[1]->{description}, $attribute_type2->description(), 'SetBorrowerAttributes stores the field description correctly' );
117 is( $borrower_attributes->[1]->{value}, $attributes->[1]->{value}, 'SetBorrowerAttributes stores the field value correctly' );
118 is( $borrower_attributes->[1]->{password}, $attributes->[1]->{password}, 'SetBorrowerAttributes stores the field password correctly' );
119
120 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber, 1);
121 is( @$borrower_attributes, 1, 'GetBorrowerAttributes returns the correct number of borrower attributes with the filter opac_only' );
122 is( $borrower_attributes->[0]->{code}, $attributes->[1]->{code}, 'GetBorrowerAttributes returns the correct code' );
123 is( $borrower_attributes->[0]->{description}, $attribute_type2->description(), 'GetBorrowerAttributes returns the correct description' );
124 is( $borrower_attributes->[0]->{value}, $attributes->[1]->{value}, 'GetBorrowerAttributes returns the correct value' );
125 is( $borrower_attributes->[0]->{password}, $attributes->[1]->{password}, 'GetBorrowerAttributes returns the correct password' );
126
127
128 my $attribute_value = C4::Members::Attributes::GetBorrowerAttributeValue();
129 is( $attribute_value, undef, 'GetBorrowerAttributeValue without arguments returns undef' );
130 $attribute_value = C4::Members::Attributes::GetBorrowerAttributeValue($borrowernumber);
131 is( $attribute_value, undef, 'GetBorrowerAttributeValue without the attribute code returns undef' );
132 $attribute_value = C4::Members::Attributes::GetBorrowerAttributeValue(undef, $attributes->[0]->{code});
133 is( $attribute_value, undef, 'GetBorrowerAttributeValue with a undef borrower number returns undef' );
134 $attribute_value = C4::Members::Attributes::GetBorrowerAttributeValue($borrowernumber, 'my invalid code');
135 is( $attribute_value, undef, 'GetBorrowerAttributeValue with an invalid code retuns undef' );
136
137 $attribute_value = C4::Members::Attributes::GetBorrowerAttributeValue($borrowernumber, $attributes->[0]->{code});
138 is( $attribute_value, $attributes->[0]->{value}, 'GetBorrowerAttributeValue returns the correct attribute value' );
139 $attribute_value = C4::Members::Attributes::GetBorrowerAttributeValue($borrowernumber, $attributes->[1]->{code});
140 is( $attribute_value, $attributes->[1]->{value}, 'GetBorrowerAttributeValue returns the correct attribute value' );
141
142
143 my $attribute = {
144     attribute => 'my attribute3',
145     code => $attribute_type1->code(),
146     password => 'my password3',
147 };
148 C4::Members::Attributes::UpdateBorrowerAttribute($borrowernumber, $attribute);
149 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
150 is( @$borrower_attributes, 2, 'UpdateBorrowerAttribute does not change the number of borrower attributes' );
151 is( $borrower_attributes->[0]->{code}, $attribute->{code}, 'UpdateBorrowerAttribute updates the field code correctly' );
152 is( $borrower_attributes->[0]->{description}, $attribute_type1->description(), 'UpdateBorrowerAttribute updates the field description correctly' );
153 is( $borrower_attributes->[0]->{value}, $attribute->{attribute}, 'UpdateBorrowerAttribute updates the field value correctly' );
154 is( $borrower_attributes->[0]->{password}, $attribute->{password}, 'UpdateBorrowerAttributes updates the field password correctly' );
155
156
157 my $check_uniqueness = C4::Members::Attributes::CheckUniqueness();
158 is( $check_uniqueness, 0, 'CheckUniqueness without arguments returns false' );
159 $check_uniqueness = C4::Members::Attributes::CheckUniqueness($attribute->{code});
160 is( $check_uniqueness, 1, 'CheckUniqueness with a valid argument code returns true' );
161 $check_uniqueness = C4::Members::Attributes::CheckUniqueness(undef, $attribute->{attribute});
162 is( $check_uniqueness, 0, 'CheckUniqueness without the argument code returns false' );
163 $check_uniqueness = C4::Members::Attributes::CheckUniqueness('my invalid code');
164 is( $check_uniqueness, 0, 'CheckUniqueness with an invalid argument code returns false' );
165 $attribute_value = C4::Members::Attributes::GetBorrowerAttributeValue($borrowernumber, $attributes->[1]->{code});
166 $check_uniqueness = C4::Members::Attributes::CheckUniqueness('my invalid code', $attribute->{attribute});
167 is( $check_uniqueness, 0, 'CheckUniqueness with an invalid argument code returns fale' );
168 $check_uniqueness = C4::Members::Attributes::CheckUniqueness($attribute->{code}, 'new value');
169 is( $check_uniqueness, 1, 'CheckUniqueness with a new value returns true' );
170 $check_uniqueness = C4::Members::Attributes::CheckUniqueness('my invalid code', 'new value');
171 is( $check_uniqueness, 0, 'CheckUniqueness with an invalid argument code and a new value returns false' );
172 $check_uniqueness = C4::Members::Attributes::CheckUniqueness($attributes->[1]->{code}, $attributes->[1]->{value});
173 is( $check_uniqueness, 1, 'CheckUniqueness with an attribute unique_id=0 returns true' );
174 $check_uniqueness = C4::Members::Attributes::CheckUniqueness($attribute->{code}, $attribute->{attribute});
175 is( $check_uniqueness, '', 'CheckUniqueness returns false' );
176
177
178 my $borrower_numbers = C4::Members::Attributes::SearchIdMatchingAttribute('attribute1');
179 is( @$borrower_numbers, 0, 'SearchIdMatchingAttribute searchs only in attributes with staff_searchable=1' );
180 for my $attr( split(' ', $attributes->[1]->{value}) ) {
181     $borrower_numbers = C4::Members::Attributes::SearchIdMatchingAttribute($attr);
182     is( $borrower_numbers->[0], $borrowernumber, 'SearchIdMatchingAttribute returns the borrower numbers matching' );
183 }
184
185
186 C4::Members::Attributes::DeleteBorrowerAttribute();
187 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
188 is( @$borrower_attributes, 2, 'DeleteBorrowerAttribute without arguments deletes nothing' );
189 C4::Members::Attributes::DeleteBorrowerAttribute($borrowernumber);
190 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
191 is( @$borrower_attributes, 2, 'DeleteBorrowerAttribute without the attribute deletes nothing' );
192 C4::Members::Attributes::DeleteBorrowerAttribute(undef, $attribute);
193 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
194 is( @$borrower_attributes, 2, 'DeleteBorrowerAttribute with a undef borrower number deletes nothing' );
195
196 C4::Members::Attributes::DeleteBorrowerAttribute($borrowernumber, $attribute);
197 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
198 is( @$borrower_attributes, 1, 'DeleteBorrowerAttribute deletes a borrower attribute' );
199 is( $borrower_attributes->[0]->{code}, $attributes->[1]->{code}, 'DeleteBorrowerAttribute deletes the correct entry');
200 is( $borrower_attributes->[0]->{description}, $attribute_type2->description(), 'DeleteBorrowerAttribute deletes the correct entry');
201 is( $borrower_attributes->[0]->{value}, $attributes->[1]->{value}, 'DeleteBorrowerAttribute deletes the correct entry');
202 is( $borrower_attributes->[0]->{password}, $attributes->[1]->{password}, 'DeleteBorrowerAttribute deletes the correct entry');
203
204 C4::Members::Attributes::DeleteBorrowerAttribute($borrowernumber, $attributes->[1]);
205 $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
206 is( @$borrower_attributes, 0, 'DeleteBorrowerAttribute deletes a borrower attribute' );