Bug 19759: Fix failing test in Chargelostitem.t
[koha.git] / t / db_dependent / Circulation / Chargelostitem.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use Test::More tests => 6;
6 use Test::MockModule;
7 use t::lib::Mocks;
8 use t::lib::TestBuilder;
9
10 use C4::Biblio;
11 use C4::Items;
12 use C4::Members;
13 use C4::Circulation;
14 use MARC::Record;
15
16 BEGIN {
17     use_ok('C4::Accounts');
18 }
19
20 my $schema = Koha::Database->schema;
21 $schema->storage->txn_begin;
22 my $builder = t::lib::TestBuilder->new;
23 my $dbh = C4::Context->dbh;
24
25 $dbh->do(q|DELETE FROM accountlines|);
26
27 t::lib::Mocks::mock_preference('ProcessingFeeNote', 'Test Note');
28
29 my $library = $builder->build({
30     source => 'Branch',
31 });
32 my $branchcode = $library->{branchcode};
33
34 my $itemtype = $builder->build({
35     source => 'Itemtype'
36 });
37
38 my %item_branch_infos = (
39     homebranch => $branchcode,
40     holdingbranch => $branchcode,
41     itype => $itemtype->{itemtype},
42 );
43
44 my ($biblionumber1) = AddBiblio(MARC::Record->new, '');
45 my $itemnumber1 = AddItem({ barcode => '0101', %item_branch_infos }, $biblionumber1);
46 my $itemnumber2 = AddItem({ barcode => '0102', %item_branch_infos }, $biblionumber1);
47
48 my ($biblionumber2) = AddBiblio(MARC::Record->new, '');
49 my $itemnumber3 = AddItem({ barcode => '0203', %item_branch_infos }, $biblionumber2);
50
51 my $categorycode = $builder->build({
52     source => 'Category'
53 })->{categorycode};
54
55 my $borrowernumber = AddMember(categorycode => $categorycode, branchcode => $branchcode);
56 my $borrower = Koha::Patrons->find( $borrowernumber )->unblessed();
57
58 # Need to mock userenv for AddIssue
59 my $module = new Test::MockModule('C4::Context');
60 $module->mock('userenv', sub { { branch => $branchcode } });
61 AddIssue($borrower, '0101');
62 AddIssue($borrower, '0203');
63
64 # Begin tests...
65 Koha::Account::Offsets->delete();
66 my $issue = Koha::Checkouts->search( { borrowernumber => $borrowernumber } )->next()->unblessed();
67 C4::Accounts::chargelostitem( $borrowernumber, $issue->{itemnumber}, '1.00');
68
69 my $accountline = Koha::Account::Lines->search( { borrowernumber => $borrowernumber, accounttype => 'PF' } )->next();
70
71 is( int($accountline->amount), int($itemtype->{processfee}), "The accountline amount should be precessfee value " );
72 is( $accountline->itemnumber, $itemnumber1, "The accountline itemnumber should the linked with barcode '0101'" );
73 is( $accountline->note, C4::Context->preference("ProcessingFeeNote"), "The accountline description should be 'test'" );
74
75 my $lost_ao = Koha::Account::Offsets->single( { type => 'Lost Item' } );
76 ok( $lost_ao, 'Account offset of type "Lost Item" created' );
77
78 my $processing_fee_ao = Koha::Account::Offsets->single( { type => 'Processing Fee' } );
79 ok( $processing_fee_ao, 'Account offset of type "Processing Fee" created' );