Bug 16330: Move patches to OpenAPI
[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 => 17;
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 my $groupB  = Koha::Library::Group->new({ parent_id => $root_group->id, title => 'Group B' })->store();
46
47 my $groupA_library1  = Koha::Library::Group->new({ parent_id => $groupA->id,  branchcode => $library1->{branchcode} })->store();
48 my $groupB_library1  = Koha::Library::Group->new({ parent_id => $groupB->id,  branchcode => $library1->{branchcode} })->store();
49 my $groupA1_library2 = Koha::Library::Group->new({ parent_id => $groupA1->id, branchcode => $library2->{branchcode} })->store();
50
51 my @children = $root_group->children()->as_list();
52 is( $children[0]->id, $groupA->id, 'Child of root group set correctly' );
53
54 @children = $groupA->children()->as_list();
55 is( $children[1]->id, $groupA1->id, 'Child 1 of 2nd level group set correctly' );
56 is( $children[2]->id, $groupA2->id, 'Child 2 of 2nd level group set correctly' );
57 is( $children[0]->id, $groupA_library1->id, 'Child 3 of 2nd level group set correctly' );
58
59 is( $children[0]->branchcode, $groupA_library1->branchcode, 'Child 3 is correctly set as leaf node' );
60
61 @children = $groupA1->children()->as_list();
62 is( $children[0]->branchcode, $library2->{branchcode}, 'Child 1 of 3rd level group correctly set as leaf node' );
63
64 my $library = $groupA_library1->library();
65 is( ref( $library ), 'Koha::Library', 'Method library returns a Koha::Library object' );
66 is( $library->id, $groupA_library1->branchcode, 'Branchcode for fetched library matches' );
67
68 my @libraries_not_direct_children = $groupA->libraries_not_direct_children();
69 $in_list = any { $_->id eq $groupA_library1->branchcode } @libraries_not_direct_children;
70 ok( !$in_list, 'Method libraries_not_direct_children returns all libraries not direct descendants of group, library 1 is not in the list');
71 $in_list = any { $_->id eq $groupA1_library2->branchcode } @libraries_not_direct_children;
72 ok( $in_list, 'Method libraries_not_direct_children returns all libraries not direct descendants of group, library 2 is in the list');
73
74 subtest 'Koha::Library->library_groups' => sub {
75     plan tests => 4;
76     my $library3 = Koha::Libraries->find( $library3->{branchcode} );
77     my $groups = $library3->library_groups;
78     is( ref( $groups ), 'Koha::Library::Groups', 'Koha::Library->library_groups should return Koha::Library::Groups' );
79     is( $groups->count, 0, 'Library 3 should not be part of any groups');
80
81     my $library1 = Koha::Libraries->find( $library1->{branchcode} );
82     $groups = $library1->library_groups;
83     is( ref( $groups ), 'Koha::Library::Groups', 'Koha::Library->library_groups should return Koha::Library::Groups' );
84     is( $groups->count, 2, 'Library 1 should be part of 2 groups' );
85 };
86
87 # root_group
88 #     + groupA
89 #         + groupA1
90 #             + groupA1_library2
91 #         + groupA_library1
92 #         + groupA2
93 #     + groupB
94 #         + groupB_library1
95
96 subtest 'Koha::Library::Group->has_child' => sub {
97     plan tests => 2;
98     is( $groupA->has_child( $library1->{branchcode} ), 1, 'library1 should be condidered as a child of groupA' );
99     is( $groupB->has_child( $library2->{branchcode} ), 0, 'library2 should not be considered as a child of groupB' );
100
101     # TODO This is not implemented because not used yet
102     # ->has_child only works with libraries
103     #is( $groupA->has_child( $groupA1 ), 1, 'groupA1 should be condidered as a child of groupA' );
104
105     # FIXME At the time of writing this test fails because the ->children methods does not return more than 1 level of depth
106     # See Bug 15707 comments 166-170+
107     #is( $groupA->has_child( $groupA1_library2->branchcode ), 1, 'groupA1_library2 should be considered as a child of groupA (it is a grandchild)' );
108 };
109
110 my $groupX = Koha::Library::Group->new( { title => "Group X" } )->store();
111 my $groupX_library1 = Koha::Library::Group->new({ parent_id => $groupX->id,  branchcode => $library1->{branchcode} })->store();
112 my $groupX_library2 = Koha::Library::Group->new({ parent_id => $groupX->id,  branchcode => $library2->{branchcode} })->store();
113 my $groupX1 = Koha::Library::Group->new({ parent_id => $groupX->id, title => 'Group X1' })->store();
114 my $groupX1_library3 = Koha::Library::Group->new({ parent_id => $groupX1->id,  branchcode => $library3->{branchcode} })->store();
115 my $groupX1_library4 = Koha::Library::Group->new({ parent_id => $groupX1->id,  branchcode => $library4->{branchcode} })->store();
116 my $groupX2 = Koha::Library::Group->new({ parent_id => $groupX->id, title => 'Group X2' })->store();
117 my $groupX2_library5 = Koha::Library::Group->new({ parent_id => $groupX2->id,  branchcode => $library5->{branchcode} })->store();
118 my $groupX2_library6 = Koha::Library::Group->new({ parent_id => $groupX2->id,  branchcode => $library6->{branchcode} })->store();
119
120 my @branchcodes = sort( $library1->{branchcode}, $library2->{branchcode} );
121 my @group_branchcodes = sort( map { $_->branchcode } $groupX->libraries->as_list );
122 is_deeply( \@branchcodes, \@group_branchcodes, "Group libraries are returned correctly" );
123
124 @branchcodes = sort( $library1->{branchcode}, $library2->{branchcode}, $library3->{branchcode}, $library4->{branchcode}, $library5->{branchcode}, $library6->{branchcode} );
125 @group_branchcodes = sort( map { $_->branchcode } $groupX->all_libraries );
126 is_deeply( \@branchcodes, \@group_branchcodes, "Group all_libraries are returned correctly" );