use List::MoreUtils 'any';
-use Test::More tests => 20;
+use Test::More tests => 21;
use t::lib::TestBuilder;
+use Koha::Database;
BEGIN {
use FindBin;
use_ok('Koha::Library::Groups');
}
-our $dbh = C4::Context->dbh;
-$dbh->{AutoCommit} = 0;
-$dbh->{RaiseError} = 1;
+my $schema = Koha::Database->new->schema;
+$schema->storage->txn_begin;
+my $dbh = C4::Context->dbh;
$dbh->do(q|DELETE FROM issues|);
$dbh->do(q|DELETE FROM library_groups|);
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' );
-
- # 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();
@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");
+};