Bug 16735: Use libraries in all subgroups, not just immediate children
[koha.git] / t / db_dependent / LibraryGroups.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use List::MoreUtils 'any';
6
7 use Test::More tests => 15;
8
9 use t::lib::TestBuilder;
10
11 BEGIN {
12     use FindBin;
13     use lib $FindBin::Bin;
14     use_ok('Koha::Library::Group');
15     use_ok('Koha::Library::Groups');
16 }
17
18 our $dbh = C4::Context->dbh;
19 $dbh->{AutoCommit} = 0;
20 $dbh->{RaiseError} = 1;
21
22 $dbh->do(q|DELETE FROM issues|);
23 $dbh->do(q|DELETE FROM library_groups|);
24
25 my $builder = t::lib::TestBuilder->new();
26
27 my $library1 = $builder->build( { source => 'Branch' } );
28 my $library2 = $builder->build( { source => 'Branch' } );
29 my $library3 = $builder->build( { source => 'Branch' } );
30 my $library4 = $builder->build( { source => 'Branch' } );
31 my $library5 = $builder->build( { source => 'Branch' } );
32 my $library6 = $builder->build( { source => 'Branch' } );
33 my $library7 = $builder->build( { source => 'Branch' } );
34
35 my $root_group =
36   Koha::Library::Group->new( { title => "Test root group" } )->store();
37
38 my @root_groups = Koha::Library::Groups->get_root_groups();
39 my $in_list = any { $_->id eq $root_group->id } @root_groups;
40 ok( $in_list, 'New root group is in the list returned by the get_root_groups method');
41
42 my $groupA  = Koha::Library::Group->new({ parent_id => $root_group->id, title => 'Group A' })->store();
43 my $groupA1 = Koha::Library::Group->new({ parent_id => $groupA->id,     title => 'Group A1' })->store();
44 my $groupA2 = Koha::Library::Group->new({ parent_id => $groupA->id,     title => 'Group A2' })->store();
45
46 my $groupA_library1  = Koha::Library::Group->new({ parent_id => $groupA->id,  branchcode => $library1->{branchcode} })->store();
47 my $groupA1_library2 = Koha::Library::Group->new({ parent_id => $groupA1->id, branchcode => $library2->{branchcode} })->store();
48
49 my @children = $root_group->children()->as_list();
50 is( $children[0]->id, $groupA->id, 'Child of root group set correctly' );
51
52 @children = $groupA->children()->as_list();
53 is( $children[1]->id, $groupA1->id, 'Child 1 of 2nd level group set correctly' );
54 is( $children[2]->id, $groupA2->id, 'Child 2 of 2nd level group set correctly' );
55 is( $children[0]->id, $groupA_library1->id, 'Child 3 of 2nd level group set correctly' );
56
57 is( $children[0]->branchcode, $groupA_library1->branchcode, 'Child 3 is correctly set as leaf node' );
58
59 @children = $groupA1->children()->as_list();
60 is( $children[0]->branchcode, $library2->{branchcode}, 'Child 1 of 3rd level group correctly set as leaf node' );
61
62 my $library = $groupA_library1->library();
63 is( ref( $library ), 'Koha::Library', 'Method library returns a Koha::Library object' );
64 is( $library->id, $groupA_library1->branchcode, 'Branchcode for fetched library matches' );
65
66 my @libraries_not_direct_children = $groupA->libraries_not_direct_children();
67 $in_list = any { $_->id eq $groupA_library1->branchcode } @libraries_not_direct_children;
68 ok( !$in_list, 'Method libraries_not_direct_children returns all libraries not direct descendants of group, library 1 is not in the list');
69 $in_list = any { $_->id eq $groupA1_library2->branchcode } @libraries_not_direct_children;
70 ok( $in_list, 'Method libraries_not_direct_children returns all libraries not direct descendants of group, library 2 is in the list');
71
72 my $groupX = Koha::Library::Group->new( { title => "Group X" } )->store();
73 my $groupX_library1 = Koha::Library::Group->new({ parent_id => $groupX->id,  branchcode => $library1->{branchcode} })->store();
74 my $groupX_library2 = Koha::Library::Group->new({ parent_id => $groupX->id,  branchcode => $library2->{branchcode} })->store();
75 my $groupX1 = Koha::Library::Group->new({ parent_id => $groupX->id, title => 'Group X1' })->store();
76 my $groupX1_library3 = Koha::Library::Group->new({ parent_id => $groupX1->id,  branchcode => $library3->{branchcode} })->store();
77 my $groupX1_library4 = Koha::Library::Group->new({ parent_id => $groupX1->id,  branchcode => $library4->{branchcode} })->store();
78 my $groupX2 = Koha::Library::Group->new({ parent_id => $groupX->id, title => 'Group X2' })->store();
79 my $groupX2_library5 = Koha::Library::Group->new({ parent_id => $groupX2->id,  branchcode => $library5->{branchcode} })->store();
80 my $groupX2_library6 = Koha::Library::Group->new({ parent_id => $groupX2->id,  branchcode => $library6->{branchcode} })->store();
81
82 my @branchcodes = sort( $library1->{branchcode}, $library2->{branchcode} );
83 my @group_branchcodes = sort( map { $_->branchcode } $groupX->libraries->as_list );
84 is_deeply( \@branchcodes, \@group_branchcodes, "Group libraries are returned correctly" );
85
86 @branchcodes = sort( $library1->{branchcode}, $library2->{branchcode}, $library3->{branchcode}, $library4->{branchcode}, $library5->{branchcode}, $library6->{branchcode} );
87 @group_branchcodes = sort( map { $_->branchcode } $groupX->all_libraries );
88 is_deeply( \@branchcodes, \@group_branchcodes, "Group all_libraries are returned correctly" );