Bug 31351: (QA follow-up) Extend the encode/decode test
[koha-ffzg.git] / t / db_dependent / LibraryGroups.t
old mode 100644 (file)
new mode 100755 (executable)
index 69f9e9d..00e699b
@@ -4,7 +4,7 @@ use Modern::Perl;
 
 use List::MoreUtils 'any';
 
-use Test::More tests => 20;
+use Test::More tests => 21;
 
 use t::lib::TestBuilder;
 use Koha::Database;
@@ -36,7 +36,7 @@ my $library7 = $builder->build( { source => 'Branch' } );
 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');
 
@@ -66,7 +66,7 @@ my $library = $groupA_library1->library();
 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;
@@ -119,9 +119,9 @@ subtest 'Koha::Library::Group->get_search_groups' => sub {
     $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' );
 };
 
@@ -144,3 +144,22 @@ is( ref($groupX->libraries), 'Koha::Libraries', '->libraries should return a Koh
 @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");
+};