Skip to content

Commit

Permalink
Bug 14697: Add exceptions and overload ReturnClaim->store
Browse files Browse the repository at this point in the history
This patch adds an overloaded Koha::Checkouts::ReturnClaim->store method
that will raise an exception if the mandatory created_by attribute is
missing.

Tests are added for this overloaded method.

To test:
1. Apply this patch
2. Run:
   $ kshell
  k$ prove t/db_dependent/Koha/Checkouts/ReturnClaim.t
=> SUCCESS: Tests pass
3. Sign off :-D
  • Loading branch information
tomascohen committed Oct 23, 2019
1 parent 0edfe37 commit dee172d
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 1 deletion.
39 changes: 38 additions & 1 deletion Koha/Checkouts/ReturnClaim.pm
Expand Up @@ -22,6 +22,7 @@ use Modern::Perl;
use base qw(Koha::Object);

use Koha::Checkouts;
use Koha::Exceptions::Checkouts::ReturnClaims;
use Koha::Old::Checkouts;
use Koha::Patrons;

Expand All @@ -31,10 +32,29 @@ Koha::Checkouts::ReturnClaim - Koha ReturnClaim object class
=head1 API
=head2 Class Methods
=head2 Class methods
=cut

=head3 store
my $return_claim = Koha::Checkout::ReturnClaim->new($args)->store;
Overloaded I<store> method that validates the attributes and raises relevant
exceptions as needed.
=cut

sub store {
my ( $self ) = @_;

unless ( $self->created_by ) {
Koha::Exceptions::Checkouts::ReturnClaims::NoCreatedBy->throw();
}

return $self->SUPER::store;
}

=head3 checkout
=cut
Expand All @@ -60,6 +80,23 @@ sub patron {
return Koha::Patron->_new_from_dbic( $borrower ) if $borrower;
}

=head3 to_api_mapping
This method returns the mapping for representing a Koha::Checkouts::ReturnClaim object
on the API.
=cut

sub to_api_mapping {
return {
id => 'claim_id',
itemnumber => 'item_id',
borrowernumber => 'patron_id'
};
}

=head2 Internal methods
=head3 _type
=cut
Expand Down
50 changes: 50 additions & 0 deletions Koha/Exceptions/Checkouts/ReturnClaims.pm
@@ -0,0 +1,50 @@
package Koha::Exceptions::Checkouts::ReturnClaims;

# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 3 of the License, or (at your option) any later
# version.
#
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with Koha; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

use Modern::Perl;

use Koha::Exceptions::Exception;

use Exception::Class (
'Koha::Exceptions::Checkouts::ReturnClaims' => {
isa => 'Koha::Exceptions::Exception',
description => 'Something went wrong!',
},
'Koha::Exceptions::Checkouts::ReturnClaims::NoCreatedBy' => {
isa => 'Koha::Exceptions::Checkouts::ReturnClaims',
description => 'created_by is mandatory'
},
);

=head1 NAME
Koha::Exceptions::Checkouts - Base class for Checkouts exceptions
=head1 Exceptions
=head2 Koha::Exceptions::Checkouts::ReturnClaims
Generic return claim exception
=head2 Koha::Exceptions::Checkouts::ReturnClaims::NoCreatedBy
Exception to be used when a return claim is requested to be store but
the 'created_by' param is not passed.
=cut

1;
105 changes: 105 additions & 0 deletions t/db_dependent/Koha/Checkouts/ReturnClaim.t
@@ -0,0 +1,105 @@
#!/usr/bin/perl

# This file is part of Koha
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.

use Modern::Perl;

use Test::More tests => 1;
use Test::Exception;

use Koha::Database;
use Koha::Checkouts::ReturnClaims;

use t::lib::TestBuilder;

my $schema = Koha::Database->new->schema;
my $builder = t::lib::TestBuilder->new;

subtest "store() tests" => sub {

plan tests => 6;

$schema->storage->txn_begin;

my $librarian = $builder->build_object({ class => 'Koha::Patrons' });
my $patron = $builder->build_object({ class => 'Koha::Patrons' });
my $item = $builder->build_sample_item;

my $checkout = $builder->build_object(
{
class => 'Koha::Checkouts',
value => {
borrowernumber => $patron->borrowernumber,
itemnumber => $item->itemnumber,
branchcode => $patron->branchcode
}
}
);

throws_ok
{ Koha::Checkouts::ReturnClaim->new(
{
issue_id => $checkout->id,
itemnumber => $checkout->itemnumber,
borrowernumber => $checkout->borrowernumber,
notes => 'Some notes'
}
)->store }
'Koha::Exceptions::Checkouts::ReturnClaims::NoCreatedBy',
'Exception thrown correctly';

is( Koha::Checkouts::ReturnClaims->search({ issue_id => $checkout->id })->count, 0, 'No claims stored' );

my $claim = Koha::Checkouts::ReturnClaim->new(
{
issue_id => $checkout->id,
itemnumber => $checkout->itemnumber,
borrowernumber => $checkout->borrowernumber,
notes => 'Some notes',
created_by => $librarian->borrowernumber
}
)->store;

is( ref($claim), 'Koha::Checkouts::ReturnClaim', 'Object type is correct' );
is( Koha::Checkouts::ReturnClaims->search( { issue_id => $checkout->id } )->count, 1, 'Claim stored on the DB');

{ # hide useless warnings
local *STDERR;
open STDERR, '>', '/dev/null';
throws_ok {
Koha::Checkouts::ReturnClaim->new(
{
issue_id => $checkout->id + 1000,
itemnumber => $checkout->itemnumber,
borrowernumber => $checkout->borrowernumber,
notes => 'Some notes',
created_by => $librarian->borrowernumber
}
)->store;
}
'Koha::Exceptions::Object::FKConstraint',
'An exception is thrown on invalid issue_id';
close STDERR;

is(
$@->broken_fk,
'issue_id',
'Exception field is correct'
);
}

$schema->storage->txn_rollback;
};

0 comments on commit dee172d

Please sign in to comment.