From a968e5165d474edf4bd5282e8dbc9e9e18c54ab0 Mon Sep 17 00:00:00 2001
From: Tomas Cohen Arazi <tomascohen@theke.io>
Date: Wed, 22 Sep 2021 16:11:14 -0300
Subject: [PATCH] Bug 29084: Unit tests

This patch adds missing tests for Koha::Biblio->article_requests and
reorganizes (and extends) the tests for 'article_requests_current' and
'article_requests_finished' that were originally in ArticleRequests.t
into Koha/Biblio.t as we do now.

To test:
1. Apply this patch
2. Run:
   $ kshell
  k$ prove t/db_dependent/ArticleRequests.t \
           t/db_dependent/Koha/Biblio.t
=> SUCCESS: Tests pass!

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
---
 t/db_dependent/ArticleRequests.t |  24 +-----
 t/db_dependent/Koha/Biblio.t     | 122 ++++++++++++++++++++++++++++++-
 2 files changed, 122 insertions(+), 24 deletions(-)

diff --git a/t/db_dependent/ArticleRequests.t b/t/db_dependent/ArticleRequests.t
index cb25c00b95..d740008172 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 => 46;
+use Test::More tests => 37;
 use Test::MockModule;
 
 use t::lib::TestBuilder;
@@ -110,28 +110,6 @@ is( $article_request->biblio->id,   $biblio->id, '$ar->biblio() gets correspondi
 is( $article_request->item->id,     $item->id,   '$ar->item() gets corresponding Koha::Item object' );
 is( $article_request->borrower->id, $patron->id, '$ar->borrower() gets corresponding Koha::Patron object' );
 
-my $ar = $biblio->article_requests();
-is( ref($ar),      'Koha::ArticleRequests', '$biblio->article_requests returns Koha::ArticleRequests object' );
-is( $ar->next->id, $article_request->id,    'Returned article request matches' );
-
-is( $biblio->article_requests_current()->count(), 1, 'Open request returned for article_requests_current' );
-$article_request->process();
-is( $biblio->article_requests_current()->count(), 1, 'Processing request returned for article_requests_current' );
-$article_request->complete();
-is( $biblio->article_requests_current()->count(), 0, 'Completed request not returned for article_requests_current' );
-$article_request->cancel();
-is( $biblio->article_requests_current()->count(), 0, 'Canceled request not returned for article_requests_current' );
-
-$article_request->status(Koha::ArticleRequest::Status::Pending);
-$article_request->store();
-
-is( $biblio->article_requests_finished()->count(), 0, 'Open request returned for article_requests_finished' );
-$article_request->process();
-is( $biblio->article_requests_finished()->count(), 0, 'Processing request returned for article_requests_finished' );
-$article_request->complete();
-$article_request->cancel();
-is( $biblio->article_requests_finished()->count(), 1, 'Canceled request not returned for article_requests_finished' );
-
 my $rule = Koha::CirculationRules->set_rule(
     {
         categorycode => undef,
diff --git a/t/db_dependent/Koha/Biblio.t b/t/db_dependent/Koha/Biblio.t
index 83de0634c6..8e92dccad3 100755
--- a/t/db_dependent/Koha/Biblio.t
+++ b/t/db_dependent/Koha/Biblio.t
@@ -17,7 +17,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 14;
+use Test::More tests => 17;
 
 use C4::Biblio qw( AddBiblio ModBiblio );
 use Koha::Database;
@@ -637,3 +637,123 @@ subtest 'get_marc_notes() UNIMARC tests' => sub {
 
     $schema->storage->txn_rollback;
 };
+
+subtest 'article_requests() tests' => sub {
+
+    plan tests => 3;
+
+    $schema->storage->txn_begin;
+
+    my $item   = $builder->build_sample_item;
+    my $biblio = $item->biblio;
+
+    my $article_requests = $biblio->article_requests;
+    is( ref($article_requests), 'Koha::ArticleRequests',
+        'In scalar context, type is correct' );
+    is( $article_requests->count, 0, 'No article requests' );
+
+    foreach my $i ( 0 .. 3 ) {
+
+        my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
+
+        Koha::ArticleRequest->new(
+            {
+                borrowernumber => $patron->id,
+                biblionumber   => $biblio->id,
+                itemnumber     => $item->id,
+                title          => $biblio->title,
+            }
+        )->request;
+    }
+
+    $article_requests = $biblio->article_requests;
+    is( $article_requests->count, 4, '4 article requests' );
+
+    $schema->storage->txn_rollback;
+};
+
+subtest 'article_requests_current() tests' => sub {
+
+    plan tests => 7;
+
+    $schema->storage->txn_begin;
+
+    my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
+    my $item   = $builder->build_sample_item;
+    my $biblio = $item->biblio;
+
+    my $article_request = Koha::ArticleRequest->new(
+        {
+            borrowernumber => $patron->id,
+            biblionumber   => $biblio->id,
+            itemnumber     => $item->id,
+            title          => $biblio->title,
+        }
+    )->request();
+
+    my $ar = $biblio->article_requests();
+    is( ref($ar), 'Koha::ArticleRequests',
+        '$biblio->article_requests returns Koha::ArticleRequests object' );
+    is( $ar->next->id, $article_request->id,
+        'Returned article request matches' );
+
+    is( $biblio->article_requests_current()->count(),
+        1, 'Open request returned for article_requests_current' );
+    $article_request->set_pending();
+    is( $biblio->article_requests_current()->count(),
+        1, 'Pending request returned for article_requests_current' );
+    $article_request->process();
+    is( $biblio->article_requests_current()->count(),
+        1, 'Processing request returned for article_requests_current' );
+    $article_request->complete();
+    is( $biblio->article_requests_current()->count(),
+        0, 'Completed request not returned for article_requests_current' );
+    $article_request->cancel();
+    is( $biblio->article_requests_current()->count(),
+        0, 'Canceled request not returned for article_requests_current' );
+
+    $schema->storage->txn_rollback;
+};
+
+subtest 'article_requests_finished() tests' => sub {
+
+    plan tests => 7;
+
+    $schema->storage->txn_begin;
+
+    my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
+    my $item   = $builder->build_sample_item;
+    my $biblio = $item->biblio;
+
+    my $article_request = Koha::ArticleRequest->new(
+        {
+            borrowernumber => $patron->id,
+            biblionumber   => $biblio->id,
+            itemnumber     => $item->id,
+            title          => $biblio->title,
+        }
+    )->request();
+
+    my $ar = $biblio->article_requests();
+    is( ref($ar), 'Koha::ArticleRequests',
+        '$biblio->article_requests returns Koha::ArticleRequests object' );
+    is( $ar->next->id, $article_request->id,
+        'Returned article request matches' );
+
+    is( $biblio->article_requests_finished->count,
+        0, 'Open request returned for article_requests_finished' );
+    $article_request->set_pending();
+    is( $biblio->article_requests_finished->count,
+        0, 'Pending request returned for article_requests_finished' );
+    $article_request->process();
+    is( $biblio->article_requests_finished->count,
+        0, 'Processing request returned for article_requests_finished' );
+    $article_request->complete();
+    is( $biblio->article_requests_finished->count,
+        1, 'Completed request returned for article_requests_finished' );
+    $article_request->cancel();
+    is( $biblio->article_requests_finished->count,
+        1, 'Cancelled request not returned for article_requests_finished' );
+
+    $schema->storage->txn_rollback;
+};
-- 
2.30.2