Bug 20978: Unit tests
authorTomas Cohen Arazi <tomascohen@theke.io>
Thu, 21 Jun 2018 16:03:13 +0000 (13:03 -0300)
committerNick Clemens <nick@bywatersolutions.com>
Fri, 6 Jul 2018 12:39:36 +0000 (12:39 +0000)
This patch adds unit tests for Koha::Account::add_credit.

Signed-off-by: Josef Moravec <josef.moravec@gmail.com>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>

t/db_dependent/Koha/Account.t

index 6ef46af..e5cb215 100755 (executable)
 
 use Modern::Perl;
 
-use Test::More tests => 1;
+use Test::More tests => 2;
 
 use Koha::Account;
 use Koha::Account::Lines;
+use Koha::Account::Offsets;
 
+use t::lib::Mocks;
 use t::lib::TestBuilder;
 
 my $schema  = Koha::Database->new->schema;
@@ -80,3 +82,66 @@ subtest 'outstanding_debits() tests' => sub {
 
     $schema->storage->txn_rollback;
 };
+
+subtest 'add_credit() tests' => sub {
+
+    plan tests => 13;
+
+    $schema->storage->txn_begin;
+
+    # delete logs and statistics
+    $schema->resultset('ActionLog')->search()->delete();
+    $schema->resultset('Statistic')->search()->delete();
+
+    my $patron  = $builder->build_object( { class => 'Koha::Patrons' } );
+    my $account = Koha::Account->new( { patron_id => $patron->borrowernumber } );
+
+    is( $account->balance, 0, 'Patron has no balance' );
+
+    # Disable logs
+    t::lib::Mocks::mock_preference( 'FinesLog', 0 );
+
+    my $line_1 = $account->add_credit(
+        {   amount      => 25,
+            description => 'Payment of 25',
+            library_id  => $patron->branchcode,
+            note        => 'not really important',
+            user_id     => $patron->id
+        }
+    );
+
+    is( $account->balance, -25, 'Patron has a balance of -25' );
+    is( $schema->resultset('ActionLog')->count(), 0, 'No log was added' );
+    is( $schema->resultset('Statistic')->count(), 1, 'Action added to statistics' );
+    is( $line_1->accounttype, $Koha::Account::account_type->{'payment'}, 'Account type is correctly set' );
+
+    # Enable logs
+    t::lib::Mocks::mock_preference( 'FinesLog', 1 );
+
+    my $sip_code = "1";
+    my $line_2 = $account->add_credit(
+        {   amount      => 37,
+            description => 'Payment of 37',
+            library_id  => $patron->branchcode,
+            note        => 'not really important',
+            user_id     => $patron->id,
+            sip         => $sip_code
+        }
+    );
+
+    is( $account->balance, -62, 'Patron has a balance of -25' );
+    is( $schema->resultset('ActionLog')->count(), 1, 'Log was added' );
+    is( $schema->resultset('Statistic')->count(), 2, 'Action added to statistics' );
+    is( $line_2->accounttype, $Koha::Account::account_type->{'payment'} . $sip_code, 'Account type is correctly set' );
+
+    # offsets have the credit_id set to accountlines_id, and debit_id is undef
+    my $offset_1 = Koha::Account::Offsets->search({ credit_id => $line_1->id })->next;
+    my $offset_2 = Koha::Account::Offsets->search({ credit_id => $line_2->id })->next;
+
+    is( $offset_1->credit_id, $line_1->id, 'No debit_id is set for credits' );
+    is( $offset_1->debit_id, undef, 'No debit_id is set for credits' );
+    is( $offset_2->credit_id, $line_2->id, 'No debit_id is set for credits' );
+    is( $offset_2->debit_id, undef, 'No debit_id is set for credits' );
+
+    $schema->storage->txn_rollback;
+};