use List::MoreUtils 'any';
-use Test::More tests => 20;
+use Test::More tests => 21;
use t::lib::TestBuilder;
use Koha::Database;
my $root_group =
Koha::Library::Group->new( { title => "Test root group" } )->store();
-my @root_groups = Koha::Library::Groups->get_root_groups();
+my @root_groups = Koha::Library::Groups->get_root_groups->as_list;
my $in_list = any { $_->id eq $root_group->id } @root_groups;
ok( $in_list, 'New root group is in the list returned by the get_root_groups method');
is( ref( $library ), 'Koha::Library', 'Method library returns a Koha::Library object' );
is( $library->id, $groupA_library1->branchcode, 'Branchcode for fetched library matches' );
-my @libraries_not_direct_children = $groupA->libraries_not_direct_children();
+my @libraries_not_direct_children = $groupA->libraries_not_direct_children->as_list;
$in_list = any { $_->id eq $groupA_library1->branchcode } @libraries_not_direct_children;
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;
$groupA = Koha::Library::Groups->find( $groupA->id );
$groupB = Koha::Library::Groups->find( $groupB->id );
- my @groups = Koha::Library::Groups->get_search_groups({ interface => 'opac' });
+ my @groups = Koha::Library::Groups->get_search_groups({ interface => 'opac' })->as_list;
is_deeply( $groups[0]->unblessed, $groupA->unblessed, 'Get search groups opac should return enabled group' );
- @groups = Koha::Library::Groups->get_search_groups({ interface => 'staff' });
+ @groups = Koha::Library::Groups->get_search_groups({ interface => 'staff' })->as_list;
is_deeply( $groups[0]->unblessed, $groupB->unblessed, 'Get search groups staff should return enabled group' );
};
@group_branchcodes = sort( map { $_->branchcode } $groupX->all_libraries );
is_deeply( \@branchcodes, \@group_branchcodes, "Group all_libraries are returned correctly" );
is( ref(($groupX->all_libraries)[0]), 'Koha::Library', '->all_libraries should return a list of Koha::Library - in the future it should be fixed to return a Koha::Libraries iterator instead'); # FIXME
+
+subtest 'Koha::Library::Groups->get_root_ancestor' => sub {
+ plan tests => 2;
+
+ my $groupY = Koha::Library::Group->new( { title => "Group Y" } )->store();
+ my $groupY_library1 = Koha::Library::Group->new({ parent_id => $groupY->id, branchcode => $library1->{branchcode} })->store();
+ my $groupY1 = Koha::Library::Group->new( { parent_id => $groupY->id, title => "Group Y1" } )->store();
+ my $groupY1_library2 = Koha::Library::Group->new({ parent_id => $groupY1->id, branchcode => $library2->{branchcode} })->store();
+ my $groupZ = Koha::Library::Group->new({ title => "Group Z" })->store();
+ my $groupZ1 = Koha::Library::Group->new({ parent_id => $groupZ->id, title => "Group Z1" })->store();
+ my $groupZ2 = Koha::Library::Group->new({ parent_id => $groupZ1->id, title => "Group Z2" })->store();
+ my $groupZ2_library2 = Koha::Library::Group->new({ parent_id => $groupZ2->id, branchcode => $library2->{branchcode} })->store();
+
+ my $ancestor1 = Koha::Library::Groups->get_root_ancestor($groupY1_library2->unblessed);
+ my $ancestor2 = Koha::Library::Groups->get_root_ancestor($groupZ2_library2->unblessed);
+
+ is($ancestor1->id, $groupY->id, "Get root ancestor should return group's root ancestor");
+ ok($ancestor1->id ne $ancestor2->id, "Both root groups should have different ids");
+};