From 26eae25c11ad2cd46c18660527200f626303910f Mon Sep 17 00:00:00 2001
From: Tomas Cohen Arazi <tomascohen@theke.io>
Date: Fri, 2 Feb 2024 16:25:41 -0300
Subject: [PATCH] Bug 31791: Add TT plugin method Biblio.CanBeEdited

Sponsored-by: ByWater Solutions
---
 Koha/Template/Plugin/Biblio.pm          |  6 +++
 t/db_dependent/Template/Plugin/Biblio.t | 68 +++++++++++++++++++++++++
 2 files changed, 74 insertions(+)
 create mode 100755 t/db_dependent/Template/Plugin/Biblio.t

diff --git a/Koha/Template/Plugin/Biblio.pm b/Koha/Template/Plugin/Biblio.pm
index e339263b9c9..f0a3dd9f0fd 100644
--- a/Koha/Template/Plugin/Biblio.pm
+++ b/Koha/Template/Plugin/Biblio.pm
@@ -69,4 +69,10 @@ sub RecallsCount {
     return $recalls->count;
 }
 
+sub CanBeEdited {
+    my ( $self, $biblio, $patron ) = @_;
+
+    return $biblio->can_be_edited($patron);
+}
+
 1;
diff --git a/t/db_dependent/Template/Plugin/Biblio.t b/t/db_dependent/Template/Plugin/Biblio.t
new file mode 100755
index 00000000000..368ff0d02c5
--- /dev/null
+++ b/t/db_dependent/Template/Plugin/Biblio.t
@@ -0,0 +1,68 @@
+#!/usr/bin/perl
+
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation; either version 3 of the License, or (at your option) any later
+# version.
+#
+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with Koha; if not, see <http://www.gnu.org/licenses>.
+
+use Modern::Perl;
+
+use Test::More tests => 2;
+use Test::MockModule;
+
+use Koha::Database;
+
+use t::lib::TestBuilder;
+
+BEGIN {
+    use_ok('Koha::Template::Plugin::Biblio');
+}
+
+my $schema  = Koha::Database->schema;
+my $builder = t::lib::TestBuilder->new;
+
+subtest 'CanBeEdited() tests' => sub {
+
+    plan tests => 3;
+
+    $schema->storage->txn_begin;
+
+    my $biblio = $builder->build_sample_biblio();
+    my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
+
+    my $can_be_edited;
+
+    my $mock_biblio = Test::MockModule->new('Koha::Biblio');
+    $mock_biblio->mock( 'can_be_edited', sub { return $can_be_edited; } );
+
+    my $plugin = Koha::Template::Plugin::Biblio->new();
+
+    $can_be_edited = undef;
+    is(
+        $plugin->CanBeEdited( $biblio, $patron ), undef,
+        'CanBeEdited is just a wrapper around Koha::Biblio->can_be_edited()'
+    );
+
+    $can_be_edited = 0;
+    is(
+        $plugin->CanBeEdited( $biblio, $patron ), 0,
+        'CanBeEdited is just a wrapper around Koha::Biblio->can_be_edited()'
+    );
+
+    $can_be_edited = 1;
+    is(
+        $plugin->CanBeEdited( $biblio, $patron ), 1,
+        'CanBeEdited is just a wrapper around Koha::Biblio->can_be_edited()'
+    );
+
+    $schema->storage->txn_rollback;
+};
-- 
2.44.0