Bug 26922: Regression tests
[koha.git] / t / db_dependent / LibraryGroups.t
blob26824210e87ea5d520d8fb0c60fcefa71084f3b2
1 #!/usr/bin/perl
3 use Modern::Perl;
5 use List::MoreUtils 'any';
7 use Test::More tests => 21;
9 use t::lib::TestBuilder;
10 use Koha::Database;
12 BEGIN {
13 use FindBin;
14 use lib $FindBin::Bin;
15 use_ok('Koha::Library::Group');
16 use_ok('Koha::Library::Groups');
19 my $schema = Koha::Database->new->schema;
20 $schema->storage->txn_begin;
21 my $dbh = C4::Context->dbh;
23 $dbh->do(q|DELETE FROM issues|);
24 $dbh->do(q|DELETE FROM library_groups|);
26 my $builder = t::lib::TestBuilder->new();
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' } );
36 my $root_group =
37 Koha::Library::Group->new( { title => "Test root group" } )->store();
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');
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();
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();
52 my @children = $root_group->children()->as_list();
53 is( $children[0]->id, $groupA->id, 'Child of root group set correctly' );
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' );
60 is( $children[0]->branchcode, $groupA_library1->branchcode, 'Child 3 is correctly set as leaf node' );
62 @children = $groupA1->children()->as_list();
63 is( $children[0]->branchcode, $library2->{branchcode}, 'Child 1 of 3rd level group correctly set as leaf node' );
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' );
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');
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');
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' );
88 # root_group
89 # + groupA
90 # + groupA1
91 # + groupA1_library2
92 # + groupA_library1
93 # + groupA2
94 # + groupB
95 # + groupB_library1
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' );
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' );
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)' );
111 subtest 'Koha::Library::Group->get_search_groups' => sub {
112 plan tests => 2;
114 #Enable as search groups
115 $groupA->ft_search_groups_opac(1)->store();
116 $groupB->ft_search_groups_staff(1)->store();
118 #Update the objects
119 $groupA = Koha::Library::Groups->find( $groupA->id );
120 $groupB = Koha::Library::Groups->find( $groupB->id );
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' );
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();
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' );
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
148 subtest 'Koha::Library::Groups->get_root_ancestor' => sub {
149 plan tests => 2;
151 my $groupY = Koha::Library::Group->new( { title => "Group Y" } )->store();
152 my $groupY_library1 = Koha::Library::Group->new({ parent_id => $groupY->id, branchcode => $library1->{branchcode} })->store();
153 my $groupY1 = Koha::Library::Group->new( { parent_id => $groupY->id, title => "Group Y1" } )->store();
154 my $groupY1_library2 = Koha::Library::Group->new({ parent_id => $groupY1->id, branchcode => $library2->{branchcode} })->store();
155 my $groupZ = Koha::Library::Group->new({ title => "Group Z" })->store();
156 my $groupZ1 = Koha::Library::Group->new({ parent_id => $groupZ->id, title => "Group Z1" })->store();
157 my $groupZ2 = Koha::Library::Group->new({ parent_id => $groupZ1->id, title => "Group Z2" })->store();
158 my $groupZ2_library2 = Koha::Library::Group->new({ parent_id => $groupZ2->id, branchcode => $library2->{branchcode} })->store();
160 my $ancestor1 = Koha::Library::Groups->get_root_ancestor($groupY1_library2->unblessed);
161 my $ancestor2 = Koha::Library::Groups->get_root_ancestor($groupZ2_library2->unblessed);
163 is($ancestor1->id, $groupY->id, "Get root ancestor should return group's root ancestor");
164 ok($ancestor1->id ne $ancestor2->id, "Both root groups should have different ids");