Bug 17828: Unit tests for Koha::Patron::Attribute->store
[koha.git] / t / db_dependent / Koha / Patron / Attributes.t
blob36b04df39d30c92edc32eb6595fd579305e16b4d
1 #!/usr/bin/perl
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>.
20 use Modern::Perl;
22 use Test::More tests => 4;
24 use t::lib::TestBuilder;
25 use Test::Exception;
27 use Koha::Database;
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 {
36 plan tests => 4;
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},
49 attribute => 'Foo'
51 )->store;
52 Koha::Patron::Attribute->new(
53 { borrowernumber => $patron,
54 code => $attribute_type_1->{code},
55 attribute => 'Bar'
57 )->store;
58 my $attr_count
59 = Koha::Patron::Attributes->search(
60 { borrowernumber => $patron, code => $attribute_type_1->{code} } )
61 ->count;
62 is( $attr_count, 2,
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},
74 attribute => 'Foo'
76 )->store;
77 throws_ok {
78 Koha::Patron::Attribute->new(
79 { borrowernumber => $patron,
80 code => $attribute_type_2->{code},
81 attribute => 'Bar'
83 )->store;
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 {
98 plan tests => 4;
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},
113 attribute => 'Foo'
115 )->store;
116 Koha::Patron::Attribute->new(
117 { borrowernumber => $patron_2,
118 code => $attribute_type_1->{code},
119 attribute => 'Bar'
121 )->store;
122 my $attr_count
123 = Koha::Patron::Attributes->search(
124 { code => $attribute_type_1->{code} } )
125 ->count;
126 is( $attr_count, 2,
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},
138 attribute => 'Foo'
140 )->store;
141 throws_ok {
142 Koha::Patron::Attribute->new(
143 { borrowernumber => $patron_2,
144 code => $attribute_type_2->{code},
145 attribute => 'Foo'
147 )->store;
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 {
162 plan tests => 2;
164 $schema->storage->txn_begin;
166 my $patron
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},
177 attribute => $patron
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},
191 attribute => $patron
194 is( $attribute_2->opac_display, 0, '->opac_display returns 0' );
196 $schema->storage->txn_rollback;
199 subtest 'opac_editable() tests' => sub {
201 plan tests => 2;
203 $schema->storage->txn_begin;
205 my $patron
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},
216 attribute => $patron
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},
230 attribute => $patron
233 is( $attribute_2->opac_editable, 0, '->opac_editable returns 0' );
235 $schema->storage->txn_rollback;