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;
16 my $schema = Koha::Database->new()->schema();
17 $schema->storage->txn_begin();
18 my $dbh = C4::Context->dbh;
19 $dbh->{RaiseError} = 1;
21 t::lib::Mocks::mock_userenv({ branchcode => 'CPL' });
23 my $bookseller = Koha::Acquisition::Bookseller->new(
26 address1 => "bookseller's address",
32 my $basketno = C4::Acquisition::NewBasket(
36 my $budgetid = C4::Budgets::AddBudget(
38 budget_code => "budget_code_test",
39 budget_name => "budget_name_test",
43 my $budget = C4::Budgets::GetBudget( $budgetid );
45 my ($biblionumber1, $biblioitemnumber1) = AddBiblio(MARC::Record->new, '');
46 my ($biblionumber2, $biblioitemnumber2) = AddBiblio(MARC::Record->new, '');
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 = $@;
54 && ( defined $return_error ),
55 "Inserting an order with no params returns undef and croaks"
58 my $mandatoryparams = {
59 basketno => $basketno,
61 biblionumber => $biblionumber1,
62 budget_id => $budgetid,
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};
69 Koha::Acquisition::Order->new( \%test_missing_mandatoryparams )->store;
72 my $expected_error = "Cannot insert order: Mandatory parameter $mandatoryparams_key is missing";
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"
80 $order = Koha::Acquisition::Order->new(
82 basketno => $basketno,
84 biblionumber => $biblionumber1,
85 budget_id => $budget->{budget_id},
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' );
94 $schema->storage->txn_rollback();