Bug 24464: Unit tests
[koha.git] / t / db_dependent / Koha / Acquisition / Basket.t
blobdfa7b6fab36fd16c52a6c262defccda3390c5ffd
1 #!/usr/bin/perl
3 # Copyright 2018 Koha Development team
5 # This file is part of Koha
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.
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.
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>.
20 use Modern::Perl;
22 use Test::More tests => 6;
23 use t::lib::TestBuilder;
24 use t::lib::Mocks;
26 use C4::Acquisition;
27 use Koha::Database;
28 use Koha::DateUtils qw(dt_from_string);
30 use_ok('Koha::Acquisition::Basket');
31 use_ok('Koha::Acquisition::Baskets');
33 my $schema = Koha::Database->schema;
34 my $builder = t::lib::TestBuilder->new;
36 subtest 'create_items + effective_create_items tests' => sub {
38 plan tests => 7;
40 $schema->storage->txn_begin;
42 my $basket = $builder->build_object(
44 class => 'Koha::Acquisition::Baskets',
45 value => { create_items => undef }
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
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" );
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"
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" );
80 $schema->storage->txn_rollback;
83 subtest 'basket_group' => sub {
84 plan tests => 2;
86 $schema->storage->txn_begin;
87 my $b = $builder->build_object(
89 class => 'Koha::Acquisition::Baskets',
90 value => { basketgroupid => undef }, # not linked to a basketgroupid
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');
98 $b = $builder->build_object(
100 class => 'Koha::Acquisition::Baskets',
101 # Will be linked to a basket group by TestBuilder
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');
109 $schema->storage->txn_rollback;
112 subtest 'creator() tests' => sub {
114 plan tests => 4;
116 $schema->storage->txn_begin;
118 my $basket = $builder->build_object(
120 class => 'Koha::Acquisition::Baskets',
121 value => { authorisedby => undef }
125 is( $basket->creator, undef, 'Undef is handled as expected' );
127 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
128 $basket->authorisedby( $patron->borrowernumber )->store->discard_changes;
130 my $creator = $basket->creator;
131 is( ref($creator), 'Koha::Patron', 'Return type is correct' );
132 is( $creator->borrowernumber, $patron->borrowernumber, 'Returned object is the right one' );
134 # Delete the patron
135 $patron->delete;
137 is( $basket->creator, undef, 'Undef is handled as expected' );
139 $schema->storage->txn_rollback;
142 subtest 'to_api() tests' => sub {
144 plan tests => 6;
146 $schema->storage->txn_begin;
148 my $vendor = $builder->build_object({ class => 'Koha::Acquisition::Booksellers' });
149 my $basket = $builder->build_object(
151 class => 'Koha::Acquisition::Baskets',
152 value => {
153 closedate => undef
158 my $closed = $basket->to_api->{closed};
159 ok( defined $closed, 'closed is defined' );
160 ok( !$closed, 'closedate is undef, closed evaluates to false' );
162 $basket->closedate( dt_from_string )->store->discard_changes;
163 $closed = $basket->to_api->{closed};
164 ok( defined $closed, 'closed is defined' );
165 ok( $closed, 'closedate is defined, closed evaluates to true' );
167 $basket->booksellerid( $vendor->id )->store->discard_changes;
168 my $basket_json = $basket->to_api({ embed => { bookseller => {} } });
169 ok( exists $basket_json->{bookseller} );
170 is_deeply( $basket_json->{bookseller}, $vendor->to_api );
172 $schema->storage->txn_rollback;