Bug 21946: Display parent-child relationship on smart-rules.pl
[koha-equinox.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
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21
22 use Data::Dumper;
23 use Test::More tests => 13;
24
25 use t::lib::Mocks;
26 use t::lib::TestBuilder;
27
28 use C4::Calendar;
29 use Koha::Biblioitems;
30 use Koha::Libraries;
31 use Koha::Database;
32 use Koha::DateUtils qw(dt_from_string);;
33 use Koha::Items;
34
35 BEGIN {
36     use_ok('Koha::ItemType');
37     use_ok('Koha::ItemTypes');
38 }
39
40 my $database = Koha::Database->new();
41 my $schema   = $database->schema();
42 $schema->txn_begin;
43
44 my $builder     = t::lib::TestBuilder->new;
45 my $initial_count = Koha::ItemTypes->search->count;
46
47 my $parent1 = $builder->build_object({ class => 'Koha::ItemTypes', value => { description => 'description' } });
48 my $child1  = $builder->build_object({
49         class => 'Koha::ItemTypes',
50         value => {
51             parent_type => $parent1->itemtype,
52             description => 'description',
53         }
54     });
55 my $child2  = $builder->build_object({
56         class => 'Koha::ItemTypes',
57         value => {
58             parent_type => $parent1->itemtype,
59             description => 'description',
60         }
61     });
62 my $child3  = $builder->build_object({
63         class => 'Koha::ItemTypes',
64         value => {
65             parent_type => $parent1->itemtype,
66             description => 'description',
67         }
68     });
69
70 Koha::Localization->new(
71     {
72         entity      => 'itemtypes',
73         code        => $child1->itemtype,
74         lang        => 'en',
75         translation => 'b translated itemtype desc'
76     }
77 )->store;
78 Koha::Localization->new(
79     {
80         entity      => 'itemtypes',
81         code        => $child2->itemtype,
82         lang        => 'en',
83         translation => 'a translated itemtype desc'
84     }
85 )->store;
86 Koha::Localization->new(
87     {
88         entity      => 'something_else',
89         code        => $child2->itemtype,
90         lang        => 'en',
91         translation => 'another thing'
92     }
93 )->store;
94
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
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" );
102
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;
108 is(
109     $first_itemtype->translated_description,
110     'a translated itemtype desc',
111     'item types should be sorted by translated description'
112 );
113
114 my $children = $parent1->children_with_localization;
115 my $first_child = $children->next;
116 is(
117     $first_child->translated_description,
118     'a translated itemtype desc',
119     'item types should be sorted by translated description'
120 );
121
122 my $item_type = $builder->build_object({ class => 'Koha::ItemTypes' });
123
124 is( $item_type->can_be_deleted, 1, 'An item type that is not used can be deleted');
125
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' );
128 $item->delete;
129
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' );
132 $biblioitem->delete;
133
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' );
135
136 $schema->txn_rollback;