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
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 use Test::More tests => 15;
25 use t::lib::TestBuilder;
27 use C4::Calendar qw( new );
28 use Koha::Biblioitems;
31 use Koha::DateUtils qw(dt_from_string);;
35 use_ok('Koha::ItemType');
36 use_ok('Koha::ItemTypes');
39 my $database = Koha::Database->new();
40 my $schema = $database->schema();
43 my $builder = t::lib::TestBuilder->new;
44 my $initial_count = Koha::ItemTypes->search->count;
46 my $parent1 = $builder->build_object({ class => 'Koha::ItemTypes', value => { description => 'description' } });
47 my $child1 = $builder->build_object({
48 class => 'Koha::ItemTypes',
50 parent_type => $parent1->itemtype,
51 description => 'description',
54 my $child2 = $builder->build_object({
55 class => 'Koha::ItemTypes',
57 parent_type => $parent1->itemtype,
58 description => 'description',
61 my $child3 = $builder->build_object({
62 class => 'Koha::ItemTypes',
64 parent_type => $parent1->itemtype,
65 description => 'description',
69 Koha::Localization->new(
71 entity => 'itemtypes',
72 code => $child1->itemtype,
74 translation => 'b translated itemtype desc'
77 Koha::Localization->new(
79 entity => 'itemtypes',
80 code => $child2->itemtype,
82 translation => 'a translated itemtype desc'
85 Koha::Localization->new(
87 entity => 'something_else',
88 code => $child2->itemtype,
90 translation => 'another thing'
94 my $type = Koha::ItemTypes->find($child1->itemtype);
95 ok( defined($type), 'first result' );
96 is_deeply( $type->unblessed, $child1->unblessed, "We got back the same object" );
97 is_deeply( $type->parent->unblessed, $parent1->unblessed, 'The parent method returns the correct object');
99 $type = Koha::ItemTypes->find($child2->itemtype);
100 ok( defined($type), 'second result' );
101 is_deeply( $type->unblessed, $child2->unblessed, "We got back the same object" );
103 t::lib::Mocks::mock_preference('language', 'en');
104 t::lib::Mocks::mock_preference('OPACLanguages', 'en');
105 my $itemtypes = Koha::ItemTypes->search_with_localization;
106 is( $itemtypes->count, $initial_count + 4, 'We added 4 item types' );
107 my $first_itemtype = $itemtypes->next;
109 $first_itemtype->translated_description,
110 'a translated itemtype desc',
111 'item types should be sorted by translated description'
114 my $children = $parent1->children_with_localization;
115 my $first_child = $children->next;
117 $first_child->translated_description,
118 'a translated itemtype desc',
119 'item types should be sorted by translated description'
122 my $item_type = $builder->build_object({ class => 'Koha::ItemTypes' });
124 is( $item_type->can_be_deleted, 1, 'An item type that is not used can be deleted');
126 my $item = $builder->build_sample_item({ itype => $item_type->itemtype });
127 is( $item_type->can_be_deleted, 0, 'An item type that is used by an item cannot be deleted' );
130 my $biblio = $builder->build_sample_biblio({ itemtype => $item_type->itemtype });
131 is ( $item_type->can_be_deleted, 0, 'An item type that is used by an item and a biblioitem cannot be deleted' );
134 is ( $item_type->can_be_deleted, 1, 'The item type that was being used by the removed item and biblioitem can now be deleted' );
136 subtest 'image_location' => sub {
139 my $item_type = $builder->build_object( { class => 'Koha::ItemTypes' } );
140 $item_type->imageurl('https://myserver.org/image01');
141 is( $item_type->image_location, 'https://myserver.org/image01', 'Check URL' );
142 $item_type->imageurl('bridge/newthing.png');
144 $item_type->image_location('opac'), '/opac-tmpl/bootstrap/itemtypeimg/bridge/newthing.png',
145 'Check path for opac'
148 $item_type->image_location('intranet'), '/intranet-tmpl/prog/img/itemtypeimg/bridge/newthing.png',
149 'Check path for intranet'
153 $schema->txn_rollback;