View | Details | Raw Unified | Return to bug 27945
Collapse All | Expand All

(-)a/Koha/ArticleRequest.pm (-4 / +4 lines)
Lines 48-53 Koha::ArticleRequest - Koha Article Request Object class Link Here
48
sub request {
48
sub request {
49
    my ($self) = @_;
49
    my ($self) = @_;
50
50
51
    Koha::Exceptions::ArticleRequest::LimitReached->throw(
52
        error => 'Patron cannot request more articles for today'
53
    ) unless $self->borrower->can_request_article;
54
51
    $self->status(Koha::ArticleRequest::Status::Requested);
55
    $self->status(Koha::ArticleRequest::Status::Requested);
52
    $self->SUPER::store();
56
    $self->SUPER::store();
53
    $self->notify();
57
    $self->notify();
Lines 61-70 sub request { Link Here
61
sub set_pending {
65
sub set_pending {
62
    my ($self) = @_;
66
    my ($self) = @_;
63
67
64
    Koha::Exceptions::ArticleRequest::LimitReached->throw(
65
        error => 'Patron cannot request more articles for today'
66
    ) unless $self->borrower->can_request_article;
67
68
    $self->status(Koha::ArticleRequest::Status::Pending);
68
    $self->status(Koha::ArticleRequest::Status::Pending);
69
    $self->SUPER::store();
69
    $self->SUPER::store();
70
    $self->notify();
70
    $self->notify();
(-)a/Koha/Patron.pm (-3 / +2 lines)
Lines 970-987 sub can_request_article { Link Here
970
    my ($self) = @_;
970
    my ($self) = @_;
971
    my $limit = $self->category->article_request_limit;
971
    my $limit = $self->category->article_request_limit;
972
972
973
    return 1 if !$limit;
973
    return 1 unless defined $limit;
974
974
975
    my $dtf = Koha::Database->new->schema->storage->datetime_parser;
975
    my $dtf = Koha::Database->new->schema->storage->datetime_parser;
976
    my $compdate = dt_from_string->add( days => -1 );
976
    my $compdate = dt_from_string->add( days => -1 );
977
    my $count = Koha::ArticleRequests->search([
977
    my $count = Koha::ArticleRequests->search([
978
        { borrowernumber => $self->borrowernumber, status => ['PENDING','PROCESSING'] },
978
        { borrowernumber => $self->borrowernumber, status => ['REQUESTED','PENDING','PROCESSING'] },
979
        { borrowernumber => $self->borrowernumber, status => 'COMPLETED', updated_on => { '>', $dtf->format_date($compdate) }},
979
        { borrowernumber => $self->borrowernumber, status => 'COMPLETED', updated_on => { '>', $dtf->format_date($compdate) }},
980
    ])->count;
980
    ])->count;
981
    return $count < $limit ? 1 : 0;
981
    return $count < $limit ? 1 : 0;
982
}
982
}
983
983
984
985
=head3 article_requests
984
=head3 article_requests
986
985
987
my @requests = $borrower->article_requests();
986
my @requests = $borrower->article_requests();
(-)a/t/db_dependent/ArticleRequests.t (-97 lines)
Lines 252-354 subtest 'may_article_request' => sub { Link Here
252
    $cache->clear_from_cache( Koha::CirculationRules::GUESSED_ITEMTYPES_KEY );
252
    $cache->clear_from_cache( Koha::CirculationRules::GUESSED_ITEMTYPES_KEY );
253
};
253
};
254
254
255
256
subtest 'article request limit' => sub {
257
  plan tests => 13;
258
259
  t::lib::Mocks::mock_preference('ArticleRequests', 1);
260
261
  my $item = $builder->build_sample_item;
262
263
  my $category = $builder->build_object(
264
    {
265
      class => 'Koha::Patron::Categories',
266
      value => {
267
        article_request_limit => 1
268
      }
269
    }
270
  );
271
  my $patron   = $builder->build_object(
272
    {
273
      class => 'Koha::Patrons',
274
      value => {
275
        categorycode => $category->categorycode
276
      },
277
    }
278
  );
279
  $patron->article_requests->delete();
280
281
  is($patron->can_request_article, 1, 'There are no AR, so patron can request more articles');
282
283
  my $article_request_1 = Koha::ArticleRequest->new(
284
    {
285
      borrowernumber => $patron->id,
286
      biblionumber   => $item->biblionumber,
287
      itemnumber     => $item->itemnumber,
288
      title          => 'an article request',
289
    }
290
  )->store();
291
292
  is($patron->can_request_article, 0, 'Limit is 1, so patron cannot request more articles');
293
  is($patron->article_requests->count, 1, 'There is one current article request');
294
295
  try {
296
    Koha::ArticleRequest->new(
297
      {
298
        borrowernumber => $patron->id,
299
        biblionumber   => $item->biblionumber,
300
        itemnumber     => $item->itemnumber,
301
        title          => 'a second article request',
302
      }
303
    )->store();
304
  }
305
  catch {
306
    is(ref($_), 'Koha::Exceptions::ArticleRequest::LimitReached', 'When limit was reached and we ask for a new AR, Limit reached is thrown');
307
  };
308
309
  is($patron->can_request_article, 0, 'There is still an AR, so patron cannot request more articles');
310
  is($patron->article_requests->count, 1, 'There is still one article request');
311
312
  $article_request_1->complete();
313
314
  is($patron->can_request_article, 0, 'AR was completed but within one day, so patron cannot request more articles');
315
316
  $article_request_1->updated_on(dt_from_string->add(days => -2))->store();
317
318
  is($patron->can_request_article, 1, 'There are no completed AR within one day, so patron can request more articles');
319
320
  my $article_request_3 = Koha::ArticleRequest->new(
321
    {
322
      borrowernumber => $patron->id,
323
      biblionumber   => $item->biblionumber,
324
      itemnumber     => $item->itemnumber,
325
      title          => 'a third article request',
326
    }
327
  )->store();
328
329
  is($patron->can_request_article, 0, 'A new AR was created, so patron cannot request more articles');
330
  is($patron->article_requests->count, 2, 'There are 2 article requests');
331
332
  $article_request_3->cancel();
333
334
  is($patron->can_request_article, 1, 'New AR was cancelled, so patron can request more articles');
335
336
  my $article_request_4 = Koha::ArticleRequest->new(
337
    {
338
      borrowernumber => $patron->id,
339
      biblionumber   => $item->biblionumber,
340
      itemnumber     => $item->itemnumber,
341
      title          => 'an fourth article request',
342
    }
343
  )->store();
344
345
  $article_request_4->updated_on(dt_from_string->add(days => -30))->store();
346
347
  is($patron->can_request_article, 0, 'There is an old AR but not completed or cancelled, so patron cannot request more articles');
348
  is($patron->article_requests->count, 3, 'There are 3 current article requests');
349
350
};
351
352
$schema->storage->txn_rollback();
255
$schema->storage->txn_rollback();
353
256
354
subtest 'set_pending() tests' => sub {
257
subtest 'set_pending() tests' => sub {
(-)a/t/db_dependent/Koha/Patron.t (-2 / +116 lines)
Lines 19-30 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 8;
22
use Test::More tests => 9;
23
use Test::Exception;
23
use Test::Exception;
24
use Test::Warn;
24
use Test::Warn;
25
25
26
use Koha::Database;
26
use Koha::Database;
27
use Koha::DateUtils qw(dt_from_string);
27
use Koha::DateUtils qw(dt_from_string);
28
use Koha::ArticleRequests;
28
use Koha::Patrons;
29
use Koha::Patrons;
29
use Koha::Patron::Relationships;
30
use Koha::Patron::Relationships;
30
31
Lines 716-718 subtest 'can_log_into() tests' => sub { Link Here
716
717
717
    $schema->storage->txn_rollback;
718
    $schema->storage->txn_rollback;
718
};
719
};
719
- 
720
721
subtest 'can_request_article() tests' => sub {
722
723
    plan tests => 13;
724
725
    $schema->storage->txn_begin;
726
727
    t::lib::Mocks::mock_preference( 'ArticleRequests', 1 );
728
729
    my $item = $builder->build_sample_item;
730
731
    my $category = $builder->build_object(
732
        {
733
            class => 'Koha::Patron::Categories',
734
            value => {
735
                article_request_limit => 1
736
            }
737
        }
738
    );
739
    my $patron = $builder->build_object(
740
        {
741
            class => 'Koha::Patrons',
742
            value => {
743
                categorycode => $category->categorycode
744
            },
745
        }
746
    );
747
748
    is( $patron->can_request_article,
749
        1, 'There are no AR, so patron can request more articles' );
750
751
    my $article_request_1 = Koha::ArticleRequest->new(
752
        {
753
            borrowernumber => $patron->id,
754
            biblionumber   => $item->biblionumber,
755
            itemnumber     => $item->itemnumber,
756
            title          => 'an article request',
757
        }
758
    )->request;
759
760
    is( $patron->can_request_article,
761
        0, 'Limit is 1, so patron cannot request more articles' );
762
    is( $patron->article_requests->count,
763
        1, 'There is one current article request' );
764
765
    throws_ok {
766
        Koha::ArticleRequest->new(
767
            {
768
                borrowernumber => $patron->id,
769
                biblionumber   => $item->biblionumber,
770
                itemnumber     => $item->itemnumber,
771
                title          => 'a second article request',
772
            }
773
        )->request;
774
    }
775
    'Koha::Exceptions::ArticleRequest::LimitReached',
776
      'When limit was reached and we ask for a new AR, Limit reached is thrown';
777
778
    is( $patron->can_request_article,
779
        0, 'There is still an AR, so patron cannot request more articles' );
780
    is( $patron->article_requests->count,
781
        1, 'There is still one article request' );
782
783
    $article_request_1->complete();
784
785
    is( $patron->can_request_article, 0,
786
'AR was completed but within one day, so patron cannot request more articles'
787
    );
788
789
    $article_request_1->updated_on( dt_from_string->add( days => -2 ) )
790
      ->store();
791
792
    is( $patron->can_request_article, 1,
793
'There are no completed AR within one day, so patron can request more articles'
794
    );
795
796
    my $article_request_3 = Koha::ArticleRequest->new(
797
        {
798
            borrowernumber => $patron->id,
799
            biblionumber   => $item->biblionumber,
800
            itemnumber     => $item->itemnumber,
801
            title          => 'a third article request',
802
        }
803
    )->request;
804
805
    is( $patron->can_request_article,
806
        0, 'A new AR was created, so patron cannot request more articles' );
807
    is( $patron->article_requests->count, 2, 'There are 2 article requests' );
808
809
    $article_request_3->cancel();
810
811
    is( $patron->can_request_article,
812
        1, 'New AR was cancelled, so patron can request more articles' );
813
814
    my $article_request_4 = Koha::ArticleRequest->new(
815
        {
816
            borrowernumber => $patron->id,
817
            biblionumber   => $item->biblionumber,
818
            itemnumber     => $item->itemnumber,
819
            title          => 'an fourth article request',
820
        }
821
    )->request;
822
823
    $article_request_4->updated_on( dt_from_string->add( days => -30 ) )
824
      ->store();
825
826
    is( $patron->can_request_article, 0,
827
'There is an old AR but not completed or cancelled, so patron cannot request more articles'
828
    );
829
    is( $patron->article_requests->count,
830
        3, 'There are 3 current article requests' );
831
832
    $schema->storage->txn_rollback;
833
};

Return to bug 27945