Bug 18403: Add new method Koha::Library::Group->has_child
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Thu, 6 Apr 2017 19:10:02 +0000 (16:10 -0300)
committerJonathan Druart <jonathan.druart@bugs.koha-community.org>
Mon, 12 Feb 2018 18:41:37 +0000 (15:41 -0300)
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 <jon.mcgowan@ptfs-europe.com>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Koha/Library/Group.pm
t/db_dependent/LibraryGroups.t

index 677ae1e..a54e5b1 100644 (file)
@@ -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();
index 69c6fa0..02e9817 100644 (file)
@@ -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();