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