From 4b174a1575f3f3fabf22445a326183aaa244471b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Cohen=20Arazi?= Date: Wed, 29 Oct 2025 09:36:13 -0300 Subject: [PATCH] Bug 40841: Add Z39.50 library limits This patch adds the library limits mixin to the Z39.50 classes following a well established pattern in Koha. --- Koha/Z3950Server.pm | 16 +++++++- Koha/Z3950Servers.pm | 2 +- t/db_dependent/Koha/Z3950Servers.t | 65 +++++++++++++++++++++++++++++- 3 files changed, 79 insertions(+), 4 deletions(-) diff --git a/Koha/Z3950Server.pm b/Koha/Z3950Server.pm index f4296e30e4c..7c80c7a4bb7 100644 --- a/Koha/Z3950Server.pm +++ b/Koha/Z3950Server.pm @@ -19,7 +19,7 @@ use Modern::Perl; use Koha::Database; -use base qw(Koha::Object); +use base qw(Koha::Object Koha::Object::Limit::Library); =head1 NAME @@ -27,10 +27,22 @@ Koha::Z3950Server - Koha Z3950Server Object class =head1 API -=head2 Class Methods +=head2 Internal methods + +=head3 _library_limits + +Configure library limits for Z39.50 servers =cut +sub _library_limits { + return { + class => "Z3950serversBranch", + id => "server_id", + library => "branchcode", + }; +} + =head3 _type Return type of Object relating to Schema ResultSet diff --git a/Koha/Z3950Servers.pm b/Koha/Z3950Servers.pm index 1cab6800137..968c7efcd19 100644 --- a/Koha/Z3950Servers.pm +++ b/Koha/Z3950Servers.pm @@ -21,7 +21,7 @@ use Koha::Database; use Koha::Z3950Server; -use base qw(Koha::Objects); +use base qw(Koha::Objects Koha::Objects::Limit::Library); =head1 NAME diff --git a/t/db_dependent/Koha/Z3950Servers.t b/t/db_dependent/Koha/Z3950Servers.t index 6ae9f91fb19..66f1ef8a885 100755 --- a/t/db_dependent/Koha/Z3950Servers.t +++ b/t/db_dependent/Koha/Z3950Servers.t @@ -19,7 +19,7 @@ use Modern::Perl; use Test::NoWarnings; -use Test::More tests => 3; +use Test::More tests => 4; use Test::Exception; use t::lib::TestBuilder; @@ -105,3 +105,66 @@ subtest 'Host, syntax and encoding are NOT NULL now (BZ 30571)' => sub { $schema->storage->txn_rollback; }; + +subtest 'library_limits' => sub { + plan tests => 8; + + $schema->storage->txn_begin; + + my $library1 = $builder->build_object( { class => 'Koha::Libraries' } ); + my $library2 = $builder->build_object( { class => 'Koha::Libraries' } ); + + my $server = $builder->build_object( + { + class => 'Koha::Z3950Servers', + value => { + host => 'test.example.com', + port => 210, + db => 'testdb' + } + } + ); + + # Test adding library limit + $server->add_library_limit( $library1->branchcode ); + my $limits = $server->library_limits(); + is( $limits->count, 1, 'One library limit added' ); + is( $limits->next->branchcode, $library1->branchcode, 'Correct library limit' ); + + # Test search with library limits + my $servers = Koha::Z3950Servers->search_with_library_limits( + { id => $server->id }, + {}, + $library1->branchcode + ); + is( $servers->count, 1, 'Server found for authorized library' ); + + $servers = Koha::Z3950Servers->search_with_library_limits( + { id => $server->id }, + {}, + $library2->branchcode + ); + is( $servers->count, 0, 'Server not found for unauthorized library' ); + + # Test replacing library limits + $server->library_limits( [ $library1->branchcode, $library2->branchcode ] ); + $limits = $server->library_limits(); + is( $limits->count, 2, 'Two library limits set' ); + + # Test removing library limit + $server->del_library_limit( $library1->branchcode ); + $limits = $server->library_limits(); + is( $limits->count, 1, 'One library limit remains' ); + is( $limits->next->branchcode, $library2->branchcode, 'Correct remaining limit' ); + + # Test server with no limits (available to all) + my $unrestricted_server = $builder->build_object( { class => 'Koha::Z3950Servers' } ); + $servers = Koha::Z3950Servers->search_with_library_limits( + { id => $unrestricted_server->id }, + {}, + $library1->branchcode + ); + is( $servers->count, 1, 'Unrestricted server available to all libraries' ); + + $schema->storage->txn_rollback; +}; -- 2.51.2