View | Details | Raw Unified | Return to bug 27858
Collapse All | Expand All

(-)a/t/db_dependent/Koha/Patron/Attribute.t (+221 lines)
Line 0 Link Here
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 => 2;
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() tests' => sub {
35
36
    plan tests => 2;
37
38
    subtest 'repeatable attributes tests' => sub {
39
40
        plan tests => 5;
41
42
        $schema->storage->txn_begin;
43
44
        my $patron = $builder->build( { source => 'Borrower' } )->{borrowernumber};
45
        my $attribute_type_1 = $builder->build(
46
            {   source => 'BorrowerAttributeType',
47
                value  => { repeatable => 1 }
48
            }
49
        );
50
        Koha::Patron::Attribute->new(
51
            {   borrowernumber => $patron,
52
                code           => $attribute_type_1->{code},
53
                attribute      => 'Foo'
54
            }
55
        )->store;
56
        Koha::Patron::Attribute->new(
57
            {   borrowernumber => $patron,
58
                code           => $attribute_type_1->{code},
59
                attribute      => 'Bar'
60
            }
61
        )->store;
62
        my $attr_count
63
            = Koha::Patron::Attributes->search(
64
            { borrowernumber => $patron, code => $attribute_type_1->{code} } )
65
            ->count;
66
        is( $attr_count, 2,
67
            '2 repeatable attributes stored and retrieved correcctly' );
68
69
        my $attribute_type_2 = $builder->build(
70
            {   source => 'BorrowerAttributeType',
71
                value  => { repeatable => 0 }
72
            }
73
        );
74
75
        Koha::Patron::Attribute->new(
76
            {   borrowernumber => $patron,
77
                code           => $attribute_type_2->{code},
78
                attribute      => 'Foo'
79
            }
80
        )->store;
81
        throws_ok {
82
            Koha::Patron::Attribute->new(
83
                {   borrowernumber => $patron,
84
                    code           => $attribute_type_2->{code},
85
                    attribute      => 'Bar'
86
                }
87
            )->store;
88
        }
89
        'Koha::Exceptions::Patron::Attribute::NonRepeatable',
90
            'Exception thrown trying to store more than one non-repeatable attribute';
91
92
        is(
93
            "$@",
94
            "Tried to add more than one non-repeatable attributes. code="
95
            . $attribute_type_2->{code}
96
            . " attribute=Bar",
97
            'Exception stringified correctly, attribute passed correctly'
98
        );
99
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' );
105
106
        $schema->storage->txn_rollback;
107
    };
108
109
    subtest 'unique_id attributes tests' => sub {
110
111
        plan tests => 5;
112
113
        $schema->storage->txn_begin;
114
115
        my $patron_1 = $builder->build( { source => 'Borrower' } )->{borrowernumber};
116
        my $patron_2 = $builder->build( { source => 'Borrower' } )->{borrowernumber};
117
118
        my $attribute_type_1 = $builder->build(
119
            {   source => 'BorrowerAttributeType',
120
                value  => { unique_id => 0 }
121
            }
122
        );
123
        Koha::Patron::Attribute->new(
124
            {   borrowernumber => $patron_1,
125
                code           => $attribute_type_1->{code},
126
                attribute      => 'Foo'
127
            }
128
        )->store;
129
        Koha::Patron::Attribute->new(
130
            {   borrowernumber => $patron_2,
131
                code           => $attribute_type_1->{code},
132
                attribute      => 'Bar'
133
            }
134
        )->store;
135
        my $attr_count
136
            = Koha::Patron::Attributes->search(
137
            { code => $attribute_type_1->{code} } )
138
            ->count;
139
        is( $attr_count, 2,
140
            '2 non-unique attributes stored and retrieved correcctly' );
141
142
        my $attribute_type_2 = $builder->build(
143
            {   source => 'BorrowerAttributeType',
144
                value  => { unique_id => 1 }
145
            }
146
        );
147
148
        Koha::Patron::Attribute->new(
149
            {   borrowernumber => $patron_1,
150
                code           => $attribute_type_2->{code},
151
                attribute      => 'Foo'
152
            }
153
        )->store;
154
        throws_ok {
155
            Koha::Patron::Attribute->new(
156
                {   borrowernumber => $patron_2,
157
                    code           => $attribute_type_2->{code},
158
                    attribute      => 'Foo'
159
                }
160
            )->store;
161
        }
162
        'Koha::Exceptions::Patron::Attribute::UniqueIDConstraint',
163
            'Exception thrown trying to store more than one unique attribute';
164
165
        is(
166
            "$@",
167
            "Your action breaks a unique constraint on the attribute. code="
168
            . $attribute_type_2->{code}
169
            . " attribute=Foo",
170
            'Exception stringified correctly, attribute passed correctly'
171
        );
172
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' );
178
179
        $schema->storage->txn_rollback;
180
    };
181
};
182
183
subtest 'type() tests' => sub {
184
185
    plan tests => 4;
186
187
    $schema->storage->txn_begin;
188
189
    my $patron
190
        = $builder->build( { source => 'Borrower' } )->{borrowernumber};
191
    my $attr_type = $builder->build( { source => 'BorrowerAttributeType' } );
192
    my $attribute = Koha::Patron::Attribute->new(
193
        {   borrowernumber => $patron,
194
            code           => $attr_type->{code},
195
            attribute      => $patron
196
        }
197
    );
198
199
    my $attribute_type = $attribute->type;
200
201
    is( ref($attribute_type),
202
        'Koha::Patron::Attribute::Type',
203
        '->type returns a Koha::Patron::Attribute::Type object'
204
    );
205
206
    is( $attribute_type->code,
207
        $attr_type->{code},
208
        '->type returns the right Koha::Patron::Attribute::Type object' );
209
210
    is( $attribute_type->opac_editable,
211
        $attr_type->{opac_editable},
212
        '->type returns the right Koha::Patron::Attribute::Type object'
213
    );
214
215
    is( $attribute_type->opac_display,
216
        $attr_type->{opac_display},
217
        '->type returns the right Koha::Patron::Attribute::Type object'
218
    );
219
220
    $schema->storage->txn_rollback;
221
};
(-)a/t/db_dependent/Koha/Patron/Attributes.t (-217 lines)
Lines 1-216 Link Here
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 => 5;
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
88
    is(
89
        "$@",
90
        "Tried to add more than one non-repeatable attributes. code="
91
          . $attribute_type_2->{code}
92
          . " attribute=Bar",
93
        'Exception stringified correctly, attribute passed correctly'
94
    );
95
96
    my $attributes = Koha::Patron::Attributes->search(
97
        { borrowernumber => $patron, code => $attribute_type_2->{code} } );
98
    is( $attributes->count, 1, '1 non-repeatable attribute stored' );
99
    is( $attributes->next->attribute,
100
        'Foo', 'Non-repeatable attribute remains unchanged' );
101
102
    $schema->storage->txn_rollback;
103
};
104
105
subtest 'store() unique_id attributes tests' => sub {
106
107
    plan tests => 5;
108
109
    $schema->storage->txn_begin;
110
111
    my $patron_1 = $builder->build( { source => 'Borrower' } )->{borrowernumber};
112
    my $patron_2 = $builder->build( { source => 'Borrower' } )->{borrowernumber};
113
114
    my $attribute_type_1 = $builder->build(
115
        {   source => 'BorrowerAttributeType',
116
            value  => { unique_id => 0 }
117
        }
118
    );
119
    Koha::Patron::Attribute->new(
120
        {   borrowernumber => $patron_1,
121
            code           => $attribute_type_1->{code},
122
            attribute      => 'Foo'
123
        }
124
    )->store;
125
    Koha::Patron::Attribute->new(
126
        {   borrowernumber => $patron_2,
127
            code           => $attribute_type_1->{code},
128
            attribute      => 'Bar'
129
        }
130
    )->store;
131
    my $attr_count
132
        = Koha::Patron::Attributes->search(
133
        { code => $attribute_type_1->{code} } )
134
        ->count;
135
    is( $attr_count, 2,
136
        '2 non-unique attributes stored and retrieved correcctly' );
137
138
    my $attribute_type_2 = $builder->build(
139
        {   source => 'BorrowerAttributeType',
140
            value  => { unique_id => 1 }
141
        }
142
    );
143
144
    Koha::Patron::Attribute->new(
145
        {   borrowernumber => $patron_1,
146
            code           => $attribute_type_2->{code},
147
            attribute      => 'Foo'
148
        }
149
    )->store;
150
    throws_ok {
151
        Koha::Patron::Attribute->new(
152
            {   borrowernumber => $patron_2,
153
                code           => $attribute_type_2->{code},
154
                attribute      => 'Foo'
155
            }
156
        )->store;
157
    }
158
    'Koha::Exceptions::Patron::Attribute::UniqueIDConstraint',
159
        'Exception thrown trying to store more than one unique attribute';
160
161
    is(
162
        "$@",
163
        "Your action breaks a unique constraint on the attribute. code="
164
          . $attribute_type_2->{code}
165
          . " attribute=Foo",
166
        'Exception stringified correctly, attribute passed correctly'
167
    );
168
169
    my $attributes = Koha::Patron::Attributes->search(
170
        { borrowernumber => $patron_1, code => $attribute_type_2->{code} } );
171
    is( $attributes->count, 1, '1 unique attribute stored' );
172
    is( $attributes->next->attribute,
173
        'Foo', 'unique attribute remains unchanged' );
174
175
    $schema->storage->txn_rollback;
176
};
177
178
subtest 'type() tests' => sub {
179
180
    plan tests => 4;
181
182
    $schema->storage->txn_begin;
183
184
    my $patron
185
        = $builder->build( { source => 'Borrower' } )->{borrowernumber};
186
    my $attr_type = $builder->build( { source => 'BorrowerAttributeType' } );
187
    my $attribute = Koha::Patron::Attribute->new(
188
        {   borrowernumber => $patron,
189
            code           => $attr_type->{code},
190
            attribute      => $patron
191
        }
192
    );
193
194
    my $attribute_type = $attribute->type;
195
196
    is( ref($attribute_type),
197
        'Koha::Patron::Attribute::Type',
198
        '->type returns a Koha::Patron::Attribute::Type object'
199
    );
200
201
    is( $attribute_type->code,
202
        $attr_type->{code},
203
        '->type returns the right Koha::Patron::Attribute::Type object' );
204
205
    is( $attribute_type->opac_editable,
206
        $attr_type->{opac_editable},
207
        '->type returns the right Koha::Patron::Attribute::Type object'
208
    );
209
210
    is( $attribute_type->opac_display,
211
        $attr_type->{opac_display},
212
        '->type returns the right Koha::Patron::Attribute::Type object'
213
    );
214
215
    $schema->storage->txn_rollback;
216
};
217
- 

Return to bug 27858