Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 14697: Add exceptions and overload ReturnClaim->store
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
1 parent
0edfe37
commit dee172d
Showing
3 changed files
with
193 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}; |