Bug 23177: (QA follow-up) Move rollback to the end
[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 => 20;
8
9 use t::lib::TestBuilder;
10 use Koha::Database;
11
12 BEGIN {
13     use FindBin;
14     use lib $FindBin::Bin;
15     use_ok('Koha::Library::Group');
16     use_ok('Koha::Library::Groups');
17 }
18
19 my $schema = Koha::Database->new->schema;
20 $schema->storage->txn_begin;
21 my $dbh = C4::Context->dbh;
22
23 $dbh->do(q|DELETE FROM issues|);
24 $dbh->do(q|DELETE FROM library_groups|);
25
26 my $builder = t::lib::TestBuilder->new();
27
28 my $library1 = $builder->build( { source => 'Branch' } );
29 my $library2 = $builder->build( { source => 'Branch' } );
30 my $library3 = $builder->build( { source => 'Branch' } );
31 my $library4 = $builder->build( { source => 'Branch' } );
32 my $library5 = $builder->build( { source => 'Branch' } );
33 my $library6 = $builder->build( { source => 'Branch' } );
34 my $library7 = $builder->build( { source => 'Branch' } );
35
36 my $root_group =
37   Koha::Library::Group->new( { title => "Test root group" } )->store();
38
39 my @root_groups = Koha::Library::Groups->get_root_groups();
40 my $in_list = any { $_->id eq $root_group->id } @root_groups;
41 ok( $in_list, 'New root group is in the list returned by the get_root_groups method');
42
43 my $groupA  = Koha::Library::Group->new({ parent_id => $root_group->id, title => 'Group A' })->store();
44 my $groupA1 = Koha::Library::Group->new({ parent_id => $groupA->id,     title => 'Group A1' })->store();
45 my $groupA2 = Koha::Library::Group->new({ parent_id => $groupA->id,     title => 'Group A2' })->store();
46 my $groupB  = Koha::Library::Group->new({ parent_id => $root_group->id, title => 'Group B' })->store();
47
48 my $groupA_library1  = Koha::Library::Group->new({ parent_id => $groupA->id,  branchcode => $library1->{branchcode} })->store();
49 my $groupB_library1  = Koha::Library::Group->new({ parent_id => $groupB->id,  branchcode => $library1->{branchcode} })->store();
50 my $groupA1_library2 = Koha::Library::Group->new({ parent_id => $groupA1->id, branchcode => $library2->{branchcode} })->store();
51
52 my @children = $root_group->children()->as_list();
53 is( $children[0]->id, $groupA->id, 'Child of root group set correctly' );
54
55 @children = $groupA->children()->as_list();
56 is( $children[1]->id, $groupA1->id, 'Child 1 of 2nd level group set correctly' );
57 is( $children[2]->id, $groupA2->id, 'Child 2 of 2nd level group set correctly' );
58 is( $children[0]->id, $groupA_library1->id, 'Child 3 of 2nd level group set correctly' );
59
60 is( $children[0]->branchcode, $groupA_library1->branchcode, 'Child 3 is correctly set as leaf node' );
61
62 @children = $groupA1->children()->as_list();
63 is( $children[0]->branchcode, $library2->{branchcode}, 'Child 1 of 3rd level group correctly set as leaf node' );
64
65 my $library = $groupA_library1->library();
66 is( ref( $library ), 'Koha::Library', 'Method library returns a Koha::Library object' );
67 is( $library->id, $groupA_library1->branchcode, 'Branchcode for fetched library matches' );
68
69 my @libraries_not_direct_children = $groupA->libraries_not_direct_children();
70 $in_list = any { $_->id eq $groupA_library1->branchcode } @libraries_not_direct_children;
71 ok( !$in_list, 'Method libraries_not_direct_children returns all libraries not direct descendants of group, library 1 is not in the list');
72 $in_list = any { $_->id eq $groupA1_library2->branchcode } @libraries_not_direct_children;
73 ok( $in_list, 'Method libraries_not_direct_children returns all libraries not direct descendants of group, library 2 is in the list');
74
75 subtest 'Koha::Library->library_groups' => sub {
76     plan tests => 4;
77     my $library3 = Koha::Libraries->find( $library3->{branchcode} );
78     my $groups = $library3->library_groups;
79     is( ref( $groups ), 'Koha::Library::Groups', 'Koha::Library->library_groups should return Koha::Library::Groups' );
80     is( $groups->count, 0, 'Library 3 should not be part of any groups');
81
82     my $library1 = Koha::Libraries->find( $library1->{branchcode} );
83     $groups = $library1->library_groups;
84     is( ref( $groups ), 'Koha::Library::Groups', 'Koha::Library->library_groups should return Koha::Library::Groups' );
85     is( $groups->count, 2, 'Library 1 should be part of 2 groups' );
86 };
87
88 # root_group
89 #     + groupA
90 #         + groupA1
91 #             + groupA1_library2
92 #         + groupA_library1
93 #         + groupA2
94 #     + groupB
95 #         + groupB_library1
96
97 subtest 'Koha::Library::Group->has_child' => sub {
98     plan tests => 2;
99     is( $groupA->has_child( $library1->{branchcode} ), 1, 'library1 should be condidered as a child of groupA' );
100     is( $groupB->has_child( $library2->{branchcode} ), 0, 'library2 should not be considered as a child of groupB' );
101
102     # TODO This is not implemented because not used yet
103     # ->has_child only works with libraries
104     #is( $groupA->has_child( $groupA1 ), 1, 'groupA1 should be condidered as a child of groupA' );
105
106     # FIXME At the time of writing this test fails because the ->children methods does not return more than 1 level of depth
107     # See Bug 15707 comments 166-170+
108     #is( $groupA->has_child( $groupA1_library2->branchcode ), 1, 'groupA1_library2 should be considered as a child of groupA (it is a grandchild)' );
109 };
110
111 subtest 'Koha::Library::Group->get_search_groups' => sub {
112     plan tests => 2;
113
114     #Enable as search groups
115     $groupA->ft_search_groups_opac(1)->store();
116     $groupB->ft_search_groups_staff(1)->store();
117
118     #Update the objects
119     $groupA = Koha::Library::Groups->find( $groupA->id );
120     $groupB = Koha::Library::Groups->find( $groupB->id );
121
122     my @groups = Koha::Library::Groups->get_search_groups({ interface => 'opac' });
123     is_deeply( $groups[0]->unblessed, $groupA->unblessed, 'Get search groups opac should return enabled group' );
124     @groups = Koha::Library::Groups->get_search_groups({ interface => 'staff' });
125     is_deeply( $groups[0]->unblessed, $groupB->unblessed, 'Get search groups staff should return enabled group' );
126 };
127
128 my $groupX = Koha::Library::Group->new( { title => "Group X" } )->store();
129 my $groupX_library1 = Koha::Library::Group->new({ parent_id => $groupX->id,  branchcode => $library1->{branchcode} })->store();
130 my $groupX_library2 = Koha::Library::Group->new({ parent_id => $groupX->id,  branchcode => $library2->{branchcode} })->store();
131 my $groupX1 = Koha::Library::Group->new({ parent_id => $groupX->id, title => 'Group X1' })->store();
132 my $groupX1_library3 = Koha::Library::Group->new({ parent_id => $groupX1->id,  branchcode => $library3->{branchcode} })->store();
133 my $groupX1_library4 = Koha::Library::Group->new({ parent_id => $groupX1->id,  branchcode => $library4->{branchcode} })->store();
134 my $groupX2 = Koha::Library::Group->new({ parent_id => $groupX->id, title => 'Group X2' })->store();
135 my $groupX2_library5 = Koha::Library::Group->new({ parent_id => $groupX2->id,  branchcode => $library5->{branchcode} })->store();
136 my $groupX2_library6 = Koha::Library::Group->new({ parent_id => $groupX2->id,  branchcode => $library6->{branchcode} })->store();
137
138 my @branchcodes = sort( $library1->{branchcode}, $library2->{branchcode} );
139 my @group_branchcodes = sort( map { $_->branchcode } $groupX->libraries->as_list );
140 is_deeply( \@branchcodes, \@group_branchcodes, "Group libraries are returned correctly" );
141 is( ref($groupX->libraries), 'Koha::Libraries', '->libraries should return a Koha::Libraries iterator' );
142
143 @branchcodes = sort( $library1->{branchcode}, $library2->{branchcode}, $library3->{branchcode}, $library4->{branchcode}, $library5->{branchcode}, $library6->{branchcode} );
144 @group_branchcodes = sort( map { $_->branchcode } $groupX->all_libraries );
145 is_deeply( \@branchcodes, \@group_branchcodes, "Group all_libraries are returned correctly" );
146 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