Bug 34916: Fix wrong borrowernumber in ArticleRequests.t
[koha.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 Test::More tests => 15;
23
24 use t::lib::Mocks;
25 use t::lib::TestBuilder;
26
27 use C4::Calendar qw( new );
28 use Koha::Biblioitems;
29 use Koha::Libraries;
30 use Koha::Database;
31 use Koha::DateUtils qw(dt_from_string);;
32 use Koha::Items;
33
34 BEGIN {
35     use_ok('Koha::ItemType');
36     use_ok('Koha::ItemTypes');
37 }
38
39 my $database = Koha::Database->new();
40 my $schema   = $database->schema();
41 $schema->txn_begin;
42
43 my $builder     = t::lib::TestBuilder->new;
44 my $initial_count = Koha::ItemTypes->search->count;
45
46 my $parent1 = $builder->build_object({ class => 'Koha::ItemTypes', value => { description => 'description' } });
47 my $child1  = $builder->build_object({
48         class => 'Koha::ItemTypes',
49         value => {
50             parent_type => $parent1->itemtype,
51             description => 'description',
52         }
53     });
54 my $child2  = $builder->build_object({
55         class => 'Koha::ItemTypes',
56         value => {
57             parent_type => $parent1->itemtype,
58             description => 'description',
59         }
60     });
61 my $child3  = $builder->build_object({
62         class => 'Koha::ItemTypes',
63         value => {
64             parent_type => $parent1->itemtype,
65             description => 'description',
66         }
67     });
68
69 Koha::Localization->new(
70     {
71         entity      => 'itemtypes',
72         code        => $child1->itemtype,
73         lang        => 'en',
74         translation => 'b translated itemtype desc'
75     }
76 )->store;
77 Koha::Localization->new(
78     {
79         entity      => 'itemtypes',
80         code        => $child2->itemtype,
81         lang        => 'en',
82         translation => 'a translated itemtype desc'
83     }
84 )->store;
85 Koha::Localization->new(
86     {
87         entity      => 'something_else',
88         code        => $child2->itemtype,
89         lang        => 'en',
90         translation => 'another thing'
91     }
92 )->store;
93
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');
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_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' );
128 $item->delete;
129
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' );
132 $biblio->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 subtest 'image_location' => sub {
137     plan tests => 3;
138
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');
143     is(
144         $item_type->image_location('opac'), '/opac-tmpl/bootstrap/itemtypeimg/bridge/newthing.png',
145         'Check path for opac'
146     );
147     is(
148         $item_type->image_location('intranet'), '/intranet-tmpl/prog/img/itemtypeimg/bridge/newthing.png',
149         'Check path for intranet'
150     );
151 };
152
153 $schema->txn_rollback;