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
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.
22 use Test::More tests => 26;
27 use Koha::Biblioitems;
28 use t::lib::TestBuilder;
31 use_ok('Koha::ItemType');
32 use_ok('Koha::ItemTypes');
35 my $database = Koha::Database->new();
36 my $schema = $database->schema();
38 Koha::ItemTypes->delete;
43 description => 'description',
44 rentalcharge => '0.00',
45 imageurl => 'imageurl',
47 checkinmsg => 'checkinmsg',
48 checkinmsgtype => 'checkinmsgtype',
55 description => 'description',
56 rentalcharge => '0.00',
57 imageurl => 'imageurl',
59 checkinmsg => 'checkinmsg',
60 checkinmsgtype => 'checkinmsgtype',
67 description => 'description',
68 rentalcharge => '0.00',
69 imageurl => 'imageurl',
71 checkinmsg => 'checkinmsg',
72 checkinmsgtype => 'checkinmsgtype',
76 Koha::Localization->new(
78 entity => 'itemtypes',
81 translation => 'b translated itemtype desc'
84 Koha::Localization->new(
86 entity => 'itemtypes',
89 translation => 'a translated itemtype desc'
92 Koha::Localization->new(
94 entity => 'something_else',
97 translation => 'another thing'
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.0000', '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' );
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.0000', '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' );
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;
127 $first_itemtype->translated_description,
128 'a translated itemtype desc',
129 'item types should be sorted by translated description'
132 my $builder = t::lib::TestBuilder->new;
133 my $item_type = $builder->build_object({ class => 'Koha::ItemTypes' });
135 is( $item_type->can_be_deleted, 1, 'An item type that is not used can be deleted');
137 my $item = $builder->build_object({ class => 'Koha::Items', value => { itype => $item_type->itemtype }});
139 is( $item_type->can_be_deleted, 0, 'An item type that is used by an item cannot be deleted' );
141 my $biblio = $builder->build_object({ class => 'Koha::Biblioitems', value => { itemtype => $item_type->itemtype }});
143 is ( $item_type->can_be_deleted, 0, 'An item type that is used by an item and a biblioitem cannot be deleted' );
145 is ( $item->delete, 1, 'An item has been deleted' );
147 is ( $biblio->delete, 1, 'A biblioitem has been deleted' );
149 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 $schema->txn_rollback;