3 use Test::More tests => 11;
8 use Koha::Template::Plugin::Branches;
10 my $dbh = C4::Context->dbh;
11 $dbh->{AutoCommit} = 0;
12 $dbh->{RaiseError} = 1;
14 for my $i ( 1 .. 5 ) {
17 branchcode => "test_br_$i",
18 branchname => "test_br_$i",
22 my $library = Koha::Libraries->search->next->unblessed;
24 my $plugin = Koha::Template::Plugin::Branches->new();
25 ok($plugin, "initialized Branches plugin");
27 my $name = $plugin->GetName($library->{branchcode});
28 is($name, $library->{branchname}, 'retrieved expected name for library');
30 $name = $plugin->GetName('__ANY__');
31 is($name, '', 'received empty string as name of the "__ANY__" placeholder library code');
33 $name = $plugin->GetName(undef);
34 is($name, '', 'received empty string as name of NULL/undefined library code');
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');
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' );
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' );