21f870d205c1a908ccf944c33673e0d27562dab2
[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     }
55 )->store;
56
57 Koha::ItemType->new(
58     {
59         itemtype       => 'type2',
60         description    => 'description',
61         rentalcharge   => '0.00',
62         imageurl       => 'imageurl',
63         summary        => 'summary',
64         checkinmsg     => 'checkinmsg',
65         checkinmsgtype => 'checkinmsgtype',
66     }
67 )->store;
68
69 Koha::ItemType->new(
70     {
71         itemtype       => 'type3',
72         description    => 'description',
73         rentalcharge   => '0.00',
74         imageurl       => 'imageurl',
75         summary        => 'summary',
76         checkinmsg     => 'checkinmsg',
77         checkinmsgtype => 'checkinmsgtype',
78     }
79 )->store;
80
81 Koha::Localization->new(
82     {
83         entity      => 'itemtypes',
84         code        => 'type1',
85         lang        => 'en',
86         translation => 'b translated itemtype desc'
87     }
88 )->store;
89 Koha::Localization->new(
90     {
91         entity      => 'itemtypes',
92         code        => 'type2',
93         lang        => 'en',
94         translation => 'a translated itemtype desc'
95     }
96 )->store;
97 Koha::Localization->new(
98     {
99         entity      => 'something_else',
100         code        => 'type2',
101         lang        => 'en',
102         translation => 'another thing'
103     }
104 )->store;
105
106 my $type = Koha::ItemTypes->find('type1');
107 ok( defined($type), 'first result' );
108 is( $type->itemtype,       'type1',          'itemtype/code' );
109 is( $type->description,    'description',    'description' );
110 is( $type->rentalcharge,   '0.000000',       'rentalcharge' );
111 is( $type->imageurl,       'imageurl',       'imageurl' );
112 is( $type->summary,        'summary',        'summary' );
113 is( $type->checkinmsg,     'checkinmsg',     'checkinmsg' );
114 is( $type->checkinmsgtype, 'checkinmsgtype', 'checkinmsgtype' );
115
116 $type = Koha::ItemTypes->find('type2');
117 ok( defined($type), 'second result' );
118 is( $type->itemtype,       'type2',          'itemtype/code' );
119 is( $type->description,    'description',    'description' );
120 is( $type->rentalcharge,   '0.000000',       'rentalcharge' );
121 is( $type->imageurl,       'imageurl',       'imageurl' );
122 is( $type->summary,        'summary',        'summary' );
123 is( $type->checkinmsg,     'checkinmsg',     'checkinmsg' );
124 is( $type->checkinmsgtype, 'checkinmsgtype', 'checkinmsgtype' );
125
126 t::lib::Mocks::mock_preference('language', 'en');
127 t::lib::Mocks::mock_preference('opaclanguages', 'en');
128 my $itemtypes = Koha::ItemTypes->search_with_localization;
129 is( $itemtypes->count, 3, 'There are 3 item types' );
130 my $first_itemtype = $itemtypes->next;
131 is(
132     $first_itemtype->translated_description,
133     'a translated itemtype desc',
134     'item types should be sorted by translated description'
135 );
136
137 my $builder = t::lib::TestBuilder->new;
138 my $item_type = $builder->build_object({ class => 'Koha::ItemTypes' });
139
140 is( $item_type->can_be_deleted, 1, 'An item type that is not used can be deleted');
141
142 my $item = $builder->build_object({ class => 'Koha::Items', value => { itype => $item_type->itemtype }});
143 is( $item_type->can_be_deleted, 0, 'An item type that is used by an item cannot be deleted' );
144 $item->delete;
145
146 my $biblioitem = $builder->build_object({ class => 'Koha::Biblioitems', value => { itemtype => $item_type->itemtype }});
147 is ( $item_type->can_be_deleted, 0, 'An item type that is used by an item and a biblioitem cannot be deleted' );
148 $biblioitem->delete;
149
150 is ( $item_type->can_be_deleted, 1, 'The item type that was being used by the removed item and biblioitem can now be deleted' );
151
152 subtest 'Koha::ItemType::calc_rental_charge_daily tests' => sub {
153     plan tests => 4;
154
155     my $library = Koha::Libraries->search()->next();
156     my $module = new Test::MockModule('C4::Context');
157     $module->mock('userenv', sub { { branch => $library->id } });
158
159     my $itemtype = Koha::ItemType->new(
160         {
161             itemtype            => 'type4',
162             description         => 'description',
163             rental_charge_daily => 1.00,
164         }
165     )->store;
166
167     is( $itemtype->rental_charge_daily, 1.00, 'Daily rental charge stored and retreived correctly' );
168
169     my $dt_from = dt_from_string();
170     my $dt_to = dt_from_string()->add( days => 7 );
171
172     t::lib::Mocks::mock_preference('finesCalendar', 'ignoreCalendar');
173     my $charge = $itemtype->calc_rental_charge_daily( { from => $dt_from, to => $dt_to } );
174     is( $charge, 7.00, "Daily rental charge calculated correctly with finesCalendar = ignoreCalendar" );
175
176     t::lib::Mocks::mock_preference('finesCalendar', 'noFinesWhenClosed');
177     $charge = $itemtype->calc_rental_charge_daily( { from => $dt_from, to => $dt_to } );
178     is( $charge, 7.00, "Daily rental charge calculated correctly with finesCalendar = noFinesWhenClosed" );
179
180     my $calendar = C4::Calendar->new( branchcode => $library->id );
181     $calendar->insert_week_day_holiday(
182         weekday     => 3,
183         title       => 'Test holiday',
184         description => 'Test holiday'
185     );
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 and closed Wednesdays" );
188
189 };
190
191 $schema->txn_rollback;