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();
use List::MoreUtils 'any';
-use Test::More tests => 16;
+use Test::More tests => 17;
use t::lib::TestBuilder;
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();