From 0f7c2e8bec35bf37b62f1fc5454021c2f6869c23 Mon Sep 17 00:00:00 2001 From: Lari Taskula Date: Tue, 15 Dec 2015 09:01:42 +0000 Subject: [PATCH] Bug 13906 (follow-up) - New TestObjectFactory for creating fines into accountlines --- t/lib/TestObjects/FinesFactory.pm | 150 +++++++++++++++++++++++++++++++++++++ t/lib/TestObjects/ObjectFactory.pm | 6 ++ 2 files changed, 156 insertions(+) create mode 100644 t/lib/TestObjects/FinesFactory.pm diff --git a/t/lib/TestObjects/FinesFactory.pm b/t/lib/TestObjects/FinesFactory.pm new file mode 100644 index 0000000..2d307d9 --- /dev/null +++ b/t/lib/TestObjects/FinesFactory.pm @@ -0,0 +1,150 @@ +package t::lib::TestObjects::FinesFactory; + +# Copyright Vaara-kirjastot 2015 +# +# 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 Carp; +use Scalar::Util qw(blessed); + +use C4::Members; +use C4::Accounts; +use Koha::Borrowers; + +use base qw(t::lib::TestObjects::ObjectFactory); + +use Koha::Exception::ObjectExists; + +sub new { + my ($class) = @_; + + my $self = {}; + bless($self, $class); + return $self; +} + +sub getDefaultHashKey { + return 'note'; +} +sub getObjectType { + return 'HASH'; +} + +=head createTestGroup( $data [, $hashKey, $testContexts...] ) +@OVERLOADED + + FinesFactory creates new fines into accountlines. + After testing, all fines created by the FinesFactory will be + deleted at tearDown. + + my $fines = t::lib::TestObjects::FinesFactory->createTestGroup([ + amount => 10.0, + cardnumber => $borrowers->{'superuberadmin'}->cardnumber, + accounttype => 'FU', + note => 'unique identifier', + }, + ], undef, $testContext1, $testContext2, $testContext3); + + #Do test stuff... + + t::lib::TestObjects::ObjectFactory->tearDownTestContext($testContext1); + t::lib::TestObjects::ObjectFactory->tearDownTestContext($testContext2); + t::lib::TestObjects::ObjectFactory->tearDownTestContext($testContext3); + +See t::lib::TestObjects::ObjectFactory for more documentation +=cut + +sub handleTestObject { + my ($class, $fine, $stashes) = @_; + + my $borrower = Koha::Borrowers->cast($fine->{cardnumber}); + + my $accountno = C4::Accounts::getnextacctno($borrower->borrowernumber); + + C4::Accounts::manualinvoice( + $borrower->borrowernumber, # borrowernumber + undef, # itemnumber + "Test payment", # description + $fine->{accounttype}, # accounttype + $fine->{amount}, # amountoutstanding + $fine->{note} # note, unique identifier + ); + + my $new_fine = $fine; + + $new_fine->{accountno} = $accountno; + + return $new_fine; +} + +=head validateAndPopulateDefaultValues +@OVERLOAD + +Validates given Object parameters and makes sure that critical fields are given +and populates defaults for missing values. +=cut + +sub validateAndPopulateDefaultValues { + my ($class, $object, $hashKey) = @_; + $class->SUPER::validateAndPopulateDefaultValues($object, $hashKey); + + unless ($object->{cardnumber}) { + Koha::Exception::BadParameter->throw(error => __PACKAGE__."->createTestGroup():> 'cardnumber' is a mandatory parameter!"); + } + unless ($object->{note}) { + Koha::Exception::BadParameter->throw(error => __PACKAGE__."->createTestGroup():> 'note' is a mandatory parameter!"); + } + + $object->{accounttype} = "FU" unless defined $object->{accounttype}; +} + +=head deleteTestGroup +@OVERLOADED + + my $records = createTestGroup(); + ##Do funky stuff + deleteTestGroup($prefs); + +Removes the given test group from the DB. + +=cut + +sub deleteTestGroup { + my ($class, $acct) = @_; + + my $schema = Koha::Database->new_schema(); + while( my ($key, $val) = each %$acct) { + if ($schema->resultset('Accountline')->find({"accountno" => $val->{accountno}, "borrowernumber" => $val->{borrowernumber} })) { + $schema->resultset('Accountline')->find({"accountno" => $val->{accountno}, "borrowernumber" => $val->{borrowernumber} })->delete(); + } + } +} + +sub _deleteTestGroupFromIdentifiers { + my ($self, $testGroupIdentifiers) = @_; + + my $schema = Koha::Database->new_schema(); + foreach my $key (@$testGroupIdentifiers) { + if ($schema->resultset('Accountline')->find({"note" => $key})) { + $schema->resultset('Accountline')->find({"note" => $key})->delete(); + } + } +} + + +1; diff --git a/t/lib/TestObjects/ObjectFactory.pm b/t/lib/TestObjects/ObjectFactory.pm index 8c92b69..b117334 100644 --- a/t/lib/TestObjects/ObjectFactory.pm +++ b/t/lib/TestObjects/ObjectFactory.pm @@ -196,6 +196,12 @@ sub tearDownTestContext { t::lib::TestObjects::MessageQueueFactory->deleteTestGroup($stash->{messagequeue}); delete $stash->{messagequeue}; } + if ($stash->{fines}) { + require t::lib::TestObjects::FinesFactory; + t::lib::TestObjects::FinesFactory->deleteTestGroup($stash->{fines}); + delete $stash->{fines}; + } + } =head getHashKey -- 1.9.1