Bug 29886: Unit tests
authorTomas Cohen Arazi <tomascohen@theke.io>
Fri, 14 Jan 2022 17:49:29 +0000 (14:49 -0300)
committerFridolin Somers <fridolin.somers@biblibre.com>
Fri, 4 Mar 2022 02:11:49 +0000 (16:11 -1000)
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: David Nind <david@davidnind.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Fridolin Somers <fridolin.somers@biblibre.com>
t/db_dependent/Koha/Suggestions.t

index 9cd1bff..8dbb93a 100755 (executable)
@@ -19,7 +19,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 8;
+use Test::More tests => 9;
 use Test::Exception;
 
 use Koha::Suggestion;
@@ -27,6 +27,7 @@ use Koha::Suggestions;
 use Koha::Database;
 use Koha::DateUtils qw( dt_from_string output_pref );
 
+use t::lib::Mocks;
 use t::lib::TestBuilder;
 
 my $schema = Koha::Database->new->schema;
@@ -257,3 +258,74 @@ subtest 'fund' => sub {
 
     $schema->storage->txn_rollback;
 };
+
+subtest 'search_limited() tests' => sub {
+
+    plan tests => 4;
+
+    $schema->storage->txn_begin;
+
+    # Two libraries
+    my $library_1 = $builder->build_object({ class => 'Koha::Libraries' });
+    my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
+
+    # A patron from $library_1, that is not superlibrarian at all
+    my $patron = $builder->build_object(
+        {
+            class => 'Koha::Patrons',
+            value => { branchcode => $library_1->id, flags => 0 }
+        }
+    );
+
+    # Add 3 suggestions, to be sorted by author
+    my $suggestion_1 = $builder->build_object(
+        {
+            class => 'Koha::Suggestions',
+            value => { branchcode => $library_1->id, author => 'A' }
+        }
+    );
+    my $suggestion_2 = $builder->build_object(
+        {
+            class => 'Koha::Suggestions',
+            value => { branchcode => $library_2->id, author => 'B' }
+        }
+    );
+    my $suggestion_3 = $builder->build_object(
+        {
+            class => 'Koha::Suggestions',
+            value => { branchcode => $library_2->id, author => 'C' }
+        }
+    );
+
+    my $resultset = Koha::Suggestions->search(
+        { branchcode => [ $library_1->id, $library_2->id ] },
+        { order_by   => { -desc => ['author'] } } );
+
+    is( $resultset->count, 3, 'Only this three suggestions are returned' );
+
+    # Now the tests
+    t::lib::Mocks::mock_userenv({ patron => $patron, branchcode => $library_1->id });
+
+    # Disable IndependentBranches
+    t::lib::Mocks::mock_preference( 'IndependentBranches', 0 );
+
+    my $filtered_rs = $resultset->search_limited;
+    is( $filtered_rs->count, 3, 'No IndependentBranches, all suggestions returned' );
+
+    # Enable IndependentBranches
+    t::lib::Mocks::mock_preference( 'IndependentBranches', 1 );
+
+    $filtered_rs = $resultset->search_limited;
+
+    is( $filtered_rs->count, 1, 'IndependentBranches, only suggestions from own branch returned' );
+
+    # Make the patron superlibrarian to override IndependentBranches
+    $patron->flags(1)->store;
+    # So it reloads C4::Context->userenv->{flags}
+    t::lib::Mocks::mock_userenv({ patron => $patron, branchcode => $library_1->id });
+
+    $filtered_rs = $resultset->search_limited;
+    is( $filtered_rs->count, 3, 'IndependentBranches but patron is superlibrarian, all suggestions returned' );
+
+    $schema->storage->txn_rollback;
+};