Bug 14878: Tests - Create the branchcodes when needed
[koha.git] / t / db_dependent / Holds / RevertWaitingStatus.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use t::lib::Mocks;
6 use C4::Context;
7 use C4::Branch;
8
9 use Test::More tests => 3;
10 use MARC::Record;
11 use C4::Biblio;
12 use C4::Items;
13 use C4::Members;
14 use C4::Reserves;
15
16 use t::lib::TestBuilder;
17
18 my $schema = Koha::Database->schema;
19 $schema->storage->txn_begin;
20 my $builder = t::lib::TestBuilder->new;
21 my $dbh = C4::Context->dbh;
22 $dbh->{RaiseError} = 1;
23
24 $dbh->do("DELETE FROM reserves");
25 $dbh->do("DELETE FROM old_reserves");
26
27 my $library = $builder->build({
28     source => 'Branch',
29 });
30
31 local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /redefined/ };
32 *C4::Context::userenv = \&Mock_userenv;
33
34 sub Mock_userenv {
35     my $userenv = { flags => 1, id => '1', branch => $library->{branchcode} };
36     return $userenv;
37 }
38
39 my $borrowers_count = 3;
40
41 # Setup Test------------------------
42 # Helper biblio.
43 diag("Creating biblio instance for testing.");
44 my ( $bibnum, $title, $bibitemnum ) = create_helper_biblio();
45
46 # Helper item for that biblio.
47 diag("Creating item instance for testing.");
48 my $item_barcode = 'my_barcode';
49 my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem(
50     { homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode}, barcode => $item_barcode },
51     $bibnum );
52
53 # Create some borrowers
54 my @borrowernumbers;
55 foreach my $i ( 1 .. $borrowers_count ) {
56     my $borrowernumber = AddMember(
57         firstname    => 'my firstname',
58         surname      => 'my surname ' . $i,
59         categorycode => 'S',
60         branchcode   => $library->{branchcode},
61     );
62     push @borrowernumbers, $borrowernumber;
63 }
64
65 my $biblionumber = $bibnum;
66
67 my @branches = GetBranchesLoop();
68 my $branch   = $branches[0][0]{value};
69
70 # Create five item level holds
71 foreach my $borrowernumber (@borrowernumbers) {
72     AddReserve(
73         $branch,
74         $borrowernumber,
75         $biblionumber,
76         my $bibitems   = q{},
77         my $priority,
78         my $resdate,
79         my $expdate,
80         my $notes = q{},
81         $title,
82         my $checkitem,
83         my $found,
84     );
85 }
86
87 ModReserveAffect( $itemnumber, $borrowernumbers[0] );
88 C4::Circulation::AddIssue( GetMember( borrowernumber => $borrowernumbers[1] ),
89     $item_barcode, my $datedue, my $cancelreserve = 'revert' );
90
91 my $priorities = $dbh->selectall_arrayref(
92     "SELECT priority FROM reserves ORDER BY priority ASC");
93 ok( scalar @$priorities == 2,   'Only 2 holds remain in the reserves table' );
94 ok( $priorities->[0]->[0] == 1, 'First hold has a priority of 1' );
95 ok( $priorities->[1]->[0] == 2, 'Second hold has a priority of 2' );
96
97 # Helper method to set up a Biblio.
98 sub create_helper_biblio {
99     my $bib   = MARC::Record->new();
100     my $title = 'Silence in the library';
101     $bib->append_fields(
102         MARC::Field->new( '100', ' ', ' ', a => 'Moffat, Steven' ),
103         MARC::Field->new( '245', ' ', ' ', a => $title ),
104     );
105     return ( $bibnum, $title, $bibitemnum ) = AddBiblio( $bib, '' );
106 }