Bug 24368: Comprehensive tests for Koha::Template::Plugin::Branches::pickup_locations
authorTomas Cohen Arazi <tomascohen@theke.io>
Fri, 8 May 2020 19:04:17 +0000 (16:04 -0300)
committerMartin Renvoize <martin.renvoize@ptfs-europe.com>
Wed, 13 May 2020 09:30:41 +0000 (10:30 +0100)
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
t/db_dependent/Template/Plugin/Branches.t

index 103476b..b75b376 100644 (file)
@@ -113,38 +113,80 @@ subtest 'pickup_locations() tests' => sub {
 
     plan tests => 8;
 
-    my $library_1 = { branchcode => 'A' };
-    my $library_2 = { branchcode => 'B' };
-    my $library_3 = { branchcode => 'C' };
-    my @library_array = ( $library_1, $library_2, $library_3 );
+    $schema->storage->txn_begin;
+
+    Koha::Libraries->search->update({ pickup_location => 0 });
+
+    my $library_1 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1 } });
+    my $library_2 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1 } });
+    my $library_3 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1 } });
+
+    my $plugin           = Koha::Template::Plugin::Branches->new();
+    my $pickup_locations = $plugin->pickup_locations();
+
+    is( scalar @{$pickup_locations}, 3, 'Libraries count is correct' );
 
-    my $libraries = Test::MockModule->new('Koha::Libraries');
-    $libraries->mock(
+    $pickup_locations = $plugin->pickup_locations({ search_params => { item => undef }});
+    is( scalar @{$pickup_locations}, 3, 'item parameter not a ref, fallback to general search' );
+
+    $pickup_locations = $plugin->pickup_locations({ search_params => { biblio => undef }});
+    is( scalar @{$pickup_locations}, 3, 'biblio parameter not a ref, fallback to general search' );
+
+    my $item_class = Test::MockModule->new('Koha::Item');
+    $item_class->mock(
         'pickup_locations',
         sub {
-            my $result = clone(\@library_array);
-            return @$result;
+            return [$library_1];
         }
     );
 
-    my $plugin           = Koha::Template::Plugin::Branches->new();
-    my $pickup_locations = $plugin->pickup_locations();
+    my $item   = $builder->build_sample_item();
+    my $patron = $builder->build_object({ class => 'Koha::Patrons' });
 
-    is( scalar @{$pickup_locations}, 3, 'Libraries count is correct' );
-    for my $library ( @{$pickup_locations} ) {
-        ok( ( any { $_->{branchcode} eq $library->{branchcode} } @library_array ),
-            'Library ' . $library->{branchcode} . ' in results' );
-    }
-
-    $pickup_locations = $plugin->pickup_locations({ selected => $library_2->{branchcode} });
-    my @selected = grep { exists $_->{selected} } @{ $pickup_locations };
-    is( scalar @selected, 1, '(param) Only one is selected' );
-    is( $selected[0]->{branchcode}, $library_2->{branchcode}, '(param) The selected one is the right one' );
-
-    t::lib::Mocks::mock_userenv({ branchcode => $library_3->{branchcode} });
-    $pickup_locations = $plugin->pickup_locations();
-    @selected = grep { exists $_->{selected} } @{ $pickup_locations };
-    is( scalar @selected, 1, '(userenv) Only one is selected' );
-    is( $selected[0]->{branchcode}, $library_3->{branchcode}, '(userenv) The selected one is the right one' );
+    $pickup_locations = $plugin->pickup_locations(
+        { search_params => { item => $item, patron => Koha::Patron->new } } );
+
+    is( scalar @{$pickup_locations}, 1, 'Only the library returned by $item->pickup_locations is returned' );
+    is( $pickup_locations->[0]->{branchcode}, $library_1->branchcode, 'Not cheating' );
+
+    my $biblio_class = Test::MockModule->new('Koha::Biblio');
+    $biblio_class->mock(
+        'pickup_locations',
+        sub {
+            return [$library_2];
+        }
+    );
+
+    my $biblio = $builder->build_sample_biblio();
+
+    $pickup_locations = $plugin->pickup_locations(
+        { search_params => { biblio => $biblio, patron => Koha::Patron->new } } );
+
+    is( scalar @{$pickup_locations}, 1, 'Only the library returned by $biblio->pickup_locations is returned' );
+    is( $pickup_locations->[0]->{branchcode}, $library_2->branchcode, 'Not cheating' );
+
+    subtest 'selected tests' => sub {
+
+        plan tests => 4;
 
+        t::lib::Mocks::mock_userenv({ branchcode => $library_2->branchcode });
+
+        $pickup_locations = $plugin->pickup_locations();
+
+        is( scalar @{$pickup_locations}, 3, 'Libraries count is correct' );
+        foreach my $pickup_location (@{ $pickup_locations }) {
+            next unless exists $pickup_location->{selected} and $pickup_location->{selected} == 1;
+            is( $pickup_location->{branchcode}, $library_2->branchcode, 'The right library is marked as selected' );
+        }
+
+        $pickup_locations = $plugin->pickup_locations({ selected => $library_3->branchcode });
+
+        is( scalar @{$pickup_locations}, 3, 'Libraries count is correct' );
+        foreach my $pickup_location (@{ $pickup_locations }) {
+            next unless exists $pickup_location->{selected} and $pickup_location->{selected} == 1;
+            is( $pickup_location->{branchcode}, $library_3->branchcode, 'The right library is marked as selected' );
+        }
+    };
+
+    $schema->storage->txn_rollback;
 };