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 => 2;
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() tests' => sub {
38 subtest 'repeatable attributes tests' => sub {
42 $schema->storage->txn_begin;
44 my $patron = $builder->build( { source => 'Borrower' } )->{borrowernumber};
45 my $attribute_type_1 = $builder->build(
46 { source => 'BorrowerAttributeType',
47 value => { repeatable => 1 }
50 Koha::Patron::Attribute->new(
51 { borrowernumber => $patron,
52 code => $attribute_type_1->{code},
56 Koha::Patron::Attribute->new(
57 { borrowernumber => $patron,
58 code => $attribute_type_1->{code},
63 = Koha::Patron::Attributes->search(
64 { borrowernumber => $patron, code => $attribute_type_1->{code} } )
67 '2 repeatable attributes stored and retrieved correcctly' );
69 my $attribute_type_2 = $builder->build(
70 { source => 'BorrowerAttributeType',
71 value => { repeatable => 0 }
75 Koha::Patron::Attribute->new(
76 { borrowernumber => $patron,
77 code => $attribute_type_2->{code},
82 Koha::Patron::Attribute->new(
83 { borrowernumber => $patron,
84 code => $attribute_type_2->{code},
89 'Koha::Exceptions::Patron::Attribute::NonRepeatable',
90 'Exception thrown trying to store more than one non-repeatable attribute';
94 "Tried to add more than one non-repeatable attributes. type="
95 . $attribute_type_2->{code}
97 'Exception stringified correctly, attribute passed correctly'
100 my $attributes = Koha::Patron::Attributes->search(
101 { borrowernumber => $patron, code => $attribute_type_2->{code} } );
102 is( $attributes->count, 1, '1 non-repeatable attribute stored' );
103 is( $attributes->next->attribute,
104 'Foo', 'Non-repeatable attribute remains unchanged' );
106 $schema->storage->txn_rollback;
109 subtest 'unique_id attributes tests' => sub {
113 $schema->storage->txn_begin;
115 my $patron_1 = $builder->build( { source => 'Borrower' } )->{borrowernumber};
116 my $patron_2 = $builder->build( { source => 'Borrower' } )->{borrowernumber};
118 my $attribute_type_1 = $builder->build(
119 { source => 'BorrowerAttributeType',
120 value => { unique_id => 0 }
123 Koha::Patron::Attribute->new(
124 { borrowernumber => $patron_1,
125 code => $attribute_type_1->{code},
129 Koha::Patron::Attribute->new(
130 { borrowernumber => $patron_2,
131 code => $attribute_type_1->{code},
136 = Koha::Patron::Attributes->search(
137 { code => $attribute_type_1->{code} } )
140 '2 non-unique attributes stored and retrieved correcctly' );
142 my $attribute_type_2 = $builder->build(
143 { source => 'BorrowerAttributeType',
144 value => { unique_id => 1 }
148 Koha::Patron::Attribute->new(
149 { borrowernumber => $patron_1,
150 code => $attribute_type_2->{code},
155 Koha::Patron::Attribute->new(
156 { borrowernumber => $patron_2,
157 code => $attribute_type_2->{code},
162 'Koha::Exceptions::Patron::Attribute::UniqueIDConstraint',
163 'Exception thrown trying to store more than one unique attribute';
167 "Your action breaks a unique constraint on the attribute. type="
168 . $attribute_type_2->{code}
170 'Exception stringified correctly, attribute passed correctly'
173 my $attributes = Koha::Patron::Attributes->search(
174 { borrowernumber => $patron_1, code => $attribute_type_2->{code} } );
175 is( $attributes->count, 1, '1 unique attribute stored' );
176 is( $attributes->next->attribute,
177 'Foo', 'unique attribute remains unchanged' );
179 $schema->storage->txn_rollback;
182 subtest 'invalid type tests' => sub {
186 $schema->storage->txn_begin;
188 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
189 my $attribute_type = $builder->build_object(
191 class => 'Koha::Patron::Attribute::Types',
199 my $code = $attribute_type->code;
200 $attribute_type->delete;
203 { Koha::Patron::Attribute->new(
205 borrowernumber => $patron->borrowernumber,
207 attribute => 'Who knows'
211 'Koha::Exceptions::Patron::Attribute::InvalidType',
212 'Exception thrown on invalid attribute code';
214 is( $@->type, $code, 'type exception parameter passed' );
216 $schema->storage->txn_rollback;
219 subtest 'Edit attribute tests for non-repeatable tests (Bug 28031)' => sub {
223 $schema->storage->txn_begin;
225 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
226 my $non_repeatable_type = $builder->build_object(
228 class => 'Koha::Patron::Attribute::Types',
233 category_code => undef
238 # Here we test the case of editing an already stored attribute
239 my $non_repeatable_attr = $patron->add_extended_attribute(
241 code => $non_repeatable_type->code,
246 $non_repeatable_attr->set({ attribute => 'HEY' })
250 is( $non_repeatable_attr->attribute, 'HEY', 'Value stored correctly' );
252 $schema->storage->txn_rollback;
256 subtest 'type() tests' => sub {
260 $schema->storage->txn_begin;
263 = $builder->build( { source => 'Borrower' } )->{borrowernumber};
264 my $attr_type = $builder->build( { source => 'BorrowerAttributeType' } );
265 my $attribute = Koha::Patron::Attribute->new(
266 { borrowernumber => $patron,
267 code => $attr_type->{code},
272 my $attribute_type = $attribute->type;
274 is( ref($attribute_type),
275 'Koha::Patron::Attribute::Type',
276 '->type returns a Koha::Patron::Attribute::Type object'
279 is( $attribute_type->code,
281 '->type returns the right Koha::Patron::Attribute::Type object' );
283 is( $attribute_type->opac_editable,
284 $attr_type->{opac_editable},
285 '->type returns the right Koha::Patron::Attribute::Type object'
288 is( $attribute_type->opac_display,
289 $attr_type->{opac_display},
290 '->type returns the right Koha::Patron::Attribute::Type object'
293 $schema->storage->txn_rollback;