From ed84970fa429e5cf2d5aecbf01d9f093e7bb71ac Mon Sep 17 00:00:00 2001
From: Matt Blenkinsop <matt.blenkinsop@ptfs-europe.com>
Date: Thu, 19 Dec 2024 16:16:31 +0000
Subject: [PATCH] Bug 38290: Add unit tests
prove t/db_dependent/Koha/*/Limit/LibraryGroup.t
---
Koha/Object/Limit/LibraryGroup.pm | 9 +-
.../Koha/Object/Limit/LibraryGroup.t | 103 +++++++++++
.../Koha/Objects/Limit/LibraryGroup.t | 169 ++++++++++++++++++
3 files changed, 279 insertions(+), 2 deletions(-)
create mode 100755 t/db_dependent/Koha/Object/Limit/LibraryGroup.t
create mode 100755 t/db_dependent/Koha/Objects/Limit/LibraryGroup.t
diff --git a/Koha/Object/Limit/LibraryGroup.pm b/Koha/Object/Limit/LibraryGroup.pm
index ad3f15e67cf..3e366d315d2 100644
--- a/Koha/Object/Limit/LibraryGroup.pm
+++ b/Koha/Object/Limit/LibraryGroup.pm
@@ -53,7 +53,7 @@ A method that can be used to embed or simply retrieve the library group limits f
sub lib_group_limits {
my ($self) = @_;
- my $lib_group_visibility_parameters = $self->object_class()->_library_group_visibility_parameters;
+ my $lib_group_visibility_parameters = $self->_library_group_visibility_parameters;
my $visibility_column = $lib_group_visibility_parameters->{visibility_column};
my $lib_group_visibility = $self->$visibility_column;
@@ -78,9 +78,14 @@ sub set_lib_group_visibility {
my ( $self, $args ) = @_;
my $new_visibility = $args->{new_visibility} || $self->lib_group_visibility;
- my $lib_group_visibility_parameters = $self->object_class()->_library_group_visibility_parameters;
+ my $lib_group_visibility_parameters = $self->_library_group_visibility_parameters;
my $visibility_column = $lib_group_visibility_parameters->{visibility_column};
+ if ( !$new_visibility ) {
+ $self->$visibility_column(undef);
+ return $self;
+ }
+
if ( ref $new_visibility eq 'ARRAY' ) {
if ( scalar( @{$new_visibility} ) == 0 ) {
$self->$visibility_column(undef);
diff --git a/t/db_dependent/Koha/Object/Limit/LibraryGroup.t b/t/db_dependent/Koha/Object/Limit/LibraryGroup.t
new file mode 100755
index 00000000000..cec76df19ec
--- /dev/null
+++ b/t/db_dependent/Koha/Object/Limit/LibraryGroup.t
@@ -0,0 +1,103 @@
+#!/usr/bin/perl
+
+# This file is part of Koha.
+#
+# Copyright PTFS Europe 2024
+#
+# 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 t::lib::TestBuilder;
+
+my $builder = t::lib::TestBuilder->new;
+my $schema = Koha::Database->schema;
+
+subtest 'set_lib_group_visibility' => sub {
+ plan tests => 3;
+
+ $schema->txn_begin;
+
+ my $root_group1 = Koha::Library::Group->new(
+ {
+ title => "LibGroup1",
+ }
+ )->store();
+
+ my $lg1groupA =
+ Koha::Library::Group->new( { parent_id => $root_group1->id, title => 'LibGroup1 SubGroupA' } )->store();
+ my $lg1groupA1 =
+ Koha::Library::Group->new( { parent_id => $lg1groupA->id, title => 'LibGroup1 SubGroupA SubGroup1' } )->store();
+ my $lg1groupB =
+ Koha::Library::Group->new( { parent_id => $root_group1->id, title => 'LibGroup1 SubGroupB' } )->store();
+
+ my $vendor = $builder->build_object(
+ { class => 'Koha::Acquisition::Booksellers', value => { lib_group_visibility => undef } } );
+
+ # Pass in an array
+ my @vendor_lib_groups = ( $lg1groupA->id, $lg1groupB->id );
+ $vendor->set_lib_group_visibility( { new_visibility => \@vendor_lib_groups } );
+ is(
+ $vendor->lib_group_visibility, "|" . join( '|', @vendor_lib_groups ) . "|",
+ 'Got correct lib group visibility'
+ );
+
+ # Update with another array
+ @vendor_lib_groups = ( $lg1groupA1->id, $lg1groupB->id );
+ $vendor->set_lib_group_visibility( { new_visibility => \@vendor_lib_groups } );
+ is(
+ $vendor->lib_group_visibility, "|" . join( '|', @vendor_lib_groups ) . "|",
+ 'Got correct lib group visibility'
+ );
+
+ # Use the call in the object's "store" method
+ $vendor->lib_group_visibility( "|" . $lg1groupA->id . "|" )->store;
+ is(
+ $vendor->lib_group_visibility, "|" . $lg1groupA->id . "|",
+ 'Lib group visibility has been overridden correctly'
+ );
+
+ $schema->txn_rollback;
+};
+
+subtest 'lib_group_limits' => sub {
+ plan tests => 2;
+
+ $schema->txn_begin;
+
+ my $root_group1 = Koha::Library::Group->new(
+ {
+ title => "LibGroup1",
+ }
+ )->store();
+
+ my $lg1groupA =
+ Koha::Library::Group->new( { parent_id => $root_group1->id, title => 'LibGroup1 SubGroupA' } )->store();
+ my $lg1groupB =
+ Koha::Library::Group->new( { parent_id => $root_group1->id, title => 'LibGroup1 SubGroupB' } )->store();
+
+ my $vendor = $builder->build_object(
+ { class => 'Koha::Acquisition::Booksellers', value => { lib_group_visibility => undef } } );
+
+ my @vendor_lib_groups = ( $lg1groupA->id, $lg1groupB->id );
+ $vendor->set_lib_group_visibility( { new_visibility => \@vendor_lib_groups } );
+
+ my $lib_groups = $vendor->lib_group_limits;
+
+ is( scalar(@$lib_groups), 2, 'Got correct number of lib groups' );
+ my @ids = sort { $a <=> $b } map { $_->id } @$lib_groups;
+ is( join( '|', @ids ), join( '|', @vendor_lib_groups ), 'Got correct lib groups' );
+
+ $schema->txn_rollback;
+};
diff --git a/t/db_dependent/Koha/Objects/Limit/LibraryGroup.t b/t/db_dependent/Koha/Objects/Limit/LibraryGroup.t
new file mode 100755
index 00000000000..9156f747318
--- /dev/null
+++ b/t/db_dependent/Koha/Objects/Limit/LibraryGroup.t
@@ -0,0 +1,169 @@
+#!/usr/bin/perl
+
+# This file is part of Koha.
+#
+# Copyright PTFS Europe 2024
+#
+# 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 => 1;
+use t::lib::TestBuilder;
+use t::lib::Mocks;
+
+my $builder = t::lib::TestBuilder->new;
+my $schema = Koha::Database->schema;
+
+subtest 'define_library_group_limits' => sub {
+ plan tests => 8;
+
+ $schema->txn_begin;
+
+ Koha::Acquisition::Baskets->delete;
+ Koha::Acquisition::Booksellers->delete;
+
+ my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
+ my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
+ my $library3 = $builder->build_object( { class => 'Koha::Libraries' } );
+ my $library4 = $builder->build_object( { class => 'Koha::Libraries' } );
+
+ # Group 1 - two sub groups: A, B. Sub group A then has two sub groups: A1, A2
+ my $root_group1 = Koha::Library::Group->new(
+ {
+ title => "LibGroup1",
+ }
+ )->store();
+
+ my $lg1groupA =
+ Koha::Library::Group->new( { parent_id => $root_group1->id, title => 'LibGroup1 SubGroupA' } )->store();
+ my $lg1groupA1 =
+ Koha::Library::Group->new( { parent_id => $lg1groupA->id, title => 'LibGroup1 SubGroupA SubGroup1' } )->store();
+ my $lg1groupA2 =
+ Koha::Library::Group->new( { parent_id => $lg1groupA->id, title => 'LibGroup1 SubGroupA SubGroup2' } )->store();
+ my $lg1groupB =
+ Koha::Library::Group->new( { parent_id => $root_group1->id, title => 'LibGroup1 SubGroupB' } )->store();
+
+ my $lg1groupA_library1 =
+ Koha::Library::Group->new( { parent_id => $lg1groupA->id, branchcode => $library1->branchcode } )->store();
+ my $lg1groupA_library2 =
+ Koha::Library::Group->new( { parent_id => $lg1groupA->id, branchcode => $library2->branchcode } )->store();
+ my $lg1groupB_library1 =
+ Koha::Library::Group->new( { parent_id => $lg1groupB->id, branchcode => $library1->branchcode } )->store();
+ my $lg1groupA1_library1 =
+ Koha::Library::Group->new( { parent_id => $lg1groupA1->id, branchcode => $library2->branchcode } )->store();
+ my $lg1groupA2_library1 =
+ Koha::Library::Group->new( { parent_id => $lg1groupA2->id, branchcode => $library1->branchcode } )->store();
+
+ # Group 2 - three sub groups: A, B, C. Sub group A then has two sub groups: A1, A2 and sub group C has two: C1, C2
+ my $root_group2 = Koha::Library::Group->new(
+ {
+ title => "LibGroup2",
+ }
+ )->store();
+
+ my $lg2groupA =
+ Koha::Library::Group->new( { parent_id => $root_group2->id, title => 'LibGroup2 SubGroupA' } )->store();
+ my $lg2groupA1 =
+ Koha::Library::Group->new( { parent_id => $lg2groupA->id, title => 'LibGroup2 SubGroupA SubGroup1' } )->store();
+ my $lg2groupA2 =
+ Koha::Library::Group->new( { parent_id => $lg2groupA->id, title => 'LibGroup2 SubGroupA SubGroup2' } )->store();
+ my $lg2groupB =
+ Koha::Library::Group->new( { parent_id => $root_group2->id, title => 'LibGroup2 SubGroupB' } )->store();
+ my $lg2groupC =
+ Koha::Library::Group->new( { parent_id => $root_group2->id, title => 'LibGroup2 SubGroupC' } )->store();
+ my $lg2groupC1 =
+ Koha::Library::Group->new( { parent_id => $lg2groupC->id, title => 'LibGroup2 SubGroupC SubGroup1' } )->store();
+ my $lg2groupC2 =
+ Koha::Library::Group->new( { parent_id => $lg2groupC->id, title => 'LibGroup2 SubGroupC SubGroup2' } )->store();
+
+ my $lg2groupA_library1 =
+ Koha::Library::Group->new( { parent_id => $lg2groupA->id, branchcode => $library1->branchcode } )->store();
+ my $lg2groupA_library2 =
+ Koha::Library::Group->new( { parent_id => $lg2groupA->id, branchcode => $library3->branchcode } )->store();
+ my $lg2groupB_library1 =
+ Koha::Library::Group->new( { parent_id => $lg2groupB->id, branchcode => $library1->branchcode } )->store();
+ my $lg2groupC_library1 =
+ Koha::Library::Group->new( { parent_id => $lg2groupC->id, branchcode => $library1->branchcode } )->store();
+ my $lg2groupC_library2 =
+ Koha::Library::Group->new( { parent_id => $lg2groupC->id, branchcode => $library3->branchcode } )->store();
+ my $lg2groupC_library3 =
+ Koha::Library::Group->new( { parent_id => $lg2groupC->id, branchcode => $library4->branchcode } )->store();
+ my $lg2groupA1_library1 =
+ Koha::Library::Group->new( { parent_id => $lg2groupA1->id, branchcode => $library3->branchcode } )->store();
+ my $lg2groupA2_library1 =
+ Koha::Library::Group->new( { parent_id => $lg2groupA2->id, branchcode => $library1->branchcode } )->store();
+ my $lg2groupC1_library1 =
+ Koha::Library::Group->new( { parent_id => $lg2groupC1->id, branchcode => $library3->branchcode } )->store();
+ my $lg2groupC2_library1 =
+ Koha::Library::Group->new( { parent_id => $lg2groupC2->id, branchcode => $library1->branchcode } )->store();
+ my $lg2groupC2_library2 =
+ Koha::Library::Group->new( { parent_id => $lg2groupC2->id, branchcode => $library4->branchcode } )->store();
+
+ my $vendor1 = $builder->build_object(
+ {
+ class => 'Koha::Acquisition::Booksellers', value => { lib_group_visibility => "|" . $root_group1->id . "|" }
+ }
+ );
+ my $vendor2 = $builder->build_object(
+ {
+ class => 'Koha::Acquisition::Booksellers', value => { lib_group_visibility => "|" . $root_group2->id . "|" }
+ }
+ );
+
+ t::lib::Mocks::mock_userenv( { branchcode => $library1->branchcode } );
+ my $vendors = Koha::Acquisition::Booksellers->search()->as_list;
+ is( scalar(@$vendors), 2, "Two vendors returned" );
+
+ t::lib::Mocks::mock_userenv( { branchcode => $library2->branchcode } );
+ $vendors = Koha::Acquisition::Booksellers->search()->as_list;
+ is( scalar(@$vendors), 1, "One vendor returned as library2 is not in root_group2" );
+
+ my $vendor3 = $builder->build_object(
+ { class => 'Koha::Acquisition::Booksellers', value => { lib_group_visibility => "|" . $lg2groupA->id . "|" } }
+ );
+ $vendors = Koha::Acquisition::Booksellers->search()->as_list;
+ is( scalar(@$vendors), 1, "Still only one vendor returned as library2 is not in root_group2 or lg2groupA" );
+
+ t::lib::Mocks::mock_userenv( { branchcode => $library3->branchcode } );
+ $vendors = Koha::Acquisition::Booksellers->search()->as_list;
+ is( scalar(@$vendors), 2, "Two vendors now returned as library3 is in lg2groupA and root_group2" );
+
+ my $vendor4 = $builder->build_object(
+ { class => 'Koha::Acquisition::Booksellers', value => { lib_group_visibility => "|" . $lg2groupB->id . "|" } }
+ );
+ $vendors = Koha::Acquisition::Booksellers->search()->as_list;
+ is(
+ scalar(@$vendors), 2,
+ "Still two vendors returned as library3 is in lg2groupA and root_group2 but not lg2groupB"
+ );
+
+ t::lib::Mocks::mock_userenv( { branchcode => $library4->branchcode } );
+ $vendors = Koha::Acquisition::Booksellers->search()->as_list;
+ is(
+ scalar(@$vendors), 1,
+ "One vendor returned as library2 is only in root_group2 but not the previously assigned sub groups"
+ );
+ $vendor2->lib_group_visibility( "|" . $root_group1->id . "|" )->store;
+ $vendors = Koha::Acquisition::Booksellers->search()->as_list;
+ is( scalar(@$vendors), 0, "No vendors returned as none are visible to this branch code" );
+
+ $vendor2->lib_group_visibility(undef)->store;
+ $vendors = Koha::Acquisition::Booksellers->search()->as_list;
+ is(
+ scalar(@$vendors), 1,
+ "One vendor returned as its lib_group_visibility is now undefined and so is visible to all"
+ );
+
+ $schema->txn_rollback;
+};
--
2.39.5 (Apple Git-154)