Bug 7534: Use search parameters for Koha::Template::Plugin::Branches->all
authorLari Taskula <lari.taskula@jns.fi>
Tue, 7 Feb 2017 17:40:38 +0000 (19:40 +0200)
committerNick Clemens <nick@bywatersolutions.com>
Thu, 6 Sep 2018 17:27:05 +0000 (17:27 +0000)
This lets us query libraries with additional search parameters and is useful
for this particular Bug by allowing us to select pickup locations.

Unit tests included.

To test:
1. prove t/db_dependent/Template/Plugin/Branches.t

Signed-off-by: Koha Team AMU <axelle.clarisse@univ-amu.fr>
Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Koha/Template/Plugin/Branches.pm
t/db_dependent/Template/Plugin/Branches.t

index 18c53de..1f50ef3 100644 (file)
@@ -59,11 +59,15 @@ sub all {
     my ( $self, $params ) = @_;
     my $selected = $params->{selected};
     my $unfiltered = $params->{unfiltered} || 0;
-    my $only_from_group = $params->{only_from_group} || 0;
+    my $search_params = $params->{search_params} || {};
+
+    if ( !$unfiltered ) {
+        $search_params->{only_from_group} = $params->{only_from_group} || 0;
+    }
 
     my $libraries = $unfiltered
-      ? Koha::Libraries->search( {}, { order_by => ['branchname'] } )->unblessed
-      : Koha::Libraries->search_filtered( { only_from_group => $only_from_group }, { order_by => ['branchname'] } )->unblessed;
+      ? Koha::Libraries->search( $search_params, { order_by => ['branchname'] } )->unblessed
+      : Koha::Libraries->search_filtered( $search_params, { order_by => ['branchname'] } )->unblessed;
 
     for my $l ( @$libraries ) {
         if (       defined $selected and $l->{branchcode} eq $selected
index 507cea9..860d8b8 100644 (file)
@@ -16,7 +16,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 15;
+use Test::More tests => 17;
 
 use C4::Context;
 use Koha::Database;
@@ -75,6 +75,21 @@ is( grep ( { $_->{branchcode} eq 'ANOTHERLIB' and $_->{selected} == 1 } @$librar
 $libraries = $plugin->all( { selected => '' } );
 is( grep ( { exists $_->{selected} } @$libraries ), 0, 'With selected parameter set to an empty string, no library should be preselected' );
 
+my $total = @{$plugin->all};
+my $pickupable = @{$plugin->all( { search_params => { pickup_location => 1 } }) };
+my $yet_another_library = $builder->build({
+    source => 'Branch',
+    value => {
+        branchcode => 'CANTPICKUP',
+        pickup_location => 0,
+    }
+});
+is(@{$plugin->all( { search_params => { pickup_location => 1 } }) }, $pickupable,
+   'Adding a new library with pickups'
+   .' disabled does not increase the amount returned by ->pickup_locations');
+is(@{$plugin->all}, $total+1, 'However, adding a new library increases'
+   .' the total amount gotten with ->all');
+
 t::lib::Mocks::mock_preference( 'IndependentBranches', 1 );
 $libraries = $plugin->all();
 is( scalar(@$libraries), 1, 'If IndependentBranches is set, only 1 library should be returned' );