Bug 26384: Fix executable flags
[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 => 3;
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 'type() tests' => sub {
161
162     plan tests => 4;
163
164     $schema->storage->txn_begin;
165
166     my $patron
167         = $builder->build( { source => 'Borrower' } )->{borrowernumber};
168     my $attr_type = $builder->build( { source => 'BorrowerAttributeType' } );
169     my $attribute = Koha::Patron::Attribute->new(
170         {   borrowernumber => $patron,
171             code           => $attr_type->{code},
172             attribute      => $patron
173         }
174     );
175
176     my $attribute_type = $attribute->type;
177
178     is( ref($attribute_type),
179         'Koha::Patron::Attribute::Type',
180         '->type returns a Koha::Patron::Attribute::Type object'
181     );
182
183     is( $attribute_type->code,
184         $attr_type->{code},
185         '->type returns the right Koha::Patron::Attribute::Type object' );
186
187     is( $attribute_type->opac_editable,
188         $attr_type->{opac_editable},
189         '->type returns the right Koha::Patron::Attribute::Type object'
190     );
191
192     is( $attribute_type->opac_display,
193         $attr_type->{opac_display},
194         '->type returns the right Koha::Patron::Attribute::Type object'
195     );
196
197     $schema->storage->txn_rollback;
198 };