Bug 21817: Centralize the mock of userenv from tests
[koha.git] / t / db_dependent / Acquisition / NewOrder.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use Test::More tests => 8;
6 use C4::Acquisition;
7 use C4::Biblio;
8 use C4::Budgets;
9 use MARC::Record;
10 use Koha::Database;
11 use Koha::DateUtils qw( dt_from_string output_pref );
12 use Koha::Acquisition::Booksellers;
13 use Koha::Acquisition::Orders;
14 use t::lib::Mocks;
15
16 my $schema = Koha::Database->new()->schema();
17 $schema->storage->txn_begin();
18 my $dbh = C4::Context->dbh;
19 $dbh->{RaiseError} = 1;
20
21 t::lib::Mocks::mock_userenv({ branchcode => 'CPL' });
22
23 my $bookseller = Koha::Acquisition::Bookseller->new(
24     {
25         name => "my vendor",
26         address1 => "bookseller's address",
27         phone => "0123456",
28         active => 1
29     }
30 )->store;
31
32 my $basketno = C4::Acquisition::NewBasket(
33     $bookseller->id
34 );
35
36 my $budgetid = C4::Budgets::AddBudget(
37     {
38         budget_code => "budget_code_test",
39         budget_name => "budget_name_test",
40     }
41 );
42
43 my $budget = C4::Budgets::GetBudget( $budgetid );
44
45 my ($biblionumber1, $biblioitemnumber1) = AddBiblio(MARC::Record->new, '');
46 my ($biblionumber2, $biblioitemnumber2) = AddBiblio(MARC::Record->new, '');
47
48
49 # returns undef and croaks if basketno, quantity, biblionumber or budget_id is missing
50 my $order = eval { Koha::Acquisition::Order->new->store };
51 my $return_error = $@;
52 ok(
53     ( ! defined $order )
54       && ( defined $return_error ),
55     "Inserting an order with no params returns undef and croaks"
56 );
57
58 my $mandatoryparams = {
59     basketno     => $basketno,
60     quantity     => 24,
61     biblionumber => $biblionumber1,
62     budget_id    => $budgetid,
63 };
64 my @mandatoryparams_keys = keys %$mandatoryparams;
65 foreach my $mandatoryparams_key (@mandatoryparams_keys) {
66     my %test_missing_mandatoryparams = %$mandatoryparams;
67     delete $test_missing_mandatoryparams{$mandatoryparams_key};
68     $order = eval {
69           Koha::Acquisition::Order->new( \%test_missing_mandatoryparams )->store;
70     };
71     $return_error = $@;
72     my $expected_error = "Cannot insert order: Mandatory parameter $mandatoryparams_key is missing";
73     ok(
74         ( !( defined $order ) )
75           && ( index( $return_error, $expected_error ) >= 0 ),
76 "Inserting an order with no $mandatoryparams_key returns undef and croaks with expected error message"
77     );
78 }
79
80 $order = Koha::Acquisition::Order->new(
81     {
82         basketno => $basketno,
83         quantity => 24,
84         biblionumber => $biblionumber1,
85         budget_id => $budget->{budget_id},
86     }
87 )->store;
88 my $ordernumber = $order->ordernumber;
89 $order = Koha::Acquisition::Orders->find( $ordernumber );
90 is( $order->quantityreceived, 0, 'Koha::Acquisition::Order->insert set quantityreceivedto 0 if undef is given' );
91 is( $order->entrydate, output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }), 'Koha::Acquisition::Order->store set entrydate to today' );
92 is( $order->created_by, 42, 'Koha::Acquisition::Order->store set created_by to logged in user if not given' );
93
94 $schema->storage->txn_rollback();