|
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 => 34; |
| 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' ) |
| 78 |
->json_is( '/error_code', q{missing_configuration} ); |
| 79 |
|
| 80 |
t::lib::Mocks::mock_preference( 'itemcallnumber', '050ab' ); |
| 81 |
|
| 82 |
$t->post_ok( "//$userid:$password@/api/v1/rpc/biblios/$biblio_id/items/populate_empty_callnumbers" => json => {} ) |
| 83 |
->status_is( 409, 'Callnumber empty' ) |
| 84 |
->json_is( '/error_code', q{callnumber_empty} ); |
| 85 |
|
| 86 |
ModBiblio( $record, $biblio_id ); |
| 87 |
|
| 88 |
$t->post_ok( "//$userid:$password@/api/v1/rpc/biblios/$biblio_id/items/populate_empty_callnumbers" => json => {} ) |
| 89 |
->status_is( 200, 'No items but request successful' ) |
| 90 |
->json_is( '/updated_items_count', 0 ); |
| 91 |
|
| 92 |
my $item_1 = $builder->build_sample_item( { biblionumber => $biblio->id, itemcallnumber => q{} } ); |
| 93 |
my $item_2 = $builder->build_sample_item( { biblionumber => $biblio->id, itemcallnumber => q{} } ); |
| 94 |
my $item_3 = $builder->build_sample_item( { biblionumber => $biblio->id, itemcallnumber => q{} } ); |
| 95 |
my $item_4 = $builder->build_sample_item( { biblionumber => $biblio->id, itemcallnumber => q{someCallNumber} } ); |
| 96 |
|
| 97 |
my $item1_id = $item_1->id; |
| 98 |
|
| 99 |
$t->post_ok( |
| 100 |
"//$userid:$password@/api/v1/rpc/biblios/$biblio_id/items/$item1_id/populate_empty_callnumbers" => json => {} ) |
| 101 |
->status_is( 200, 'Item updated' ) |
| 102 |
->json_is( '/updated_items_count', 1 ) |
| 103 |
->json_is( '/callnumber', $expected_callnumber ); |
| 104 |
|
| 105 |
my @items_to_update = ( $item_2->id, $item_3->id ); |
| 106 |
|
| 107 |
$t->post_ok( "//$userid:$password@/api/v1/rpc/biblios/$biblio_id/items/populate_empty_callnumbers" => json => {} ) |
| 108 |
->status_is( 200, 'Items updated' ) |
| 109 |
->json_is( '/updated_items_count', 2 ) |
| 110 |
->json_is( '/callnumber', $expected_callnumber ) |
| 111 |
->json_is( '/modified_item_ids', \@items_to_update ); |
| 112 |
|
| 113 |
$t->post_ok( "//$userid:$password@/api/v1/rpc/biblios/$biblio_id/items/populate_empty_callnumbers" => json => {} ) |
| 114 |
->status_is( 200, 'Items updated' ) |
| 115 |
->json_is( '/updated_items_count', 0 ); |
| 116 |
|
| 117 |
$t->post_ok( |
| 118 |
"//$userid:$password@/api/v1/rpc/biblios/$biblio_id/items/$item1_id/populate_empty_callnumbers" => json => {} ) |
| 119 |
->status_is( 200, 'Item updated' ) |
| 120 |
->json_is( '/updated_items_count', 0 ); |
| 121 |
|
| 122 |
$item_1->delete(); |
| 123 |
|
| 124 |
$t->post_ok( |
| 125 |
"//$userid:$password@/api/v1/rpc/biblios/$biblio_id/items/$item1_id/populate_empty_callnumbers" => json => {} ) |
| 126 |
->status_is( 404, 'Item not found' ) |
| 127 |
->json_is( '/error' => q{Item not found} ) |
| 128 |
->json_is( '/error_code' => q{not_found} ); |
| 129 |
|
| 130 |
$biblio->delete(); |
| 131 |
|
| 132 |
$t->post_ok( "//$userid:$password@/api/v1/rpc/biblios/$biblio_id/items/populate_empty_callnumbers" => json => {} ) |
| 133 |
->status_is( 404, 'Record not found' ) |
| 134 |
->json_is( '/error' => q{Bibliographic record not found} ) |
| 135 |
->json_is( '/error_code' => q{not_found} ); |
| 136 |
|
| 137 |
$schema->storage->txn_rollback; |
| 138 |
}; |