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