use Modern::Perl;
use File::Basename qw/basename/;
+
+use C4::Circulation qw(AddIssue AddReturn);
+
use Koha::Database;
use Koha::Illrequestattributes;
use Koha::Illrequest::Config;
use Test::MockObject;
use Test::MockModule;
use Test::Exception;
+use Test::Deep qw/ cmp_deeply ignore /;
+use Test::Warn;
-use Test::More tests => 12;
+use Test::More tests => 13;
my $schema = Koha::Database->new->schema;
my $builder = t::lib::TestBuilder->new;
subtest 'Working with related objects' => sub {
- plan tests => 5;
+ plan tests => 7;
$schema->storage->txn_begin;
is($illrq_obj->illrequestattributes->count + 1, Koha::Illrequestattributes->search->count,
"Fetching expected number of Illrequestattributes for our request.");
+ is($illrq_obj->biblio, undef, "->biblio returns undef if no biblio");
+ my $biblio = $builder->build_object({ class => 'Koha::Biblios' });
+ my $req_bib = $builder->build_object({
+ class => 'Koha::Illrequests',
+ value => {
+ biblio_id => $biblio->biblionumber
+ }
+ });
+ isa_ok($req_bib->biblio, 'Koha::Biblio', "OK accessing related biblio");
+
$illrq_obj->delete;
is(Koha::Illrequestattributes->search->count, 1,
"Correct number of illrequestattributes after delete.");
# Compare the updated graph to the expected graph
# The structure we compare against here is just a copy of the structure found
# in Koha::Illrequest::_core_status_graph() + the new node we created above
- is_deeply( $new_graph,
+ cmp_deeply( $new_graph,
{
+ TEST => {
+ prev_actions => [ ],
+ id => 'TEST',
+ next_actions => [ ],
+ },
NEW => {
prev_actions => [ ], # Actions containing buttons
# leading to this status
name => 'Requested',
ui_method_name => 'Confirm request',
method => 'confirm',
- next_actions => [ 'REQREV', 'COMP' ],
+ next_actions => [ 'REQREV', 'COMP', 'CHK' ],
ui_method_icon => 'fa-check',
},
GENREQ => {
name => 'Requested from partners',
ui_method_name => 'Place request with partners',
method => 'generic_confirm',
- next_actions => [ 'COMP' ],
+ next_actions => [ 'COMP', 'CHK' ],
ui_method_icon => 'fa-send-o',
},
REQREV => {
next_actions => [ 'REQ', 'GENREQ', 'KILL' ],
ui_method_icon => 'fa-times',
},
- TEST => {
- prev_actions => [ ],
- id => 'TEST',
- next_actions => [ ],
- },
QUEUED => {
prev_actions => [ ],
id => 'QUEUED',
name => 'Completed',
ui_method_name => 'Mark completed',
method => 'mark_completed',
- next_actions => [ ],
+ next_actions => [ 'CHK' ],
ui_method_icon => 'fa-check',
},
KILL => {
next_actions => [ ],
ui_method_icon => 'fa-trash',
},
+ CHK => {
+ prev_actions => [ 'REQ', 'GENREQ', 'COMP' ],
+ id => 'CHK',
+ name => 'Checked out',
+ ui_method_name => 'Check out',
+ needs_prefs => [ 'CirculateILL' ],
+ needs_perms => [ 'user_circulate_circulate_remaining_permissions' ],
+ needs_all => ignore(),
+ method => 'check_out',
+ next_actions => [ ],
+ ui_method_icon => 'fa-upload',
+ },
+ RET => {
+ prev_actions => [ 'CHK' ],
+ id => 'RET',
+ name => 'Returned to library',
+ ui_method_name => 'Check in',
+ method => 'check_in',
+ next_actions => [ 'COMP' ],
+ ui_method_icon => 'fa-download',
+ }
},
"new node + core_status_graph = bigger status graph"
) || diag explain $new_graph;
subtest 'Checking out' => sub {
- plan tests => 16;
+ plan tests => 17;
$schema->storage->txn_begin;
- my $itemtype = $builder->build_object({ class => 'Koha::ItemTypes' });
+ my $itemtype = $builder->build_object({
+ class => 'Koha::ItemTypes',
+ value => {
+ notforloan => 1
+ }
+ });
my $library = $builder->build_object({ class => 'Koha::Libraries' });
- my $biblio = $builder->build_object({ class => 'Koha::Biblios' });
+ my $biblio = $builder->build_sample_biblio();
my $patron = $builder->build_object({
class => 'Koha::Patrons',
value => { category_type => 'x' }
inhouse => 1
});
# Too many items attached to biblio
- my $item1 = $builder->build_object({
- class => 'Koha::Items',
- value => {
- biblionumber => $biblio->biblionumber,
- biblioitemnumber => 1
- }
- });
- my $item2 = $builder->build_object({
- class => 'Koha::Items',
- value => {
- biblionumber => $biblio->biblionumber,
- biblioitemnumber => 2
- }
- });
+ my $item1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
+ my $item2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
my $form_stage_two_items = $request->check_out({
stage => 'form',
item_type => $itemtype->itemtype,
itemcount => 1
});
- # Passed validation
- #
# Delete the items we created, so we can test that we can create one
- Koha::Items->find({ itemnumber => $item1->itemnumber })->delete;
- Koha::Items->find({ itemnumber => $item2->itemnumber })->delete;
- # Create a biblioitem
- my $biblioitem = $builder->build_object({
- class => 'Koha::Biblioitems',
- value => {
- biblionumber => $biblio->biblionumber
- }
- });
+ $item1->delete;
+ $item2->delete;
+
+ # We need to mock the user environment for AddIssue
+ t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
+ #
+
# First we pass bad parameters to the item creation to test we're
# catching the failure of item creation
- # Note: This will generate a DBD::mysql error when running this test!
- my $form_stage_bad_branchcode = $request->check_out({
- stage => 'form',
- item_type => $itemtype->itemtype,
- branchcode => '---'
- });
+ my $form_stage_bad_branchcode;
+ warning_like {
+ $form_stage_bad_branchcode = $request->check_out({
+ stage => 'form',
+ item_type => $itemtype->itemtype,
+ branchcode => '---'
+ });
+ } qr/DBD::mysql::st execute failed: Cannot add or update a child row: a foreign key constraint fails/,
+ "Item creation fails on bad parameters";
+
is_deeply($form_stage_bad_branchcode->{value}->{errors}, {
item_creation => 1
- });
+ },"We get expected failure of item creation");
+
# Now create a proper item
my $form_stage_good_branchcode = $request->check_out({
stage => 'form',
NOT_FOR_LOAN => 1,
itemtype_notforloan => $itemtype->itemtype
}
- }
+ },
+ "We get expected error on notforloan of item"
);
# Delete the item that was created
$biblio->items->delete;
}
});
# We need to mock the user environment for AddIssue
- t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} });
+ t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
my $form_stage_loanable = $request->check_out({
stage => 'form',
item_type => $itemtype_loanable->itemtype,
$schema->storage->txn_rollback;
};
+
+subtest 'Checking in hook' => sub {
+
+ plan tests => 2;
+
+ $schema->storage->txn_begin;
+
+ # Build infrastructure
+ my $backend = Test::MockObject->new;
+ $backend->set_isa('Koha::Illbackends::Mock');
+ $backend->set_always('name', 'Mock');
+
+ my $config = Test::MockObject->new;
+ $config->set_always('backend_dir', "/tmp");
+
+ my $item = $builder->build_sample_item();
+ my $patron = $builder->build_object({ class => 'Koha::Patrons' });
+
+ t::lib::Mocks::mock_userenv(
+ {
+ patron => $patron,
+ branchcode => $patron->branchcode
+ }
+ );
+
+ my $illrq = $builder->build_object(
+ {
+ class => 'Koha::Illrequests',
+ value => {
+ biblio_id => $item->biblio->biblionumber,
+ status => 'NEW'
+ }
+ }
+ );
+
+ $illrq->_config($config);
+ $illrq->_backend($backend);
+
+ t::lib::Mocks::mock_preference('CirculateILL', 1);
+
+ # Add an issue
+ AddIssue( $patron->unblessed, $item->barcode );
+ # Make the item withdrawn so checking-in is rejected
+ t::lib::Mocks::mock_preference('BlockReturnOfWithdrawnItems', 1);
+ $item->set({ withdrawn => 1 })->store;
+ AddReturn( $item->barcode, $patron->branchcode );
+ # refresh request
+ $illrq->discard_changes;
+ isnt( $illrq->status, 'RET' );
+
+ # allow the check-in
+ $item->set({ withdrawn => 0 })->store;
+ AddReturn( $item->barcode, $patron->branchcode );
+ # refresh request
+ $illrq->discard_changes;
+ is( $illrq->status, 'RET' );
+
+ $schema->storage->txn_rollback;
+};