Bug 21817: Fix 2 failing 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::TestBuilder;
15 use t::lib::Mocks;
16
17 my $schema = Koha::Database->new()->schema();
18 $schema->storage->txn_begin();
19 my $dbh = C4::Context->dbh;
20 $dbh->{RaiseError} = 1;
21
22 my $builder = t::lib::TestBuilder->new;
23 my $logged_in_user = $builder->build_object({ class => 'Koha::Patrons' });
24 t::lib::Mocks::mock_userenv({ patron => $logged_in_user });
25
26 my $bookseller = Koha::Acquisition::Bookseller->new(
27     {
28         name => "my vendor",
29         address1 => "bookseller's address",
30         phone => "0123456",
31         active => 1
32     }
33 )->store;
34
35 my $basketno = C4::Acquisition::NewBasket(
36     $bookseller->id
37 );
38
39 my $budgetid = C4::Budgets::AddBudget(
40     {
41         budget_code => "budget_code_test",
42         budget_name => "budget_name_test",
43     }
44 );
45
46 my $budget = C4::Budgets::GetBudget( $budgetid );
47
48 my ($biblionumber1, $biblioitemnumber1) = AddBiblio(MARC::Record->new, '');
49 my ($biblionumber2, $biblioitemnumber2) = AddBiblio(MARC::Record->new, '');
50
51
52 # returns undef and croaks if basketno, quantity, biblionumber or budget_id is missing
53 my $order = eval { Koha::Acquisition::Order->new->store };
54 my $return_error = $@;
55 ok(
56     ( ! defined $order )
57       && ( defined $return_error ),
58     "Inserting an order with no params returns undef and croaks"
59 );
60
61 my $mandatoryparams = {
62     basketno     => $basketno,
63     quantity     => 24,
64     biblionumber => $biblionumber1,
65     budget_id    => $budgetid,
66 };
67 my @mandatoryparams_keys = keys %$mandatoryparams;
68 foreach my $mandatoryparams_key (@mandatoryparams_keys) {
69     my %test_missing_mandatoryparams = %$mandatoryparams;
70     delete $test_missing_mandatoryparams{$mandatoryparams_key};
71     $order = eval {
72           Koha::Acquisition::Order->new( \%test_missing_mandatoryparams )->store;
73     };
74     $return_error = $@;
75     my $expected_error = "Cannot insert order: Mandatory parameter $mandatoryparams_key is missing";
76     ok(
77         ( !( defined $order ) )
78           && ( index( $return_error, $expected_error ) >= 0 ),
79 "Inserting an order with no $mandatoryparams_key returns undef and croaks with expected error message"
80     );
81 }
82
83 $order = Koha::Acquisition::Order->new(
84     {
85         basketno => $basketno,
86         quantity => 24,
87         biblionumber => $biblionumber1,
88         budget_id => $budget->{budget_id},
89     }
90 )->store;
91 my $ordernumber = $order->ordernumber;
92 $order = Koha::Acquisition::Orders->find( $ordernumber );
93 is( $order->quantityreceived, 0, 'Koha::Acquisition::Order->insert set quantityreceivedto 0 if undef is given' );
94 is( $order->entrydate, output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }), 'Koha::Acquisition::Order->store set entrydate to today' );
95 is( $order->created_by, $logged_in_user->borrowernumber, 'Koha::Acquisition::Order->store set created_by to logged in user if not given' );
96
97 $schema->storage->txn_rollback();