From eb68ca2af2c9ebf32091f98380dbbd07cd7ff2e6 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 6 Apr 2017 16:10:02 -0300 Subject: [PATCH] Bug 18403: Add new method Koha::Library::Group->has_child This is more a follow-up for bug 15707. It could be moved on its own bug report if necessary. IMPORTANT NOTE: At the moment the feature only works for 1 level depth, see bug 15707 comment 166+ for the discussion It means that if we have: root_group + groupA + groupA1 + groupA1_library2 + groupA_library1 + groupA2 + groupB + groupB_library1 groupA1_library2 is not considered a child of groupA1. Note that this can change. Test plan: prove t/db_dependent/LibraryGroups.t should return green Signed-off-by: Signed-off-by: Jon McGowan Signed-off-by: Jonathan Druart --- Koha/Library/Group.pm | 15 +++++++++++++++ t/db_dependent/LibraryGroups.t | 25 ++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/Koha/Library/Group.pm b/Koha/Library/Group.pm index 677ae1e6ad..a54e5b1a60 100644 --- a/Koha/Library/Group.pm +++ b/Koha/Library/Group.pm @@ -62,6 +62,21 @@ sub children { return wantarray ? $children->as_list : $children; } +=head3 has_child + +my $has_child = $group->has_child( $branchcode ); + +Return true if the given branchcode library is a child of this group. + +=cut + +sub has_child { + my ( $self, $branchcode ) = @_; + return unless $branchcode; # Does not support group of libraries. + return ( grep { $_ and $_ eq $branchcode } + $self->children->get_column('branchcode') ) ? 1 : 0; +} + =head3 library my $library = $group->library(); diff --git a/t/db_dependent/LibraryGroups.t b/t/db_dependent/LibraryGroups.t index 69c6fa03ce..02e9817948 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 => 16; +use Test::More tests => 17; use t::lib::TestBuilder; @@ -84,6 +84,29 @@ subtest 'Koha::Library->library_groups' => sub { is( $groups->count, 2, 'Library 1 should be part of 2 groups' ); }; +# root_group +# + groupA +# + groupA1 +# + groupA1_library2 +# + groupA_library1 +# + groupA2 +# + groupB +# + groupB_library1 + +subtest 'Koha::Library::Group->has_child' => sub { + plan tests => 2; + is( $groupA->has_child( $library1->{branchcode} ), 1, 'library1 should be condidered as a child of groupA' ); + is( $groupB->has_child( $library2->{branchcode} ), 0, 'library2 should not be considered as a child of groupB' ); + + # TODO This is not implemented because not used yet + # ->has_child only works with libraries + #is( $groupA->has_child( $groupA1 ), 1, 'groupA1 should be condidered as a child of groupA' ); + + # FIXME At the time of writing this test fails because the ->children methods does not return more than 1 level of depth + # See Bug 15707 comments 166-170+ + #is( $groupA->has_child( $groupA1_library2->branchcode ), 1, 'groupA1_library2 should be considered as a child of groupA (it is a grandchild)' ); +}; + 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(); -- 2.39.5