From 19638bed6e3e53c3049754b0c80e863b4d9085d2 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Fri, 24 Sep 2021 12:00:21 -0300 Subject: [PATCH] Bug 29110: Unit tests --- t/db_dependent/ArticleRequests.t | 25 +--- t/db_dependent/Koha/ArticleRequest.t | 178 +++++++++++++++++++++++++++ 2 files changed, 179 insertions(+), 24 deletions(-) create mode 100755 t/db_dependent/Koha/ArticleRequest.t diff --git a/t/db_dependent/ArticleRequests.t b/t/db_dependent/ArticleRequests.t index d740008172..c1e16d9424 100644 --- a/t/db_dependent/ArticleRequests.t +++ b/t/db_dependent/ArticleRequests.t @@ -19,7 +19,7 @@ use Modern::Perl; use POSIX qw(strftime); -use Test::More tests => 37; +use Test::More tests => 36; use Test::MockModule; use t::lib::TestBuilder; @@ -208,26 +208,3 @@ subtest 'may_article_request' => sub { }; $schema->storage->txn_rollback(); - -subtest 'set_pending() tests' => sub { - - plan tests => 2; - - $schema->storage->txn_begin; - - my $ar_mock = Test::MockModule->new('Koha::ArticleRequest'); - $ar_mock->mock( 'notify', sub { ok( 1, '->notify() called' ); } ); - - my $ar = $builder->build_object( - { - class => 'Koha::ArticleRequests', - value => { status => Koha::ArticleRequest::Status::Requested } - } - ); - - $ar->set_pending()->discard_changes; - - is( $ar->status, Koha::ArticleRequest::Status::Pending ); - - $schema->storage->txn_rollback; -}; diff --git a/t/db_dependent/Koha/ArticleRequest.t b/t/db_dependent/Koha/ArticleRequest.t new file mode 100755 index 0000000000..cb057ff7c7 --- /dev/null +++ b/t/db_dependent/Koha/ArticleRequest.t @@ -0,0 +1,178 @@ +#!/usr/bin/perl + +# This file is part of Koha +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Test::More tests => 5; +use Test::Exception; +use Test::MockModule; + +use Koha::ArticleRequest::Status; +use Koha::ArticleRequests; + +use t::lib::TestBuilder; + +my $schema = Koha::Database->new->schema; +my $builder = t::lib::TestBuilder->new; + +subtest 'store() tests' => sub { + + plan tests => 8; + + $schema->storage->txn_begin; + + my $mock_article_request = Test::MockModule->new('Koha::ArticleRequest'); + my $notify_called; + $mock_article_request->mock( 'notify', sub { $notify_called = 1; } ); + + my $item = $builder->build_sample_item; + my $article_request = Koha::ArticleRequest->new( + { itemnumber => $item->id, biblionumber => $item->biblionumber } ); + + throws_ok { $article_request->store; } + 'Koha::Exceptions::MissingParameter', + 'Exception thrown because of missing borrowernumber parameter'; + + my $deleted_patron = $builder->build_object( { class => 'Koha::Patrons' } ); + my $deleted_patron_id = $deleted_patron->id; + $deleted_patron->delete; + + # use the invalid patron to trigger fk exception + $article_request->borrowernumber($deleted_patron_id); + + throws_ok { $article_request->store; } + 'Koha::Exceptions::Object::FKConstraint', + 'Exception thrown because of missing borrowernumber parameter'; + + is( $@->broken_fk, 'borrowernumber', 'Exception build correctly (1/2)' ); + is( $@->value, $deleted_patron_id, 'Exception build correctly (1/2)' ); + + my $patron_can_request = 0; + + my $mocked_patron = Test::MockModule->new('Koha::Patron'); + $mocked_patron->mock( 'can_request_article', sub { return $patron_can_request; } ); + + my $patron = $builder->build_object({ class => 'Koha::Patrons' }); + $article_request->borrowernumber( $patron->id ); + + throws_ok + { $article_request->store; } + 'Koha::Exceptions::ArticleRequest::LimitReached', + 'Exception thrown because patron reached its limit'; + + # allow the patron to place requests + $patron_can_request = 1; + $article_request->store->discard_changes; + + is( $article_request->status, Koha::ArticleRequest::Status::Requested, 'Status set correctly' ); + isnt( $article_request->created_on, undef, 'created_on set correctly' ); + ok( $notify_called, 'Notify was called' ); + + $schema->storage->txn_rollback; +}; + +subtest 'set_pending() tests' => sub { + + plan tests => 2; + + $schema->storage->txn_begin; + + my $ar_mock = Test::MockModule->new('Koha::ArticleRequest'); + $ar_mock->mock( 'notify', sub { ok( 1, '->notify() called' ); } ); + + my $ar = $builder->build_object( + { + class => 'Koha::ArticleRequests', + value => { status => Koha::ArticleRequest::Status::Requested } + } + ); + + $ar->set_pending()->discard_changes; + + is( $ar->status, Koha::ArticleRequest::Status::Pending ); + + $schema->storage->txn_rollback; +}; + +subtest 'request() tests' => sub { + + plan tests => 2; + + $schema->storage->txn_begin; + + my $ar_mock = Test::MockModule->new('Koha::ArticleRequest'); + $ar_mock->mock( 'notify', sub { ok( 1, '->notify() called' ); } ); + + my $ar = $builder->build_object( + { + class => 'Koha::ArticleRequests', + value => { status => Koha::ArticleRequest::Status::Pending } + } + ); + + $ar->request()->discard_changes; + + is( $ar->status, Koha::ArticleRequest::Status::Requested ); + + $schema->storage->txn_rollback; +}; + +subtest 'process() tests' => sub { + + plan tests => 2; + + $schema->storage->txn_begin; + + my $ar_mock = Test::MockModule->new('Koha::ArticleRequest'); + $ar_mock->mock( 'notify', sub { ok( 1, '->notify() called' ); } ); + + my $ar = $builder->build_object( + { + class => 'Koha::ArticleRequests', + value => { status => Koha::ArticleRequest::Status::Requested } + } + ); + + $ar->process()->discard_changes; + + is( $ar->status, Koha::ArticleRequest::Status::Processing ); + + $schema->storage->txn_rollback; +}; + +subtest 'complete() tests' => sub { + + plan tests => 2; + + $schema->storage->txn_begin; + + my $ar_mock = Test::MockModule->new('Koha::ArticleRequest'); + $ar_mock->mock( 'notify', sub { ok( 1, '->notify() called' ); } ); + + my $ar = $builder->build_object( + { + class => 'Koha::ArticleRequests', + value => { status => Koha::ArticleRequest::Status::Requested } + } + ); + + $ar->complete()->discard_changes; + + is( $ar->status, Koha::ArticleRequest::Status::Completed ); + + $schema->storage->txn_rollback; +}; -- 2.30.2