From ad6a7aaf85d1c10fe74c9c1c503384b04372a29c Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Tue, 8 Aug 2017 12:40:45 -0400 Subject: [PATCH] Bug 16735: Use libraries in all subgroups, not just immediate children Signed-off-by: Tomas Cohen Arazi Signed-off-by: Jonathan Druart --- Koha/Library/Group.pm | 31 +++++++++++++++++++++++ catalogue/search.pl | 2 +- opac/opac-search.pl | 2 +- t/db_dependent/LibraryGroups.t | 45 ++++++++++++++++++++-------------- 4 files changed, 60 insertions(+), 20 deletions(-) diff --git a/Koha/Library/Group.pm b/Koha/Library/Group.pm index cc4bd6d831..5ac1360fb9 100644 --- a/Koha/Library/Group.pm +++ b/Koha/Library/Group.pm @@ -117,6 +117,37 @@ sub libraries { ); } +=head3 all_libraries + +my @libraries = $group->libraries( { [invert => 1] } ); + +Returns the libraries set as children of this group or any subgroup. + +=cut + +sub all_libraries { + my ( $self, $params ) = @_; + + my @libraries; + + my @children = $self->children; + foreach my $c (@children) { + if ( $c->branchcode ) { + push( @libraries, $c ); + } + else { + push( @libraries, $c->all_libraries ); + } + } + + my %seen; + @libraries = + grep { !$seen{ $_->id }++ } @libraries; + +# return wantarray ? @li : $children; + return @libraries; +} + =head3 libraries_not_direct_children my @libraries = $group->libraries_not_direct_children(); diff --git a/catalogue/search.pl b/catalogue/search.pl index 4041bd84aa..26b7ef1f38 100755 --- a/catalogue/search.pl +++ b/catalogue/search.pl @@ -400,7 +400,7 @@ my %is_nolimit = map { $_ => 1 } @nolimits; if($params->{'multibranchlimit'}) { my $search_group = Koha::Library::Groups->find( $params->{multibranchlimit} ); - my @libraries = $search_group->libraries; + my @libraries = $search_group->all_libraries; my $multibranch = '('.join( " or ", map { 'branch: ' . $_->id } @libraries ) .')'; push @limits, $multibranch if ($multibranch ne '()'); } diff --git a/opac/opac-search.pl b/opac/opac-search.pl index 81e6b3cd1f..763c8441b1 100755 --- a/opac/opac-search.pl +++ b/opac/opac-search.pl @@ -492,7 +492,7 @@ if (@searchCategories > 0) { if($params->{'multibranchlimit'}) { my $search_group = Koha::Library::Groups->find( $params->{multibranchlimit} ); - my @libraries = $search_group->libraries; + my @libraries = $search_group->all_libraries; my $multibranch = '('.join( " or ", map { 'branch: ' . $_->id } @libraries ) .')'; push @limits, $multibranch if ($multibranch ne '()'); } diff --git a/t/db_dependent/LibraryGroups.t b/t/db_dependent/LibraryGroups.t index c3aef9ff97..52bde2050b 100644 --- a/t/db_dependent/LibraryGroups.t +++ b/t/db_dependent/LibraryGroups.t @@ -4,7 +4,7 @@ use Modern::Perl; use List::MoreUtils 'any'; -use Test::More tests => 13; +use Test::More tests => 15; use t::lib::TestBuilder; @@ -20,26 +20,17 @@ $dbh->{AutoCommit} = 0; $dbh->{RaiseError} = 1; $dbh->do(q|DELETE FROM issues|); +$dbh->do(q|DELETE FROM library_groups|); my $builder = t::lib::TestBuilder->new(); -my $library1 = $builder->build( - { - source => 'Branch', - } -); - -my $library2 = $builder->build( - { - source => 'Branch', - } -); - -my $library3 = $builder->build( - { - source => 'Branch', - } -); +my $library1 = $builder->build( { source => 'Branch' } ); +my $library2 = $builder->build( { source => 'Branch' } ); +my $library3 = $builder->build( { source => 'Branch' } ); +my $library4 = $builder->build( { source => 'Branch' } ); +my $library5 = $builder->build( { source => 'Branch' } ); +my $library6 = $builder->build( { source => 'Branch' } ); +my $library7 = $builder->build( { source => 'Branch' } ); my $root_group = Koha::Library::Group->new( { title => "Test root group" } )->store(); @@ -77,3 +68,21 @@ $in_list = any { $_->id eq $groupA_library1->branchcode } @libraries_not_direct_ ok( !$in_list, 'Method libraries_not_direct_children returns all libraries not direct descendants of group, library 1 is not in the list'); $in_list = any { $_->id eq $groupA1_library2->branchcode } @libraries_not_direct_children; ok( $in_list, 'Method libraries_not_direct_children returns all libraries not direct descendants of group, library 2 is in the list'); + +my $groupX = Koha::Library::Group->new( { title => "Group X" } )->store(); +my $groupX_library1 = Koha::Library::Group->new({ parent_id => $groupX->id, branchcode => $library1->{branchcode} })->store(); +my $groupX_library2 = Koha::Library::Group->new({ parent_id => $groupX->id, branchcode => $library2->{branchcode} })->store(); +my $groupX1 = Koha::Library::Group->new({ parent_id => $groupX->id, title => 'Group X1' })->store(); +my $groupX1_library3 = Koha::Library::Group->new({ parent_id => $groupX1->id, branchcode => $library3->{branchcode} })->store(); +my $groupX1_library4 = Koha::Library::Group->new({ parent_id => $groupX1->id, branchcode => $library4->{branchcode} })->store(); +my $groupX2 = Koha::Library::Group->new({ parent_id => $groupX->id, title => 'Group X2' })->store(); +my $groupX2_library5 = Koha::Library::Group->new({ parent_id => $groupX2->id, branchcode => $library5->{branchcode} })->store(); +my $groupX2_library6 = Koha::Library::Group->new({ parent_id => $groupX2->id, branchcode => $library6->{branchcode} })->store(); + +my @branchcodes = sort( $library1->{branchcode}, $library2->{branchcode} ); +my @group_branchcodes = sort( map { $_->branchcode } $groupX->libraries->as_list ); +is_deeply( \@branchcodes, \@group_branchcodes, "Group libraries are returned correctly" ); + +@branchcodes = sort( $library1->{branchcode}, $library2->{branchcode}, $library3->{branchcode}, $library4->{branchcode}, $library5->{branchcode}, $library6->{branchcode} ); +@group_branchcodes = sort( map { $_->branchcode } $groupX->all_libraries ); +is_deeply( \@branchcodes, \@group_branchcodes, "Group all_libraries are returned correctly" ); -- 2.39.5