Bug 20912: (QA follow-up) Make unit tests reliable and get rid of perl warnings
[koha.git] / t / db_dependent / Koha / ItemTypes.t
1 #!/usr/bin/perl
2 #
3 # Copyright 2014 Catalyst IT
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use Modern::Perl;
21
22 use Data::Dumper;
23 use Test::More tests => 25;
24
25 use t::lib::Mocks;
26 use t::lib::TestBuilder;
27
28 use C4::Calendar;
29 use Koha::Biblioitems;
30 use Koha::Libraries;
31 use Koha::Database;
32 use Koha::DateUtils qw(dt_from_string);;
33 use Koha::Items;
34
35 BEGIN {
36     use_ok('Koha::ItemType');
37     use_ok('Koha::ItemTypes');
38 }
39
40 my $database = Koha::Database->new();
41 my $schema   = $database->schema();
42 $schema->txn_begin;
43 Koha::ItemTypes->delete;
44
45 Koha::ItemType->new(
46     {
47         itemtype       => 'type1',
48         description    => 'description',
49         rentalcharge   => '0.00',
50         imageurl       => 'imageurl',
51         summary        => 'summary',
52         checkinmsg     => 'checkinmsg',
53         checkinmsgtype => 'checkinmsgtype',
54         processfee         => '0.00',
55         defaultreplacecost => '0.00',
56     }
57 )->store;
58
59 Koha::ItemType->new(
60     {
61         itemtype       => 'type2',
62         description    => 'description',
63         rentalcharge   => '0.00',
64         imageurl       => 'imageurl',
65         summary        => 'summary',
66         checkinmsg     => 'checkinmsg',
67         checkinmsgtype => 'checkinmsgtype',
68         processfee         => '0.00',
69         defaultreplacecost => '0.00',
70     }
71 )->store;
72
73 Koha::ItemType->new(
74     {
75         itemtype       => 'type3',
76         description    => 'description',
77         rentalcharge   => '0.00',
78         imageurl       => 'imageurl',
79         summary        => 'summary',
80         checkinmsg     => 'checkinmsg',
81         checkinmsgtype => 'checkinmsgtype',
82         processfee         => '0.00',
83         defaultreplacecost => '0.00',
84     }
85 )->store;
86
87 Koha::Localization->new(
88     {
89         entity      => 'itemtypes',
90         code        => 'type1',
91         lang        => 'en',
92         translation => 'b translated itemtype desc'
93     }
94 )->store;
95 Koha::Localization->new(
96     {
97         entity      => 'itemtypes',
98         code        => 'type2',
99         lang        => 'en',
100         translation => 'a translated itemtype desc'
101     }
102 )->store;
103 Koha::Localization->new(
104     {
105         entity      => 'something_else',
106         code        => 'type2',
107         lang        => 'en',
108         translation => 'another thing'
109     }
110 )->store;
111
112 my $type = Koha::ItemTypes->find('type1');
113 ok( defined($type), 'first result' );
114 is( $type->itemtype,       'type1',          'itemtype/code' );
115 is( $type->description,    'description',    'description' );
116 is( $type->rentalcharge,   '0.000000',       'rentalcharge' );
117 is( $type->imageurl,       'imageurl',       'imageurl' );
118 is( $type->summary,        'summary',        'summary' );
119 is( $type->checkinmsg,     'checkinmsg',     'checkinmsg' );
120 is( $type->checkinmsgtype, 'checkinmsgtype', 'checkinmsgtype' );
121
122 $type = Koha::ItemTypes->find('type2');
123 ok( defined($type), 'second result' );
124 is( $type->itemtype,       'type2',          'itemtype/code' );
125 is( $type->description,    'description',    'description' );
126 is( $type->rentalcharge,   '0.000000',       'rentalcharge' );
127 is( $type->imageurl,       'imageurl',       'imageurl' );
128 is( $type->summary,        'summary',        'summary' );
129 is( $type->checkinmsg,     'checkinmsg',     'checkinmsg' );
130 is( $type->checkinmsgtype, 'checkinmsgtype', 'checkinmsgtype' );
131
132 t::lib::Mocks::mock_preference('language', 'en');
133 t::lib::Mocks::mock_preference('opaclanguages', 'en');
134 my $itemtypes = Koha::ItemTypes->search_with_localization;
135 is( $itemtypes->count, 3, 'There are 3 item types' );
136 my $first_itemtype = $itemtypes->next;
137 is(
138     $first_itemtype->translated_description,
139     'a translated itemtype desc',
140     'item types should be sorted by translated description'
141 );
142
143 my $builder = t::lib::TestBuilder->new;
144 my $item_type = $builder->build_object({ class => 'Koha::ItemTypes' });
145
146 is( $item_type->can_be_deleted, 1, 'An item type that is not used can be deleted');
147
148 my $item = $builder->build_object({ class => 'Koha::Items', value => { itype => $item_type->itemtype }});
149 is( $item_type->can_be_deleted, 0, 'An item type that is used by an item cannot be deleted' );
150 $item->delete;
151
152 my $biblioitem = $builder->build_object({ class => 'Koha::Biblioitems', value => { itemtype => $item_type->itemtype }});
153 is ( $item_type->can_be_deleted, 0, 'An item type that is used by an item and a biblioitem cannot be deleted' );
154 $biblioitem->delete;
155
156 is ( $item_type->can_be_deleted, 1, 'The item type that was being used by the removed item and biblioitem can now be deleted' );
157
158 subtest 'Koha::ItemType::calc_rental_charge_daily tests' => sub {
159     plan tests => 4;
160
161     my $library = Koha::Libraries->search()->next();
162     my $module = new Test::MockModule('C4::Context');
163     $module->mock('userenv', sub { { branch => $library->id } });
164
165     my $itemtype = Koha::ItemType->new(
166         {
167             itemtype            => 'type4',
168             description         => 'description',
169             rental_charge_daily => 1.00,
170             rentalcharge        => '0.00',
171             processfee          => '0.00',
172             defaultreplacecost  => '0.00',
173         }
174     )->store;
175
176     is( $itemtype->rental_charge_daily, 1.00, 'Daily rental charge stored and retreived correctly' );
177
178     my $dt_from = dt_from_string();
179     my $dt_to = dt_from_string()->add( days => 6 );
180
181     t::lib::Mocks::mock_preference('finesCalendar', 'ignoreCalendar');
182     my $charge = $itemtype->calc_rental_charge_daily( { from => $dt_from, to => $dt_to } );
183     is( $charge, 6.00, "Daily rental charge calculated correctly with finesCalendar = ignoreCalendar" );
184
185     t::lib::Mocks::mock_preference('finesCalendar', 'noFinesWhenClosed');
186     $charge = $itemtype->calc_rental_charge_daily( { from => $dt_from, to => $dt_to } );
187     is( $charge, 6.00, "Daily rental charge calculated correctly with finesCalendar = noFinesWhenClosed" );
188
189     my $calendar = C4::Calendar->new( branchcode => $library->id );
190     $calendar->insert_week_day_holiday(
191         weekday     => 3,
192         title       => 'Test holiday',
193         description => 'Test holiday'
194     );
195     $charge = $itemtype->calc_rental_charge_daily( { from => $dt_from, to => $dt_to } );
196     is( $charge, 5.00, "Daily rental charge calculated correctly with finesCalendar = noFinesWhenClosed and closed Wednesdays" );
197
198 };
199
200 $schema->txn_rollback;