@@ -, +, @@ root_group + groupA + groupA1 + groupA1_library2 + groupA_library1 + groupA2 + groupB + groupB_library1 prove t/db_dependent/LibraryGroups.t --- Koha/Library/Group.pm | 15 +++++++++++++++ t/db_dependent/LibraryGroups.t | 25 ++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) --- a/Koha/Library/Group.pm +++ a/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(); --- a/t/db_dependent/LibraryGroups.t +++ a/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(); --