From 57fc40a5ba4e5c58474849161b1f4e6e7902edea Mon Sep 17 00:00:00 2001
From: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Date: Fri, 17 Jul 2020 08:01:06 +0100
Subject: [PATCH] Bug 25998: Add Unit Tests

---
 t/db_dependent/Koha/Account/Line.t | 44 +++++++++++++++++++++++++++++-
 1 file changed, 43 insertions(+), 1 deletion(-)

diff --git a/t/db_dependent/Koha/Account/Line.t b/t/db_dependent/Koha/Account/Line.t
index 0b35a590c6..fc4901ca54 100644
--- a/t/db_dependent/Koha/Account/Line.t
+++ b/t/db_dependent/Koha/Account/Line.t
@@ -19,7 +19,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 12;
+use Test::More tests => 13;
 use Test::Exception;
 use Test::MockModule;
 
@@ -105,6 +105,48 @@ subtest 'item() tests' => sub {
     $schema->storage->txn_rollback;
 };
 
+subtest 'library() tests' => sub {
+
+    plan tests => 4;
+
+    $schema->storage->txn_begin;
+
+    my $library = $builder->build_object( { class => 'Koha::Libraries' } );
+    my $patron  = $builder->build( { source => 'Borrower' } );
+
+    my $line = Koha::Account::Line->new(
+        {
+            borrowernumber  => $patron->{borrowernumber},
+            branchcode      => $library->branchcode,
+            debit_type_code => "OVERDUE",
+            status          => "RETURNED",
+            amount          => 10,
+            interface       => 'commandline',
+        }
+    )->store;
+
+    my $account_line_library = $line->library;
+    is( ref($account_line_library),
+        'Koha::Library',
+        'Koha::Account::Line->library should return a Koha::Library' );
+    is(
+        $line->branchcode,
+        $account_line_library->branchcode,
+        'Koha::Account::Line->library should return the correct library'
+    );
+
+    # Test ON DELETE SET NULL
+    $library->delete;
+    my $found = Koha::Account::Lines->find( $line->accountlines_id );
+    ok( $found, "Koha::Account::Line not deleted when the linked library is deleted" );
+
+    is( $found->library, undef,
+'Koha::Account::Line->library should return undef if linked library has been deleted'
+    );
+
+    $schema->storage->txn_rollback;
+};
+
 subtest 'is_credit() and is_debit() tests' => sub {
 
     plan tests => 4;
-- 
2.20.1