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

(-)a/Koha/Exceptions/Patron/Attribute.pm (+12 lines)
Lines 23-28 use Exception::Class ( Link Here
23
        isa         => 'Koha::Exceptions::Patron::Attribute',
23
        isa         => 'Koha::Exceptions::Patron::Attribute',
24
        description => "unique_id set for attribute type and tried to add a new with the same code and value",
24
        description => "unique_id set for attribute type and tried to add a new with the same code and value",
25
        fields      => [ "attribute" ]
25
        fields      => [ "attribute" ]
26
    },
27
    'Koha::Exceptions::Patron::Attribute::InvalidAttributeValue' => {
28
        isa => 'Koha::Exceptions::Patron::Attribute',
29
        description => "the passed value is invalid for attribute type",
30
        fields      => [ "attribute" ]
26
    }
31
    }
27
);
32
);
28
33
Lines 52-57 sub full_message { Link Here
52
                $self->type
57
                $self->type
53
            );
58
            );
54
        }
59
        }
60
        elsif ( $self->isa('Koha::Exceptions::Patron::Attribute::InvalidAttributeValue') ) {
61
            $msg = sprintf(
62
                "Tried to use an invalid value for attribute type. type=%s value=%s",
63
                $self->attribute->code,
64
                $self->attribute->attribute
65
            );
66
        }
55
    }
67
    }
56
68
57
    return $msg;
69
    return $msg;
(-)a/Koha/Patron/Attribute.pm (+25 lines)
Lines 21-26 use Koha::Database; Link Here
21
use Koha::Exceptions::Patron::Attribute;
21
use Koha::Exceptions::Patron::Attribute;
22
use Koha::Patron::Attribute::Types;
22
use Koha::Patron::Attribute::Types;
23
use Koha::AuthorisedValues;
23
use Koha::AuthorisedValues;
24
use Koha::DateUtils qw( dt_from_string );
24
25
25
use base qw(Koha::Object);
26
use base qw(Koha::Object);
26
27
Lines 55-60 sub store { Link Here
55
    Koha::Exceptions::Patron::Attribute::UniqueIDConstraint->throw( attribute => $self )
56
    Koha::Exceptions::Patron::Attribute::UniqueIDConstraint->throw( attribute => $self )
56
        unless $self->unique_ok();
57
        unless $self->unique_ok();
57
58
59
    Koha::Exceptions::Patron::Attribute::InvalidAttributeValue->throw( attribute => $self )
60
        unless $self->value_ok();
61
58
    return $self->SUPER::store();
62
    return $self->SUPER::store();
59
}
63
}
60
64
Lines 189-194 sub unique_ok { Link Here
189
    return $ok;
193
    return $ok;
190
}
194
}
191
195
196
=head3 value_ok
197
198
Checks if the value of the attribute is valid for the type
199
200
=cut
201
202
sub value_ok {
203
204
    my ( $self ) = @_;
205
206
    my $ok = 1;
207
    if ( $self->type->is_date ) {
208
        eval { dt_from_string($self->attribute); };
209
        if ($@) {
210
            $ok = 0;
211
        }
212
    }
213
214
    return $ok;
215
}
216
192
=head2 Internal methods
217
=head2 Internal methods
193
218
194
=head3 _type
219
=head3 _type
(-)a/t/db_dependent/Koha/Patron/Attribute.t (-2 / +49 lines)
Lines 33-39 my $builder = t::lib::TestBuilder->new; Link Here
33
33
34
subtest 'store() tests' => sub {
34
subtest 'store() tests' => sub {
35
35
36
    plan tests => 4;
36
    plan tests => 5;
37
37
38
    subtest 'repeatable attributes tests' => sub {
38
    subtest 'repeatable attributes tests' => sub {
39
39
Lines 106-111 subtest 'store() tests' => sub { Link Here
106
        $schema->storage->txn_rollback;
106
        $schema->storage->txn_rollback;
107
    };
107
    };
108
108
109
    subtest 'is_date attributes tests' => sub {
110
        plan tests => 2;
111
112
        $schema->storage->txn_begin;
113
114
        my $patron = $builder->build( { source => 'Borrower' } )->{borrowernumber};
115
        my $attribute_type_1 = $builder->build(
116
            {   source => 'BorrowerAttributeType',
117
                value  => { is_date => 1 }
118
            }
119
        );
120
121
        throws_ok {
122
            Koha::Patron::Attribute->new(
123
                {   borrowernumber => $patron,
124
                    code           => $attribute_type_1->{code},
125
                    attribute      => 'not_a_date'
126
                }
127
            )->store;
128
        }
129
        'Koha::Exceptions::Patron::Attribute::InvalidAttributeValue',
130
            'Exception thrown trying to store a date attribute with non-date value';
131
132
        is(
133
            "$@",
134
            "Tried to use an invalid value for attribute type. type="
135
            . $attribute_type_1->{code}
136
            . " value=not_a_date",
137
            'Exception stringified correctly, attribute passed correctly'
138
        );
139
140
        Koha::Patron::Attribute->new(
141
            {   borrowernumber => $patron,
142
                code           => $attribute_type_1->{code},
143
                attribute      => '2024-03-04'
144
            }
145
        )->store;
146
147
        my $attr_count
148
            = Koha::Patron::Attributes->search(
149
            { borrowernumber => $patron, code => $attribute_type_1->{code} } )
150
            ->count;
151
        is( $attr_count, 1,
152
            '1 date attribute stored and retrieved correctly' );
153
154
        $schema->storage->txn_rollback;
155
    };
156
109
    subtest 'unique_id attributes tests' => sub {
157
    subtest 'unique_id attributes tests' => sub {
110
158
111
        plan tests => 5;
159
        plan tests => 5;
112
- 

Return to bug 32610