Bug 20287: Replace occurrences of AddMember with Koha::Patron->new->store->borrowernumber
[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::Circulation;
13 use Koha::Patrons;
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     value => {
37         processfee => 42,
38     }
39 });
40
41 my %item_branch_infos = (
42     homebranch => $branchcode,
43     holdingbranch => $branchcode,
44     itype => $itemtype->{itemtype},
45 );
46
47 my ($biblionumber1) = AddBiblio(MARC::Record->new, '');
48 my $itemnumber1 = AddItem({ barcode => '0101', %item_branch_infos }, $biblionumber1);
49 my $itemnumber2 = AddItem({ barcode => '0102', %item_branch_infos }, $biblionumber1);
50
51 my ($biblionumber2) = AddBiblio(MARC::Record->new, '');
52 my $itemnumber3 = AddItem({ barcode => '0203', %item_branch_infos }, $biblionumber2);
53
54 my $categorycode = $builder->build({
55     source => 'Category'
56 })->{categorycode};
57
58 my $borrowernumber = Koha::Patron->new({categorycode => $categorycode, branchcode => $branchcode})->store->borrowernumber;
59 # TODO following code must be simplified to use the Koha::Patron object
60 my $borrower = Koha::Patrons->find( $borrowernumber )->unblessed();
61
62 # Need to mock userenv for AddIssue
63 my $module = new Test::MockModule('C4::Context');
64 $module->mock('userenv', sub { { branch => $branchcode } });
65 AddIssue($borrower, '0101');
66 AddIssue($borrower, '0203');
67
68 # Begin tests...
69 Koha::Account::Offsets->delete();
70 my $issue = Koha::Checkouts->search( { borrowernumber => $borrowernumber } )->next()->unblessed();
71 C4::Accounts::chargelostitem( $borrowernumber, $issue->{itemnumber}, '1.00');
72
73 my $accountline = Koha::Account::Lines->search( { borrowernumber => $borrowernumber, accounttype => 'PF' } )->next();
74
75 is( int($accountline->amount), int($itemtype->{processfee}), "The accountline amount should be precessfee value " );
76 is( $accountline->itemnumber, $itemnumber1, "The accountline itemnumber should the linked with barcode '0101'" );
77 is( $accountline->note, C4::Context->preference("ProcessingFeeNote"), "The accountline description should be 'test'" );
78
79 my $lost_ao = Koha::Account::Offsets->search( { type => 'Lost Item' } );
80 is( $lost_ao->count, 1, 'Account offset of type "Lost Item" created' );
81
82 my $processing_fee_ao = Koha::Account::Offsets->search( { type => 'Processing Fee' } );
83 is( $processing_fee_ao->count, 1, 'Account offset of type "Processing Fee" created' );