| 
      
            Lines 17-24
          
      
      
        Link Here
      
     | 
  
        
          | 17 | 
           | 
          17 | 
           | 
        
        
          | 18 | 
          use Modern::Perl;  | 
          18 | 
          use Modern::Perl;  | 
        
        
          | 19 | 
           | 
          19 | 
           | 
        
          
            
              | 20 | 
              use Test::More tests => 6;  | 
              20 | 
              use Test::More tests => 3;  | 
            
            
              | 21 | 
              use Test::Warn;  | 
              21 | 
              use Test::Exception;  | 
            
        
          | 22 | 
           | 
          22 | 
           | 
        
        
          | 23 | 
          use t::lib::Mocks;  | 
          23 | 
          use t::lib::Mocks;  | 
        
        
          | 24 | 
          use t::lib::TestBuilder;  | 
          24 | 
          use t::lib::TestBuilder;  | 
        
  
    | 
      
            Lines 27-89
          use C4::Circulation;
      
      
        Link Here
      
     | 
  
        
          | 27 | 
          use C4::Context;  | 
          27 | 
          use C4::Context;  | 
        
        
          | 28 | 
          use Koha::Checkouts;  | 
          28 | 
          use Koha::Checkouts;  | 
        
        
          | 29 | 
          use Koha::Database;  | 
          29 | 
          use Koha::Database;  | 
        
            
               | 
               | 
              30 | 
              use Koha::Old::Checkouts;  | 
            
        
          | 30 | 
          use Koha::Patrons;  | 
          31 | 
          use Koha::Patrons;  | 
        
        
          | 31 | 
           | 
          32 | 
           | 
        
        
          | 32 | 
          my $schema = Koha::Database->schema;  | 
          33 | 
          my $schema = Koha::Database->schema;  | 
        
            
              | 33 | 
              $schema->storage->txn_begin;  | 
               | 
               | 
            
            
              | 34 | 
               | 
            
        
          | 35 | 
          my $builder = t::lib::TestBuilder->new;  | 
          34 | 
          my $builder = t::lib::TestBuilder->new;  | 
        
        
          | 36 | 
           | 
          35 | 
           | 
        
          
            
              | 37 | 
              my $library = $builder->build({ source => 'Branch' }); | 
              36 | 
              subtest 'Failure tests' => sub { | 
            
        
          | 38 | 
           | 
          37 | 
           | 
        
          
            
              | 39 | 
              t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} }); | 
              38 | 
                  plan tests => 5;  | 
            
        
          | 40 | 
           | 
          39 | 
           | 
        
          
            
              | 41 | 
              my $patron_category = $builder->build({ source => 'Category', value => { category_type => 'P', enrolmentfee => 0 } }); | 
              40 | 
                  $schema->storage->txn_begin;  | 
            
            
              | 42 | 
              my $patron = $builder->build({ source => 'Borrower', value => { branchcode => $library->{branchcode}, categorycode => $patron_category->{categorycode} } } ); | 
               | 
               | 
            
        
          | 43 | 
           | 
          41 | 
           | 
        
          
            
              | 44 | 
              my $biblioitem = $builder->build( { source => 'Biblioitem' } ); | 
              42 | 
                  my $category = $builder->build_object( { class => 'Koha::Patron::Categories', value => { category_type => 'P', enrolmentfee => 0 } } ); | 
            
            
              | 45 | 
              my $item = $builder->build(  | 
              43 | 
                  my $library  = $builder->build_object( { class => 'Koha::Libraries' } ); | 
            
            
              | 46 | 
                  { | 
              44 | 
                  my $patron   = $builder->build_object(  | 
            
            
              | 47 | 
                      source => 'Item',  | 
              45 | 
                      {   class => 'Koha::Patrons', | 
            
            
              | 48 | 
                      value  => { | 
              46 | 
                          value => { branchcode => $library->branchcode, categorycode => $category->categorycode } | 
            
            
              | 49 | 
                          homebranch    => $library->{branchcode}, | 
              47 | 
                      }  | 
            
            
              | 50 | 
                          holdingbranch => $library->{branchcode}, | 
              48 | 
                  );  | 
            
            
              | 51 | 
                          notforloan    => 0,  | 
              49 | 
                  my $biblioitem = $builder->build_object( { class => 'Koha::Biblioitems' } ); | 
            
            
              | 52 | 
                          itemlost      => 0,  | 
              50 | 
                  my $item       = $builder->build_object(  | 
            
            
              | 53 | 
                          withdrawn     => 0,  | 
              51 | 
                      {   class => 'Koha::Items', | 
            
            
              | 54 | 
                          biblionumber  => $biblioitem->{biblionumber}, | 
              52 | 
                          value  => { | 
            
            
               | 
               | 
              53 | 
                              homebranch    => $library->branchcode,  | 
            
            
              | 54 | 
                              holdingbranch => $library->branchcode,  | 
            
            
              | 55 | 
                              notforloan    => 0,  | 
            
            
              | 56 | 
                              itemlost      => 0,  | 
            
            
              | 57 | 
                              withdrawn     => 0,  | 
            
            
              | 58 | 
                              biblionumber  => $biblioitem->biblionumber,  | 
            
            
              | 59 | 
                          }  | 
            
        
          | 55 | 
                  }  | 
          60 | 
                  }  | 
        
          
            
              | 56 | 
                  }  | 
              61 | 
                  );  | 
            
            
              | 57 | 
              );  | 
              62 | 
               | 
            
            
               | 
               | 
              63 | 
                  t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } ); | 
            
            
              | 64 | 
               | 
            
            
              | 65 | 
                  my ( $issue_id, $issue );  | 
            
            
              | 66 | 
                  # The next call will return undef for invalid item number  | 
            
            
              | 67 | 
                  eval { $issue_id = C4::Circulation::MarkIssueReturned( $patron->borrowernumber, 'invalid_itemnumber', undef, 0 ) }; | 
            
            
              | 68 | 
                  is( $@, '', 'No die triggered by invalid itemnumber' );  | 
            
            
              | 69 | 
                  is( $issue_id, undef, 'No issue_id returned' );  | 
            
            
              | 70 | 
               | 
            
            
              | 71 | 
                  # In the next call we return the item and try it another time  | 
            
            
              | 72 | 
                  $issue = C4::Circulation::AddIssue( $patron, $item->barcode );  | 
            
            
              | 73 | 
                  eval { $issue_id = C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item->itemnumber, undef, 0 ) }; | 
            
            
              | 74 | 
                  is( $issue_id, $issue->issue_id, "Item has been returned (issue $issue_id)" );  | 
            
            
              | 75 | 
                  eval { $issue_id = C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item->itemnumber, undef, 0 ) }; | 
            
            
              | 76 | 
                  is( $@, '', 'No crash on returning item twice' );  | 
            
            
              | 77 | 
                  is( $issue_id, undef, 'Cannot return an item twice' );  | 
            
            
              | 78 | 
               | 
            
            
              | 79 | 
               | 
            
            
              | 80 | 
                  $schema->storage->txn_rollback;  | 
            
            
              | 81 | 
              };  | 
            
            
              | 82 | 
               | 
            
            
              | 83 | 
              subtest 'Anonymous patron tests' => sub { | 
            
        
          | 58 | 
           | 
          84 | 
           | 
        
            
              | 59 | 
              subtest 'anonymous patron' => sub { | 
               | 
               | 
            
        
          | 60 | 
              plan tests => 2;  | 
          85 | 
              plan tests => 2;  | 
        
          
            
              | 61 | 
                  # The next call will raise an error, because data are not correctly set  | 
              86 | 
               | 
            
            
              | 62 | 
                  t::lib::Mocks::mock_preference('AnonymousPatron', ''); | 
              87 | 
                  $schema->storage->txn_begin;  | 
            
            
              | 63 | 
                  my $issue = C4::Circulation::AddIssue( $patron, $item->{barcode} ); | 
              88 | 
               | 
            
            
              | 64 | 
                  eval { C4::Circulation::MarkIssueReturned( $patron->{borrowernumber}, $item->{itemnumber}, undef, 2 ) }; | 
              89 | 
                  my $category = $builder->build_object( { class => 'Koha::Patron::Categories', value => { category_type => 'P', enrolmentfee => 0 } } ); | 
            
            
               | 
               | 
              90 | 
                  my $library  = $builder->build_object( { class => 'Koha::Libraries' } ); | 
            
            
              | 91 | 
                  my $patron   = $builder->build_object(  | 
            
            
              | 92 | 
                      {   class => 'Koha::Patrons', | 
            
            
              | 93 | 
                          value => { branchcode => $library->branchcode, categorycode => $category->categorycode } | 
            
            
              | 94 | 
                      }  | 
            
            
              | 95 | 
                  );  | 
            
            
              | 96 | 
                  my $biblioitem = $builder->build_object( { class => 'Koha::Biblioitems' } ); | 
            
            
              | 97 | 
                  my $item       = $builder->build_object(  | 
            
            
              | 98 | 
                      {   class => 'Koha::Items', | 
            
            
              | 99 | 
                          value  => { | 
            
            
              | 100 | 
                              homebranch    => $library->branchcode,  | 
            
            
              | 101 | 
                              holdingbranch => $library->branchcode,  | 
            
            
              | 102 | 
                              notforloan    => 0,  | 
            
            
              | 103 | 
                              itemlost      => 0,  | 
            
            
              | 104 | 
                              withdrawn     => 0,  | 
            
            
              | 105 | 
                              biblionumber  => $biblioitem->biblionumber,  | 
            
            
              | 106 | 
                          }  | 
            
            
              | 107 | 
                      }  | 
            
            
              | 108 | 
                  );  | 
            
            
              | 109 | 
               | 
            
            
              | 110 | 
                  t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } ); | 
            
            
              | 111 | 
               | 
            
            
              | 112 | 
                  # Anonymous patron not set  | 
            
            
              | 113 | 
                  t::lib::Mocks::mock_preference( 'AnonymousPatron', '' );  | 
            
            
              | 114 | 
               | 
            
            
              | 115 | 
                  my $issue = C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );  | 
            
            
              | 116 | 
                  eval { C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item->itemnumber, undef, 2 ) }; | 
            
        
          | 65 | 
              like ( $@, qr<Fatal error: the patron \(\d+\) .* AnonymousPatron>, 'AnonymousPatron is not set - Fatal error on anonymization' );  | 
          117 | 
              like ( $@, qr<Fatal error: the patron \(\d+\) .* AnonymousPatron>, 'AnonymousPatron is not set - Fatal error on anonymization' );  | 
        
        
          | 66 | 
              Koha::Checkouts->find( $issue->issue_id )->delete;  | 
          118 | 
              Koha::Checkouts->find( $issue->issue_id )->delete;  | 
        
        
          | 67 | 
           | 
          119 | 
           | 
        
          
            
              | 68 | 
                  my $anonymous_borrowernumber = Koha::Patron->new({categorycode => $patron_category->{categorycode}, branchcode => $library->{branchcode} })->store->borrowernumber; | 
              120 | 
                  # Create a valid anonymous user  | 
            
            
              | 69 | 
                  t::lib::Mocks::mock_preference('AnonymousPatron', $anonymous_borrowernumber); | 
              121 | 
                  my $anonymous = $builder->build_object({ | 
            
            
              | 70 | 
                  $issue = C4::Circulation::AddIssue( $patron, $item->{barcode} ); | 
              122 | 
                      class => 'Koha::Patrons',  | 
            
            
              | 71 | 
                  eval { C4::Circulation::MarkIssueReturned( $patron->{borrowernumber}, $item->{itemnumber}, undef, 2 ) }; | 
              123 | 
                      value => { | 
            
            
               | 
               | 
              124 | 
                          categorycode => $category->categorycode,  | 
            
            
              | 125 | 
                          branchcode => $library->branchcode  | 
            
            
              | 126 | 
                      }  | 
            
            
              | 127 | 
                  });  | 
            
            
              | 128 | 
                  t::lib::Mocks::mock_preference('AnonymousPatron', $anonymous->borrowernumber); | 
            
            
              | 129 | 
                  $issue = C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );  | 
            
            
              | 130 | 
               | 
            
            
              | 131 | 
                  eval { C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item->itemnumber, undef, 2 ) }; | 
            
        
          | 72 | 
              is ( $@, q||, 'AnonymousPatron is set correctly - no error expected');  | 
          132 | 
              is ( $@, q||, 'AnonymousPatron is set correctly - no error expected');  | 
        
            
               | 
               | 
              133 | 
               | 
            
            
              | 134 | 
                  $schema->storage->txn_rollback;  | 
            
        
          | 73 | 
          };  | 
          135 | 
          };  | 
        
        
          | 74 | 
           | 
          136 | 
           | 
        
          
            
              | 75 | 
              my ( $issue_id, $issue );  | 
              137 | 
              subtest 'Manually pass a return date' => sub { | 
            
            
              | 76 | 
              # The next call will return undef for invalid item number  | 
              138 | 
               | 
            
            
              | 77 | 
              eval { $issue_id = C4::Circulation::MarkIssueReturned( $patron->{borrowernumber}, 'invalid_itemnumber', undef, 0 ) }; | 
              139 | 
                  plan tests => 3;  | 
            
            
              | 78 | 
              is( $@, '', 'No die triggered by invalid itemnumber' );  | 
              140 | 
               | 
            
            
              | 79 | 
              is( $issue_id, undef, 'No issue_id returned' );  | 
              141 | 
                  $schema->storage->txn_begin;  | 
            
            
              | 80 | 
               | 
              142 | 
               | 
            
            
              | 81 | 
              # In the next call we return the item and try it another time  | 
              143 | 
                  my $category = $builder->build_object( { class => 'Koha::Patron::Categories', value => { category_type => 'P', enrolmentfee => 0 } } ); | 
            
            
              | 82 | 
              $issue = C4::Circulation::AddIssue( $patron, $item->{barcode} ); | 
              144 | 
                  my $library = $builder->build_object( { class => 'Koha::Libraries' } ); | 
            
            
              | 83 | 
              eval { $issue_id = C4::Circulation::MarkIssueReturned( $patron->{borrowernumber}, $item->{itemnumber}, undef, 0 ) }; | 
              145 | 
                  my $patron  = $builder->build_object(  | 
            
            
              | 84 | 
              is( $issue_id, $issue->issue_id, "Item has been returned (issue $issue_id)" );  | 
              146 | 
                      {   class => 'Koha::Patrons', | 
            
            
              | 85 | 
              eval { $issue_id = C4::Circulation::MarkIssueReturned( $patron->{borrowernumber}, $item->{itemnumber}, undef, 0 ) }; | 
              147 | 
                          value => { branchcode => $library->branchcode, categorycode => $category->categorycode } | 
            
            
              | 86 | 
              is( $@, '', 'No crash on returning item twice' );  | 
              148 | 
                      }  | 
            
            
              | 87 | 
              is( $issue_id, undef, 'Cannot return an item twice' );  | 
              149 | 
                  );  | 
            
            
              | 88 | 
               | 
              150 | 
                  my $biblioitem = $builder->build_object( { class => 'Koha::Biblioitems' } ); | 
            
            
              | 89 | 
              $schema->storage->txn_rollback;  | 
              151 | 
                  my $item       = $builder->build_object(  | 
            
            
               | 
               | 
              152 | 
                      {   class => 'Koha::Items', | 
            
            
              | 153 | 
                          value  => { | 
            
            
              | 154 | 
                              homebranch    => $library->branchcode,  | 
            
            
              | 155 | 
                              holdingbranch => $library->branchcode,  | 
            
            
              | 156 | 
                              notforloan    => 0,  | 
            
            
              | 157 | 
                              itemlost      => 0,  | 
            
            
              | 158 | 
                              withdrawn     => 0,  | 
            
            
              | 159 | 
                              biblionumber  => $biblioitem->biblionumber,  | 
            
            
              | 160 | 
                          }  | 
            
            
              | 161 | 
                      }  | 
            
            
              | 162 | 
                  );  | 
            
            
              | 163 | 
               | 
            
            
              | 164 | 
                  t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode }); | 
            
            
              | 165 | 
               | 
            
            
              | 166 | 
                  my ( $issue, $issue_id );  | 
            
            
              | 167 | 
               | 
            
            
              | 168 | 
                  $issue = C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );  | 
            
            
              | 169 | 
                  $issue_id = C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item->itemnumber, '2018-12-25', 0 );  | 
            
            
              | 170 | 
               | 
            
            
              | 171 | 
                  is( $issue_id, $issue->issue_id, "Item has been returned" );  | 
            
            
              | 172 | 
                  my $old_checkout = Koha::Old::Checkouts->find( $issue_id );  | 
            
            
              | 173 | 
                  is( $old_checkout->returndate, '2018-12-25 00:00:00', 'Manually passed date stored correctly' );  | 
            
            
              | 174 | 
               | 
            
            
              | 175 | 
                  $issue = C4::Circulation::AddIssue( $patron, $item->barcode );  | 
            
            
              | 176 | 
               | 
            
            
              | 177 | 
                  throws_ok  | 
            
            
              | 178 | 
                      { $issue_id = C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item->itemnumber, 'bad_date', 0 ); } | 
            
            
              | 179 | 
                      'Koha::Exceptions::Object::BadValue',  | 
            
            
              | 180 | 
                      'An exception is thrown on bad date';  | 
            
            
              | 181 | 
               | 
            
            
              | 182 | 
                  $schema->storage->txn_rollback;  | 
            
            
              | 183 | 
              };  | 
            
            
              | 90 | 
              -   | 
               | 
               |