Bug 22061: (follow-up) set_password expects a hashref
[koha.git] / t / db_dependent / LibraryGroups.t
index 69c6fa0..69f9e9d 100644 (file)
@@ -4,9 +4,10 @@ use Modern::Perl;
 
 use List::MoreUtils 'any';
 
-use Test::More tests => 16;
+use Test::More tests => 20;
 
 use t::lib::TestBuilder;
+use Koha::Database;
 
 BEGIN {
     use FindBin;
@@ -15,9 +16,9 @@ BEGIN {
     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|);
@@ -84,6 +85,46 @@ 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)' );
+};
+
+subtest 'Koha::Library::Group->get_search_groups' => sub {
+    plan tests => 2;
+
+    #Enable as search groups
+    $groupA->ft_search_groups_opac(1)->store();
+    $groupB->ft_search_groups_staff(1)->store();
+
+    #Update the objects
+    $groupA = Koha::Library::Groups->find( $groupA->id );
+    $groupB = Koha::Library::Groups->find( $groupB->id );
+
+    my @groups = Koha::Library::Groups->get_search_groups({ interface => 'opac' });
+    is_deeply( $groups[0]->unblessed, $groupA->unblessed, 'Get search groups opac should return enabled group' );
+    @groups = Koha::Library::Groups->get_search_groups({ interface => 'staff' });
+    is_deeply( $groups[0]->unblessed, $groupB->unblessed, 'Get search groups staff should return enabled group' );
+};
+
 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();
@@ -97,7 +138,9 @@ my $groupX2_library6 = Koha::Library::Group->new({ parent_id => $groupX2->id,  b
 my @branchcodes = sort( $library1->{branchcode}, $library2->{branchcode} );
 my @group_branchcodes = sort( map { $_->branchcode } $groupX->libraries->as_list );
 is_deeply( \@branchcodes, \@group_branchcodes, "Group libraries are returned correctly" );
+is( ref($groupX->libraries), 'Koha::Libraries', '->libraries should return a Koha::Libraries iterator' );
 
 @branchcodes = sort( $library1->{branchcode}, $library2->{branchcode}, $library3->{branchcode}, $library4->{branchcode}, $library5->{branchcode}, $library6->{branchcode} );
 @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