Bug 12768: Fix tests - itemtypes.rentalcharge is now decimal(28,6)
[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 Test::More tests => 24;
23 use Data::Dumper;
24 use Koha::Database;
25 use t::lib::Mocks;
26 use Koha::Items;
27 use Koha::Biblioitems;
28 use t::lib::TestBuilder;
29
30 BEGIN {
31     use_ok('Koha::ItemType');
32     use_ok('Koha::ItemTypes');
33 }
34
35 my $database = Koha::Database->new();
36 my $schema   = $database->schema();
37 $schema->txn_begin;
38 Koha::ItemTypes->delete;
39
40 Koha::ItemType->new(
41     {
42         itemtype       => 'type1',
43         description    => 'description',
44         rentalcharge   => '0.00',
45         imageurl       => 'imageurl',
46         summary        => 'summary',
47         checkinmsg     => 'checkinmsg',
48         checkinmsgtype => 'checkinmsgtype',
49     }
50 )->store;
51
52 Koha::ItemType->new(
53     {
54         itemtype       => 'type2',
55         description    => 'description',
56         rentalcharge   => '0.00',
57         imageurl       => 'imageurl',
58         summary        => 'summary',
59         checkinmsg     => 'checkinmsg',
60         checkinmsgtype => 'checkinmsgtype',
61     }
62 )->store;
63
64 Koha::ItemType->new(
65     {
66         itemtype       => 'type3',
67         description    => 'description',
68         rentalcharge   => '0.00',
69         imageurl       => 'imageurl',
70         summary        => 'summary',
71         checkinmsg     => 'checkinmsg',
72         checkinmsgtype => 'checkinmsgtype',
73     }
74 )->store;
75
76 Koha::Localization->new(
77     {
78         entity      => 'itemtypes',
79         code        => 'type1',
80         lang        => 'en',
81         translation => 'b translated itemtype desc'
82     }
83 )->store;
84 Koha::Localization->new(
85     {
86         entity      => 'itemtypes',
87         code        => 'type2',
88         lang        => 'en',
89         translation => 'a translated itemtype desc'
90     }
91 )->store;
92 Koha::Localization->new(
93     {
94         entity      => 'something_else',
95         code        => 'type2',
96         lang        => 'en',
97         translation => 'another thing'
98     }
99 )->store;
100
101 my $type = Koha::ItemTypes->find('type1');
102 ok( defined($type), 'first result' );
103 is( $type->itemtype,       'type1',          'itemtype/code' );
104 is( $type->description,    'description',    'description' );
105 is( $type->rentalcharge,   '0.000000',       'rentalcharge' );
106 is( $type->imageurl,       'imageurl',       'imageurl' );
107 is( $type->summary,        'summary',        'summary' );
108 is( $type->checkinmsg,     'checkinmsg',     'checkinmsg' );
109 is( $type->checkinmsgtype, 'checkinmsgtype', 'checkinmsgtype' );
110
111 $type = Koha::ItemTypes->find('type2');
112 ok( defined($type), 'second result' );
113 is( $type->itemtype,       'type2',          'itemtype/code' );
114 is( $type->description,    'description',    'description' );
115 is( $type->rentalcharge,   '0.000000',       'rentalcharge' );
116 is( $type->imageurl,       'imageurl',       'imageurl' );
117 is( $type->summary,        'summary',        'summary' );
118 is( $type->checkinmsg,     'checkinmsg',     'checkinmsg' );
119 is( $type->checkinmsgtype, 'checkinmsgtype', 'checkinmsgtype' );
120
121 t::lib::Mocks::mock_preference('language', 'en');
122 t::lib::Mocks::mock_preference('opaclanguages', 'en');
123 my $itemtypes = Koha::ItemTypes->search_with_localization;
124 is( $itemtypes->count, 3, 'There are 3 item types' );
125 my $first_itemtype = $itemtypes->next;
126 is(
127     $first_itemtype->translated_description,
128     'a translated itemtype desc',
129     'item types should be sorted by translated description'
130 );
131
132 my $builder = t::lib::TestBuilder->new;
133 my $item_type = $builder->build_object({ class => 'Koha::ItemTypes' });
134
135 is( $item_type->can_be_deleted, 1, 'An item type that is not used can be deleted');
136
137 my $item = $builder->build_object({ class => 'Koha::Items', value => { itype => $item_type->itemtype }});
138 is( $item_type->can_be_deleted, 0, 'An item type that is used by an item cannot be deleted' );
139 $item->delete;
140
141 my $biblioitem = $builder->build_object({ class => 'Koha::Biblioitems', value => { itemtype => $item_type->itemtype }});
142 is ( $item_type->can_be_deleted, 0, 'An item type that is used by an item and a biblioitem cannot be deleted' );
143 $biblioitem->delete;
144
145 is ( $item_type->can_be_deleted, 1, 'The item type that was being used by the removed item and biblioitem can now be deleted' );
146
147 $schema->txn_rollback;