3 # Copyright 2016 Koha Development team
5 # This file is part of Koha
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.
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.
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>.
22 use Test::More tests => 5;
24 use t::lib::TestBuilder;
28 use Koha::Patron::Attribute;
29 use Koha::Patron::Attributes;
31 my $schema = Koha::Database->new->schema;
32 my $builder = t::lib::TestBuilder->new;
34 subtest 'store() repeatable attributes tests' => sub {
38 $schema->storage->txn_begin;
40 my $patron = $builder->build( { source => 'Borrower' } )->{borrowernumber};
41 my $attribute_type_1 = $builder->build(
42 { source => 'BorrowerAttributeType',
43 value => { repeatable => 1 }
46 Koha::Patron::Attribute->new(
47 { borrowernumber => $patron,
48 code => $attribute_type_1->{code},
52 Koha::Patron::Attribute->new(
53 { borrowernumber => $patron,
54 code => $attribute_type_1->{code},
59 = Koha::Patron::Attributes->search(
60 { borrowernumber => $patron, code => $attribute_type_1->{code} } )
63 '2 repeatable attributes stored and retrieved correcctly' );
65 my $attribute_type_2 = $builder->build(
66 { source => 'BorrowerAttributeType',
67 value => { repeatable => 0 }
71 Koha::Patron::Attribute->new(
72 { borrowernumber => $patron,
73 code => $attribute_type_2->{code},
78 Koha::Patron::Attribute->new(
79 { borrowernumber => $patron,
80 code => $attribute_type_2->{code},
85 'Koha::Exceptions::Patron::Attribute::NonRepeatable',
86 'Exception thrown trying to store more than one non-repeatable attribute';
87 my $attributes = Koha::Patron::Attributes->search(
88 { borrowernumber => $patron, code => $attribute_type_2->{code} } );
89 is( $attributes->count, 1, '1 non-repeatable attribute stored' );
90 is( $attributes->next->attribute,
91 'Foo', 'Non-repeatable attribute remains unchanged' );
93 $schema->storage->txn_rollback;
96 subtest 'store() unique_id attributes tests' => sub {
100 $schema->storage->txn_begin;
102 my $patron_1 = $builder->build( { source => 'Borrower' } )->{borrowernumber};
103 my $patron_2 = $builder->build( { source => 'Borrower' } )->{borrowernumber};
105 my $attribute_type_1 = $builder->build(
106 { source => 'BorrowerAttributeType',
107 value => { unique_id => 0 }
110 Koha::Patron::Attribute->new(
111 { borrowernumber => $patron_1,
112 code => $attribute_type_1->{code},
116 Koha::Patron::Attribute->new(
117 { borrowernumber => $patron_2,
118 code => $attribute_type_1->{code},
123 = Koha::Patron::Attributes->search(
124 { code => $attribute_type_1->{code} } )
127 '2 non-unique attributes stored and retrieved correcctly' );
129 my $attribute_type_2 = $builder->build(
130 { source => 'BorrowerAttributeType',
131 value => { unique_id => 1 }
135 Koha::Patron::Attribute->new(
136 { borrowernumber => $patron_1,
137 code => $attribute_type_2->{code},
142 Koha::Patron::Attribute->new(
143 { borrowernumber => $patron_2,
144 code => $attribute_type_2->{code},
149 'Koha::Exceptions::Patron::Attribute::UniqueIDConstraint',
150 'Exception thrown trying to store more than one unique attribute';
151 my $attributes = Koha::Patron::Attributes->search(
152 { borrowernumber => $patron_1, code => $attribute_type_2->{code} } );
153 is( $attributes->count, 1, '1 unique attribute stored' );
154 is( $attributes->next->attribute,
155 'Foo', 'unique attribute remains unchanged' );
157 $schema->storage->txn_rollback;
160 subtest 'opac_display() tests' => sub {
164 $schema->storage->txn_begin;
167 = $builder->build( { source => 'Borrower' } )->{borrowernumber};
168 my $attribute_type_1 = $builder->build(
169 { source => 'BorrowerAttributeType',
170 value => { opac_display => 1 }
174 my $attribute_1 = Koha::Patron::Attribute->new(
175 { borrowernumber => $patron,
176 code => $attribute_type_1->{code},
180 is( $attribute_1->opac_display, 1, '->opac_display returns 1' );
182 my $attribute_type_2 = $builder->build(
183 { source => 'BorrowerAttributeType',
184 value => { opac_display => 0 }
188 my $attribute_2 = Koha::Patron::Attribute->new(
189 { borrowernumber => $patron,
190 code => $attribute_type_2->{code},
194 is( $attribute_2->opac_display, 0, '->opac_display returns 0' );
196 $schema->storage->txn_rollback;
199 subtest 'opac_editable() tests' => sub {
203 $schema->storage->txn_begin;
206 = $builder->build( { source => 'Borrower' } )->{borrowernumber};
207 my $attribute_type_1 = $builder->build(
208 { source => 'BorrowerAttributeType',
209 value => { opac_editable => 1 }
213 my $attribute_1 = Koha::Patron::Attribute->new(
214 { borrowernumber => $patron,
215 code => $attribute_type_1->{code},
219 is( $attribute_1->opac_editable, 1, '->opac_editable returns 1' );
221 my $attribute_type_2 = $builder->build(
222 { source => 'BorrowerAttributeType',
223 value => { opac_editable => 0 }
227 my $attribute_2 = Koha::Patron::Attribute->new(
228 { borrowernumber => $patron,
229 code => $attribute_type_2->{code},
233 is( $attribute_2->opac_editable, 0, '->opac_editable returns 0' );
235 $schema->storage->txn_rollback;
238 subtest 'type() tests' => sub {
242 $schema->storage->txn_begin;
245 = $builder->build( { source => 'Borrower' } )->{borrowernumber};
246 my $attr_type = $builder->build( { source => 'BorrowerAttributeType' } );
247 my $attribute = Koha::Patron::Attribute->new(
248 { borrowernumber => $patron,
249 code => $attr_type->{code},
254 my $attribute_type = $attribute->type;
256 is( ref($attribute_type),
257 'Koha::Patron::Attribute::Type',
258 '->type returns a Koha::Patron::Attribute::Type object'
261 is( $attribute_type->code,
263 '->type returns the right Koha::Patron::Attribute::Type object' );
265 is( $attribute_type->opac_editable,
266 $attr_type->{opac_editable},
267 '->type returns the right Koha::Patron::Attribute::Type object'
270 is( $attribute_type->opac_display,
271 $attr_type->{opac_display},
272 '->type returns the right Koha::Patron::Attribute::Type object'
275 $schema->storage->txn_rollback;