From 8621389224f3d440a268173919ddd53513de45ca Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Thu, 21 Jul 2016 18:32:05 +0000 Subject: [PATCH] Bug 14826 - Unit Tests --- t/db_dependent/Accounts.t | 13 +- t/db_dependent/Circulation.t | 15 +- .../Circulation/NoIssuesChargeGuarantees.t | 12 +- t/db_dependent/Koha/Checkouts.t~HEAD | 63 +++++++ t/db_dependent/Patron/Relationships.t | 181 +++++++++++++++++++++ 5 files changed, 280 insertions(+), 4 deletions(-) create mode 100644 t/db_dependent/Koha/Checkouts.t~HEAD create mode 100755 t/db_dependent/Patron/Relationships.t diff --git a/t/db_dependent/Accounts.t b/t/db_dependent/Accounts.t index dfc7f65..879a086 100644 --- a/t/db_dependent/Accounts.t +++ b/t/db_dependent/Accounts.t @@ -27,6 +27,7 @@ use t::lib::TestBuilder; use Koha::Account; use Koha::Account::Lines; use Koha::Account::Line; +use Koha::Account::Offsets; BEGIN { use_ok('C4::Accounts'); @@ -253,7 +254,7 @@ subtest "Koha::Account::pay tests" => sub { subtest "More Koha::Account::pay tests" => sub { - plan tests => 6; + plan tests => 8; # Create a borrower my $category = $builder->build({ source => 'Category' })->{ categorycode }; @@ -283,6 +284,10 @@ subtest "More Koha::Account::pay tests" => sub { # make the full payment $account->pay({ lines => [$line], amount => $amount, library_id => $branch, note => 'A payment note' }); + my $offset = Koha::Account::Offsets->search({ debit_id => $accountline->{accountlines_id} })->next(); + is( $offset->amount(), '-100.000000', 'Offset amount is -100.00' ); + is( $offset->type(), 'Payment', 'Offset type is Payment' ); + my $stat = $schema->resultset('Statistic')->search({ branch => $branch, type => 'payment' @@ -302,7 +307,7 @@ subtest "More Koha::Account::pay tests" => sub { subtest "Even more Koha::Account::pay tests" => sub { - plan tests => 6; + plan tests => 8; # Create a borrower my $category = $builder->build({ source => 'Category' })->{ categorycode }; @@ -333,6 +338,10 @@ subtest "Even more Koha::Account::pay tests" => sub { # make the full payment $account->pay({ lines => [$line], amount => $partialamount, library_id => $branch, note => 'A payment note' }); + my $offset = Koha::Account::Offsets->search( { debit_id => $accountline->{ accountlines_id } } )->next(); + is( $offset->amount, '-60.000000', 'Offset amount is -60.00' ); + is( $offset->type, 'Payment', 'Offset type is payment' ); + my $stat = $schema->resultset('Statistic')->search({ branch => $branch, type => 'payment' diff --git a/t/db_dependent/Circulation.t b/t/db_dependent/Circulation.t index f06e4d5..76f49c9 100755 --- a/t/db_dependent/Circulation.t +++ b/t/db_dependent/Circulation.t @@ -27,10 +27,12 @@ use C4::Reserves; use C4::Overdues qw(UpdateFine); use Koha::DateUtils; use Koha::Database; +use Koha::Account::Lines; +use Koha::Account::Offsets; use t::lib::TestBuilder; -use Test::More tests => 86; +use Test::More tests => 93; BEGIN { use_ok('C4::Circulation'); @@ -556,6 +558,17 @@ C4::Context->dbh->do("DELETE FROM accountlines"); } ); + my $line = Koha::Account::Lines->search({ borrowernumber => $renewing_borrower->{borrowernumber} })->next(); + is( $line->accounttype, 'FU', 'Account line type is FU' ); + is( $line->lastincrement, '15.000000', 'Account line last increment is 15.00' ); + is( $line->amountoutstanding, '15.000000', 'Account line amount outstanding is 15.00' ); + is( $line->amount, '15.000000', 'Account line amount is 15.00' ); + is( $line->issue_id, $issue->id, 'Account line issue id matches' ); + + my $offset = Koha::Account::Offsets->search({ debit_id => $line->id })->next(); + is( $offset->type, 'Fine', 'Account offset type is Fine' ); + is( $offset->amount, '15.000000', 'Account offset amount is 15.00' ); + LostItem( $itemnumber, 1 ); my $item = Koha::Database->new()->schema()->resultset('Item')->find($itemnumber); diff --git a/t/db_dependent/Circulation/NoIssuesChargeGuarantees.t b/t/db_dependent/Circulation/NoIssuesChargeGuarantees.t index a5cbb62..1993ec8 100644 --- a/t/db_dependent/Circulation/NoIssuesChargeGuarantees.t +++ b/t/db_dependent/Circulation/NoIssuesChargeGuarantees.t @@ -17,12 +17,14 @@ use Modern::Perl; -use Test::More tests => 2; +use Test::More tests => 6; use t::lib::TestBuilder; use C4::Accounts qw( manualinvoice ); use C4::Circulation qw( CanBookBeIssued ); +use Koha::Account::Lines; +use Koha::Account::Offsets; my $schema = Koha::Database->new->schema; $schema->storage->txn_begin; @@ -62,6 +64,14 @@ manualinvoice( $guarantee->{borrowernumber}, undef, undef, 'L', 10.00 ); ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->{barcode} ); is( $issuingimpossible->{DEBT_GUARANTEES} + 0, '10.00' + 0, "Patron cannot check out item due to debt for guarantee" ); +my $accountline = Koha::Account::Lines->search({ borrowernumber => $guarantee->{borrowernumber} })->next(); +is( $accountline->amountoutstanding, "10.000000", "Found 10.00 amount outstanding" ); +is( $accountline->accounttype, "L", "Account type is L" ); + +my $offset = Koha::Account::Offsets->search({ debit_id => $accountline->id })->next(); +is( $offset->type, 'Manual Debit', 'Got correct offset type' ); +is( $offset->amount, '10.000000', 'Got amount of $10.00' ); + $schema->storage->txn_rollback; 1; diff --git a/t/db_dependent/Koha/Checkouts.t~HEAD b/t/db_dependent/Koha/Checkouts.t~HEAD new file mode 100644 index 0000000..e8b1341 --- /dev/null +++ b/t/db_dependent/Koha/Checkouts.t~HEAD @@ -0,0 +1,63 @@ +#!/usr/bin/perl + +# Copyright 2015 Koha Development team +# +# 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 . + +use Modern::Perl; + +use Test::More tests => 4; + +use Koha::Checkout; +use Koha::Checkouts; +use Koha::Database; + +use t::lib::TestBuilder; + +my $schema = Koha::Database->new->schema; +$schema->storage->txn_begin; + +my $builder = t::lib::TestBuilder->new; +my $library = $builder->build( { source => 'Branch' } ); +my $patron = $builder->build( { source => 'Borrower', value => { branchcode => $library->{branchcode} } } ); +my $item_1 = $builder->build( { source => 'Item' } ); +my $item_2 = $builder->build( { source => 'Item' } ); +my $nb_of_checkouts = Koha::Checkouts->search->count; +my $new_checkout_1 = Koha::Checkout->new( + { borrowernumber => $patron->{borrowernumber}, + itemnumber => $item_1->{itemnumber}, + branchcode => $library->{branchcode}, + } +)->store; +my $new_checkout_2 = Koha::Checkout->new( + { borrowernumber => $patron->{borrowernumber}, + itemnumber => $item_2->{itemnumber}, + branchcode => $library->{branchcode}, + } +)->store; + +like( $new_checkout_1->issue_id, qr|^\d+$|, 'Adding a new checkout should have set the issue_id' ); +is( Koha::Checkouts->search->count, $nb_of_checkouts + 2, 'The 2 checkouts should have been added' ); + +my $retrieved_checkout_1 = Koha::Checkouts->find( $new_checkout_1->issue_id ); +is( $retrieved_checkout_1->itemnumber, $new_checkout_1->itemnumber, 'Find a checkout by id should return the correct checkout' ); + +$retrieved_checkout_1->delete; +is( Koha::Checkouts->search->count, $nb_of_checkouts + 1, 'Delete should have deleted the checkout' ); + +$schema->storage->txn_rollback; + +1; diff --git a/t/db_dependent/Patron/Relationships.t b/t/db_dependent/Patron/Relationships.t new file mode 100755 index 0000000..4aec07a --- /dev/null +++ b/t/db_dependent/Patron/Relationships.t @@ -0,0 +1,181 @@ +#!/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 . + +use Modern::Perl; + +use Test::More tests => 67; + +use C4::Context; + +use t::lib::TestBuilder; + +BEGIN { + use_ok('Koha::Objects'); + use_ok('Koha::Patrons'); + use_ok('Koha::Patron::Relationship'); + use_ok('Koha::Patron::Relationships'); +} + +# Start transaction +my $dbh = C4::Context->dbh; +$dbh->{AutoCommit} = 0; +$dbh->{RaiseError} = 1; + +my $builder = t::lib::TestBuilder->new(); + +# Father +my $kyle = Koha::Patrons->find( + $builder->build( + { + source => 'Borrower', + value => { + firstname => 'Kyle', + surname => 'Hall', + } + } + )->{borrowernumber} +); + +# Mother +my $chelsea = Koha::Patrons->find( + $builder->build( + { + source => 'Borrower', + value => { + firstname => 'Chelsea', + surname => 'Hall', + } + } + )->{borrowernumber} +); + +# Children +my $daria = Koha::Patrons->find( + $builder->build( + { + source => 'Borrower', + value => { + firstname => 'Daria', + surname => 'Hall', + } + } + )->{borrowernumber} +); + +my $kylie = Koha::Patrons->find( + $builder->build( + { + source => 'Borrower', + value => { + firstname => 'Kylie', + surname => 'Hall', + } + } + )->{borrowernumber} +); + +Koha::Patron::Relationship->new({ guarantor_id => $kyle->id, guarantee_id => $daria->id, relationship => 'father' })->store(); +Koha::Patron::Relationship->new({ guarantor_id => $kyle->id, guarantee_id => $kylie->id, relationship => 'father' })->store(); +Koha::Patron::Relationship->new({ guarantor_id => $chelsea->id, guarantee_id => $daria->id, relationship => 'mother' })->store(); +Koha::Patron::Relationship->new({ guarantor_id => $chelsea->id, guarantee_id => $kylie->id, relationship => 'mother' })->store(); +Koha::Patron::Relationship->new({ firstname => 'John', surname => 'Hall', guarantee_id => $daria->id, relationship => 'grandfather' })->store(); +Koha::Patron::Relationship->new({ firstname => 'Debra', surname => 'Hall', guarantee_id => $daria->id, relationship => 'grandmother' })->store(); +Koha::Patron::Relationship->new({ firstname => 'John', surname => 'Hall', guarantee_id => $kylie->id, relationship => 'grandfather' })->store(); +Koha::Patron::Relationship->new({ firstname => 'Debra', surname => 'Hall', guarantee_id => $kylie->id, relationship => 'grandmother' })->store(); + +my @gr; + +@gr = $kyle->guarantee_relationships(); +is( @gr, 2, 'Found 2 guarantee relationships for father' ); +is( $gr[0]->guarantor_id, $kyle->id, 'Guarantor matches for first relationship' ); +is( $gr[0]->guarantee_id, $daria->id, 'Guarantee matches for first relationship' ); +is( $gr[0]->relationship, 'father', 'Relationship is father' ); +is( ref($gr[0]->guarantee), 'Koha::Patron', 'Method guarantee returns a Koha::Patron' ); +is( $gr[0]->guarantee->id, $daria->id, 'Koha::Patron returned is the correct guarantee' ); +is( ref($gr[0]->guarantor), 'Koha::Patron', 'Method guarantor returns a Koha::Patron' ); +is( $gr[0]->guarantor->id, $kyle->id, 'Koha::Patron returned is the correct guarantor' ); + +is( $gr[1]->guarantor_id, $kyle->id, 'Guarantor matches for first relationship' ); +is( $gr[1]->guarantee_id, $kylie->id, 'Guarantee matches for first relationship' ); +is( $gr[1]->relationship, 'father', 'Relationship is father' ); +is( ref($gr[1]->guarantee), 'Koha::Patron', 'Method guarantee returns a Koha::Patron' ); +is( $gr[1]->guarantee->id, $kylie->id, 'Koha::Patron returned is the correct guarantee' ); +is( ref($gr[1]->guarantor), 'Koha::Patron', 'Method guarantor returns a Koha::Patron' ); +is( $gr[1]->guarantor->id, $kyle->id, 'Koha::Patron returned is the correct guarantor' ); + +@gr = $chelsea->guarantee_relationships(); +is( @gr, 2, 'Found 2 guarantee relationships for mother' ); +is( $gr[0]->guarantor_id, $chelsea->id, 'Guarantor matches for first relationship' ); +is( $gr[0]->guarantee_id, $daria->id, 'Guarantee matches for first relationship' ); +is( $gr[0]->relationship, 'mother', 'Relationship is mother' ); +is( ref($gr[0]->guarantee), 'Koha::Patron', 'Method guarantee returns a Koha::Patron' ); +is( $gr[0]->guarantee->id, $daria->id, 'Koha::Patron returned is the correct guarantee' ); +is( ref($gr[0]->guarantor), 'Koha::Patron', 'Method guarantor returns a Koha::Patron' ); +is( $gr[0]->guarantor->id, $chelsea->id, 'Koha::Patron returned is the correct guarantor' ); + +is( $gr[1]->guarantor_id, $chelsea->id, 'Guarantor matches for first relationship' ); +is( $gr[1]->guarantee_id, $kylie->id, 'Guarantee matches for first relationship' ); +is( $gr[1]->relationship, 'mother', 'Relationship is mother' ); +is( ref($gr[1]->guarantee), 'Koha::Patron', 'Method guarantee returns a Koha::Patron' ); +is( $gr[1]->guarantee->id, $kylie->id, 'Koha::Patron returned is the correct guarantee' ); +is( ref($gr[1]->guarantor), 'Koha::Patron', 'Method guarantor returns a Koha::Patron' ); +is( $gr[1]->guarantor->id, $chelsea->id, 'Koha::Patron returned is the correct guarantor' ); + +@gr = $daria->guarantor_relationships(); +is( @gr, 4, 'Found 4 guarantor relationships for child' ); +is( $gr[0]->guarantor_id, $kyle->id, 'Guarantor matches for first relationship' ); +is( $gr[0]->guarantee_id, $daria->id, 'Guarantee matches for first relationship' ); +is( $gr[0]->relationship, 'father', 'Relationship is father' ); +is( ref($gr[0]->guarantee), 'Koha::Patron', 'Method guarantee returns a Koha::Patron' ); +is( $gr[0]->guarantee->id, $daria->id, 'Koha::Patron returned is the correct guarantee' ); +is( ref($gr[0]->guarantor), 'Koha::Patron', 'Method guarantor returns a Koha::Patron' ); +is( $gr[0]->guarantor->id, $kyle->id, 'Koha::Patron returned is the correct guarantor' ); + +is( $gr[1]->guarantor_id, $chelsea->id, 'Guarantor matches for first relationship' ); +is( $gr[1]->guarantee_id, $daria->id, 'Guarantee matches for first relationship' ); +is( $gr[1]->relationship, 'mother', 'Relationship is mother' ); +is( ref($gr[1]->guarantee), 'Koha::Patron', 'Method guarantee returns a Koha::Patron' ); +is( $gr[1]->guarantee->id, $daria->id, 'Koha::Patron returned is the correct guarantee' ); +is( ref($gr[1]->guarantor), 'Koha::Patron', 'Method guarantor returns a Koha::Patron' ); +is( $gr[1]->guarantor->id, $chelsea->id, 'Koha::Patron returned is the correct guarantor' ); + +is( $gr[2]->guarantor_id, undef, 'Grandfather has no id, not a Koha patron' ); +is( $gr[2]->firstname, 'John', 'Grandfather has first name of John' ); +is( $gr[2]->surname, 'Hall', 'Grandfather has surname of Hall' ); +is( $gr[2]->guarantor, undef, 'Calling guarantor method for Grandfather returns undef' ); + +is( $gr[3]->guarantor_id, undef, 'Grandmother has no id, not a Koha patron' ); +is( $gr[3]->firstname, 'Debra', 'Grandmother has first name of John' ); +is( $gr[3]->surname, 'Hall', 'Grandmother has surname of Hall' ); +is( $gr[3]->guarantor, undef, 'Calling guarantor method for Grandmother returns undef' ); + +my @siblings = $daria->siblings; +is( @siblings, 1, 'Method siblings called in list context returns list' ); +is( ref($siblings[0]), 'Koha::Patron', 'List contains a Koha::Patron' ); +is( $siblings[0]->firstname, 'Kylie', 'Sibling from list first name matches correctly' ); +is( $siblings[0]->surname, 'Hall', 'Sibling from list surname matches correctly' ); +is( $siblings[0]->id, $kylie->id, 'Sibling from list patron id matches correctly' ); + +my $siblings = $daria->siblings; +my $sibling = $siblings->next(); +is( ref($siblings), 'Koha::Patrons', 'Calling siblings in scalar context results in a Koha::Patrons object' ); +is( ref($sibling), 'Koha::Patron', 'Method next returns a Koha::Patron' ); +is( $sibling->firstname, 'Kylie', 'Sibling from scalar first name matches correctly' ); +is( $sibling->surname, 'Hall', 'Sibling from scalar surname matches correctly' ); +is( $sibling->id, $kylie->id, 'Sibling from scalar patron id matches correctly' ); + +1; -- 2.1.4