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 => 14;
26 use t::lib::TestBuilder;
28 use C4::Calendar qw( new );
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" );
98 is_deeply( $type->parent->unblessed, $parent1->unblessed, 'The parent method returns the correct object');
100 $type = Koha::ItemTypes->find($child2->itemtype);
101 ok( defined($type), 'second result' );
102 is_deeply( $type->unblessed, $child2->unblessed, "We got back the same object" );
104 t::lib::Mocks::mock_preference('language', 'en');
105 t::lib::Mocks::mock_preference('OPACLanguages', 'en');
106 my $itemtypes = Koha::ItemTypes->search_with_localization;
107 is( $itemtypes->count, $initial_count + 4, 'We added 4 item types' );
108 my $first_itemtype = $itemtypes->next;
110 $first_itemtype->translated_description,
111 'a translated itemtype desc',
112 'item types should be sorted by translated description'
115 my $children = $parent1->children_with_localization;
116 my $first_child = $children->next;
118 $first_child->translated_description,
119 'a translated itemtype desc',
120 'item types should be sorted by translated description'
123 my $item_type = $builder->build_object({ class => 'Koha::ItemTypes' });
125 is( $item_type->can_be_deleted, 1, 'An item type that is not used can be deleted');
127 my $item = $builder->build_sample_item({ itype => $item_type->itemtype });
128 is( $item_type->can_be_deleted, 0, 'An item type that is used by an item cannot be deleted' );
131 my $biblio = $builder->build_sample_biblio({ itemtype => $item_type->itemtype });
132 is ( $item_type->can_be_deleted, 0, 'An item type that is used by an item and a biblioitem cannot be deleted' );
135 is ( $item_type->can_be_deleted, 1, 'The item type that was being used by the removed item and biblioitem can now be deleted' );
137 $schema->txn_rollback;