Bug 34008: Harmonize attribute names
[koha.git] / t / db_dependent / api / v1 / item_types.t
1 #!/usr/bin/env perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 1;
21 use Test::Mojo;
22
23 use t::lib::TestBuilder;
24 use t::lib::Mocks;
25
26 use Mojo::JSON qw(encode_json);
27
28 use Koha::ItemTypes;
29 use Koha::Database;
30
31 my $schema  = Koha::Database->new->schema;
32 my $builder = t::lib::TestBuilder->new;
33
34 my $t = Test::Mojo->new('Koha::REST::V1');
35 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
36
37 subtest 'list() tests' => sub {
38
39     plan tests => 13;
40
41     $schema->storage->txn_begin;
42
43     my $item_type = $builder->build_object(
44         {
45             class => 'Koha::ItemTypes',
46             value => {
47                 parent_type                  => undef,
48                 description                  => 'Test item type',
49                 rentalcharge                 => 100.0,
50                 rentalcharge_daily           => 50.,
51                 rentalcharge_daily_calendar  => 0,
52                 rentalcharge_hourly          => 0.60,
53                 rentalcharge_hourly_calendar => 1,
54                 defaultreplacecost           => 1000.0,
55                 processfee                   => 20.0,
56                 notforloan                   => 0,
57                 imageurl          => 'https://upload.wikimedia.org/wikipedia/commons/1/1f/202208_test-tube-4.svg',
58                 summary           => 'An item type for testing',
59                 checkinmsg        => 'Checking in test',
60                 checkinmsgtype    => 'message',
61                 sip_media_type    => 'spt',
62                 hideinopac        => 1,
63                 searchcategory    => 'test search category',
64                 automatic_checkin => 0,
65             }
66         }
67     );
68
69     my $en = $builder->build_object(
70         {
71             class => 'Koha::Localizations',
72             value => {
73                 entity      => 'itemtypes',
74                 code        => $item_type->id,
75                 lang        => 'en',
76                 translation => 'English word "test"',
77             }
78         }
79     );
80     my $sv = $builder->build_object(
81         {
82             class => 'Koha::Localizations',
83             value => {
84                 entity      => 'itemtypes',
85                 code        => $item_type->id,
86                 lang        => 'sv_SE',
87                 translation => 'Swedish word "test"',
88             }
89         }
90     );
91
92     my $librarian = $builder->build_object(
93         {
94             class => 'Koha::Patrons',
95             value => { flags => 2**2 }    # catalogue flag = 2
96         }
97     );
98     my $password = 'thePassword123';
99     $librarian->set_password( { password => $password, skip_validation => 1 } );
100     my $userid = $librarian->userid;
101
102     my $patron = $builder->build_object(
103         {
104             class => 'Koha::Patrons',
105             value => { flags => 0 }
106         }
107     );
108
109     $patron->set_password( { password => $password, skip_validation => 1 } );
110     my $unauth_userid = $patron->userid;
111
112     ## Authorized user tests
113     my $query = encode_json( { item_type_id => $item_type->id } );
114     $t->get_ok("//$userid:$password@/api/v1/item_types?q=$query")->status_is(200)->json_has('/0')
115         ->json_is( '/0/description', $item_type->description )->json_hasnt('/0/translated_descriptions');
116
117     $t->get_ok( "//$userid:$password@/api/v1/item_types?q=$query" => { 'x-koha-embed' => 'translated_descriptions' } )
118         ->status_is(200)->json_has('/0')->json_is( '/0/description', $item_type->description )
119         ->json_has('/0/translated_descriptions')->json_is(
120         '/0/translated_descriptions',
121         [
122             { lang => 'en',    translation => 'English word "test"' },
123             { lang => 'sv_SE', translation => 'Swedish word "test"' },
124         ]
125         );
126
127     # Unauthorized access
128     $t->get_ok("//$unauth_userid:$password@/api/v1/item_types")->status_is(403);
129
130     $schema->storage->txn_rollback;
131 };