Lines 27-32
use JSON qw(encode_json);
Link Here
|
27 |
|
27 |
|
28 |
use Koha::Bookings; |
28 |
use Koha::Bookings; |
29 |
use Koha::Database; |
29 |
use Koha::Database; |
|
|
30 |
use Koha::DateUtils qw (dt_from_string); |
30 |
|
31 |
|
31 |
my $schema = Koha::Database->new->schema; |
32 |
my $schema = Koha::Database->new->schema; |
32 |
my $builder = t::lib::TestBuilder->new; |
33 |
my $builder = t::lib::TestBuilder->new; |
Lines 70-77
subtest 'list() tests' => sub {
Link Here
|
70 |
$t->get_ok("//$userid:$password@/api/v1/bookings")->status_is(200)->json_is( [] ); |
71 |
$t->get_ok("//$userid:$password@/api/v1/bookings")->status_is(200)->json_is( [] ); |
71 |
|
72 |
|
72 |
# One booking |
73 |
# One booking |
73 |
my $start_0 = DateTime->now->subtract( days => 2 )->truncate( to => 'day' ); |
74 |
my $start_0 = dt_from_string->subtract( days => 2 )->truncate( to => 'day' ); |
74 |
my $end_0 = DateTime->now->add( days => 4 )->truncate( to => 'day' ); |
75 |
my $end_0 = dt_from_string->add( days => 4 )->truncate( to => 'day' ); |
75 |
my $booking_0 = $builder->build_object( |
76 |
my $booking_0 = $builder->build_object( |
76 |
{ |
77 |
{ |
77 |
class => 'Koha::Bookings', |
78 |
class => 'Koha::Bookings', |
Lines 85-92
subtest 'list() tests' => sub {
Link Here
|
85 |
$t->get_ok("//$userid:$password@/api/v1/bookings")->status_is(200)->json_is( [ $booking_0->to_api ] ); |
86 |
$t->get_ok("//$userid:$password@/api/v1/bookings")->status_is(200)->json_is( [ $booking_0->to_api ] ); |
86 |
|
87 |
|
87 |
# More bookings |
88 |
# More bookings |
88 |
my $start_1 = DateTime->now->add( days => 1 )->truncate( to => 'day' ); |
89 |
my $start_1 = dt_from_string->add( days => 1 )->truncate( to => 'day' ); |
89 |
my $end_1 = DateTime->now->add( days => 6 )->truncate( to => 'day' ); |
90 |
my $end_1 = dt_from_string->add( days => 6 )->truncate( to => 'day' ); |
90 |
my $booking_1 = $builder->build_object( |
91 |
my $booking_1 = $builder->build_object( |
91 |
{ |
92 |
{ |
92 |
class => 'Koha::Bookings', |
93 |
class => 'Koha::Bookings', |
Lines 97-104
subtest 'list() tests' => sub {
Link Here
|
97 |
} |
98 |
} |
98 |
); |
99 |
); |
99 |
|
100 |
|
100 |
my $start_2 = DateTime->now->add( days => 4 )->truncate( to => 'day' ); |
101 |
my $start_2 = dt_from_string->add( days => 4 )->truncate( to => 'day' ); |
101 |
my $end_2 = DateTime->now->add( days => 8 )->truncate( to => 'day' ); |
102 |
my $end_2 = dt_from_string->add( days => 8 )->truncate( to => 'day' ); |
102 |
my $booking_2 = $builder->build_object( |
103 |
my $booking_2 = $builder->build_object( |
103 |
{ |
104 |
{ |
104 |
class => 'Koha::Bookings', |
105 |
class => 'Koha::Bookings', |
Lines 120-126
subtest 'list() tests' => sub {
Link Here
|
120 |
); |
121 |
); |
121 |
|
122 |
|
122 |
# Filtering works, two bookings after today |
123 |
# Filtering works, two bookings after today |
123 |
my $api_filter = encode_json( { 'me.start_date' => { '>=' => DateTime->now->rfc3339 } } ); |
124 |
my $api_filter = encode_json( { 'me.start_date' => { '>=' => dt_from_string->rfc3339 } } ); |
124 |
$t->get_ok("//$userid:$password@/api/v1/bookings?q=$api_filter")->status_is(200)->json_is( |
125 |
$t->get_ok("//$userid:$password@/api/v1/bookings?q=$api_filter")->status_is(200)->json_is( |
125 |
'' => [ |
126 |
'' => [ |
126 |
$booking_1->to_api, |
127 |
$booking_1->to_api, |
Lines 129-135
subtest 'list() tests' => sub {
Link Here
|
129 |
'filtered returns two future bookings' |
130 |
'filtered returns two future bookings' |
130 |
); |
131 |
); |
131 |
|
132 |
|
132 |
$api_filter = encode_json( { 'me.start_date' => { '<=' => DateTime->now->rfc3339 } } ); |
133 |
$api_filter = encode_json( { 'me.start_date' => { '<=' => dt_from_string->rfc3339 } } ); |
133 |
$t->get_ok("//$userid:$password@/api/v1/bookings?q=$api_filter")->status_is(200) |
134 |
$t->get_ok("//$userid:$password@/api/v1/bookings?q=$api_filter")->status_is(200) |
134 |
->json_is( '' => [ $booking_0->to_api ], 'filtering to before today also works' ); |
135 |
->json_is( '' => [ $booking_0->to_api ], 'filtering to before today also works' ); |
135 |
|
136 |
|
Lines 216-223
subtest 'add() tests' => sub {
Link Here
|
216 |
biblio_id => $biblio->id, |
217 |
biblio_id => $biblio->id, |
217 |
item_id => undef, |
218 |
item_id => undef, |
218 |
patron_id => $patron->id, |
219 |
patron_id => $patron->id, |
219 |
start_date => DateTime->now->add( days => 2 )->rfc3339, |
220 |
start_date => dt_from_string->add( days => 2 )->rfc3339, |
220 |
end_date => DateTime->now->add( days => 6 )->rfc3339, |
221 |
end_date => dt_from_string->add( days => 6 )->rfc3339, |
221 |
}; |
222 |
}; |
222 |
|
223 |
|
223 |
# Unauthorized attempt to write |
224 |
# Unauthorized attempt to write |
Lines 303-310
subtest 'update() tests' => sub {
Link Here
|
303 |
my $booking_with_missing_field = { |
304 |
my $booking_with_missing_field = { |
304 |
item_id => undef, |
305 |
item_id => undef, |
305 |
patron_id => $patron->id, |
306 |
patron_id => $patron->id, |
306 |
start_date => DateTime->now->add( days => 2 )->rfc3339, |
307 |
start_date => dt_from_string->add( days => 2 )->rfc3339, |
307 |
end_date => DateTime->now->add( days => 6 )->rfc3339, |
308 |
end_date => dt_from_string->add( days => 6 )->rfc3339, |
308 |
}; |
309 |
}; |
309 |
|
310 |
|
310 |
$t->put_ok( "//$userid:$password@/api/v1/bookings/$booking_id" => json => $booking_with_missing_field ) |
311 |
$t->put_ok( "//$userid:$password@/api/v1/bookings/$booking_id" => json => $booking_with_missing_field ) |
Lines 315-322
subtest 'update() tests' => sub {
Link Here
|
315 |
biblio_id => $biblio->id, |
316 |
biblio_id => $biblio->id, |
316 |
item_id => undef, |
317 |
item_id => undef, |
317 |
patron_id => $patron->id, |
318 |
patron_id => $patron->id, |
318 |
start_date => DateTime->now->add( days => 2 )->rfc3339, |
319 |
start_date => dt_from_string->add( days => 2 )->rfc3339, |
319 |
end_date => DateTime->now->add( days => 6 )->rfc3339, |
320 |
end_date => dt_from_string->add( days => 6 )->rfc3339, |
320 |
}; |
321 |
}; |
321 |
|
322 |
|
322 |
$t->put_ok( "//$userid:$password@/api/v1/bookings/$booking_id" => json => $booking_with_updated_field ) |
323 |
$t->put_ok( "//$userid:$password@/api/v1/bookings/$booking_id" => json => $booking_with_updated_field ) |
Lines 328-335
subtest 'update() tests' => sub {
Link Here
|
328 |
biblio_id => $biblio->id, |
329 |
biblio_id => $biblio->id, |
329 |
item_id => undef, |
330 |
item_id => undef, |
330 |
patron_id => $patron->id, |
331 |
patron_id => $patron->id, |
331 |
start_date => DateTime->now->add( days => 2 )->rfc3339, |
332 |
start_date => dt_from_string->add( days => 2 )->rfc3339, |
332 |
end_date => DateTime->now->add( days => 6 )->rfc3339, |
333 |
end_date => dt_from_string->add( days => 6 )->rfc3339, |
333 |
}; |
334 |
}; |
334 |
|
335 |
|
335 |
$t->put_ok( "//$userid:$password@/api/v1/bookings/$booking_id" => json => $booking_with_invalid_field ) |
336 |
$t->put_ok( "//$userid:$password@/api/v1/bookings/$booking_id" => json => $booking_with_invalid_field ) |
336 |
- |
|
|