5 use Test::More tests => 8;
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;
17 my $schema = Koha::Database->new()->schema();
18 $schema->storage->txn_begin();
19 my $dbh = C4::Context->dbh;
20 $dbh->{RaiseError} = 1;
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 });
26 my $bookseller = Koha::Acquisition::Bookseller->new(
29 address1 => "bookseller's address",
35 my $basketno = C4::Acquisition::NewBasket(
39 my $budgetid = C4::Budgets::AddBudget(
41 budget_code => "budget_code_test",
42 budget_name => "budget_name_test",
46 my $budget = C4::Budgets::GetBudget( $budgetid );
48 my ($biblionumber1, $biblioitemnumber1) = AddBiblio(MARC::Record->new, '');
49 my ($biblionumber2, $biblioitemnumber2) = AddBiblio(MARC::Record->new, '');
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 = $@;
57 && ( defined $return_error ),
58 "Inserting an order with no params returns undef and croaks"
61 my $mandatoryparams = {
62 basketno => $basketno,
64 biblionumber => $biblionumber1,
65 budget_id => $budgetid,
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};
72 Koha::Acquisition::Order->new( \%test_missing_mandatoryparams )->store;
75 my $expected_error = "Cannot insert order: Mandatory parameter $mandatoryparams_key is missing";
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"
83 $order = Koha::Acquisition::Order->new(
85 basketno => $basketno,
87 biblionumber => $biblionumber1,
88 budget_id => $budget->{budget_id},
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' );
97 $schema->storage->txn_rollback();