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

(-)a/Koha/Checkout.pm (+26 lines)
Lines 29-34 use Koha::Database; Link Here
29
use Koha::DateUtils;
29
use Koha::DateUtils;
30
use Koha::Items;
30
use Koha::Items;
31
31
32
use Koha::Exceptions::Object;
33
32
use base qw(Koha::Object);
34
use base qw(Koha::Object);
33
35
34
=head1 NAME
36
=head1 NAME
Lines 104-109 sub patron { Link Here
104
    return Koha::Patron->_new_from_dbic( $patron_rs );
106
    return Koha::Patron->_new_from_dbic( $patron_rs );
105
}
107
}
106
108
109
=head3 store
110
111
my $checkout = $checkout->store
112
113
Overrides the default store subroutine.
114
115
=cut
116
117
sub store {
118
    my ( $self ) = @_;
119
120
    my $checkout_type = $self->checkout_type;
121
    if ( $checkout_type ) {
122
        unless ( Koha::Checkouts::is_valid_checkout_type( $checkout_type ) ) {
123
            Koha::Exceptions::Object::FKConstraint->throw(
124
                broken_fk => 'checkout_type',
125
                value     => $self->checkout_type,
126
            );
127
        }
128
    }
129
130
    return $self->SUPER::store();
131
}
132
107
=head3 to_api_mapping
133
=head3 to_api_mapping
108
134
109
This method returns the mapping for representing a Koha::Checkout object
135
This method returns the mapping for representing a Koha::Checkout object
(-)a/Koha/Checkouts.pm (+20 lines)
Lines 22-27 use Modern::Perl; Link Here
22
use Carp;
22
use Carp;
23
23
24
use C4::Context;
24
use C4::Context;
25
use Koha::AuthorisedValues;
25
use Koha::Checkout;
26
use Koha::Checkout;
26
use Koha::Database;
27
use Koha::Database;
27
use Koha::DateUtils;
28
use Koha::DateUtils;
Lines 55-62 sub calculate_dropbox_date { Link Here
55
    return $dropbox_date;
56
    return $dropbox_date;
56
}
57
}
57
58
59
=head3 is_valid_checkout_type
60
61
my $valid_checkout_type = Koha::Checkouts::is_valid_checkout_type( $type );
62
63
Checks authorised values for valid checkout types.
64
58
=cut
65
=cut
59
66
67
sub is_valid_checkout_type {
68
    my ( $type ) = @_;
69
70
    return 1 if not defined $type; # undefined refers to normal checkout
71
72
    my $av = Koha::AuthorisedValues->search({
73
        category         => 'CHECKOUT_TYPE',
74
        authorised_value => $type,
75
    });
76
77
    return $av->count ? 1 : 0;
78
}
79
60
=head2 Name to code mappings
80
=head2 Name to code mappings
61
81
62
=head3 $type
82
=head3 $type
(-)a/t/db_dependent/Koha/Checkouts.t (-2 / +52 lines)
Lines 19-28 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 9;
22
use Test::More tests => 10;
23
use Test::Exception;
23
use Test::Exception;
24
24
25
use C4::Circulation;
25
use C4::Circulation;
26
use Koha::AuthorisedValues;
27
use Koha::AuthorisedValueCategories;
26
use Koha::Checkouts;
28
use Koha::Checkouts;
27
use Koha::Database;
29
use Koha::Database;
28
use Koha::DateUtils qw( dt_from_string );
30
use Koha::DateUtils qw( dt_from_string );
Lines 141-146 subtest 'patron' => sub { Link Here
141
    );
143
    );
142
};
144
};
143
145
146
subtest 'store' => sub {
147
    plan tests => 1;
148
149
    subtest 'validate checkout_type' => sub {
150
        plan tests => 3;
151
152
        # This should exist on clean installations, but just make sure...
153
        unless ( Koha::AuthorisedValueCategories->search( {
154
            category_name => 'CHECKOUT_TYPE'
155
        } )->count ) {
156
            $builder->build_object( {
157
                class=> 'Koha::AuthorisedValueCategories',
158
                value => { category_name => 'CHECKOUT_TYPE' }
159
            } );
160
        }
161
162
        my $av = $builder->build_object( {
163
            class=> 'Koha::AuthorisedValues',
164
            value => { category => 'CHECKOUT_TYPE' }
165
        } );
166
167
        my $patron = $builder->build_object( {
168
            class=> 'Koha::Patrons',
169
            value => { branchcode => $library->{branchcode} }
170
        } );
171
        my $item   = $builder->build_object( { class=> 'Koha::Items' } );
172
        my $checkout = Koha::Checkout->new(
173
            {   borrowernumber => $patron->borrowernumber,
174
                itemnumber     => $item->itemnumber,
175
                branchcode     => $library->{branchcode},
176
            }
177
        )->store;
178
179
        ok( $checkout->issue_id > 0,
180
            'store() was successful with undef checkout_type' );
181
182
        $checkout->set({ checkout_type => $av->authorised_value })->store;
183
        is( $checkout->checkout_type, $av->authorised_value,
184
            'store() was successful with a valid checkout_type' );
185
186
        throws_ok {
187
            $checkout->set({ checkout_type => 'NON_EXISTENT' })->store
188
        } 'Koha::Exceptions::Object::FKConstraint',
189
          'store() throws an exception with an invalid checkout_type';
190
191
        $checkout->delete;
192
    };
193
};
194
144
$retrieved_checkout_1->delete;
195
$retrieved_checkout_1->delete;
145
is( Koha::Checkouts->search->count, $nb_of_checkouts + 1, 'Delete should have deleted the checkout' );
196
is( Koha::Checkouts->search->count, $nb_of_checkouts + 1, 'Delete should have deleted the checkout' );
146
197
147
- 

Return to bug 25103