Bug 15707: Unit Tests
[koha-ffzg.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 => 13;
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
24 my $builder = t::lib::TestBuilder->new();
25
26 my $library1 = $builder->build(
27     {
28         source => 'Branch',
29     }
30 );
31
32 my $library2 = $builder->build(
33     {
34         source => 'Branch',
35     }
36 );
37
38 my $library3 = $builder->build(
39     {
40         source => 'Branch',
41     }
42 );
43
44 my $root_group =
45   Koha::Library::Group->new( { title => "Test root group" } )->store();
46
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');
50
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();
54
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();
57
58 my @children = $root_group->children()->as_list();
59 is( $children[0]->id, $groupA->id, 'Child of root group set correctly' );
60
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' );
65
66 is( $children[0]->branchcode, $groupA_library1->branchcode, 'Child 3 is correctly set as leaf node' );
67
68 @children = $groupA1->children()->as_list();
69 is( $children[0]->branchcode, $library2->{branchcode}, 'Child 1 of 3rd level group correctly set as leaf node' );
70
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' );
74
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');