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

(-)a/Koha/Items.pm (-4 / +43 lines)
Lines 20-25 package Koha::Items; Link Here
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Carp;
22
use Carp;
23
use Try::Tiny;
23
24
24
use Koha::Database;
25
use Koha::Database;
25
26
Lines 33-41 Koha::Items - Koha Item object set class Link Here
33
34
34
=head1 API
35
=head1 API
35
36
36
=head2 Class Methods
37
=head2 Class methods
37
38
=cut
39
38
40
=head3 filter_by_for_loan
39
=head3 filter_by_for_loan
41
40
Lines 50-56 sub filter_by_for_loan { Link Here
50
    return $self->search( { notforloan => [ 0, undef ] } );
49
    return $self->search( { notforloan => [ 0, undef ] } );
51
}
50
}
52
51
53
=head3 type
52
=head3 safe_delete
53
54
    $items->safe_delete;
55
56
Tries to mark all items in the resultset as deleted. This is done inside a transaction
57
which will be rolled back if an exception is raised.
58
59
=cut
60
61
sub safe_delete {
62
    my ($self) = @_;
63
64
    try {
65
        $self->_resultset->result_source->schema->txn_do(
66
            sub {
67
                while ( my $item = $self->next ) {
68
                    my $result = $item->safe_delete;
69
                    # FIXME: $item->safe_delete has a weird set of return value options:
70
                    # - a string representing the blocking condition
71
                    # - the result of calling $self->delete (Koha::Item ref)
72
                    unless ( ref($result) eq 'Koha::Item' ) {
73
                        Koha::Exceptions::Object::CannotBeDeleted->throw(
74
                            object => $item,
75
                            reason => $result
76
                        );
77
                    }
78
                }
79
            }
80
        );
81
82
        return;
83
    }
84
    catch {
85
        $_->rethrow;
86
    };
87
}
88
89
90
=head2 Internal methods
91
92
=head3 _type
54
93
55
=cut
94
=cut
56
95
(-)a/t/db_dependent/Koha/Items.t (-2 / +50 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 12;
22
use Test::More tests => 13;
23
use Test::Exception;
23
use Test::Exception;
24
use Time::Fake;
24
use Time::Fake;
25
25
Lines 947-949 $retrieved_item_1->delete; Link Here
947
is( Koha::Items->search->count, $nb_of_items - 1, 'Delete should have deleted the item' );
947
is( Koha::Items->search->count, $nb_of_items - 1, 'Delete should have deleted the item' );
948
948
949
$schema->storage->txn_rollback;
949
$schema->storage->txn_rollback;
950
- 
950
951
subtest 'safe_delete() tests' => sub {
952
953
    plan tests => 7;
954
955
    $schema->storage->txn_begin;
956
957
    my $library = $builder->build_object({ class => 'Koha::Libraries' });
958
    my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
959
    my $item_1  = $builder->build_sample_item({ library => $library->branchcode });
960
    my $item_2  = $builder->build_sample_item({ library => $library->branchcode });
961
962
    my $item_1_id = $item_1->id;
963
    my $item_2_id = $item_2->id;
964
965
    t::lib::Mocks::mock_userenv(
966
        { patron => $patron, branchcode => $patron->branchcode } );
967
968
    # book_on_loan
969
    C4::Circulation::AddIssue( $patron->unblessed, $item_1->barcode );
970
971
    is(
972
        $item_1->safe_delete,
973
        'book_on_loan',
974
        'item that is on loan cannot be deleted',
975
    );
976
977
    my $items_rs = Koha::Items->search({ itemnumber => { -in => [ $item_1_id, $item_2_id ] } });
978
    is( $items_rs->count, 2, 'We get the right items count' );
979
980
    throws_ok
981
        { $items_rs->safe_delete }
982
        'Koha::Exceptions::Object::CannotBeDeleted',
983
        'Exception thrown';
984
985
    is( $@->reason, 'book_on_loan', 'Exception reason is correct' );
986
987
    $items_rs = Koha::Items->search({ itemnumber => { -in => [ $item_1_id, $item_2_id ] } });
988
    is( $items_rs->count, 2, 'We get the right items count, rollback happened' );
989
990
    C4::Circulation::AddReturn( $item_1->barcode );
991
992
    my $result = $items_rs->safe_delete;
993
    is( $result, undef, 'Successful call returns undef' );
994
    $items_rs = Koha::Items->search({ itemnumber => { -in => [ $item_1_id, $item_2_id ] } });
995
    is( $items_rs->count, 0, 'Successful safe delete, items are not ' );
996
997
    $schema->storage->txn_rollback;
998
};

Return to bug 26509