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

(-)a/t/db_dependent/Koha/Patron/Attributes.t (-2 / +129 lines)
Lines 19-34 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 2;
22
use Test::More tests => 4;
23
23
24
use t::lib::TestBuilder;
24
use t::lib::TestBuilder;
25
use Test::Exception;
25
26
26
use Koha::Database;
27
use Koha::Database;
27
use Koha::Patron::Attribute;
28
use Koha::Patron::Attribute;
29
use Koha::Patron::Attributes;
28
30
29
my $schema  = Koha::Database->new->schema;
31
my $schema  = Koha::Database->new->schema;
30
my $builder = t::lib::TestBuilder->new;
32
my $builder = t::lib::TestBuilder->new;
31
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
32
subtest 'opac_display() tests' => sub {
160
subtest 'opac_display() tests' => sub {
33
161
34
    plan tests => 2;
162
    plan tests => 2;
35
- 

Return to bug 17828