Bug 15758: Koha::Libraries - Ultimate duel for C4::Branch
[koha.git] / t / db_dependent / Template / Plugin / Branches.t
1 use Modern::Perl;
2
3 use Test::More tests => 11;
4
5 use C4::Context;
6 use Koha::Library;
7 use Koha::Libraries;
8 use Koha::Template::Plugin::Branches;
9
10 my $dbh = C4::Context->dbh;
11 $dbh->{AutoCommit} = 0;
12 $dbh->{RaiseError} = 1;
13
14 for my $i ( 1 .. 5 ) {
15     Koha::Library->new(
16 {
17         branchcode     => "test_br_$i",
18         branchname     => "test_br_$i",
19 }
20     )->store;
21 }
22 my $library = Koha::Libraries->search->next->unblessed;
23
24 my $plugin = Koha::Template::Plugin::Branches->new();
25 ok($plugin, "initialized Branches plugin");
26
27 my $name = $plugin->GetName($library->{branchcode});
28 is($name, $library->{branchname}, 'retrieved expected name for library');
29
30 $name = $plugin->GetName('__ANY__');
31 is($name, '', 'received empty string as name of the "__ANY__" placeholder library code');
32
33 $name = $plugin->GetName(undef);
34 is($name, '', 'received empty string as name of NULL/undefined library code');
35
36 $library = $plugin->GetLoggedInBranchcode();
37 is($library, '', 'no active library if there is no active user session');
38 C4::Context->_new_userenv('DUMMY_SESSION_ID');
39 C4::Context->set_userenv(123, 'userid', 'usercnum', 'First name', 'Surname', 'MYLIBRARY', 'My Library', 0);
40 $library = $plugin->GetLoggedInBranchcode();
41 is($library, 'MYLIBRARY', 'GetLoggedInBranchcode() returns active library');
42
43 my $branches = $plugin->all;
44 my $test_branches = [ grep { $_->{branchcode} =~ m|^test_br_| } @$branches ];
45 is( scalar( @$test_branches ), 5, 'Plugin Branches should return the branches' );
46 my $selected_branches = [ grep { $_->{selected} } @$branches ];
47 is( scalar( @$selected_branches ), 0, 'Plugin Branches should not select a branch if not needed' );
48
49 $branches = $plugin->all({selected => 'test_br_3'});
50 $test_branches = [ grep { $_->{branchcode} =~ m|^test_br_| } @$branches ];
51 is( scalar( @$test_branches ), 5, 'Plugin Branches should return the branches if selected passed' );
52 $selected_branches = [ grep { $_->{selected} } @$branches ];
53 is( scalar( @$selected_branches ), 1, 'Plugin Branches should return only 1 selected if passed' );
54 is( $selected_branches->[0]->{branchcode}, 'test_br_3', 'Plugin Branches should select the good one' );