Bug 17553: Move existing tests
[koha.git] / t / db_dependent / Koha / Patron / Attributes.t
1 #!/usr/bin/perl
2
3 # Copyright 2016 Koha Development team
4 #
5 # This file is part of Koha
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 Test::More tests => 5;
23
24 use t::lib::TestBuilder;
25 use Test::Exception;
26
27 use Koha::Database;
28 use Koha::Patron::Attribute;
29 use Koha::Patron::Attributes;
30
31 my $schema  = Koha::Database->new->schema;
32 my $builder = t::lib::TestBuilder->new;
33
34 subtest 'store() repeatable attributes tests' => sub {
35
36     plan tests => 4;
37
38     $schema->storage->txn_begin;
39
40     my $patron = $builder->build( { source => 'Borrower' } )->{borrowernumber};
41     my $attribute_type_1 = $builder->build(
42         {   source => 'BorrowerAttributeType',
43             value  => { repeatable => 1 }
44         }
45     );
46     Koha::Patron::Attribute->new(
47         {   borrowernumber => $patron,
48             code           => $attribute_type_1->{code},
49             attribute      => 'Foo'
50         }
51     )->store;
52     Koha::Patron::Attribute->new(
53         {   borrowernumber => $patron,
54             code           => $attribute_type_1->{code},
55             attribute      => 'Bar'
56         }
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' );
64
65     my $attribute_type_2 = $builder->build(
66         {   source => 'BorrowerAttributeType',
67             value  => { repeatable => 0 }
68         }
69     );
70
71     Koha::Patron::Attribute->new(
72         {   borrowernumber => $patron,
73             code           => $attribute_type_2->{code},
74             attribute      => 'Foo'
75         }
76     )->store;
77     throws_ok {
78         Koha::Patron::Attribute->new(
79             {   borrowernumber => $patron,
80                 code           => $attribute_type_2->{code},
81                 attribute      => 'Bar'
82             }
83         )->store;
84     }
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' );
92
93     $schema->storage->txn_rollback;
94 };
95
96 subtest 'store() unique_id attributes tests' => sub {
97
98     plan tests => 4;
99
100     $schema->storage->txn_begin;
101
102     my $patron_1 = $builder->build( { source => 'Borrower' } )->{borrowernumber};
103     my $patron_2 = $builder->build( { source => 'Borrower' } )->{borrowernumber};
104
105     my $attribute_type_1 = $builder->build(
106         {   source => 'BorrowerAttributeType',
107             value  => { unique_id => 0 }
108         }
109     );
110     Koha::Patron::Attribute->new(
111         {   borrowernumber => $patron_1,
112             code           => $attribute_type_1->{code},
113             attribute      => 'Foo'
114         }
115     )->store;
116     Koha::Patron::Attribute->new(
117         {   borrowernumber => $patron_2,
118             code           => $attribute_type_1->{code},
119             attribute      => 'Bar'
120         }
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' );
128
129     my $attribute_type_2 = $builder->build(
130         {   source => 'BorrowerAttributeType',
131             value  => { unique_id => 1 }
132         }
133     );
134
135     Koha::Patron::Attribute->new(
136         {   borrowernumber => $patron_1,
137             code           => $attribute_type_2->{code},
138             attribute      => 'Foo'
139         }
140     )->store;
141     throws_ok {
142         Koha::Patron::Attribute->new(
143             {   borrowernumber => $patron_2,
144                 code           => $attribute_type_2->{code},
145                 attribute      => 'Foo'
146             }
147         )->store;
148     }
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' );
156
157     $schema->storage->txn_rollback;
158 };
159
160 subtest 'opac_display() tests' => sub {
161
162     plan tests => 2;
163
164     $schema->storage->txn_begin;
165
166     my $patron
167         = $builder->build( { source => 'Borrower' } )->{borrowernumber};
168     my $attribute_type_1 = $builder->build(
169         {   source => 'BorrowerAttributeType',
170             value  => { opac_display => 1 }
171         }
172     );
173
174     my $attribute_1 = Koha::Patron::Attribute->new(
175         {   borrowernumber => $patron,
176             code           => $attribute_type_1->{code},
177             attribute      => $patron
178         }
179     );
180     is( $attribute_1->opac_display, 1, '->opac_display returns 1' );
181
182     my $attribute_type_2 = $builder->build(
183         {   source => 'BorrowerAttributeType',
184             value  => { opac_display => 0 }
185         }
186     );
187
188     my $attribute_2 = Koha::Patron::Attribute->new(
189         {   borrowernumber => $patron,
190             code           => $attribute_type_2->{code},
191             attribute      => $patron
192         }
193     );
194     is( $attribute_2->opac_display, 0, '->opac_display returns 0' );
195
196     $schema->storage->txn_rollback;
197 };
198
199 subtest 'opac_editable() tests' => sub {
200
201     plan tests => 2;
202
203     $schema->storage->txn_begin;
204
205     my $patron
206         = $builder->build( { source => 'Borrower' } )->{borrowernumber};
207     my $attribute_type_1 = $builder->build(
208         {   source => 'BorrowerAttributeType',
209             value  => { opac_editable => 1 }
210         }
211     );
212
213     my $attribute_1 = Koha::Patron::Attribute->new(
214         {   borrowernumber => $patron,
215             code           => $attribute_type_1->{code},
216             attribute      => $patron
217         }
218     );
219     is( $attribute_1->opac_editable, 1, '->opac_editable returns 1' );
220
221     my $attribute_type_2 = $builder->build(
222         {   source => 'BorrowerAttributeType',
223             value  => { opac_editable => 0 }
224         }
225     );
226
227     my $attribute_2 = Koha::Patron::Attribute->new(
228         {   borrowernumber => $patron,
229             code           => $attribute_type_2->{code},
230             attribute      => $patron
231         }
232     );
233     is( $attribute_2->opac_editable, 0, '->opac_editable returns 0' );
234
235     $schema->storage->txn_rollback;
236 };
237
238 subtest 'type() tests' => sub {
239
240     plan tests => 4;
241
242     $schema->storage->txn_begin;
243
244     my $patron
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},
250             attribute      => $patron
251         }
252     );
253
254     my $attribute_type = $attribute->type;
255
256     is( ref($attribute_type),
257         'Koha::Patron::Attribute::Type',
258         '->type returns a Koha::Patron::Attribute::Type object'
259     );
260
261     is( $attribute_type->code,
262         $attr_type->{code},
263         '->type returns the right Koha::Patron::Attribute::Type object' );
264
265     is( $attribute_type->opac_editable,
266         $attr_type->{opac_editable},
267         '->type returns the right Koha::Patron::Attribute::Type object'
268     );
269
270     is( $attribute_type->opac_display,
271         $attr_type->{opac_display},
272         '->type returns the right Koha::Patron::Attribute::Type object'
273     );
274
275     $schema->storage->txn_rollback;
276 };
277