Bug 24459: Regression tests
[koha.git] / t / db_dependent / Koha / Acquisition / Basket.t
1 #!/usr/bin/perl
2
3 # Copyright 2018 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 => 5;
23 use t::lib::TestBuilder;
24 use t::lib::Mocks;
25
26 use C4::Acquisition;
27 use Koha::Database;
28 use Koha::DateUtils qw(dt_from_string);
29
30 use_ok('Koha::Acquisition::Basket');
31 use_ok('Koha::Acquisition::Baskets');
32
33 my $schema  = Koha::Database->schema;
34 my $builder = t::lib::TestBuilder->new;
35
36 subtest 'create_items + effective_create_items tests' => sub {
37
38     plan tests => 7;
39
40     $schema->storage->txn_begin;
41
42     my $basket = $builder->build_object(
43         {
44             class => 'Koha::Acquisition::Baskets',
45             value => { create_items => undef }
46         }
47     );
48     my $created_basketno = C4::Acquisition::NewBasket(
49         $basket->booksellerid,   $basket->authorisedby,
50         $basket->basketname,     $basket->note,
51         $basket->booksellernote, $basket->contractnumber,
52         $basket->deliveryplace,  $basket->billingplace,
53         $basket->is_standing,    $basket->create_items
54     );
55     my $created_basket = Koha::Acquisition::Baskets->find($created_basketno);
56     is( $created_basket->basketno, $created_basketno,
57         "Basket created by NewBasket matches db basket" );
58     is( $basket->create_items, undef, "Create items value can be null" );
59
60     t::lib::Mocks::mock_preference( 'AcqCreateItem', 'cataloguing' );
61     is( $basket->effective_create_items,
62         "cataloguing",
63         "We use AcqCreateItem if basket create items is not set" );
64     C4::Acquisition::ModBasketHeader(
65         $basket->basketno,       $basket->basketname,
66         $basket->note,           $basket->booksellernote,
67         $basket->contractnumber, $basket->booksellerid,
68         $basket->deliveryplace,  $basket->billingplace,
69         $basket->is_standing,    "ordering"
70     );
71     my $retrieved_basket = Koha::Acquisition::Baskets->find( $basket->basketno );
72     $basket->create_items("ordering");
73     is( $retrieved_basket->create_items, "ordering", "Should be able to set with ModBasketHeader" );
74     is( $basket->create_items, "ordering", "Should be able to set with object methods" );
75     is_deeply( $retrieved_basket->unblessed,
76         $basket->unblessed, "Correct basket found and updated" );
77     is( $retrieved_basket->effective_create_items,
78         "ordering", "We use basket create items if it is set" );
79
80     $schema->storage->txn_rollback;
81 };
82
83 subtest 'basket_group' => sub {
84     plan tests => 2;
85
86     $schema->storage->txn_begin;
87     my $b = $builder->build_object(
88         {
89             class => 'Koha::Acquisition::Baskets',
90             value => { basketgroupid => undef }, # not linked to a basketgroupid
91         }
92     );
93
94     my $basket = Koha::Acquisition::Baskets->find( $b->basketno );
95     is( $basket->basket_group, undef,
96         '->basket_group should return undef if not linked to a basket group');
97
98     $b = $builder->build_object(
99         {
100             class => 'Koha::Acquisition::Baskets',
101             # Will be linked to a basket group by TestBuilder
102         }
103     );
104
105     $basket = Koha::Acquisition::Baskets->find( $b->basketno );
106     is( ref( $basket->basket_group ), 'Koha::Acquisition::BasketGroup',
107         '->basket_group should return a Koha::Acquisition::BasketGroup object if linked to a basket group');
108
109     $schema->storage->txn_rollback;
110 };
111
112 subtest 'to_api() tests' => sub {
113
114     plan tests => 6;
115
116     $schema->storage->txn_begin;
117
118     my $vendor = $builder->build_object({ class => 'Koha::Acquisition::Booksellers' });
119     my $basket = $builder->build_object(
120         {
121             class => 'Koha::Acquisition::Baskets',
122             value => {
123                 closedate => undef
124             }
125         }
126     );
127
128     my $closed = $basket->to_api->{closed};
129     ok( defined $closed, 'closed is defined' );
130     ok( !$closed, 'closedate is undef, closed evaluates to false' );
131
132     $basket->closedate( dt_from_string )->store->discard_changes;
133     $closed = $basket->to_api->{closed};
134     ok( defined $closed, 'closed is defined' );
135     ok( $closed, 'closedate is defined, closed evaluates to true' );
136
137     $basket->booksellerid( $vendor->id )->store->discard_changes;
138     my $basket_json = $basket->to_api({ embed => { bookseller => {} } });
139     ok( exists $basket_json->{bookseller} );
140     is_deeply( $basket_json->{bookseller}, $vendor->to_api );
141
142     $schema->storage->txn_rollback;
143 };