From 35cd031af5c89ed3a2b2a20a81a1e32169d8b769 Mon Sep 17 00:00:00 2001
From: Maryse Simard <maryse.simard@inlibro.com>
Date: Fri, 29 Jan 2021 01:21:15 -0500
Subject: [PATCH] Bug 27066: Unit tests for after_patron_action hook

Adds unit tests for the new hook.

To test:
    prove -v t/db_dependent/Koha/Plugins/Patron.t
---
 t/db_dependent/Koha/Plugins/Patron.t | 42 +++++++++++++++++++++++++++-
 t/lib/Koha/Plugin/Test.pm            | 17 +++++++++++
 2 files changed, 58 insertions(+), 1 deletion(-)

diff --git a/t/db_dependent/Koha/Plugins/Patron.t b/t/db_dependent/Koha/Plugins/Patron.t
index c8b2e86f25..fa619e93be 100755
--- a/t/db_dependent/Koha/Plugins/Patron.t
+++ b/t/db_dependent/Koha/Plugins/Patron.t
@@ -16,8 +16,9 @@
 
 use Modern::Perl;
 
-use Test::More tests => 5;
+use Test::More tests => 6;
 use Test::Exception;
+use Test::Warn;
 
 use File::Basename;
 
@@ -91,4 +92,43 @@ subtest 'check_password hook tests' => sub {
     Koha::Plugins::Methods->delete;
 };
 
+subtest 'after_patron_action hook tests' => sub {
+
+    plan tests => 4;
+
+    $schema->storage->txn_begin;
+
+    my $plugins = Koha::Plugins->new;
+    $plugins->InstallPlugins;
+
+    my $plugin = Koha::Plugin::Test->new->enable;
+
+    ok( $plugin->can('after_patron_action'), 'Test plugin can after_patron_action' );
+
+    my $library  = $builder->build( { source => 'Branch' } );
+    my $category = $builder->build( { source => 'Category' } );
+    my $patron   = Koha::Patron->new(
+        {
+            cardnumber => 'test_cn_after_patron_action', # Only this cardnumber is set to react to after_patron_action hook
+            branchcode   => $library->{branchcode},
+            categorycode => $category->{categorycode},
+        }
+    );
+
+    warning_like { $patron->store->discard_changes; }
+            qr/after_patron_action called with action: create, ref: Koha::Patron/,
+            'Koha::Patron->store calls the hook with action=create';
+
+    warning_like { $patron->store; }
+            qr/after_patron_action called with action: modify, ref: Koha::Patron/,
+            'If patron exists, Koha::Patron->store calls the hook with action=modify';
+
+    warning_like { $patron->delete; }
+            qr/after_patron_action called with action: delete/,
+            'Koha::Patron->delete calls the hook with action=delete';
+
+    $schema->storage->txn_rollback;
+    Koha::Plugins::Methods->delete;
+};
+
 1;
diff --git a/t/lib/Koha/Plugin/Test.pm b/t/lib/Koha/Plugin/Test.pm
index 0a92967738..52d465bc1e 100644
--- a/t/lib/Koha/Plugin/Test.pm
+++ b/t/lib/Koha/Plugin/Test.pm
@@ -164,6 +164,23 @@ sub after_item_action {
     }
 }
 
+sub after_patron_action {
+    my ( $self, $params ) = @_;
+    my $action    = $params->{action} // '';
+    my $patron    = $params->{patron};
+    my $patron_id = $params->{patron_id};
+
+    # Continue only when testing after_patron_action hook
+    return unless (defined $patron->cardnumber && $patron->cardnumber eq "test_cn_after_patron_action");
+
+    if ( $action ne 'delete' ) {
+        Koha::Exceptions::Exception->throw("after_patron_action called with action: $action, ref: " . ref($patron) );
+    }
+    else {
+        Koha::Exceptions::Exception->throw("after_patron_action called with action: $action") if $patron_id;
+    }
+}
+
 sub after_circ_action {
     my ( $self, $params ) = @_;
 
-- 
2.17.1