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>.
23 use Test::More tests => 13;
26 use t::lib::TestBuilder;
29 use Koha::Biblioitems;
32 use Koha::DateUtils qw(dt_from_string);;
36 use_ok('Koha::ItemType');
37 use_ok('Koha::ItemTypes');
40 my $database = Koha::Database->new();
41 my $schema = $database->schema();
44 my $builder = t::lib::TestBuilder->new;
45 my $initial_count = Koha::ItemTypes->search->count;
47 my $parent1 = $builder->build_object({ class => 'Koha::ItemTypes', value => { description => 'description' } });
48 my $child1 = $builder->build_object({
49 class => 'Koha::ItemTypes',
51 parent_type => $parent1->itemtype,
52 description => 'description',
55 my $child2 = $builder->build_object({
56 class => 'Koha::ItemTypes',
58 parent_type => $parent1->itemtype,
59 description => 'description',
62 my $child3 = $builder->build_object({
63 class => 'Koha::ItemTypes',
65 parent_type => $parent1->itemtype,
66 description => 'description',
70 Koha::Localization->new(
72 entity => 'itemtypes',
73 code => $child1->itemtype,
75 translation => 'b translated itemtype desc'
78 Koha::Localization->new(
80 entity => 'itemtypes',
81 code => $child2->itemtype,
83 translation => 'a translated itemtype desc'
86 Koha::Localization->new(
88 entity => 'something_else',
89 code => $child2->itemtype,
91 translation => 'another thing'
95 my $type = Koha::ItemTypes->find($child1->itemtype);
96 ok( defined($type), 'first result' );
97 is_deeply( $type->unblessed, $child1->unblessed, "We got back the same 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_object({ class => 'Koha::Items', value => { 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 $biblioitem = $builder->build_object({ class => 'Koha::Biblioitems', value => { 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 $schema->txn_rollback;