Bug 20287: Replace occurrences of AddMember with Koha::Patron->new->store->borrowernumber
[koha.git] / t / db_dependent / Holds.t
index 241d024..93fe745 100755 (executable)
@@ -11,7 +11,6 @@ use Test::More tests => 55;
 use MARC::Record;
 use C4::Biblio;
 use C4::Items;
-use C4::Members;
 use C4::Calendar;
 use Koha::Database;
 use Koha::DateUtils qw( dt_from_string output_pref );
@@ -58,12 +57,12 @@ my ($item_bibnum, $item_bibitemnum, $itemnumber)
 # Create some borrowers
 my @borrowernumbers;
 foreach (1..$borrowers_count) {
-    my $borrowernumber = AddMember(
+    my $borrowernumber = Koha::Patron->new({
         firstname =>  'my firstname',
         surname => 'my surname ' . $_,
         categorycode => $category->{categorycode},
         branchcode => $branch_1,
-    );
+    })->store->borrowernumber;
     push @borrowernumbers, $borrowernumber;
 }
 
@@ -121,17 +120,13 @@ ok( $hold_branch == $hold->branch(), "branch method returns stashed branch" );
 my $hold_found = $hold->found();
 $hold->set({ found => 'W'})->store();
 is( Koha::Holds->waiting()->count(), 1, "Koha::Holds->waiting returns waiting holds" );
+is( Koha::Holds->unfilled()->count(), 4, "Koha::Holds->unfilled returns unfilled holds" );
 
 my $patron = Koha::Patrons->find( $borrowernumbers[0] );
 $holds = $patron->holds;
 is( $holds->next->borrowernumber, $borrowernumbers[0], "Test Koha::Patron->holds");
 
 
-Koha::Holds->find( $reserve_id )->cancel;
-
-$holds = $biblio->holds;
-is( $holds->count, $borrowers_count - 1, "Koha::Hold->cancel" );
-
 $holds = $item->current_holds;
 $first_hold = $holds->next;
 $borrowernumber = $first_hold->borrowernumber;
@@ -151,6 +146,15 @@ ok( $hold->priority eq '4', "Test ModReserve, priority changed correctly" );
 ok( $hold->suspend, "Test ModReserve, suspend hold" );
 is( $hold->suspend_until, '2013-01-01 00:00:00', "Test ModReserve, suspend until date" );
 
+ModReserve({ # call without reserve_id
+    rank          => '3',
+    biblionumber  => $item_bibnum,
+    itemnumber    => $itemnumber,
+    borrowernumber => $borrowernumber,
+});
+$hold = Koha::Holds->find( $reserve_id );
+ok( $hold->priority eq '3', "Test ModReserve, priority changed correctly" );
+
 ToggleSuspend( $reserve_id );
 $hold = Koha::Holds->find( $reserve_id );
 ok( ! $hold->suspend, "Test ToggleSuspend(), no date" );
@@ -219,7 +223,7 @@ is( $hold->priority, '1', "Test AlterPriority(), move up" );
 
 AlterPriority( 'bottom', $hold->reserve_id );
 $hold = Koha::Holds->find( $reserveid );
-is( $hold->priority, '5', "Test AlterPriority(), move to bottom" );
+is( $hold->priority, '6', "Test AlterPriority(), move to bottom" );
 
 # Regression test for bug 2394
 #
@@ -275,49 +279,22 @@ ok(
     '... unless canreservefromotherbranches is ON (bug 2394)'
 );
 
-# Regression test for bug 11336
-($bibnum, $title, $bibitemnum) = create_helper_biblio('DUMMY');
-($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
-AddReserve(
-    $branch_1,
-    $borrowernumbers[0],
-    $bibnum,
-    '',
-    1,
-);
-
-my $reserveid1 = Koha::Holds->search({ biblionumber => $bibnum, borrowernumber => $borrowernumbers[0] })->next->reserve_id;
-
-($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
-my $reserveid2 = AddReserve(
-    $branch_1,
-    $borrowernumbers[1],
-    $bibnum,
-    '',
-    2,
-);
-
-my $hold1 = Koha::Holds->find( $reserveid1 );
-$hold1->cancel;
-
-my $hold2 = Koha::Holds->find( $reserveid2 );
-is( $hold2->priority, 1, "After cancelreserve, the 2nd reserve becomes the first on the waiting list" );
-
-($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
-my $reserveid3 = AddReserve(
-    $branch_1,
-    $borrowernumbers[0],
-    $bibnum,
-    '',
-    2,
-);
-
-my $hold3 = Koha::Holds->find( $reserveid3 );
-is( $hold3->priority, 2, "New reserve for patron 0, the reserve has a priority = 2" );
-
-ModReserve({ reserve_id => $reserveid2, rank => 'del' });
-$hold3 = Koha::Holds->find( $reserveid3 );
-is( $hold3->priority, 1, "After ModReserve, the 3rd reserve becomes the first on the waiting list" );
+{
+    # Regression test for bug 11336 # Test if ModReserve correctly recalculate the priorities
+    ($bibnum, $title, $bibitemnum) = create_helper_biblio('DUMMY');
+    ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
+    my $reserveid1 = AddReserve($branch_1, $borrowernumbers[0], $bibnum, '', 1);
+    ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
+    my $reserveid2 = AddReserve($branch_1, $borrowernumbers[1], $bibnum, '', 2);
+    ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
+    my $reserveid3 = AddReserve($branch_1, $borrowernumbers[2], $bibnum, '', 3);
+    my $hhh = Koha::Holds->search({ biblionumber => $bibnum });
+    my $hold3 = Koha::Holds->find( $reserveid3 );
+    is( $hold3->priority, 3, "The 3rd hold should have a priority set to 3" );
+    ModReserve({ reserve_id => $reserveid1, rank => 'del' });
+    ModReserve({ reserve_id => $reserveid2, rank => 'del' });
+    is( $hold3->discard_changes->priority, 1, "After ModReserve, the 3rd reserve becomes the first on the waiting list" );
+}
 
 ModItem({ damaged => 1 }, $item_bibnum, $itemnumber);
 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 1 );
@@ -393,18 +370,18 @@ $dbh->do(q{
 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
     { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $bibnum);
 is(CanItemBeReserved($borrowernumbers[0], $itemnumber), 'notReservable',
-    "CanItemBeReserved should returns 'notReservable'");
+    "CanItemBeReserved should return 'notReservable'");
 
 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
     { homebranch => $branch_2, holdingbranch => $branch_1, itype => 'CAN' } , $bibnum);
 is(CanItemBeReserved($borrowernumbers[0], $itemnumber),
     'cannotReserveFromOtherBranches',
-    "CanItemBeReserved should returns 'cannotReserveFromOtherBranches'");
+    "CanItemBeReserved should return 'cannotReserveFromOtherBranches'");
 
 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
     { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CAN' } , $bibnum);
 is(CanItemBeReserved($borrowernumbers[0], $itemnumber), 'OK',
-    "CanItemBeReserved should returns 'OK'");
+    "CanItemBeReserved should return 'OK'");
 
 # Bug 12632
 t::lib::Mocks::mock_preference( 'item-level_itypes',     1 );