use Test::More tests => 1;
use Test::MockModule;
+use Test::MockObject;
use Test::Mojo;
use Test::Warn;
use C4::Auth;
use Koha::Illrequests;
+use Koha::DateUtils qw( format_sqldatetime );
my $schema = Koha::Database->new->schema;
my $builder = t::lib::TestBuilder->new;
subtest 'list() tests' => sub {
- plan tests => 18;
+ plan tests => 20;
+
+ # Mock ILLBackend (as object)
+ my $backend = Test::MockObject->new;
+ $backend->set_isa('Koha::Illbackends::Mock');
+ $backend->set_always('name', 'Mock');
+ $backend->set_always('capabilities', sub { return 'bar'; } );
+ $backend->mock(
+ 'metadata',
+ sub {
+ my ( $self, $rq ) = @_;
+ return {
+ ID => $rq->illrequest_id,
+ Title => $rq->patron->borrowernumber
+ }
+ }
+ );
+ $backend->mock(
+ 'status_graph', sub {},
+ );
+ # Mock Koha::Illrequest::load_backend (to load Mocked Backend)
my $illreqmodule = Test::MockModule->new('Koha::Illrequest');
- # Mock ->capabilities
- $illreqmodule->mock( 'capabilities', sub { return 'capable'; } );
- # Mock ->metadata
- $illreqmodule->mock( 'metadata', sub { return 'metawhat?'; } );
+ $illreqmodule->mock( 'load_backend',
+ sub { my $self = shift; $self->{_my_backend} = $backend; return $self }
+ );
$schema->storage->txn_begin;
{
class => 'Koha::Illrequests',
value => {
- backend => 'FreeForm',
+ backend => 'Mock',
branchcode => $library->branchcode,
borrowernumber => $patron->borrowernumber
}
}
);
+ # The api response is always augmented with the id_prefix
+ my $response = $illrequest->unblessed;
+ $response->{id_prefix} = $illrequest->id_prefix;
+
+ my $req_formatted = add_formatted($response);
+
# One illrequest created, should get returned
$tx = $t->ua->build_tx( GET => '/api/v1/illrequests' );
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
$tx->req->env( { REMOTE_ADDR => $remote_address } );
- $t->request_ok($tx)->status_is(200)->json_is( [ $illrequest->unblessed ] );
+ $t->request_ok($tx)->status_is(200)->json_is( [ $req_formatted ] );
# One illrequest created, returned with augmented data
$tx = $t->ua->build_tx( GET =>
- '/api/v1/illrequests?embed=patron,library,capabilities,metadata' );
+ '/api/v1/illrequests?embed=patron,library,capabilities,metadata,requested_partners' );
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
$tx->req->env( { REMOTE_ADDR => $remote_address } );
$t->request_ok($tx)->status_is(200)
- ->json_has( '/0/patron', $patron->unblessed )
- ->json_has( '/0/capabilities', 'capable' )
- ->json_has( '/0/library', $library->unblessed )
- ->json_has( '/0/metadata', 'metawhat?' );
+ ->json_has( '/0/patron', 'patron embedded' )
+ ->json_is( '/0/patron/patron_id', $patron->borrowernumber, 'The right patron is embeded')
+ ->json_has( '/0/requested_partners', 'requested_partners embedded' )
+ ->json_has( '/0/capabilities', 'capabilities embedded' )
+ ->json_has( '/0/library', 'library embedded' )
+ ->json_has( '/0/metadata', 'metadata embedded' )
+ ->json_hasnt( '/1', 'Only one request was created' );
# Create another ILL request
my $illrequest2 = $builder->build_object(
{
class => 'Koha::Illrequests',
value => {
+ backend => 'Mock',
branchcode => $library->branchcode,
borrowernumber => $patron->borrowernumber
}
}
);
+ # The api response is always augmented with the id_prefix
+ my $response2 = $illrequest2->unblessed;
+ $response2->{id_prefix} = $illrequest2->id_prefix;
+
+ my $req2_formatted = add_formatted($response2);
+
# Two illrequest created, should get returned
$tx = $t->ua->build_tx( GET => '/api/v1/illrequests' );
$tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
$tx->req->env( { REMOTE_ADDR => $remote_address } );
$t->request_ok($tx)->status_is(200)
- ->json_is( [ $illrequest->unblessed, $illrequest2->unblessed ] );
+ ->json_is( [ $req_formatted, $req2_formatted ] );
# Warn on unsupported query parameter
$tx = $t->ua->build_tx( GET => '/api/v1/illrequests?request_blah=blah' );
$schema->storage->txn_rollback;
};
+sub add_formatted {
+ my $req = shift;
+ my @format_dates = ( 'placed', 'updated', 'completed' );
+ # We need to embellish the request with properties that the API
+ # controller calculates on the fly
+ # Create new "formatted" columns for each date column
+ # that needs formatting
+ foreach my $field(@format_dates) {
+ if (defined $req->{$field}) {
+ $req->{$field . "_formatted"} = format_sqldatetime(
+ $req->{$field},
+ undef,
+ undef,
+ 1
+ );
+ }
+ }
+ return $req;
+}
+
sub create_user_and_session {
my $args = shift;