From 81c25dc0be7cb8f2e50b65b17d6c17b51dfda1cc Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Wed, 18 Oct 2023 11:50:56 -0300 Subject: [PATCH] Bug 30708: Tidy perl files Signed-off-by: Tomas Cohen Arazi --- Koha/Exceptions/Preservation.pm | 4 +- Koha/Preservation/Processing.pm | 19 +- Koha/Preservation/Train.pm | 24 +- Koha/Preservation/Train/Item.pm | 8 +- Koha/Preservation/Train/Item/Attribute.pm | 4 +- Koha/REST/V1/Preservation.pm | 2 + Koha/REST/V1/Preservation/Processings.pm | 72 ++--- Koha/REST/V1/Preservation/Trains.pm | 120 ++++---- Koha/REST/V1/Preservation/WaitingList.pm | 31 +-- preservation/home.pl | 10 +- .../Koha/Preservation/Processings.t | 19 +- t/db_dependent/Koha/Preservation/Trains.t | 36 ++- .../api/v1/preservation_Processings.t | 133 ++++----- t/db_dependent/api/v1/preservation_Trains.t | 259 ++++++++---------- .../api/v1/preservation_WaitingList.t | 56 ++-- 15 files changed, 345 insertions(+), 452 deletions(-) diff --git a/Koha/Exceptions/Preservation.pm b/Koha/Exceptions/Preservation.pm index 56407656f4..fd4adf87ad 100644 --- a/Koha/Exceptions/Preservation.pm +++ b/Koha/Exceptions/Preservation.pm @@ -58,9 +58,9 @@ sub full_message { my $msg = $self->message; - unless ( $msg ) { + unless ($msg) { if ( $self->isa('Koha::Exceptions::Preservation::MissingSettings') ) { - $msg = sprintf("The following parameter settings is required: %s", $self->parameter ); + $msg = sprintf( "The following parameter settings is required: %s", $self->parameter ); } } diff --git a/Koha/Preservation/Processing.pm b/Koha/Preservation/Processing.pm index 5fe6d2b85b..fd7692571a 100644 --- a/Koha/Preservation/Processing.pm +++ b/Koha/Preservation/Processing.pm @@ -44,22 +44,18 @@ Set or return the attributes for this processing sub attributes { my ( $self, $attributes ) = @_; - if ( $attributes ) { + if ($attributes) { my $schema = $self->_result->result_source->schema; $schema->txn_do( sub { my @existing_ids = map { $_->{processing_attribute_id} || () } @$attributes; - if (@existing_ids || !@$attributes) { + if ( @existing_ids || !@$attributes ) { $self->attributes->search( { ( # If no attributes passed we delete all the existing ones @$attributes - ? ( - processing_attribute_id => { - -not_in => \@existing_ids - } - ) + ? ( processing_attribute_id => { -not_in => \@existing_ids } ) : () ) } @@ -71,7 +67,7 @@ sub attributes { if ($existing_attribute) { $existing_attribute->set($attribute)->store; } else { - $self->_result ->add_to_preservation_processing_attributes( $attribute ); + $self->_result->add_to_preservation_processing_attributes($attribute); } } } @@ -92,10 +88,9 @@ Note that we do not enforce that in ->delete, the callers are supposed to deal w sub can_be_deleted { my ($self) = @_; - my $trains_using_it = Koha::Preservation::Trains->search( - { default_processing_id => $self->processing_id } )->count; - my $items_using_it = Koha::Preservation::Train::Items->search( - { processing_id => $self->processing_id } )->count; + my $trains_using_it = + Koha::Preservation::Trains->search( { default_processing_id => $self->processing_id } )->count; + my $items_using_it = Koha::Preservation::Train::Items->search( { processing_id => $self->processing_id } )->count; return ( $trains_using_it || $items_using_it ) ? 0 : 1; } diff --git a/Koha/Preservation/Train.pm b/Koha/Preservation/Train.pm index 46abf54682..9cd20d3309 100644 --- a/Koha/Preservation/Train.pm +++ b/Koha/Preservation/Train.pm @@ -46,7 +46,7 @@ Return the default processing object for this train =cut sub default_processing { - my ( $self ) = @_; + my ($self) = @_; my $rs = $self->_result->default_processing; return unless $rs; return Koha::Preservation::Processing->_new_from_dbic($rs); @@ -77,13 +77,11 @@ sub add_item { Koha::Exceptions::Preservation::ItemNotFound->throw unless $item; Koha::Exceptions::Preservation::ItemNotInWaitingList->throw - if !$skip_waiting_list_check && $item->notforloan != $not_for_loan; + if !$skip_waiting_list_check && $item->notforloan != $not_for_loan; my $already_in_train = Koha::Preservation::Train::Items->search( { item_id => $train_item->{item_id}, 'train.received_on' => undef }, - { - join => 'train' - } + { join => 'train' } ); if ( $already_in_train->count ) { my $train_id = $already_in_train->next->train_id; @@ -93,14 +91,14 @@ sub add_item { # FIXME We need a LOCK here # Not important for now as we have add_items # Note that there are several other places in Koha with this max+1 problem - my $max = $self->items->search->_resultset->get_column("user_train_item_id")->max || 0; + my $max = $self->items->search->_resultset->get_column("user_train_item_id")->max || 0; my $train_item_object = Koha::Preservation::Train::Item->new( { - train_id => $self->train_id, - item_id => $item->itemnumber, - processing_id => $train_item->{processing_id} || $self->default_processing_id, + train_id => $self->train_id, + item_id => $item->itemnumber, + processing_id => $train_item->{processing_id} || $self->default_processing_id, user_train_item_id => $max + 1, - added_on => \'NOW()', + added_on => \'NOW()', } )->store; $item->notforloan( $self->not_for_loan )->store; @@ -121,7 +119,7 @@ sub add_items { for my $train_item (@$train_items) { try { my $added_item = $self->add_item($train_item); - $added_item->attributes($train_item->{attributes}); + $added_item->attributes( $train_item->{attributes} ); push @added_items, $added_item; } catch { @@ -142,9 +140,9 @@ Return the items in this train. =cut sub items { - my ( $self ) = @_; + my ($self) = @_; my $items_rs = $self->_result->preservation_trains_items; - return Koha::Preservation::Train::Items->_new_from_dbic($items_rs) + return Koha::Preservation::Train::Items->_new_from_dbic($items_rs); } =head2 Internal methods diff --git a/Koha/Preservation/Train/Item.pm b/Koha/Preservation/Train/Item.pm index 39cab4356a..413e18410b 100644 --- a/Koha/Preservation/Train/Item.pm +++ b/Koha/Preservation/Train/Item.pm @@ -45,8 +45,8 @@ Return the processing object for this item =cut sub processing { - my ( $self ) = @_; - my $rs = $self->_result->processing; # FIXME Should we return train's default processing if there is no specific? + my ($self) = @_; + my $rs = $self->_result->processing; # FIXME Should we return train's default processing if there is no specific? return Koha::Preservation::Processing->_new_from_dbic($rs); } @@ -57,7 +57,7 @@ Return the catalogue item object for this train item =cut sub catalogue_item { - my ( $self ) = @_; + my ($self) = @_; my $item_rs = $self->_result->item; return Koha::Item->_new_from_dbic($item_rs); } @@ -71,7 +71,7 @@ Getter and setter for the attributes sub attributes { my ( $self, $attributes ) = @_; - if ( $attributes ) { + if ($attributes) { my $schema = $self->_result->result_source->schema; $schema->txn_do( sub { diff --git a/Koha/Preservation/Train/Item/Attribute.pm b/Koha/Preservation/Train/Item/Attribute.pm index 1618dc582e..4b9474efe4 100644 --- a/Koha/Preservation/Train/Item/Attribute.pm +++ b/Koha/Preservation/Train/Item/Attribute.pm @@ -45,9 +45,9 @@ Return the Koha::Preservation::Processing::Attribute object =cut sub processing_attribute { - my ( $self ) = @_; + my ($self) = @_; my $processing_attribute_rs = $self->_result->processing_attribute; - return Koha::Preservation::Processing::Attribute->_new_from_dbic($processing_attribute_rs) + return Koha::Preservation::Processing::Attribute->_new_from_dbic($processing_attribute_rs); } =head3 strings_map diff --git a/Koha/REST/V1/Preservation.pm b/Koha/REST/V1/Preservation.pm index 1bc2ca4f0a..48f24a09b0 100644 --- a/Koha/REST/V1/Preservation.pm +++ b/Koha/REST/V1/Preservation.pm @@ -39,7 +39,9 @@ Return the configuration options needed for the Preservation Vue app sub config { my $c = shift->openapi->valid_input or return; + my $patron = $c->stash('koha.user'); + return $c->render( status => 200, openapi => { diff --git a/Koha/REST/V1/Preservation/Processings.pm b/Koha/REST/V1/Preservation/Processings.pm index 1c0b4f385f..0aeafebf56 100644 --- a/Koha/REST/V1/Preservation/Processings.pm +++ b/Koha/REST/V1/Preservation/Processings.pm @@ -23,6 +23,7 @@ use Koha::Preservation::Processings; use Scalar::Util qw( blessed ); use Try::Tiny; + =head1 API =head2 Methods @@ -36,10 +37,9 @@ sub list { return try { my $processings_set = Koha::Preservation::Processings->new; - my $processings = $c->objects->search( $processings_set ); + my $processings = $c->objects->search($processings_set); return $c->render( status => 200, openapi => $processings ); - } - catch { + } catch { $c->unhandled_exception($_); }; } @@ -68,8 +68,7 @@ sub get { status => 200, openapi => $processing ); - } - catch { + } catch { $c->unhandled_exception($_); }; } @@ -94,15 +93,14 @@ sub add { my $processing = Koha::Preservation::Processing->new_from_api($body)->store; $processing->attributes($attributes); - $c->res->headers->location($c->req->url->to_string . '/' . $processing->processing_id); + $c->res->headers->location( $c->req->url->to_string . '/' . $processing->processing_id ); return $c->render( status => 201, openapi => $processing->to_api ); } ); - } - catch { + } catch { my $to_api_mapping = Koha::Preservation::Processing->new->to_api_mapping; @@ -112,28 +110,17 @@ sub add { status => 409, openapi => { error => $_->error, conflict => $_->duplicate_id } ); - } - elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) { + } elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) { return $c->render( status => 400, - openapi => { - error => "Given " - . $to_api_mapping->{ $_->broken_fk } - . " does not exist" - } + openapi => { error => "Given " . $to_api_mapping->{ $_->broken_fk } . " does not exist" } ); - } - elsif ( $_->isa('Koha::Exceptions::BadParameter') ) { + } elsif ( $_->isa('Koha::Exceptions::BadParameter') ) { return $c->render( status => 400, - openapi => { - error => "Given " - . $to_api_mapping->{ $_->parameter } - . " does not exist" - } + openapi => { error => "Given " . $to_api_mapping->{ $_->parameter } . " does not exist" } ); - } - elsif ( $_->isa('Koha::Exceptions::PayloadTooLarge') ) { + } elsif ( $_->isa('Koha::Exceptions::PayloadTooLarge') ) { return $c->render( status => 413, openapi => { error => $_->error } @@ -155,7 +142,7 @@ sub update { my $c = shift->openapi->valid_input or return; my $processing_id = $c->param('processing_id'); - my $processing = Koha::Preservation::Processings->find( $processing_id ); + my $processing = Koha::Preservation::Processings->find($processing_id); unless ($processing) { return $c->render( @@ -175,40 +162,28 @@ sub update { $processing->set_from_api($body)->store; $processing->attributes($attributes); - - $c->res->headers->location($c->req->url->to_string . '/' . $processing->processing_id); + $c->res->headers->location( $c->req->url->to_string . '/' . $processing->processing_id ); return $c->render( status => 200, openapi => $processing->to_api ); } ); - } - catch { + } catch { my $to_api_mapping = Koha::Preservation::Processing->new->to_api_mapping; if ( blessed $_ ) { if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) { return $c->render( status => 400, - openapi => { - error => "Given " - . $to_api_mapping->{ $_->broken_fk } - . " does not exist" - } + openapi => { error => "Given " . $to_api_mapping->{ $_->broken_fk } . " does not exist" } ); - } - elsif ( $_->isa('Koha::Exceptions::BadParameter') ) { + } elsif ( $_->isa('Koha::Exceptions::BadParameter') ) { return $c->render( status => 400, - openapi => { - error => "Given " - . $to_api_mapping->{ $_->parameter } - . " does not exist" - } + openapi => { error => "Given " . $to_api_mapping->{ $_->parameter } . " does not exist" } ); - } - elsif ( $_->isa('Koha::Exceptions::PayloadTooLarge') ) { + } elsif ( $_->isa('Koha::Exceptions::PayloadTooLarge') ) { return $c->render( status => 413, openapi => { error => $_->error } @@ -218,7 +193,7 @@ sub update { $c->unhandled_exception($_); }; -}; +} =head3 delete @@ -228,7 +203,7 @@ sub delete { my $c = shift->openapi->valid_input or return; my $processing_id = $c->param('processing_id'); - my $processing = Koha::Preservation::Processings->find( $processing_id ); + my $processing = Koha::Preservation::Processings->find($processing_id); unless ($processing) { return $c->render( status => 404, @@ -236,9 +211,9 @@ sub delete { ); } - unless ($processing->can_be_deleted) { + unless ( $processing->can_be_deleted ) { return $c->render( - status => 409, + status => 409, openapi => { error => "Processing is already used" }, ); } @@ -248,8 +223,7 @@ sub delete { status => 204, openapi => q{} ); - } - catch { + } catch { $c->unhandled_exception($_); }; } diff --git a/Koha/REST/V1/Preservation/Trains.pm b/Koha/REST/V1/Preservation/Trains.pm index 45b03716b3..49326b0a09 100644 --- a/Koha/REST/V1/Preservation/Trains.pm +++ b/Koha/REST/V1/Preservation/Trains.pm @@ -40,10 +40,9 @@ sub list { return try { my $trains_set = Koha::Preservation::Trains->new; - my $trains = $c->objects->search( $trains_set ); + my $trains = $c->objects->search($trains_set); return $c->render( status => 200, openapi => $trains ); - } - catch { + } catch { $c->unhandled_exception($_); }; } @@ -72,8 +71,7 @@ sub get { status => 200, openapi => $train ); - } - catch { + } catch { $c->unhandled_exception($_); }; } @@ -95,15 +93,14 @@ sub add { my $train = Koha::Preservation::Train->new_from_api($body)->store; - $c->res->headers->location($c->req->url->to_string . '/' . $train->train_id); + $c->res->headers->location( $c->req->url->to_string . '/' . $train->train_id ); return $c->render( status => 201, openapi => $train->to_api ); } ); - } - catch { + } catch { my $to_api_mapping = Koha::Preservation::Train->new->to_api_mapping; @@ -113,28 +110,17 @@ sub add { status => 409, openapi => { error => $_->error, conflict => $_->duplicate_id } ); - } - elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) { + } elsif ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) { return $c->render( status => 400, - openapi => { - error => "Given " - . $to_api_mapping->{ $_->broken_fk } - . " does not exist" - } + openapi => { error => "Given " . $to_api_mapping->{ $_->broken_fk } . " does not exist" } ); - } - elsif ( $_->isa('Koha::Exceptions::BadParameter') ) { + } elsif ( $_->isa('Koha::Exceptions::BadParameter') ) { return $c->render( status => 400, - openapi => { - error => "Given " - . $to_api_mapping->{ $_->parameter } - . " does not exist" - } + openapi => { error => "Given " . $to_api_mapping->{ $_->parameter } . " does not exist" } ); - } - elsif ( $_->isa('Koha::Exceptions::PayloadTooLarge') ) { + } elsif ( $_->isa('Koha::Exceptions::PayloadTooLarge') ) { return $c->render( status => 413, openapi => { error => $_->error } @@ -156,7 +142,7 @@ sub update { my $c = shift->openapi->valid_input or return; my $train_id = $c->param('train_id'); - my $train = Koha::Preservation::Trains->find( $train_id ); + my $train = Koha::Preservation::Trains->find($train_id); unless ($train) { return $c->render( @@ -173,39 +159,28 @@ sub update { $train->set_from_api($body)->store; - $c->res->headers->location($c->req->url->to_string . '/' . $train->train_id); + $c->res->headers->location( $c->req->url->to_string . '/' . $train->train_id ); return $c->render( status => 200, openapi => $train->to_api ); } ); - } - catch { + } catch { my $to_api_mapping = Koha::Preservation::Train->new->to_api_mapping; if ( blessed $_ ) { if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) { return $c->render( status => 400, - openapi => { - error => "Given " - . $to_api_mapping->{ $_->broken_fk } - . " does not exist" - } + openapi => { error => "Given " . $to_api_mapping->{ $_->broken_fk } . " does not exist" } ); - } - elsif ( $_->isa('Koha::Exceptions::BadParameter') ) { + } elsif ( $_->isa('Koha::Exceptions::BadParameter') ) { return $c->render( status => 400, - openapi => { - error => "Given " - . $to_api_mapping->{ $_->parameter } - . " does not exist" - } + openapi => { error => "Given " . $to_api_mapping->{ $_->parameter } . " does not exist" } ); - } - elsif ( $_->isa('Koha::Exceptions::PayloadTooLarge') ) { + } elsif ( $_->isa('Koha::Exceptions::PayloadTooLarge') ) { return $c->render( status => 413, openapi => { error => $_->error } @@ -215,7 +190,7 @@ sub update { $c->unhandled_exception($_); }; -}; +} =head3 delete @@ -240,8 +215,7 @@ sub delete { status => 204, openapi => q{} ); - } - catch { + } catch { $c->unhandled_exception($_); }; } @@ -256,7 +230,7 @@ sub get_item { my $c = shift->openapi->valid_input or return; my $train_id = $c->param('train_id'); - my $train = Koha::Preservation::Trains->find( $train_id ); + my $train = Koha::Preservation::Trains->find($train_id); unless ($train) { return $c->render( @@ -267,7 +241,10 @@ sub get_item { my $train_item_id = $c->param('train_item_id'); - my $train_item = $c->objects->find(Koha::Preservation::Train::Items->search, { train_item_id => $train_item_id, train_id => $train_id }); + my $train_item = $c->objects->find( + Koha::Preservation::Train::Items->search, + { train_item_id => $train_item_id, train_id => $train_id } + ); unless ($train_item) { return $c->render( @@ -282,8 +259,7 @@ sub get_item { return $c->render( status => 200, openapi => $train_item ); } ); - } - catch { + } catch { $c->unhandled_exception($_); }; } @@ -298,7 +274,7 @@ sub add_items { my $c = shift->openapi->valid_input or return; my $train_id = $c->param('train_id'); - my $train = Koha::Preservation::Trains->find( $train_id ); + my $train = Koha::Preservation::Trains->find($train_id); unless ($train) { return $c->render( @@ -315,8 +291,7 @@ sub add_items { return $c->render( status => 201, openapi => $train_items ); } ); - } - catch { + } catch { if ( blessed $_ ) { if ( $_->isa('Koha::Exceptions::Preservation::MissingSettings') ) { return $c->render( @@ -340,7 +315,7 @@ sub add_item { my $c = shift->openapi->valid_input or return; my $train_id = $c->param('train_id'); - my $train = Koha::Preservation::Trains->find( $train_id ); + my $train = Koha::Preservation::Trains->find($train_id); unless ($train) { return $c->render( @@ -357,10 +332,9 @@ sub add_item { my $train_item = $train->add_item($body); $train_item->attributes($attributes); return $c->render( status => 201, openapi => $train_item ); - } + } ); - } - catch { + } catch { if ( blessed $_ ) { if ( $_->isa('Koha::Exceptions::Preservation::MissingSettings') ) { return $c->render( @@ -409,7 +383,7 @@ sub copy_item { my $c = shift->openapi->valid_input or return; my $train_id = $c->param('train_id'); - my $train = Koha::Preservation::Trains->find( $train_id ); + my $train = Koha::Preservation::Trains->find($train_id); unless ($train) { return $c->render( @@ -420,7 +394,8 @@ sub copy_item { my $train_item_id = $c->param('train_item_id'); - my $train_item = Koha::Preservation::Train::Items->search({ train_item_id => $train_item_id, train_id => $train_id })->single; + my $train_item = + Koha::Preservation::Train::Items->search( { train_item_id => $train_item_id, train_id => $train_id } )->single; unless ($train_item) { return $c->render( @@ -434,7 +409,7 @@ sub copy_item { Koha::Database->new->schema->txn_do( sub { my $new_train_id = delete $body->{train_id}; - my $new_train = Koha::Preservation::Trains->find( $new_train_id ); + my $new_train = Koha::Preservation::Trains->find($new_train_id); unless ($train) { return $c->render( status => 404, @@ -454,17 +429,16 @@ sub copy_item { map { { processing_attribute_id => $_->processing_attribute_id, - train_item_id => $new_train_item->train_item_id, - value => $_->value + train_item_id => $new_train_item->train_item_id, + value => $_->value } } $train_item->attributes->as_list - ]; + ]; $new_train_item->attributes($attributes); return $c->render( status => 201, openapi => $train_item ); - } + } ); - } - catch { + } catch { if ( blessed $_ ) { if ( $_->isa('Koha::Exceptions::Preservation::MissingSettings') ) { return $c->render( @@ -513,7 +487,7 @@ sub update_item { my $c = shift->openapi->valid_input or return; my $train_id = $c->param('train_id'); - my $train = Koha::Preservation::Trains->find( $train_id ); + my $train = Koha::Preservation::Trains->find($train_id); unless ($train) { return $c->render( @@ -524,7 +498,8 @@ sub update_item { my $train_item_id = $c->param('train_item_id'); - my $train_item = Koha::Preservation::Train::Items->search({ train_item_id => $train_item_id, train_id => $train_id })->single; + my $train_item = + Koha::Preservation::Train::Items->search( { train_item_id => $train_item_id, train_id => $train_id } )->single; unless ($train_item) { return $c->render( @@ -542,15 +517,13 @@ sub update_item { $train_item->set_from_api($body)->store; $train_item->attributes($attributes); return $c->render( status => 200, openapi => $train_item ); - } + } ); - } - catch { + } catch { $c->unhandled_exception($_); }; } - =head3 remove_item Controller function that handles removing an item from a train @@ -561,7 +534,7 @@ sub remove_item { my $c = shift->openapi->valid_input or return; my $train_id = $c->param('train_id'); - my $train = Koha::Preservation::Trains->find( $train_id ); + my $train = Koha::Preservation::Trains->find($train_id); unless ($train) { return $c->render( @@ -588,8 +561,7 @@ sub remove_item { status => 204, openapi => q{} ); - } - catch { + } catch { $c->unhandled_exception($_); }; } diff --git a/Koha/REST/V1/Preservation/WaitingList.pm b/Koha/REST/V1/Preservation/WaitingList.pm index e43eab3ed9..a909f02f1c 100644 --- a/Koha/REST/V1/Preservation/WaitingList.pm +++ b/Koha/REST/V1/Preservation/WaitingList.pm @@ -41,16 +41,16 @@ sub list { return try { my $not_for_loan = C4::Context->preference('PreservationNotForLoanWaitingListIn'); - Koha::Exceptions::Preservation::MissingSettings->throw( parameter => 'PreservationNotForLoanWaitingListIn' ) unless $not_for_loan; + Koha::Exceptions::Preservation::MissingSettings->throw( parameter => 'PreservationNotForLoanWaitingListIn' ) + unless $not_for_loan; - my $items_set = Koha::Items->new->search({ notforloan => $not_for_loan }); - my $items = $c->objects->search( $items_set ); + my $items_set = Koha::Items->new->search( { notforloan => $not_for_loan } ); + my $items = $c->objects->search($items_set); return $c->render( status => 200, openapi => $items ); - } - catch { + } catch { if ( blessed $_ ) { if ( $_->isa('Koha::Exceptions::Preservation::MissingSettings') ) { return $c->render( @@ -74,30 +74,31 @@ sub add_items { my $c = shift->openapi->valid_input or return; return try { - my $body = $c->req->json; + my $body = $c->req->json; my $new_not_for_loan = C4::Context->preference('PreservationNotForLoanWaitingListIn'); - Koha::Exceptions::Preservation::MissingSettings->throw( parameter => 'PreservationNotForLoanWaitingListIn' ) unless $new_not_for_loan; + Koha::Exceptions::Preservation::MissingSettings->throw( parameter => 'PreservationNotForLoanWaitingListIn' ) + unless $new_not_for_loan; my @items; - for my $item_id ( @$body ) { + for my $item_id (@$body) { try { while ( my ( $k, $v ) = each %$item_id ) { my $key = $k eq 'item_id' ? 'itemnumber' : 'barcode'; my $item = Koha::Items->find( { $key => $v } ); - next unless $item; # FIXME Must return a multi-status response 207 + next unless $item; # FIXME Must return a multi-status response 207 if ( $item->notforloan != $new_not_for_loan ) { my $already_in_train = Koha::Preservation::Train::Items->search( { - item_id => $item->itemnumber, + item_id => $item->itemnumber, 'train.received_on' => undef, }, { join => 'train', } )->count; - if ( $already_in_train ) { + if ($already_in_train) { Koha::Exceptions::Preservation::ItemAlreadyInTrain->throw; } @@ -110,8 +111,7 @@ sub add_items { }; } return $c->render( status => 201, openapi => \@items ); - } - catch { + } catch { $c->unhandled_exception($_); }; } @@ -138,7 +138,7 @@ sub remove_item { } my $not_for_loan_waiting_list_in = C4::Context->preference('PreservationNotForLoanWaitingListIn'); - if ($item->notforloan ne $not_for_loan_waiting_list_in) { + if ( $item->notforloan ne $not_for_loan_waiting_list_in ) { unless ($item) { return $c->render( status => 404, @@ -153,8 +153,7 @@ sub remove_item { status => 204, openapi => q{} ); - } - catch { + } catch { $c->unhandled_exception($_); }; } diff --git a/preservation/home.pl b/preservation/home.pl index af66337c78..0e096c531a 100755 --- a/preservation/home.pl +++ b/preservation/home.pl @@ -17,8 +17,8 @@ use Modern::Perl; -use CGI qw ( -utf8 ); -use C4::Auth qw( get_template_and_user ); +use CGI qw ( -utf8 ); +use C4::Auth qw( get_template_and_user ); use C4::Output qw( output_html_with_http_headers ); use Koha::Database::Columns; @@ -39,13 +39,13 @@ $template->param( map { my $table = $_; map { ( $table . '.' . $_ => $columns->{$table}->{$_} ) } - keys %{ $columns->{$table} } + keys %{ $columns->{$table} } } qw( biblio biblioitems items ) }, api_mappings => { - items => Koha::Item->to_api_mapping, + items => Koha::Item->to_api_mapping, biblioitems => Koha::Biblioitem->to_api_mapping, - biblio => Koha::Biblio->to_api_mapping, + biblio => Koha::Biblio->to_api_mapping, }, ); diff --git a/t/db_dependent/Koha/Preservation/Processings.t b/t/db_dependent/Koha/Preservation/Processings.t index 62e8542b05..2c60173f0d 100755 --- a/t/db_dependent/Koha/Preservation/Processings.t +++ b/t/db_dependent/Koha/Preservation/Processings.t @@ -63,7 +63,7 @@ subtest 'can_be_deleted' => sub { $schema->storage->txn_begin; - my $processing = $builder->build_object( { class => 'Koha::Preservation::Processings' } ); + my $processing = $builder->build_object( { class => 'Koha::Preservation::Processings' } ); my $another_processing = $builder->build_object( { class => 'Koha::Preservation::Processings' } ); is( $processing->can_be_deleted, 1, 'processing is not used, it can be deleted' ); @@ -72,21 +72,24 @@ subtest 'can_be_deleted' => sub { { class => 'Koha::Preservation::Trains', value => { - not_for_loan => 42, + not_for_loan => 42, default_processing_id => $processing->processing_id, - closed_on => undef, - sent_on => undef, - received_on => undef + closed_on => undef, + sent_on => undef, + received_on => undef } } ); - is( $processing->can_be_deleted, 0, 'processing is used, it cannot be deleted' ); + is( $processing->can_be_deleted, 0, 'processing is used, it cannot be deleted' ); is( $another_processing->can_be_deleted, 1, 'processing is not used, it can be deleted' ); my $item = $builder->build_sample_item; - $train->add_item( { item_id => $item->itemnumber, processing_id => $another_processing->processing_id }, { skip_waiting_list_check => 1 } ); - is( $processing->can_be_deleted, 0, 'processing is used, it cannot be deleted' ); + $train->add_item( + { item_id => $item->itemnumber, processing_id => $another_processing->processing_id }, + { skip_waiting_list_check => 1 } + ); + is( $processing->can_be_deleted, 0, 'processing is used, it cannot be deleted' ); is( $another_processing->can_be_deleted, 0, 'processing is used, it cannot be deleted' ); $schema->storage->txn_rollback; diff --git a/t/db_dependent/Koha/Preservation/Trains.t b/t/db_dependent/Koha/Preservation/Trains.t index 7702875e94..b441eeee65 100755 --- a/t/db_dependent/Koha/Preservation/Trains.t +++ b/t/db_dependent/Koha/Preservation/Trains.t @@ -40,11 +40,15 @@ subtest 'default_processing' => sub { my $processing = $builder->build_object( { class => 'Koha::Preservation::Processings' } ); my $another_processing = $builder->build_object( { class => 'Koha::Preservation::Processings' } ); - my $train = $builder->build_object( { class => 'Koha::Preservation::Trains', value => { default_processing_id => $processing->processing_id } } ); + my $train = $builder->build_object( + { class => 'Koha::Preservation::Trains', value => { default_processing_id => $processing->processing_id } } ); my $default_processing = $train->default_processing; - is( ref($default_processing), 'Koha::Preservation::Processing', '->default_processing returns a Koha::Preservation::Processing object' ); - is( $default_processing->processing_id, $processing->processing_id, 'correct processing is returned' ); + is( + ref($default_processing), 'Koha::Preservation::Processing', + '->default_processing returns a Koha::Preservation::Processing object' + ); + is( $default_processing->processing_id, $processing->processing_id, 'correct processing is returned' ); $processing->delete; $default_processing = $train->get_from_storage->default_processing; is( $default_processing, undef, 'deleting the processing does not delete the train' ); @@ -59,7 +63,7 @@ subtest 'add_items & items' => sub { my $not_for_loan_waiting_list_in = 24; my $not_for_loan_train_in = 42; - my $train = $builder->build_object( + my $train = $builder->build_object( { class => 'Koha::Preservation::Trains', value => { @@ -74,19 +78,26 @@ subtest 'add_items & items' => sub { my $item_2 = $builder->build_sample_item; my $item_3 = $builder->build_sample_item; - $builder->build_object( { class => 'Koha::AuthorisedValues', value => { category => 'NOT_LOAN', authorised_value => $not_for_loan_waiting_list_in } } ); + $builder->build_object( + { + class => 'Koha::AuthorisedValues', + value => { category => 'NOT_LOAN', authorised_value => $not_for_loan_waiting_list_in } + } + ); $item_1->notforloan($not_for_loan_waiting_list_in)->store; $item_2->notforloan(0)->store; # item_2 is not in the waiting list $item_3->notforloan($not_for_loan_waiting_list_in)->store; t::lib::Mocks::mock_preference( 'PreservationNotForLoanWaitingListIn', $not_for_loan_waiting_list_in ); warning_is { - $train->add_items( [ { item_id => $item_1->itemnumber }, { item_id => $item_2->itemnumber }, { barcode => $item_3->barcode } ] ); + $train->add_items( + [ { item_id => $item_1->itemnumber }, { item_id => $item_2->itemnumber }, { barcode => $item_3->barcode } ] + ); } 'Item not added to train: [Cannot add item to train, it is not in the waiting list]'; my $items_train = $train->items; is( $items_train->count, 2, '2 items added to the train' ); - my $item_train_1 = $items_train->find( { item_id => $item_1->itemnumber } ); - my $item_train_3 = $items_train->find( { item_id => $item_3->itemnumber } ); + my $item_train_1 = $items_train->find( { item_id => $item_1->itemnumber } ); + my $item_train_3 = $items_train->find( { item_id => $item_3->itemnumber } ); my $catalogue_item_1 = $item_train_1->catalogue_item; is( ref($catalogue_item_1), 'Koha::Item' ); is( $catalogue_item_1->notforloan, $not_for_loan_train_in ); @@ -101,7 +112,8 @@ subtest 'add_items & items' => sub { warning_is { $train->add_item( { item_id => $item_2->itemnumber }, { skip_waiting_list_check => 1 } ); - } ''; + } + ''; is( $train->items->count, 3, 'the item has been added to the train' ); is( $item_2->get_from_storage->notforloan, $not_for_loan_train_in ); @@ -121,12 +133,14 @@ subtest 'add_items & items' => sub { throws_ok { $another_train->add_item( { item_id => $item_1->itemnumber }, { skip_waiting_list_check => 1 } ); - } 'Koha::Exceptions::Preservation::ItemAlreadyInAnotherTrain'; + } + 'Koha::Exceptions::Preservation::ItemAlreadyInAnotherTrain'; my $item_4 = $builder->build_sample_item; throws_ok { $train->add_item( { item_id => $item_4->itemnumber } ); - } 'Koha::Exceptions::Preservation::CannotAddItemToClosedTrain'; + } + 'Koha::Exceptions::Preservation::CannotAddItemToClosedTrain'; $schema->storage->txn_rollback; }; diff --git a/t/db_dependent/api/v1/preservation_Processings.t b/t/db_dependent/api/v1/preservation_Processings.t index f0d86aec10..6a5533fc5f 100755 --- a/t/db_dependent/api/v1/preservation_Processings.t +++ b/t/db_dependent/api/v1/preservation_Processings.t @@ -62,8 +62,7 @@ subtest 'list() tests' => sub { ## Authorized user tests # No processings, so empty array should be returned - $t->get_ok("//$userid:$password@/api/v1/preservation/processings")->status_is(200) - ->json_is( [] ); + $t->get_ok("//$userid:$password@/api/v1/preservation/processings")->status_is(200)->json_is( [] ); my $processing = $builder->build_object( { @@ -73,11 +72,10 @@ subtest 'list() tests' => sub { # One processing created, should get returned $t->get_ok("//$userid:$password@/api/v1/preservation/processings")->status_is(200) - ->json_is( [ $processing->to_api ] ); + ->json_is( [ $processing->to_api ] ); # Unauthorized access - $t->get_ok("//$unauth_userid:$password@/api/v1/preservation/processings") - ->status_is(403); + $t->get_ok("//$unauth_userid:$password@/api/v1/preservation/processings")->status_is(403); $schema->storage->txn_rollback; }; @@ -88,8 +86,7 @@ subtest 'get() tests' => sub { $schema->storage->txn_begin; - my $processing = - $builder->build_object( { class => 'Koha::Preservation::Processings' } ); + my $processing = $builder->build_object( { class => 'Koha::Preservation::Processings' } ); my $attributes = [ { name => 'color', @@ -124,27 +121,25 @@ subtest 'get() tests' => sub { my $unauth_userid = $patron->userid; # This processing exists, should get returned - $t->get_ok( "//$userid:$password@/api/v1/preservation/processings/" - . $processing->processing_id )->status_is(200) - ->json_is( $processing->to_api ); + $t->get_ok( "//$userid:$password@/api/v1/preservation/processings/" . $processing->processing_id )->status_is(200) + ->json_is( $processing->to_api ); # Return one processing with attributes $t->get_ok( "//$userid:$password@/api/v1/preservation/processings/" - . $processing->processing_id => {'x-koha-embed' => 'attributes'} )->status_is(200) - ->json_is( { %{ $processing->to_api }, attributes => $attributes->to_api }); + . $processing->processing_id => { 'x-koha-embed' => 'attributes' } )->status_is(200) + ->json_is( { %{ $processing->to_api }, attributes => $attributes->to_api } ); # Unauthorized access - $t->get_ok( "//$unauth_userid:$password@/api/v1/preservation/processings/" - . $processing->processing_id )->status_is(403); + $t->get_ok( "//$unauth_userid:$password@/api/v1/preservation/processings/" . $processing->processing_id ) + ->status_is(403); # Attempt to get non-existent processing - my $processing_to_delete = - $builder->build_object( { class => 'Koha::Preservation::Processings' } ); - my $non_existent_id = $processing_to_delete->processing_id; + my $processing_to_delete = $builder->build_object( { class => 'Koha::Preservation::Processings' } ); + my $non_existent_id = $processing_to_delete->processing_id; $processing_to_delete->delete; - $t->get_ok("//$userid:$password@/api/v1/preservation/processings/$non_existent_id") - ->status_is(404)->json_is( '/error' => 'Processing not found' ); + $t->get_ok("//$userid:$password@/api/v1/preservation/processings/$non_existent_id")->status_is(404) + ->json_is( '/error' => 'Processing not found' ); $schema->storage->txn_rollback; }; @@ -175,59 +170,54 @@ subtest 'add() tests' => sub { $patron->set_password( { password => $password, skip_validation => 1 } ); my $unauth_userid = $patron->userid; - my $default_processing = $builder->build_object({class => 'Koha::Preservation::Processings'}); - my $processing = { - name => "processing name", + my $default_processing = $builder->build_object( { class => 'Koha::Preservation::Processings' } ); + my $processing = { + name => "processing name", }; # Unauthorized attempt to write - $t->post_ok( "//$unauth_userid:$password@/api/v1/preservation/processings" => json => - $processing )->status_is(403); + $t->post_ok( "//$unauth_userid:$password@/api/v1/preservation/processings" => json => $processing )->status_is(403); # Authorized attempt to write invalid data my $processing_with_invalid_field = { - blah => "processing Blah", + blah => "processing Blah", %$processing, }; - $t->post_ok( "//$userid:$password@/api/v1/preservation/processings" => json => - $processing_with_invalid_field )->status_is(400)->json_is( + $t->post_ok( "//$userid:$password@/api/v1/preservation/processings" => json => $processing_with_invalid_field ) + ->status_is(400)->json_is( "/errors" => [ { message => "Properties not allowed: blah.", path => "/body" } ] - ); + ); # Authorized attempt to write my $processing_id = - $t->post_ok( - "//$userid:$password@/api/v1/preservation/processings" => json => $processing ) - ->status_is( 201, 'SWAGGER3.2.1' )->header_like( + $t->post_ok( "//$userid:$password@/api/v1/preservation/processings" => json => $processing ) + ->status_is( 201, 'SWAGGER3.2.1' )->header_like( Location => qr|^/api/v1/preservation/processings/\d*|, 'SWAGGER3.4.1' - )->json_is( '/name' => $processing->{name} ) - ->tx->res->json->{processing_id}; + )->json_is( '/name' => $processing->{name} )->tx->res->json->{processing_id}; # Authorized attempt to create with null id $processing->{processing_id} = undef; - $t->post_ok( - "//$userid:$password@/api/v1/preservation/processings" => json => $processing ) - ->status_is(400)->json_has('/errors'); + $t->post_ok( "//$userid:$password@/api/v1/preservation/processings" => json => $processing )->status_is(400) + ->json_has('/errors'); # Authorized attempt to create with existing id $processing->{processing_id} = $processing_id; - $t->post_ok( - "//$userid:$password@/api/v1/preservation/processings" => json => $processing ) - ->status_is(400)->json_is( + $t->post_ok( "//$userid:$password@/api/v1/preservation/processings" => json => $processing )->status_is(400) + ->json_is( "/errors" => [ { message => "Read-only.", path => "/body/processing_id" } ] - ); + ); $schema->storage->txn_rollback; }; @@ -258,67 +248,58 @@ subtest 'update() tests' => sub { $patron->set_password( { password => $password, skip_validation => 1 } ); my $unauth_userid = $patron->userid; - my $processing_id = - $builder->build_object( { class => 'Koha::Preservation::Processings' } )->processing_id; + my $processing_id = $builder->build_object( { class => 'Koha::Preservation::Processings' } )->processing_id; # Unauthorized attempt to update - $t->put_ok( - "//$unauth_userid:$password@/api/v1/preservation/processings/$processing_id" => - json => { name => 'New unauthorized name change' } )->status_is(403); + $t->put_ok( "//$unauth_userid:$password@/api/v1/preservation/processings/$processing_id" => json => + { name => 'New unauthorized name change' } )->status_is(403); # Attempt partial update on a PUT - my $processing_with_missing_field = { - }; + my $processing_with_missing_field = {}; - $t->put_ok( - "//$userid:$password@/api/v1/preservation/processings/$processing_id" => json => - $processing_with_missing_field )->status_is(400) - ->json_is( "/errors" => - [ { message => "Missing property.", path => "/body/name" } ] ); + $t->put_ok( "//$userid:$password@/api/v1/preservation/processings/$processing_id" => json => + $processing_with_missing_field )->status_is(400) + ->json_is( "/errors" => [ { message => "Missing property.", path => "/body/name" } ] ); + + my $default_processing = $builder->build_object( { class => 'Koha::Preservation::Processings' } ); - my $default_processing = $builder->build_object({class => 'Koha::Preservation::Processings'}); # Full object update on PUT my $processing_with_updated_field = { name => "New name", }; - $t->put_ok( - "//$userid:$password@/api/v1/preservation/processings/$processing_id" => json => - $processing_with_updated_field )->status_is(200) - ->json_is( '/name' => 'New name' ); + $t->put_ok( "//$userid:$password@/api/v1/preservation/processings/$processing_id" => json => + $processing_with_updated_field )->status_is(200)->json_is( '/name' => 'New name' ); # Authorized attempt to write invalid data my $processing_with_invalid_field = { - blah => "processing Blah", + blah => "processing Blah", %$processing_with_updated_field, }; - $t->put_ok( - "//$userid:$password@/api/v1/preservation/processings/$processing_id" => json => - $processing_with_invalid_field )->status_is(400)->json_is( + $t->put_ok( "//$userid:$password@/api/v1/preservation/processings/$processing_id" => json => + $processing_with_invalid_field )->status_is(400)->json_is( "/errors" => [ { message => "Properties not allowed: blah.", path => "/body" } ] - ); + ); # Attempt to update non-existent processing - my $processing_to_delete = - $builder->build_object( { class => 'Koha::Preservation::Processings' } ); - my $non_existent_id = $processing_to_delete->processing_id; + my $processing_to_delete = $builder->build_object( { class => 'Koha::Preservation::Processings' } ); + my $non_existent_id = $processing_to_delete->processing_id; $processing_to_delete->delete; - $t->put_ok( "//$userid:$password@/api/v1/preservation/processings/$non_existent_id" => - json => $processing_with_updated_field )->status_is(404); + $t->put_ok( "//$userid:$password@/api/v1/preservation/processings/$non_existent_id" => json => + $processing_with_updated_field )->status_is(404); # Wrong method (POST) $processing_with_updated_field->{processing_id} = 2; - $t->post_ok( - "//$userid:$password@/api/v1/preservation/processings/$processing_id" => json => - $processing_with_updated_field )->status_is(404); + $t->post_ok( "//$userid:$password@/api/v1/preservation/processings/$processing_id" => json => + $processing_with_updated_field )->status_is(404); $schema->storage->txn_rollback; }; @@ -349,21 +330,17 @@ subtest 'delete() tests' => sub { $patron->set_password( { password => $password, skip_validation => 1 } ); my $unauth_userid = $patron->userid; - my $processing_id = - $builder->build_object( { class => 'Koha::Preservation::Processings' } )->processing_id; + my $processing_id = $builder->build_object( { class => 'Koha::Preservation::Processings' } )->processing_id; # Unauthorized attempt to delete - $t->delete_ok( - "//$unauth_userid:$password@/api/v1/preservation/processings/$processing_id") - ->status_is(403); + $t->delete_ok("//$unauth_userid:$password@/api/v1/preservation/processings/$processing_id")->status_is(403); # Delete existing processing $t->delete_ok("//$userid:$password@/api/v1/preservation/processings/$processing_id") - ->status_is( 204, 'SWAGGER3.2.4' )->content_is( '', 'SWAGGER3.3.4' ); + ->status_is( 204, 'SWAGGER3.2.4' )->content_is( '', 'SWAGGER3.3.4' ); # Attempt to delete non-existent processing - $t->delete_ok("//$userid:$password@/api/v1/preservation/processings/$processing_id") - ->status_is(404); + $t->delete_ok("//$userid:$password@/api/v1/preservation/processings/$processing_id")->status_is(404); $schema->storage->txn_rollback; }; diff --git a/t/db_dependent/api/v1/preservation_Trains.t b/t/db_dependent/api/v1/preservation_Trains.t index 136c6d3433..482ddde5a4 100755 --- a/t/db_dependent/api/v1/preservation_Trains.t +++ b/t/db_dependent/api/v1/preservation_Trains.t @@ -63,8 +63,7 @@ subtest 'list() tests' => sub { ## Authorized user tests # No trains, so empty array should be returned - $t->get_ok("//$userid:$password@/api/v1/preservation/trains")->status_is(200) - ->json_is( [] ); + $t->get_ok("//$userid:$password@/api/v1/preservation/trains")->status_is(200)->json_is( [] ); my $train = $builder->build_object( { @@ -73,12 +72,10 @@ subtest 'list() tests' => sub { ); # One train created, should get returned - $t->get_ok("//$userid:$password@/api/v1/preservation/trains")->status_is(200) - ->json_is( [ $train->to_api ] ); + $t->get_ok("//$userid:$password@/api/v1/preservation/trains")->status_is(200)->json_is( [ $train->to_api ] ); # Unauthorized access - $t->get_ok("//$unauth_userid:$password@/api/v1/preservation/trains") - ->status_is(403); + $t->get_ok("//$unauth_userid:$password@/api/v1/preservation/trains")->status_is(403); $schema->storage->txn_rollback; }; @@ -89,10 +86,9 @@ subtest 'get() tests' => sub { $schema->storage->txn_begin; - my $train = - $builder->build_object( { class => 'Koha::Preservation::Trains' } ); + my $train = $builder->build_object( { class => 'Koha::Preservation::Trains' } ); my $default_processing = $train->default_processing; - my $librarian = $builder->build_object( + my $librarian = $builder->build_object( { class => 'Koha::Patrons', value => { flags => 2**30 } @@ -113,32 +109,38 @@ subtest 'get() tests' => sub { my $unauth_userid = $patron->userid; # This train exists, should get returned - $t->get_ok( "//$userid:$password@/api/v1/preservation/trains/" - . $train->train_id )->status_is(200) - ->json_is( $train->to_api ); + $t->get_ok( "//$userid:$password@/api/v1/preservation/trains/" . $train->train_id )->status_is(200) + ->json_is( $train->to_api ); # Return one train with some embeds $t->get_ok( "//$userid:$password@/api/v1/preservation/trains/" - . $train->train_id => {'x-koha-embed' => 'items,default_processing'} )->status_is(200) - ->json_is( { %{ $train->to_api }, items => [], default_processing => $default_processing->unblessed }); + . $train->train_id => { 'x-koha-embed' => 'items,default_processing' } )->status_is(200) + ->json_is( { %{ $train->to_api }, items => [], default_processing => $default_processing->unblessed } ); # Return one train with all embeds - $t->get_ok( "//$userid:$password@/api/v1/preservation/trains/" - . $train->train_id => { 'x-koha-embed' => 'items,items.attributes,items.attributes.processing_attribute,default_processing,default_processing.attributes' } ) - ->status_is(200)->json_is( { %{ $train->to_api }, items => [], default_processing => { %{ $default_processing->unblessed }, attributes => [] } } ); + $t->get_ok( + "//$userid:$password@/api/v1/preservation/trains/" + . $train->train_id => { + 'x-koha-embed' => + 'items,items.attributes,items.attributes.processing_attribute,default_processing,default_processing.attributes' + } + )->status_is(200)->json_is( + { + %{ $train->to_api }, items => [], + default_processing => { %{ $default_processing->unblessed }, attributes => [] } + } + ); # Unauthorized access - $t->get_ok( "//$unauth_userid:$password@/api/v1/preservation/trains/" - . $train->train_id )->status_is(403); + $t->get_ok( "//$unauth_userid:$password@/api/v1/preservation/trains/" . $train->train_id )->status_is(403); # Attempt to get non-existent train - my $train_to_delete = - $builder->build_object( { class => 'Koha::Preservation::Trains' } ); + my $train_to_delete = $builder->build_object( { class => 'Koha::Preservation::Trains' } ); my $non_existent_id = $train_to_delete->train_id; $train_to_delete->delete; - $t->get_ok("//$userid:$password@/api/v1/preservation/trains/$non_existent_id") - ->status_is(404)->json_is( '/error' => 'Train not found' ); + $t->get_ok("//$userid:$password@/api/v1/preservation/trains/$non_existent_id")->status_is(404) + ->json_is( '/error' => 'Train not found' ); $schema->storage->txn_rollback; }; @@ -169,65 +171,58 @@ subtest 'add() tests' => sub { $patron->set_password( { password => $password, skip_validation => 1 } ); my $unauth_userid = $patron->userid; - my $default_processing = $builder->build_object({class => 'Koha::Preservation::Processings'}); - my $train = { - name => "train name", - description => "train description", + my $default_processing = $builder->build_object( { class => 'Koha::Preservation::Processings' } ); + my $train = { + name => "train name", + description => "train description", default_processing_id => $default_processing->processing_id, - not_for_loan => 42, + not_for_loan => 42, }; # Unauthorized attempt to write - $t->post_ok( "//$unauth_userid:$password@/api/v1/preservation/trains" => json => - $train )->status_is(403); + $t->post_ok( "//$unauth_userid:$password@/api/v1/preservation/trains" => json => $train )->status_is(403); # Authorized attempt to write invalid data my $train_with_invalid_field = { - blah => "train Blah", + blah => "train Blah", %$train, }; - $t->post_ok( "//$userid:$password@/api/v1/preservation/trains" => json => - $train_with_invalid_field )->status_is(400)->json_is( + $t->post_ok( "//$userid:$password@/api/v1/preservation/trains" => json => $train_with_invalid_field ) + ->status_is(400)->json_is( "/errors" => [ { message => "Properties not allowed: blah.", path => "/body" } ] - ); + ); # Authorized attempt to write my $train_id = - $t->post_ok( - "//$userid:$password@/api/v1/preservation/trains" => json => $train ) - ->status_is( 201, 'SWAGGER3.2.1' )->header_like( + $t->post_ok( "//$userid:$password@/api/v1/preservation/trains" => json => $train ) + ->status_is( 201, 'SWAGGER3.2.1' )->header_like( Location => qr|^/api/v1/preservation/trains/\d*|, 'SWAGGER3.4.1' - )->json_is( '/name' => $train->{name} ) - ->json_is( '/description' => $train->{description} ) - ->json_is( '/default_processing_id' => $train->{default_processing_id} ) - ->json_is( '/not_for_loan' => $train->{not_for_loan} ) - ->tx->res->json->{train_id}; + )->json_is( '/name' => $train->{name} )->json_is( '/description' => $train->{description} ) + ->json_is( '/default_processing_id' => $train->{default_processing_id} ) + ->json_is( '/not_for_loan' => $train->{not_for_loan} )->tx->res->json->{train_id}; # Authorized attempt to create with null id $train->{train_id} = undef; - $t->post_ok( - "//$userid:$password@/api/v1/preservation/trains" => json => $train ) - ->status_is(400)->json_has('/errors'); + $t->post_ok( "//$userid:$password@/api/v1/preservation/trains" => json => $train )->status_is(400) + ->json_has('/errors'); # Authorized attempt to create with existing id $train->{train_id} = $train_id; - $t->post_ok( - "//$userid:$password@/api/v1/preservation/trains" => json => $train ) - ->status_is(400)->json_is( + $t->post_ok( "//$userid:$password@/api/v1/preservation/trains" => json => $train )->status_is(400)->json_is( "/errors" => [ { message => "Read-only.", path => "/body/train_id" } ] - ); + ); $schema->storage->txn_rollback; }; @@ -258,70 +253,61 @@ subtest 'update() tests' => sub { $patron->set_password( { password => $password, skip_validation => 1 } ); my $unauth_userid = $patron->userid; - my $train_id = - $builder->build_object( { class => 'Koha::Preservation::Trains' } )->train_id; + my $train_id = $builder->build_object( { class => 'Koha::Preservation::Trains' } )->train_id; # Unauthorized attempt to update - $t->put_ok( - "//$unauth_userid:$password@/api/v1/preservation/trains/$train_id" => - json => { name => 'New unauthorized name change' } )->status_is(403); + $t->put_ok( "//$unauth_userid:$password@/api/v1/preservation/trains/$train_id" => json => + { name => 'New unauthorized name change' } )->status_is(403); # Attempt partial update on a PUT - my $train_with_missing_field = { - }; + my $train_with_missing_field = {}; - $t->put_ok( - "//$userid:$password@/api/v1/preservation/trains/$train_id" => json => - $train_with_missing_field )->status_is(400) - ->json_is( "/errors" => - [ { message => "Missing property.", path => "/body/name" } ] ); + $t->put_ok( "//$userid:$password@/api/v1/preservation/trains/$train_id" => json => $train_with_missing_field ) + ->status_is(400)->json_is( "/errors" => [ { message => "Missing property.", path => "/body/name" } ] ); + + my $default_processing = $builder->build_object( { class => 'Koha::Preservation::Processings' } ); - my $default_processing = $builder->build_object({class => 'Koha::Preservation::Processings'}); # Full object update on PUT my $train_with_updated_field = { - name => "New name", - description => "train description", + name => "New name", + description => "train description", default_processing_id => $default_processing->processing_id, - not_for_loan => 42, + not_for_loan => 42, }; - $t->put_ok( - "//$userid:$password@/api/v1/preservation/trains/$train_id" => json => - $train_with_updated_field )->status_is(200) - ->json_is( '/name' => 'New name' ); + $t->put_ok( "//$userid:$password@/api/v1/preservation/trains/$train_id" => json => $train_with_updated_field ) + ->status_is(200)->json_is( '/name' => 'New name' ); # Authorized attempt to write invalid data my $train_with_invalid_field = { - blah => "train Blah", + blah => "train Blah", %$train_with_updated_field, }; - $t->put_ok( - "//$userid:$password@/api/v1/preservation/trains/$train_id" => json => - $train_with_invalid_field )->status_is(400)->json_is( + $t->put_ok( "//$userid:$password@/api/v1/preservation/trains/$train_id" => json => $train_with_invalid_field ) + ->status_is(400)->json_is( "/errors" => [ { message => "Properties not allowed: blah.", path => "/body" } ] - ); + ); # Attempt to update non-existent train - my $train_to_delete = - $builder->build_object( { class => 'Koha::Preservation::Trains' } ); + my $train_to_delete = $builder->build_object( { class => 'Koha::Preservation::Trains' } ); my $non_existent_id = $train_to_delete->train_id; $train_to_delete->delete; - $t->put_ok( "//$userid:$password@/api/v1/preservation/trains/$non_existent_id" => - json => $train_with_updated_field )->status_is(404); + $t->put_ok( + "//$userid:$password@/api/v1/preservation/trains/$non_existent_id" => json => $train_with_updated_field ) + ->status_is(404); # Wrong method (POST) $train_with_updated_field->{train_id} = 2; - $t->post_ok( - "//$userid:$password@/api/v1/preservation/trains/$train_id" => json => - $train_with_updated_field )->status_is(404); + $t->post_ok( "//$userid:$password@/api/v1/preservation/trains/$train_id" => json => $train_with_updated_field ) + ->status_is(404); $schema->storage->txn_rollback; }; @@ -352,21 +338,17 @@ subtest 'delete() tests' => sub { $patron->set_password( { password => $password, skip_validation => 1 } ); my $unauth_userid = $patron->userid; - my $train_id = - $builder->build_object( { class => 'Koha::Preservation::Trains' } )->train_id; + my $train_id = $builder->build_object( { class => 'Koha::Preservation::Trains' } )->train_id; # Unauthorized attempt to delete - $t->delete_ok( - "//$unauth_userid:$password@/api/v1/preservation/trains/$train_id") - ->status_is(403); + $t->delete_ok("//$unauth_userid:$password@/api/v1/preservation/trains/$train_id")->status_is(403); # Delete existing train - $t->delete_ok("//$userid:$password@/api/v1/preservation/trains/$train_id") - ->status_is( 204, 'SWAGGER3.2.4' )->content_is( '', 'SWAGGER3.3.4' ); + $t->delete_ok("//$userid:$password@/api/v1/preservation/trains/$train_id")->status_is( 204, 'SWAGGER3.2.4' ) + ->content_is( '', 'SWAGGER3.3.4' ); # Attempt to delete non-existent train - $t->delete_ok("//$userid:$password@/api/v1/preservation/trains/$train_id") - ->status_is(404); + $t->delete_ok("//$userid:$password@/api/v1/preservation/trains/$train_id")->status_is(404); $schema->storage->txn_rollback; }; @@ -390,13 +372,13 @@ subtest '*_item() tests' => sub { my $not_for_loan_waiting_list_in = 24; my $not_for_loan_train_in = 42; - t::lib::Mocks::mock_preference( 'PreservationNotForLoanWaitingListIn', - $not_for_loan_waiting_list_in ); + t::lib::Mocks::mock_preference( + 'PreservationNotForLoanWaitingListIn', + $not_for_loan_waiting_list_in + ); - my $default_processing = - $builder->build_object( { class => 'Koha::Preservation::Processings' } ); - my $another_processing = - $builder->build_object( { class => 'Koha::Preservation::Processings' } ); + my $default_processing = $builder->build_object( { class => 'Koha::Preservation::Processings' } ); + my $another_processing = $builder->build_object( { class => 'Koha::Preservation::Processings' } ); my $attributes = [ { @@ -411,12 +393,9 @@ subtest '*_item() tests' => sub { }, ]; my $processing_attributes = $default_processing->attributes($attributes); - my $color_attribute = - $processing_attributes->search( { name => 'color' } )->next; - my $title_attribute = - $processing_attributes->search( { name => 'title' } )->next; - my $height_attribute = - $processing_attributes->search( { name => 'height' } )->next; + my $color_attribute = $processing_attributes->search( { name => 'color' } )->next; + my $title_attribute = $processing_attributes->search( { name => 'title' } )->next; + my $height_attribute = $processing_attributes->search( { name => 'height' } )->next; my $train = $builder->build_object( { @@ -438,9 +417,8 @@ subtest '*_item() tests' => sub { # Add item not in waiting list $t->post_ok( - "//$userid:$password@/api/v1/preservation/trains/$train_id/items" => - json => { item_id => $item_1->itemnumber } )->status_is(400) - ->json_is( { error => 'Item not in waiting list' } ); + "//$userid:$password@/api/v1/preservation/trains/$train_id/items" => json => { item_id => $item_1->itemnumber } + )->status_is(400)->json_is( { error => 'Item not in waiting list' } ); $item_1->notforloan($not_for_loan_waiting_list_in)->store; @@ -448,97 +426,86 @@ subtest '*_item() tests' => sub { my $item_attributes = [ { processing_attribute_id => $color_attribute->processing_attribute_id, - value => 'red' + value => 'red' }, { processing_attribute_id => $title_attribute->processing_attribute_id, - value => 'my title' + value => 'my title' }, ]; my $train_item_id = - $t->post_ok( - "//$userid:$password@/api/v1/preservation/trains/$train_id/items" => - json => - { item_id => $item_1->itemnumber, attributes => $item_attributes } ) - ->status_is( 201, 'SWAGGER3.2.1' ) - ->json_is( '/item_id' => $item_1->itemnumber ) - ->json_is( '/processing_id' => $train->default_processing_id ) - ->json_has('/added_on')->tx->res->json->{train_item_id}; + $t->post_ok( "//$userid:$password@/api/v1/preservation/trains/$train_id/items" => json => + { item_id => $item_1->itemnumber, attributes => $item_attributes } )->status_is( 201, 'SWAGGER3.2.1' ) + ->json_is( '/item_id' => $item_1->itemnumber )->json_is( '/processing_id' => $train->default_processing_id ) + ->json_has('/added_on')->tx->res->json->{train_item_id}; my $train_item = Koha::Preservation::Train::Items->find($train_item_id); - $t->get_ok( - "//$userid:$password@/api/v1/preservation/trains/$train_id/items/$train_item_id" - => { 'x-koha-embed' => 'attributes' } )->status_is(200)->json_is( + $t->get_ok( "//$userid:$password@/api/v1/preservation/trains/$train_id/items/$train_item_id" => + { 'x-koha-embed' => 'attributes' } )->status_is(200)->json_is( { %{ $train_item->to_api }, attributes => $train_item->attributes->to_api } - ); + ); - is( $item_1->get_from_storage->notforloan, + is( + $item_1->get_from_storage->notforloan, $train->not_for_loan, - "Item not for loan has been set to train's not for loan" ); + "Item not for loan has been set to train's not for loan" + ); # Add deleted item $item_2->delete; $t->post_ok( - "//$userid:$password@/api/v1/preservation/trains/$train_id/items" => - json => { item_id => $item_2->itemnumber } )->status_is(404) - ->json_is( { error => 'Item not found' } ); + "//$userid:$password@/api/v1/preservation/trains/$train_id/items" => json => { item_id => $item_2->itemnumber } + )->status_is(404)->json_is( { error => 'Item not found' } ); # batch add items # Nothing added (FIXME maybe not 201?) warning_is { - $t->post_ok( - "//$userid:$password@/api/v1/preservation/trains/$train_id/items/batch" - => json => [ { item_id => $item_3->itemnumber } ] ) - ->status_is(201)->json_is( [] ); + $t->post_ok( "//$userid:$password@/api/v1/preservation/trains/$train_id/items/batch" => json => + [ { item_id => $item_3->itemnumber } ] )->status_is(201)->json_is( [] ); } 'Item not added to train: [Cannot add item to train, it is not in the waiting list]'; $item_3->notforloan($not_for_loan_waiting_list_in)->store; - $t->post_ok( - "//$userid:$password@/api/v1/preservation/trains/$train_id/items/batch" - => json => [ { item_id => $item_3->itemnumber } ] )->status_is(201) - ->json_is( '/0/item_id' => $item_3->itemnumber ) - ->json_is( '/0/processing_id' => $train->default_processing_id ) - ->json_has('/0/added_on'); + $t->post_ok( "//$userid:$password@/api/v1/preservation/trains/$train_id/items/batch" => json => + [ { item_id => $item_3->itemnumber } ] )->status_is(201)->json_is( '/0/item_id' => $item_3->itemnumber ) + ->json_is( '/0/processing_id' => $train->default_processing_id )->json_has('/0/added_on'); # Update item my $new_item_attributes = [ { processing_attribute_id => $title_attribute->processing_attribute_id, - value => 'my new title' + value => 'my new title' }, { processing_attribute_id => $height_attribute->processing_attribute_id, - value => '24cm' + value => '24cm' }, ]; $t->put_ok( - "//$userid:$password@/api/v1/preservation/trains/$train_id/items/$train_item_id" - => json => { + "//$userid:$password@/api/v1/preservation/trains/$train_id/items/$train_item_id" => json => { item_id => $item_1->itemnumber, attributes => $new_item_attributes, - } + } )->status_is(200); - $t->get_ok( - "//$userid:$password@/api/v1/preservation/trains/$train_id/items/$train_item_id" - => { 'x-koha-embed' => 'attributes' } )->status_is(200)->json_is( + $t->get_ok( "//$userid:$password@/api/v1/preservation/trains/$train_id/items/$train_item_id" => + { 'x-koha-embed' => 'attributes' } )->status_is(200)->json_is( { %{ $train_item->to_api }, attributes => $train_item->attributes->to_api } - ); + ); # Delete existing item $t->delete_ok("//$userid:$password@/api/v1/preservation/trains/$train_id/items/$train_item_id") - ->status_is( 204, 'SWAGGER3.2.4' )->content_is( '', 'SWAGGER3.3.4' ); + ->status_is( 204, 'SWAGGER3.2.4' )->content_is( '', 'SWAGGER3.3.4' ); # Delete non existing item - $t->delete_ok("//$userid:$password@/api/v1/preservation/trains/$train_id/items/$train_item_id") - ->status_is(404)->json_is( '/error' => 'Train item not found' ); + $t->delete_ok("//$userid:$password@/api/v1/preservation/trains/$train_id/items/$train_item_id")->status_is(404) + ->json_is( '/error' => 'Train item not found' ); $schema->storage->txn_rollback; }; diff --git a/t/db_dependent/api/v1/preservation_WaitingList.t b/t/db_dependent/api/v1/preservation_WaitingList.t index 4575b3c6b0..c29c0d7f1f 100755 --- a/t/db_dependent/api/v1/preservation_WaitingList.t +++ b/t/db_dependent/api/v1/preservation_WaitingList.t @@ -55,28 +55,27 @@ subtest 'add_item, list, remove_item tests' => sub { my $item_3 = $builder->build_sample_item; t::lib::Mocks::mock_preference( 'PreservationNotForLoanWaitingListIn', undef ); - $t->get_ok("//$userid:$password@/api/v1/preservation/waiting-list/items") - ->status_is(400)->json_is( + $t->get_ok("//$userid:$password@/api/v1/preservation/waiting-list/items")->status_is(400)->json_is( { error => 'MissingSettings', parameter => 'PreservationNotForLoanWaitingListIn' } - ); + ); my $not_for_loan_waiting_list_in = 24; - t::lib::Mocks::mock_preference( 'PreservationNotForLoanWaitingListIn', - $not_for_loan_waiting_list_in ); + t::lib::Mocks::mock_preference( + 'PreservationNotForLoanWaitingListIn', + $not_for_loan_waiting_list_in + ); - $t->get_ok("//$userid:$password@/api/v1/preservation/waiting-list/items") - ->status_is(200)->json_is( [] ); + $t->get_ok("//$userid:$password@/api/v1/preservation/waiting-list/items")->status_is(200)->json_is( [] ); # Add items $t->post_ok( - "//$userid:$password@/api/v1/preservation/waiting-list/items" => - json => [ + "//$userid:$password@/api/v1/preservation/waiting-list/items" => json => [ { item_id => $item_1->itemnumber }, { barcode => $item_3->barcode } - ] + ] )->status_is(201)->json_is( [ { item_id => $item_1->itemnumber }, @@ -89,22 +88,21 @@ subtest 'add_item, list, remove_item tests' => sub { is( $item_3->get_from_storage->notforloan, $not_for_loan_waiting_list_in ); $t->post_ok( - "//$userid:$password@/api/v1/preservation/waiting-list/items" => - json => [ + "//$userid:$password@/api/v1/preservation/waiting-list/items" => json => [ { item_id => $item_2->itemnumber }, { barcode => $item_3->barcode } - ] + ] )->status_is(201)->json_is( [ { item_id => $item_2->itemnumber } ] ); is( $item_1->get_from_storage->notforloan, $not_for_loan_waiting_list_in ); is( $item_2->get_from_storage->notforloan, $not_for_loan_waiting_list_in ); is( $item_3->get_from_storage->notforloan, $not_for_loan_waiting_list_in ); - $t->delete_ok( - "//$userid:$password@/api/v1/preservation/waiting-list/items/" - . $item_2->itemnumber )->status_is(204); + $t->delete_ok( "//$userid:$password@/api/v1/preservation/waiting-list/items/" . $item_2->itemnumber ) + ->status_is(204); - is( $item_2->get_from_storage->notforloan, 0, + is( + $item_2->get_from_storage->notforloan, 0, "Item removed from the waiting list has its notforloan status back to 0" ); @@ -124,10 +122,8 @@ subtest 'add_item, list, remove_item tests' => sub { # And try to add it to the waiting list warning_like { - $t->post_ok( - "//$userid:$password@/api/v1/preservation/waiting-list/items" => - json => [ { item_id => $item_1->itemnumber } ] )->status_is(201) - ->json_is( [] ); + $t->post_ok( "//$userid:$password@/api/v1/preservation/waiting-list/items" => json => + [ { item_id => $item_1->itemnumber } ] )->status_is(201)->json_is( [] ); } qr[Cannot add item to waiting list, it is already in a non-received train]; @@ -147,21 +143,17 @@ subtest 'add_item, list, remove_item tests' => sub { $train->received_on(dt_from_string)->store; $t->post_ok( - "//$userid:$password@/api/v1/preservation/waiting-list/items" => - json => [ { item_id => $item_3->itemnumber } ] )->status_is(201) - ->json_is( [ { item_id => $item_3->itemnumber } ] ); + "//$userid:$password@/api/v1/preservation/waiting-list/items" => json => [ { item_id => $item_3->itemnumber } ] + )->status_is(201)->json_is( [ { item_id => $item_3->itemnumber } ] ); - $t->delete_ok( - "//$userid:$password@/api/v1/preservation/waiting-list/items/" - . $item_2->itemnumber )->status_is(204); + $t->delete_ok( "//$userid:$password@/api/v1/preservation/waiting-list/items/" . $item_2->itemnumber ) + ->status_is(204); $item_2->delete; - $t->delete_ok("//$userid:$password@/api/v1/preservation/waiting-list/items") - ->status_is(404); + $t->delete_ok("//$userid:$password@/api/v1/preservation/waiting-list/items")->status_is(404); - $t->delete_ok( - "//$userid:$password@/api/v1/preservation/waiting-list/items/" - . $item_2->itemnumber )->status_is(404); + $t->delete_ok( "//$userid:$password@/api/v1/preservation/waiting-list/items/" . $item_2->itemnumber ) + ->status_is(404); $schema->storage->txn_rollback; }; -- 2.39.5