Bug 20912: Move calculation to Koha::Fees
[koha.git] / t / db_dependent / Koha / ItemTypes.t
blob2baef5a7ed0b94674652abeea4b36d8b19687cc8
1 #!/usr/bin/perl
3 # Copyright 2014 Catalyst IT
5 # This file is part of Koha.
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.
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.
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.
20 use Modern::Perl;
22 use Data::Dumper;
23 use Test::More tests => 24;
25 use t::lib::Mocks;
26 use t::lib::TestBuilder;
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;
35 BEGIN {
36 use_ok('Koha::ItemType');
37 use_ok('Koha::ItemTypes');
40 my $database = Koha::Database->new();
41 my $schema = $database->schema();
42 $schema->txn_begin;
43 Koha::ItemTypes->delete;
45 Koha::ItemType->new(
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',
57 )->store;
59 Koha::ItemType->new(
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',
71 )->store;
73 Koha::ItemType->new(
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',
85 )->store;
87 Koha::Localization->new(
89 entity => 'itemtypes',
90 code => 'type1',
91 lang => 'en',
92 translation => 'b translated itemtype desc'
94 )->store;
95 Koha::Localization->new(
97 entity => 'itemtypes',
98 code => 'type2',
99 lang => 'en',
100 translation => 'a translated itemtype desc'
102 )->store;
103 Koha::Localization->new(
105 entity => 'something_else',
106 code => 'type2',
107 lang => 'en',
108 translation => 'another thing'
110 )->store;
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' );
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' );
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;
138 $first_itemtype->translated_description,
139 'a translated itemtype desc',
140 'item types should be sorted by translated description'
143 my $builder = t::lib::TestBuilder->new;
144 my $item_type = $builder->build_object({ class => 'Koha::ItemTypes' });
146 is( $item_type->can_be_deleted, 1, 'An item type that is not used can be deleted');
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;
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;
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' );
158 $schema->txn_rollback;