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

(-)a/t/db_dependent/Reserves.t (-3 / +110 lines)
Lines 17-23 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 69;
20
use Test::More tests => 70;
21
use Test::NoWarnings;
21
use Test::NoWarnings;
22
use Test::MockModule;
22
use Test::MockModule;
23
use Test::Warn;
23
use Test::Warn;
Lines 35-41 use C4::Biblio qw( GetMarcFromKohaField ModBiblio ); Link Here
35
use C4::HoldsQueue;
35
use C4::HoldsQueue;
36
use C4::Members;
36
use C4::Members;
37
use C4::Reserves
37
use C4::Reserves
38
    qw( AddReserve AlterPriority CheckReserves ModReserve ModReserveAffect ReserveSlip CalculatePriority CanBookBeReserved IsAvailableForItemLevelRequest MoveReserve CanItemBeReserved MergeHolds );
38
    qw( AddReserve AlterPriority CheckReserves FixPriority ModReserve ModReserveAffect ReserveSlip CalculatePriority CanBookBeReserved IsAvailableForItemLevelRequest MoveReserve CanItemBeReserved MergeHolds ToggleLowestPriority );
39
use Koha::ActionLogs;
39
use Koha::ActionLogs;
40
use Koha::Biblios;
40
use Koha::Biblios;
41
use Koha::Caches;
41
use Koha::Caches;
Lines 2145-2147 subtest 'Bug 40866: AddReserve override JSON logging' => sub { Link Here
2145
2145
2146
    $schema->storage->txn_rollback;
2146
    $schema->storage->txn_rollback;
2147
};
2147
};
2148
- 
2148
2149
subtest 'FixPriority() lowestPriority tests' => sub {
2150
2151
    plan tests => 14;
2152
2153
    $schema->storage->txn_begin;
2154
2155
    my $library = $builder->build_object( { class => 'Koha::Libraries' } );
2156
    my $biblio  = $builder->build_sample_biblio;
2157
2158
    # Helper: place holds with explicit priorities to avoid tie-breaking ambiguity
2159
    my @patrons;
2160
    my @reserve_ids;
2161
    for my $i ( 1 .. 5 ) {
2162
        my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
2163
        push @patrons, $patron;
2164
        my $rid = AddReserve(
2165
            {
2166
                branchcode     => $library->branchcode,
2167
                borrowernumber => $patron->id,
2168
                biblionumber   => $biblio->id,
2169
                priority       => $i,
2170
            }
2171
        );
2172
        push @reserve_ids, $rid;
2173
    }
2174
2175
    # Mark holds 4 and 5 as lowestPriority; initial priorities should be 1..5
2176
    ToggleLowestPriority( $reserve_ids[3] );    # hold 4 → lowestPriority, pushed to bottom
2177
    ToggleLowestPriority( $reserve_ids[4] );    # hold 5 → lowestPriority, pushed to bottom
2178
2179
    my @holds = map { Koha::Holds->find($_) } @reserve_ids;
2180
2181
    is( $holds[0]->priority, 1, 'Hold 1 priority is 1' );
2182
    is( $holds[1]->priority, 2, 'Hold 2 priority is 2' );
2183
    is( $holds[2]->priority, 3, 'Hold 3 priority is 3' );
2184
    $holds[3]->discard_changes;
2185
    $holds[4]->discard_changes;
2186
    ok( $holds[3]->priority > $holds[2]->priority, 'lowestPriority hold 4 is after non-lowest holds' );
2187
    ok( $holds[4]->priority > $holds[2]->priority, 'lowestPriority hold 5 is after non-lowest holds' );
2188
2189
    # Attempting to move a lowestPriority hold to rank 1 should be clamped
2190
    FixPriority( { reserve_id => $reserve_ids[3], rank => 1 } );
2191
    $holds[3]->discard_changes;
2192
    $holds[0]->discard_changes;
2193
    $holds[1]->discard_changes;
2194
    $holds[2]->discard_changes;
2195
    ok(
2196
        $holds[3]->priority > $holds[2]->priority,
2197
        'lowestPriority hold cannot be moved above non-lowestPriority holds'
2198
    );
2199
    is( $holds[0]->priority, 1, 'Hold 1 priority unchanged after clamped move' );
2200
    is( $holds[1]->priority, 2, 'Hold 2 priority unchanged after clamped move' );
2201
    is( $holds[2]->priority, 3, 'Hold 3 priority unchanged after clamped move' );
2202
2203
    # Moving a lowestPriority hold within the lowest-priority group should work
2204
    my $p4_before = $holds[3]->priority;
2205
    my $p5_before = $holds[4]->priority;
2206
    FixPriority( { reserve_id => $reserve_ids[3], rank => $p5_before } );
2207
    $holds[3]->discard_changes;
2208
    $holds[4]->discard_changes;
2209
    isnt( $holds[3]->priority, $p4_before, 'lowestPriority hold moved within lowest-priority group' );
2210
    ok( $holds[3]->priority > $holds[2]->priority, 'Moved lowestPriority hold still below non-lowest holds' );
2211
2212
    # When ALL holds are lowestPriority, movement is unconstrained
2213
    my $biblio2 = $builder->build_sample_biblio;
2214
    my @all_low_ids;
2215
    for my $i ( 1 .. 3 ) {
2216
        my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
2217
        my $rid    = AddReserve(
2218
            {
2219
                branchcode     => $library->branchcode,
2220
                borrowernumber => $patron->id,
2221
                biblionumber   => $biblio2->id,
2222
                priority       => $i,
2223
            }
2224
        );
2225
        ToggleLowestPriority($rid);
2226
        push @all_low_ids, $rid;
2227
    }
2228
    my $h1 = Koha::Holds->find( $all_low_ids[2] );
2229
    FixPriority( { reserve_id => $all_low_ids[2], rank => 1 } );
2230
    $h1->discard_changes;
2231
    is( $h1->priority, 1, 'lowestPriority hold moves freely when all holds are lowestPriority' );
2232
2233
    # When NO holds are lowestPriority, FixPriority works as before
2234
    my $biblio3 = $builder->build_sample_biblio;
2235
    my @normal_ids;
2236
    for my $i ( 1 .. 3 ) {
2237
        my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
2238
        my $rid    = AddReserve(
2239
            {
2240
                branchcode     => $library->branchcode,
2241
                borrowernumber => $patron->id,
2242
                biblionumber   => $biblio3->id,
2243
                priority       => $i,
2244
            }
2245
        );
2246
        push @normal_ids, $rid;
2247
    }
2248
    FixPriority( { reserve_id => $normal_ids[2], rank => 1 } );
2249
    my $moved = Koha::Holds->find( $normal_ids[2] );
2250
    is( $moved->priority, 1, 'Non-lowestPriority hold moves to rank 1 normally' );
2251
    my $other = Koha::Holds->find( $normal_ids[0] );
2252
    is( $other->priority, 2, 'Other holds renumbered correctly after normal move' );
2253
2254
    $schema->storage->txn_rollback;
2255
};

Return to bug 41801