use Modern::Perl;
-use Koha::DateUtils;
+use Test::More tests => 33;
use DateTime::Duration;
+
use t::lib::Mocks;
-use C4::Biblio;
-use C4::Members;
+use t::lib::TestBuilder;
+
use C4::Circulation;
use C4::Items;
+use C4::Biblio;
use C4::Context;
use C4::Reserves;
+use Koha::Checkouts;
+use Koha::Database;
+use Koha::DateUtils;
+use Koha::Holds;
+use Koha::Items;
use Koha::Library;
-
-use Test::More tests => 32;
+use Koha::Patrons;
BEGIN {
- use_ok('C4::Circulation');
+ require_ok('C4::Circulation');
}
+
can_ok(
'C4::Circulation',
qw(AddIssue
AddReturn
GetBiblioIssues
GetIssuingCharges
- GetIssuingRule
- GetItemIssue
- GetItemIssues
GetOpenIssue
GetRenewCount
GetUpcomingDueIssues
);
#Start transaction
+my $schema = Koha::Database->schema;
+$schema->storage->txn_begin;
my $dbh = C4::Context->dbh;
-$dbh->{RaiseError} = 1;
-$dbh->{AutoCommit} = 0;
+
+my $builder = t::lib::TestBuilder->new();
$dbh->do(q|DELETE FROM issues|);
$dbh->do(q|DELETE FROM items|);
$dbh->do(q|DELETE FROM borrowers|);
-$dbh->do(q|DELETE FROM branches|);
$dbh->do(q|DELETE FROM categories|);
$dbh->do(q|DELETE FROM accountlines|);
$dbh->do(q|DELETE FROM issuingrules|);
+$dbh->do(q|DELETE FROM reserves|);
+$dbh->do(q|DELETE FROM old_reserves|);
+$dbh->do(q|DELETE FROM statistics|);
+
+# Generate sample datas
+my $itemtype = $builder->build(
+ { source => 'Itemtype',
+ value => { notforloan => undef, rentalcharge => 0 }
+ }
+)->{itemtype};
+my $branchcode_1 = $builder->build({ source => 'Branch' })->{branchcode};
+my $branchcode_2 = $builder->build({ source => 'Branch' })->{branchcode};
+my $branchcode_3 = $builder->build({ source => 'Branch' })->{branchcode};
+my $categorycode = $builder->build({
+ source => 'Category',
+ value => { enrolmentfee => undef }
+ })->{categorycode};
+
+# A default issuingrule should always be present
+my $issuingrule = $builder->build(
+ {
+ source => 'Issuingrule',
+ value => {
+ itemtype => '*',
+ categorycode => '*',
+ branchcode => '*',
+ lengthunit => 'days',
+ issuelength => 0,
+ renewalperiod => 0,
+ renewalsallowed => 0
+ }
+ }
+);
-#Add sample datas
-
-#Add Dates
-
-my $dt_today = dt_from_string;
-my $today = output_pref({ dt => $dt_today, dateformat => 'iso', timeformat => '24hr', dateonly => 1 });
+# Add Dates
+my $dt_today = dt_from_string;
+my $today = output_pref(
+ { dt => $dt_today,
+ dateformat => 'iso',
+ timeformat => '24hr',
+ dateonly => 1
+ }
+);
my $dt_today2 = dt_from_string;
my $dur10 = DateTime::Duration->new( days => -10 );
$dt_today2->add_duration($dur10);
-my $daysago10 = output_pref({ dt => $dt_today2, dateformat => 'iso', timeformat => '24hr', dateonly => 1 });
-
-#Add branch and category
-my $samplebranch1 = {
- branchcode => 'CPL',
- branchname => 'Sample Branch',
- branchaddress1 => 'sample adr1',
- branchaddress2 => 'sample adr2',
- branchaddress3 => 'sample adr3',
- branchzip => 'sample zip',
- branchcity => 'sample city',
- branchstate => 'sample state',
- branchcountry => 'sample country',
- branchphone => 'sample phone',
- branchfax => 'sample fax',
- branchemail => 'sample email',
- branchurl => 'sample url',
- branchip => 'sample ip',
- branchprinter => undef,
- opac_info => 'sample opac',
-};
-my $samplebranch2 = {
- branchcode => 'MPL',
- branchname => 'Sample Branch2',
- branchaddress1 => 'sample adr1_2',
- branchaddress2 => 'sample adr2_2',
- branchaddress3 => 'sample adr3_2',
- branchzip => 'sample zip2',
- branchcity => 'sample city2',
- branchstate => 'sample state2',
- branchcountry => 'sample country2',
- branchphone => 'sample phone2',
- branchfax => 'sample fax2',
- branchemail => 'sample email2',
- branchurl => 'sample url2',
- branchip => 'sample ip2',
- branchprinter => undef,
- opac_info => 'sample opac2',
-};
-Koha::Library->new($samplebranch1)->store;
-Koha::Library->new($samplebranch2)->store;
-
-my $samplecat = {
- categorycode => 'CAT1',
- description => 'Description1',
- enrolmentperiod => 'Null',
- enrolmentperioddate => 'Null',
- dateofbirthrequired => 'Null',
- finetype => 'Null',
- bulk => 'Null',
- enrolmentfee => 'Null',
- overduenoticerequired => 'Null',
- issuelimit => 'Null',
- reservefee => 'Null',
- hidelostitems => 0,
- category_type => 'Null'
-};
-my $query =
-"INSERT INTO categories (categorycode,description,enrolmentperiod,enrolmentperioddate,dateofbirthrequired ,finetype,bulk,enrolmentfee,overduenoticerequired,issuelimit ,reservefee ,hidelostitems ,category_type) VALUES( ?,?,?,?,?,?,?,?,?,?,?,?,?)";
-$dbh->do(
- $query, {},
- $samplecat->{categorycode}, $samplecat->{description},
- $samplecat->{enrolmentperiod}, $samplecat->{enrolmentperioddate},
- $samplecat->{dateofbirthrequired}, $samplecat->{finetype},
- $samplecat->{bulk}, $samplecat->{enrolmentfee},
- $samplecat->{overduenoticerequired}, $samplecat->{issuelimit},
- $samplecat->{reservefee}, $samplecat->{hidelostitems},
- $samplecat->{category_type}
+my $daysago10 = output_pref(
+ { dt => $dt_today2,
+ dateformat => 'iso',
+ timeformat => '24hr',
+ dateonly => 1
+ }
);
-#Add biblio and item
+# Add biblio and item
my $record = MARC::Record->new();
$record->append_fields(
- MARC::Field->new( '952', '0', '0', a => $samplebranch1->{branchcode} ) );
+ MARC::Field->new( '952', '0', '0', a => $branchcode_1 ) );
+
my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio( $record, '' );
my $barcode_1 = 'barcode_1';
{
barcode => $barcode_1,
itemcallnumber => 'callnumber1',
- homebranch => $samplebranch1->{branchcode},
- holdingbranch => $samplebranch1->{branchcode},
+ homebranch => $branchcode_1,
+ holdingbranch => $branchcode_1,
issue => 1,
- reserve => 1
+ reserve => 1,
+ itype => $itemtype
},
$biblionumber
);
{
barcode => $barcode_2,
itemcallnumber => 'callnumber2',
- homebranch => $samplebranch2->{branchcode},
- holdingbranch => $samplebranch2->{branchcode},
+ homebranch => $branchcode_2,
+ holdingbranch => $branchcode_2,
notforloan => 1,
- issue => 1
+ issue => 1,
+ itype => $itemtype
},
$biblionumber
);
my $item_id2 = $sampleitem2[2];
#Add borrower
-my $borrower_id1 = C4::Members::AddMember(
+my $borrower_id1 = Koha::Patron->new({
firstname => 'firstname1',
surname => 'surname1 ',
- categorycode => $samplecat->{categorycode},
- branchcode => $samplebranch1->{branchcode},
-);
-my $borrower_1 = C4::Members::GetMember(borrowernumber => $borrower_id1);
-my $borrower_id2 = C4::Members::AddMember(
+ categorycode => $categorycode,
+ branchcode => $branchcode_1
+})->store->borrowernumber;
+my $patron_1 = Koha::Patrons->find( $borrower_id1 );
+my $borrower_1 = $patron_1->unblessed;
+my $borrower_id2 = Koha::Patron->new({
firstname => 'firstname2',
surname => 'surname2 ',
- categorycode => $samplecat->{categorycode},
- branchcode => $samplebranch2->{branchcode},
-);
-my $borrower_2 = C4::Members::GetMember(borrowernumber => $borrower_id2);
-
-# NEED TO BE FIXED !!!
-# The first parameter for set_userenv is the class ref
-#my @USERENV = ( $borrower_id1, 'test', 'MASTERTEST', 'firstname', 'username', 'CPL', 'CPL', 'email@example.org' );
-my @USERENV = ( $borrower_id1, 'test', 'MASTERTEST', 'firstname', 'CPL', 'CPL', 'email@example.org' );
-
-C4::Context->_new_userenv('DUMMY_SESSION_ID');
-C4::Context->set_userenv(@USERENV);
+ categorycode => $categorycode,
+ branchcode => $branchcode_2,
+})->store->borrowernumber;
+my $patron_2 = Koha::Patrons->find( $borrower_id2 );
+my $borrower_2 = $patron_2->unblessed;
-my $userenv = C4::Context->userenv
- or BAIL_OUT("No userenv");
+t::lib::Mocks::mock_userenv({ patron => $patron_1 });
#Begin Tests
#Test AddIssue
-$query = " SELECT count(*) FROM issues";
+my $query = " SELECT count(*) FROM issues";
my $sth = $dbh->prepare($query);
$sth->execute;
my $countissue = $sth -> fetchrow_array;
is ($countissue ,0, "there is no issue");
my $issue1 = C4::Circulation::AddIssue( $borrower_1, $barcode_1, $daysago10,0, $today, '' );
-is( ref $issue1, 'Koha::Schema::Result::Issue',
- 'AddIssue returns a Koha::Schema::Result::Issue object' );
+is( ref $issue1, 'Koha::Checkout',
+ 'AddIssue returns a Koha::Checkout object' );
my $datedue1 = dt_from_string( $issue1->date_due() );
like(
$datedue1,
qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/,
- "Koha::Schema::Result::Issue->date_due() returns a date"
+ "Koha::Checkout->date_due() returns a date"
);
-my $issue_id1 = $dbh->last_insert_id( undef, undef, 'issues', undef );
+my $issue_id1 = $issue1->issue_id;
my $issue2 = C4::Circulation::AddIssue( $borrower_1, 'nonexistent_barcode' );
is( $issue2, undef, "AddIssue returns undef if no datedue is specified" );
-my $issue_id2 = $dbh->last_insert_id( undef, undef, 'issues', undef );
$sth->execute;
$countissue = $sth -> fetchrow_array;
$query = " SELECT count(*) FROM accountlines";
$sth = $dbh->prepare($query);
$sth->execute;
-my $countaccount = $sth -> fetchrow_array;
+my $countaccount = $sth->fetchrow_array;
is ($countaccount,0,"0 accountline exists");
-is( C4::Circulation::AddIssuingCharge( $item_id1, $borrower_id1, 10 ),
- 1, "An issuing charge has been added" );
-my $account_id = $dbh->last_insert_id( undef, undef, 'accountlines', undef );
+my $checkout = Koha::Checkouts->find( $issue_id1 );
+my $charge = C4::Circulation::AddIssuingCharge( $checkout, 10 );
+is( ref( $charge ), 'Koha::Account::Line', "An issuing charge has been added" );
+is( $charge->issue_id, $issue_id1, 'Issue id is set correctly for issuing charge' );
+my $offset = Koha::Account::Offsets->find( { debit_id => $charge->id } );
+is( $offset->credit_id, undef, 'Offset was created');
$sth->execute;
-$countaccount = $sth -> fetchrow_array;
+$countaccount = $sth->fetchrow_array;
is ($countaccount,1,"1 accountline has been added");
-#Test AddRenewal
-my $datedue3 =
- AddRenewal( $borrower_id1, $item_id1, $samplebranch1->{branchcode},
- $datedue1, $daysago10 );
+# Test AddRenewal
+
+my $se = Test::MockModule->new( 'C4::Context' );
+$se->mock( 'interface', sub {return 'intranet'});
+
+# Let's renew this one at a different library for statistical purposes to test Bug 17781
+# Mocking userenv with a different branchcode
+t::lib::Mocks::mock_userenv({ patron => $patron_2, branchcode => $branchcode_3 });
+
+my $datedue3 = AddRenewal( $borrower_id1, $item_id1, $branchcode_1, $datedue1, $daysago10 );
+
+# Restoring the userenv with the original branchcode
+t::lib::Mocks::mock_userenv({ patron => $patron_1});
+
like(
$datedue3,
qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/,
"AddRenewal returns a date"
);
-#Test GetBiblioIssues
-is( GetBiblioIssues(), undef, "GetBiblio Issues without parameters" );
+my $stat = $dbh->selectrow_hashref("SELECT * FROM statistics WHERE type = 'renew' AND borrowernumber = ? AND itemnumber = ? AND branch = ?", undef, $borrower_id1, $item_id1, $branchcode_3 );
+ok( $stat, "Bug 17781 - 'Improper branchcode set during renewal' still fixed" );
+
+$se->mock( 'interface', sub {return 'opac'});
-#Test GetItemIssue
-#FIXME : As the issues are not correctly added in the database, these tests don't work correctly
-is(GetItemIssue,undef,"Without parameter GetItemIssue returns undef");
-#is(GetItemIssue($item_id1),{},"Item1's issues");
+#Let's do an opac renewal - whatever branchcode we send should be used
+my $opac_renew_issue = $builder->build({
+ source=>"Issue",
+ value=>{
+ date_due => '2017-01-01',
+ branch => $branchcode_1,
+ itype => $itemtype,
+ borrowernumber => $borrower_id1
+ }
+});
-#Test GetItemIssues
-#FIXME: this routine currently doesn't work be
-#is_deeply (GetItemIssues,{},"Without parameter, GetItemIssue returns all the issues");
+my $datedue4 = AddRenewal( $opac_renew_issue->{borrowernumber}, $opac_renew_issue->{itemnumber}, "Stavromula", $datedue1, $daysago10 );
+
+$stat = $dbh->selectrow_hashref("SELECT * FROM statistics WHERE type = 'renew' AND borrowernumber = ? AND itemnumber = ? AND branch = ?", undef, $opac_renew_issue->{borrowernumber}, $opac_renew_issue->{itemnumber}, "Stavromula" );
+ok( $stat, "Bug 18572 - 'Bug 18572 - OpacRenewalBranch is now respected" );
+
+
+
+#Test GetBiblioIssues
+is( GetBiblioIssues(), undef, "GetBiblio Issues without parameters" );
#Test GetOpenIssue
is( GetOpenIssue(), undef, "Without parameter GetOpenIssue returns undef" );
@renewcount = C4::Circulation::GetRenewCount();
is_deeply(
\@renewcount,
- [ 0, undef, 0 ], # FIXME Need to be fixed
+ [ 0, 0, 0 ], # FIXME Need to be fixed, see FIXME in GetRenewCount
"Without issuing rules and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = undef, renewsleft = 0"
);
@renewcount = C4::Circulation::GetRenewCount(-1);
is_deeply(
\@renewcount,
- [ 0, undef, 0 ], # FIXME Need to be fixed
+ [ 0, 0, 0 ], # FIXME Need to be fixed
"Without issuing rules and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = undef, renewsleft = 0"
);
@renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
is_deeply(
\@renewcount,
- [ 2, undef, 0 ],
+ [ 2, 0, 0 ],
"Without issuing rules and with a valid parameter, renewcount = 2, renewsallowed = undef, renewsleft = 0"
);
$dbh->do(q|
UPDATE issuingrules SET renewalsallowed = 3
|);
-@renewcount = C4::Circulation::GetRenewCount();
-is_deeply(
- \@renewcount,
- [ 0, 3, 3 ],
- "With issuing rules (renewal allowed) and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = 3, renewsleft = 3"
-);
-@renewcount = C4::Circulation::GetRenewCount(-1);
-is_deeply(
- \@renewcount,
- [ 0, 3, 3 ],
- "With issuing rules (renewal allowed) and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = 3, renewsleft = 3"
-);
@renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
is_deeply(
\@renewcount,
"With issuing rules (renewal allowed) and with a valid parameter, Getrenewcount of item1 returns 3 renews left"
);
-AddRenewal( $borrower_id1, $item_id1, $samplebranch1->{branchcode},
+AddRenewal( $borrower_id1, $item_id1, $branchcode_1,
$datedue3, $daysago10 );
@renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
is_deeply(
{
barcode => 'barcode_3',
itemcallnumber => 'callnumber3',
- homebranch => $samplebranch1->{branchcode},
- holdingbranch => $samplebranch1->{branchcode},
- notforloan => 1,
+ homebranch => $branchcode_1,
+ holdingbranch => $branchcode_1,
+ notforloan => 1,
+ itype => $itemtype
},
$biblionumber
);
t::lib::Mocks::mock_preference( 'UpdateNotForLoanStatusOnCheckin', q{} );
-AddReturn( 'barcode_3', $samplebranch1->{branchcode} );
-my $item = GetItem( $itemnumber );
-ok( $item->{notforloan} eq 1, 'UpdateNotForLoanStatusOnCheckin does not modify value when not enabled' );
+AddReturn( 'barcode_3', $branchcode_1 );
+my $item = Koha::Items->find( $itemnumber );
+ok( $item->notforloan eq 1, 'UpdateNotForLoanStatusOnCheckin does not modify value when not enabled' );
t::lib::Mocks::mock_preference( 'UpdateNotForLoanStatusOnCheckin', '1: 9' );
-AddReturn( 'barcode_3', $samplebranch1->{branchcode} );
-$item = GetItem( $itemnumber );
-ok( $item->{notforloan} eq 9, q{UpdateNotForLoanStatusOnCheckin updates notforloan value from 1 to 9 with setting "1: 9"} );
+AddReturn( 'barcode_3', $branchcode_1 );
+$item = Koha::Items->find( $itemnumber );
+ok( $item->notforloan eq 9, q{UpdateNotForLoanStatusOnCheckin updates notforloan value from 1 to 9 with setting "1: 9"} );
-AddReturn( 'barcode_3', $samplebranch1->{branchcode} );
-$item = GetItem( $itemnumber );
-ok( $item->{notforloan} eq 9, q{UpdateNotForLoanStatusOnCheckin does not update notforloan value from 9 with setting "1: 9"} );
+AddReturn( 'barcode_3', $branchcode_1 );
+$item = Koha::Items->find( $itemnumber );
+ok( $item->notforloan eq 9, q{UpdateNotForLoanStatusOnCheckin does not update notforloan value from 9 with setting "1: 9"} );
# Bug 14640 - Cancel the hold on checking out if asked
-my $reserve_id = AddReserve('CPL', $borrower_id1, $biblionumber,
+my $reserve_id = AddReserve($branchcode_1, $borrower_id1, $biblionumber,
undef, 1, undef, undef, "a note", "a title", undef, '');
ok( $reserve_id, 'The reserve should have been inserted' );
AddIssue( $borrower_2, $barcode_1, dt_from_string, 'cancel' );
-my $reserve = GetReserve( $reserve_id );
-is( $reserve, undef, 'The reserve should have been correctly cancelled' );
+my $hold = Koha::Holds->find( $reserve_id );
+is( $hold, undef, 'The reserve should have been correctly cancelled' );
#End transaction
-$dbh->rollback;
+$schema->storage->txn_rollback;