Bug 22144: (QA follow-up) Prepare the ground for other formats
[koha.git] / t / db_dependent / ArticleRequests.t
index cc43185..84ddbde 100755 (executable)
@@ -19,16 +19,18 @@ use Modern::Perl;
 
 use POSIX qw(strftime);
 
-use Test::More tests => 55;
+use Test::More tests => 56;
 
 use t::lib::TestBuilder;
+use t::lib::Mocks;
 
 use Koha::Database;
 use Koha::Biblio;
 use Koha::Notice::Messages;
 use Koha::Patron;
-
-use t::lib::TestBuilder;
+use Koha::Library::Group;
+use Koha::IssuingRules;
+use Koha::Caches;
 
 BEGIN {
     use_ok('Koha::ArticleRequest');
@@ -39,6 +41,7 @@ BEGIN {
 my $schema = Koha::Database->new()->schema();
 $schema->storage->txn_begin();
 my $builder = t::lib::TestBuilder->new;
+our $cache = Koha::Caches->get_instance;
 
 my $dbh = C4::Context->dbh;
 $dbh->{RaiseError} = 1;
@@ -67,17 +70,16 @@ ok( $item->id, 'Koha::Item created' );
 
 my $branch   = $builder->build({ source => 'Branch' });
 my $category = $builder->build({ source => 'Category' });
-my $patron   = Koha::Patron->new(
-    {
+my $patron   = $builder->build_object({
+    class => 'Koha::Patrons',
+    value => {
         categorycode => $category->{categorycode},
         branchcode   => $branch->{branchcode},
         flags        => 1,# superlibrarian
-        userid       => 'a_userid_for_tests', # So far Koha::Patron->store does not deal with userid, see bug 20287
-    }
-)->store();
+    },
+});
 ok( $patron->id, 'Koha::Patron created' );
-my $patron_2 = $builder->build({ source => 'Borrower', value => { flags => 0 } });
-$patron_2 = Koha::Patrons->find( $patron_2->{borrowernumber} );
+my $patron_2 = $builder->build_object({ class => 'Koha::Patrons', value => { flags => 0 } });
 
 # store
 Koha::Notice::Messages->delete;
@@ -204,31 +206,40 @@ is( $item->article_request_type($patron), 'no', 'Item article request type is no
 $rule->delete();
 
 subtest 'search_limited' => sub {
-    plan tests => 4;
-    C4::Context->_new_userenv('xxx');
+    plan tests => 2;
     my $nb_article_requests = Koha::ArticleRequests->count;
 
     my $group_1 = Koha::Library::Group->new( { title => 'TEST Group 1' } )->store;
     my $group_2 = Koha::Library::Group->new( { title => 'TEST Group 2' } )->store;
     Koha::Library::Group->new({ parent_id => $group_1->id,  branchcode => $patron->branchcode })->store();
     Koha::Library::Group->new({ parent_id => $group_2->id,  branchcode => $patron_2->branchcode })->store();
-    set_logged_in_user( $patron ); # Is superlibrarian
-    is( Koha::ArticleRequests->count, $nb_article_requests, 'Koha::ArticleRequests should return all article requests' );
+    t::lib::Mocks::mock_userenv( { patron => $patron } ); # Is superlibrarian
     is( Koha::ArticleRequests->search_limited->count, $nb_article_requests, 'Koha::ArticleRequests->search_limited should return all article requests for superlibrarian' );
-    set_logged_in_user( $patron_2 ); # Is restricted
-    is( Koha::ArticleRequests->count, $nb_article_requests, 'Koha::ArticleRequests should return all article requests' );
+    t::lib::Mocks::mock_userenv( { patron => $patron_2 } ); # Is restricted
     is( Koha::ArticleRequests->search_limited->count, 0, 'Koha::ArticleRequests->search_limited should not return all article requests for restricted patron' );
 };
 
-$schema->storage->txn_rollback();
+subtest 'may_article_request' => sub {
+    plan tests => 4;
 
-sub set_logged_in_user {
-    my ($patron) = @_;
-    C4::Context->set_userenv(
-        $patron->borrowernumber, $patron->userid,
-        $patron->cardnumber,     'firstname',
-        'surname',               $patron->library->branchcode,
-        'Midway Public Library', $patron->flags,
-        '',                      ''
-    );
-}
+    # mocking
+    t::lib::Mocks::mock_preference('ArticleRequests', 1);
+    t::lib::Mocks::mock_preference('ArticleRequestsLinkControl', 'calc');
+    $cache->set_in_cache( Koha::IssuingRules::GUESSED_ITEMTYPES_KEY, {
+        '*'  => { 'CR' => 1 },
+        'S'  => { '*'  => 1 },
+        'PT' => { 'BK' => 1 },
+    });
+
+    my $itemtype = Koha::ItemTypes->find('CR') // Koha::ItemType->new({ itemtype => 'CR' })->store;
+    is( $itemtype->may_article_request, 1, 'SER/* should be true' );
+    is( $itemtype->may_article_request({ categorycode => 'S' }), 1, 'SER/S should be true' );
+    is( $itemtype->may_article_request({ categorycode => 'PT' }), '', 'SER/PT should be false' );
+    t::lib::Mocks::mock_preference('ArticleRequestsLinkControl', 'always');
+    is( $itemtype->may_article_request({ categorycode => 'PT' }), '1', 'Result should be true when LinkControl is set to always' );
+
+    # Cleanup
+    $cache->clear_from_cache( Koha::IssuingRules::GUESSED_ITEMTYPES_KEY );
+};
+
+$schema->storage->txn_rollback();