Line 0
Link Here
|
0 |
- |
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 C4::Biblio qw{ModBiblio}; |
24 |
|
25 |
use t::lib::Mocks; |
26 |
use t::lib::TestBuilder; |
27 |
|
28 |
my $schema = Koha::Database->new->schema; |
29 |
my $builder = t::lib::TestBuilder->new; |
30 |
|
31 |
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); |
32 |
|
33 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
34 |
|
35 |
subtest 'populate_empty_callnumbers() tests' => sub { |
36 |
|
37 |
plan tests => 30; |
38 |
|
39 |
$schema->storage->txn_begin; |
40 |
|
41 |
my $biblio = $builder->build_sample_biblio(); |
42 |
my $record = $biblio->record; |
43 |
|
44 |
my $cn = q{PA522}; |
45 |
my $in = q{.M38 1993}; |
46 |
|
47 |
$record->insert_fields_ordered( MARC::Field->new( '050', ' ', ' ', a => $cn, b => $in ) ); |
48 |
|
49 |
my $expected_callnumber = $cn . $in; |
50 |
|
51 |
my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { flags => 0 } } ); |
52 |
|
53 |
my $password = 'thePassword123'; |
54 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
55 |
my $userid = $patron->userid; |
56 |
|
57 |
my $biblio_id = $biblio->id; |
58 |
|
59 |
$t->post_ok( "//$userid:$password@/api/v1/rpc/biblios/$biblio_id/items/populate_empty_callnumbers" => json => |
60 |
{ external_id => 'something' } )->status_is( 403, 'Not enough permissions to update an item' ); |
61 |
|
62 |
# Add permissions |
63 |
$builder->build( |
64 |
{ |
65 |
source => 'UserPermission', |
66 |
value => { |
67 |
borrowernumber => $patron->borrowernumber, |
68 |
module_bit => 9, |
69 |
code => 'edit_catalogue' |
70 |
} |
71 |
} |
72 |
); |
73 |
|
74 |
t::lib::Mocks::mock_preference( 'itemcallnumber', '' ); |
75 |
|
76 |
$t->post_ok( "//$userid:$password@/api/v1/rpc/biblios/$biblio_id/items/populate_empty_callnumbers" => json => {} ) |
77 |
->status_is( 409, 'Wrong configuration' )->json_is( '/error_code', q{missing_configuration} ); |
78 |
|
79 |
t::lib::Mocks::mock_preference( 'itemcallnumber', '050ab' ); |
80 |
|
81 |
$t->post_ok( "//$userid:$password@/api/v1/rpc/biblios/$biblio_id/items/populate_empty_callnumbers" => json => {} ) |
82 |
->status_is( 409, 'Callnumber empty' )->json_is( '/error_code', q{callnumber_empty} ); |
83 |
|
84 |
ModBiblio( $record, $biblio_id ); |
85 |
|
86 |
$t->post_ok( "//$userid:$password@/api/v1/rpc/biblios/$biblio_id/items/populate_empty_callnumbers" => json => {} ) |
87 |
->status_is( 200, 'No items but request successful' )->json_is( '/updated_items_count', 0 ); |
88 |
|
89 |
my $item_1 = $builder->build_sample_item( { biblionumber => $biblio->id, itemcallnumber => q{} } ); |
90 |
my $item_2 = $builder->build_sample_item( { biblionumber => $biblio->id, itemcallnumber => q{} } ); |
91 |
my $item_3 = $builder->build_sample_item( { biblionumber => $biblio->id, itemcallnumber => q{} } ); |
92 |
my $item_4 = $builder->build_sample_item( { biblionumber => $biblio->id, itemcallnumber => q{someCallNumber} } ); |
93 |
|
94 |
my $item1_id = $item_1->id; |
95 |
|
96 |
$t->post_ok( |
97 |
"//$userid:$password@/api/v1/rpc/biblios/$biblio_id/items/$item1_id/populate_empty_callnumbers" => json => {} ) |
98 |
->status_is( 200, 'Item updated' )->json_is( '/updated_items_count', 1 ) |
99 |
->json_is( '/callnumber', $expected_callnumber ); |
100 |
|
101 |
my @items_to_update = ( $item_2->id, $item_3->id ); |
102 |
|
103 |
$t->post_ok( "//$userid:$password@/api/v1/rpc/biblios/$biblio_id/items/populate_empty_callnumbers" => json => {} ) |
104 |
->status_is( 200, 'Items updated' )->json_is( '/updated_items_count', 2 ) |
105 |
->json_is( '/callnumber', $expected_callnumber )->json_is( '/modified_item_ids', \@items_to_update ); |
106 |
|
107 |
$t->post_ok( "//$userid:$password@/api/v1/rpc/biblios/$biblio_id/items/populate_empty_callnumbers" => json => {} ) |
108 |
->status_is( 200, 'Items updated' )->json_is( '/updated_items_count', 0 ); |
109 |
|
110 |
$t->post_ok( |
111 |
"//$userid:$password@/api/v1/rpc/biblios/$biblio_id/items/$item1_id/populate_empty_callnumbers" => json => {} ) |
112 |
->status_is( 200, 'Item updated' )->json_is( '/updated_items_count', 0 ); |
113 |
|
114 |
$biblio->delete; |
115 |
|
116 |
$t->post_ok( "//$userid:$password@/api/v1/rpc/biblios/$biblio_id/items/populate_empty_callnumbers" => json => {} ) |
117 |
->status_is( 404, 'Record not found' )->json_is( '/error' => q{Bibliographic record not found} ) |
118 |
->json_is( '/error_code' => q{not_found} ); |
119 |
|
120 |
$schema->storage->txn_rollback; |
121 |
}; |