From 21fbdc0d1e2026325f132ef10edb1977a1f7fbb9 Mon Sep 17 00:00:00 2001
From: Josef Moravec <josef.moravec@gmail.com>
Date: Mon, 24 Sep 2018 07:43:53 +0000
Subject: [PATCH] Bug 21401: Add tests for transaction_library

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

diff --git a/t/db_dependent/Koha/Account.t b/t/db_dependent/Koha/Account.t
index 21f0e62..c5d1dfc 100755
--- a/t/db_dependent/Koha/Account.t
+++ b/t/db_dependent/Koha/Account.t
@@ -19,7 +19,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 3;
+use Test::More tests => 4;
 
 use Koha::Account;
 use Koha::Account::Lines;
@@ -192,3 +192,44 @@ subtest 'add_credit() tests' => sub {
 
     $schema->storage->txn_rollback;
 };
+
+subtest 'transaction library' => sub {
+
+    plan tests => 3;
+
+    $schema->storage->txn_begin;
+
+    my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
+    my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
+    my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
+
+    my $account = $patron->account;
+    my $credit1 = $account->add_credit({
+        amount      => 13,
+        description => 'Payment of 13',
+        note        => 'some note',
+    });
+    my $offset1 = Koha::Account::Offsets->search({ credit_id => $credit1->id })->next;
+    is($offset1->transaction_library, undef, "No transaction library without userenv and no library set");
+
+    C4::Context->_new_userenv('my_session1');
+    C4::Context->set_userenv(0,0,0,'firstname','surname', $library1->branchcode, $library1->branchname, '', '', '');
+    my $credit2 = $account->add_credit({
+        amount      => 14,
+        description => 'Payment of 14',
+        note        => 'some note',
+    });
+    my $offset2 = Koha::Account::Offsets->search({ credit_id => $credit2->id })->next;
+    is($offset2->transaction_library, $library1->branchcode, "Transaction library should be taken from userenv");
+
+    my $offset3 = Koha::Account::Offset->new({
+        amount      => 15,
+        type        => 'Payment',
+        credit_id   => $credit2->id,
+        transaction_library => $library2->branchcode,
+    })->store;
+    my $offset3_from_db = Koha::Account::Offsets->find( $offset3->id );
+    is($offset3_from_db->transaction_library, $library2->branchcode, "Transaction library should be set when param transaction_library is set");
+
+    $schema->storage->txn_rollback;
+};
-- 
2.1.4