5 use List::MoreUtils 'any';
7 use Test::More tests => 13;
9 use t::lib::TestBuilder;
13 use lib $FindBin::Bin;
14 use_ok('Koha::Library::Group');
15 use_ok('Koha::Library::Groups');
18 our $dbh = C4::Context->dbh;
19 $dbh->{AutoCommit} = 0;
20 $dbh->{RaiseError} = 1;
22 $dbh->do(q|DELETE FROM issues|);
24 my $builder = t::lib::TestBuilder->new();
26 my $library1 = $builder->build(
32 my $library2 = $builder->build(
38 my $library3 = $builder->build(
45 Koha::Library::Group->new( { title => "Test root group" } )->store();
47 my @root_groups = Koha::Library::Groups->get_root_groups();
48 my $in_list = any { $_->id eq $root_group->id } @root_groups;
49 ok( $in_list, 'New root group is in the list returned by the get_root_groups method');
51 my $groupA = Koha::Library::Group->new({ parent_id => $root_group->id, title => 'Group A' })->store();
52 my $groupA1 = Koha::Library::Group->new({ parent_id => $groupA->id, title => 'Group A1' })->store();
53 my $groupA2 = Koha::Library::Group->new({ parent_id => $groupA->id, title => 'Group A2' })->store();
55 my $groupA_library1 = Koha::Library::Group->new({ parent_id => $groupA->id, branchcode => $library1->{branchcode} })->store();
56 my $groupA1_library2 = Koha::Library::Group->new({ parent_id => $groupA1->id, branchcode => $library2->{branchcode} })->store();
58 my @children = $root_group->children()->as_list();
59 is( $children[0]->id, $groupA->id, 'Child of root group set correctly' );
61 @children = $groupA->children()->as_list();
62 is( $children[1]->id, $groupA1->id, 'Child 1 of 2nd level group set correctly' );
63 is( $children[2]->id, $groupA2->id, 'Child 2 of 2nd level group set correctly' );
64 is( $children[0]->id, $groupA_library1->id, 'Child 3 of 2nd level group set correctly' );
66 is( $children[0]->branchcode, $groupA_library1->branchcode, 'Child 3 is correctly set as leaf node' );
68 @children = $groupA1->children()->as_list();
69 is( $children[0]->branchcode, $library2->{branchcode}, 'Child 1 of 3rd level group correctly set as leaf node' );
71 my $library = $groupA_library1->library();
72 is( ref( $library ), 'Koha::Library', 'Method library returns a Koha::Library object' );
73 is( $library->id, $groupA_library1->branchcode, 'Branchcode for fetched library matches' );
75 my @libraries_not_direct_children = $groupA->libraries_not_direct_children();
76 $in_list = any { $_->id eq $groupA_library1->branchcode } @libraries_not_direct_children;
77 ok( !$in_list, 'Method libraries_not_direct_children returns all libraries not direct decendents of group, library 1 is not in the list');
78 $in_list = any { $_->id eq $groupA1_library2->branchcode } @libraries_not_direct_children;
79 ok( $in_list, 'Method libraries_not_direct_children returns all libraries not direct decendents of group, library 2 is in the list');