Bug 20199: Add tests for Koha::Acq::Order->store
[koha.git] / t / db_dependent / Koha / Acquisition / Order.t
1 #!/usr/bin/perl
2
3 # Copyright 2017 Koha Development team
4 #
5 # This file is part of Koha
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use Test::More tests => 2;
23
24 use t::lib::TestBuilder;
25 use t::lib::Mocks;
26
27 use Koha::Database;
28
29 my $schema  = Koha::Database->schema;
30 my $builder = t::lib::TestBuilder->new;
31
32 subtest 'basket() tests' => sub {
33
34     plan tests => 2;
35
36     $schema->storage->txn_begin;
37
38     my $basket = $builder->build_object(
39         {
40             class => 'Koha::Acquisition::Baskets'
41         }
42     );
43     my $order = $builder->build_object(
44         {
45             class => 'Koha::Acquisition::Orders',
46             value => { basketno => $basket->basketno }
47         }
48     );
49
50     my $retrieved_basket = $order->basket;
51     is( ref($retrieved_basket), 'Koha::Acquisition::Basket',
52         'Type is correct for ->basket' );
53     is_deeply( $retrieved_basket->unblessed,
54         $basket->unblessed, "Correct basket found and updated" );
55
56     $schema->storage->txn_rollback;
57 };
58
59 subtest 'store' => sub {
60     plan tests => 1;
61
62     $schema->storage->txn_begin;
63     my $o = $builder->build_object(
64         {
65             class => 'Koha::Acquisition::Orders'
66         }
67     );
68
69     subtest 'entrydate' => sub {
70         plan tests => 2;
71
72         my $order;
73
74         t::lib::Mocks::mock_preference( 'TimeFormat', '12hr' );
75         $order = Koha::Acquisition::Order->new(
76             {
77                 basketno     => $o->basketno,
78                 biblionumber => $o->biblionumber,
79                 budget_id    => $o->budget_id,
80             }
81         )->store;
82         $order->discard_changes;
83         like( $order->entrydate, qr|^\d{4}-\d{2}-\d{2}$| );
84
85         t::lib::Mocks::mock_preference( 'TimeFormat', '24hr' );
86         $order = Koha::Acquisition::Order->new(
87             {
88                 basketno     => $o->basketno,
89                 biblionumber => $o->biblionumber,
90                 budget_id    => $o->budget_id,
91             }
92         )->store;
93         $order->discard_changes;
94         like( $order->entrydate, qr|^\d{4}-\d{2}-\d{2}$| );
95     };
96     $schema->storage->txn_rollback;
97 };