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

(-)a/Koha/Patron.pm (-23 / +24 lines)
Lines 388-419 sub delete { Link Here
388
                $hold->cancel;
388
                $hold->cancel;
389
            }
389
            }
390
390
391
            # FIXME Could be $patron->get_lists
391
            # Handle lists (virtualshelves)
392
            # If ListOwnershipUponPatronDeletion = transfer, change ownership of all
392
            my $userenv = C4::Context->userenv;
393
            # public and shared lists to the user who deleted them.
393
            my $usernumber = ref($userenv) eq 'HASH' ? $userenv->{'number'} : 0;
394
            if ( C4::Context->preference('ListOwnershipUponPatronDeletion') eq 'transfer' ) {
394
            my $lists = $self->virtualshelves;
395
                my $userenv = C4::Context->userenv();
395
            while( my $list = $lists->next ) {
396
                my $usernumber = (ref($userenv) eq 'HASH') ? $userenv->{'number'} : 0;
396
                # if staff member had a share, remove it
397
                my @publiclists = Koha::Virtualshelves->get_public_shelves->as_list;
397
                $list->remove_share( $usernumber ) if $usernumber && $list->is_private;
398
                my @sharedlists = Koha::Virtualshelves->get_shared_shelves({ borrowernumber => $self->borrowernumber })->as_list;
398
                if( C4::Context->preference('ListOwnershipUponPatronDeletion') eq 'transfer' &&
399
                foreach my $plist ( @publiclists ) {
399
                    $usernumber && ( $list->is_public || $list->is_shared ))
400
                    if ( $plist->owner == $self->borrowernumber ) {
400
                {
401
                        my $unique_name = $plist->shelfname . '_' . $self->borrowernumber;
401
                    $list->set({ owner => $usernumber })->store; # transfer ownership of public/shared list
402
                        $plist->set({ owner => $usernumber, shelfname => $unique_name })->store;
402
                } else { # delete
403
                    }
403
                    $list->delete;
404
                }
405
                foreach my $slist ( @sharedlists ) {
406
                    my $unique_name = $slist->shelfname . '_' . $self->borrowernumber;
407
                    $slist->set({ owner => $usernumber, shelfname => $unique_name })->store;
408
                    # if staff member had a share, remove it
409
                    $slist->remove_share( $usernumber );
410
                }
404
                }
411
            }
405
            }
412
406
413
            # Delete any remaining lists that this user is an owner of (always private lists,
414
            # only public and shared lists if ListOwnershipUponPatronDeletion = delete)
415
            $_->delete for Koha::Virtualshelves->search({ owner => $self->borrowernumber })->as_list;
416
417
            # We cannot have a FK on borrower_modifications.borrowernumber, the table is also used
407
            # We cannot have a FK on borrower_modifications.borrowernumber, the table is also used
418
            # for patron selfreg
408
            # for patron selfreg
419
            $_->delete for Koha::Patron::Modifications->search( { borrowernumber => $self->borrowernumber } )->as_list;
409
            $_->delete for Koha::Patron::Modifications->search( { borrowernumber => $self->borrowernumber } )->as_list;
Lines 2196-2201 sub decoded_secret { Link Here
2196
    return $self->secret;
2186
    return $self->secret;
2197
}
2187
}
2198
2188
2189
=head3 virtualshelves
2190
2191
    my $shelves = $patron->virtualshelves;
2192
2193
=cut
2194
2195
sub virtualshelves {
2196
    my $self = shift;
2197
    return Koha::Virtualshelves->_new_from_dbic( scalar $self->_result->virtualshelves );
2198
}
2199
2199
=head2 Internal methods
2200
=head2 Internal methods
2200
2201
2201
=head3 _type
2202
=head3 _type
(-)a/t/db_dependent/Koha/Patrons.t (-60 / +49 lines)
Lines 460-550 subtest "move_to_deleted" => sub { Link Here
460
};
460
};
461
461
462
subtest "delete" => sub {
462
subtest "delete" => sub {
463
    plan tests => 11;
463
    plan tests => 13;
464
    t::lib::Mocks::mock_preference( 'BorrowersLog', 1 );
464
    t::lib::Mocks::mock_preference( 'BorrowersLog', 1 );
465
    t::lib::Mocks::mock_preference( 'ListOwnershipUponPatronDeletion', 'transfer' );
465
    t::lib::Mocks::mock_preference( 'ListOwnershipUponPatronDeletion', 'transfer' );
466
    Koha::Virtualshelves->delete;
466
    Koha::Virtualshelves->delete;
467
467
468
    my $patron = $builder->build_object({ class => 'Koha::Patrons' });
469
    my $patron_for_sharing = $builder->build_object({ class => 'Koha::Patrons' });
468
    my $staff_patron = $builder->build_object({ class => 'Koha::Patrons' });
470
    my $staff_patron = $builder->build_object({ class => 'Koha::Patrons' });
469
    t::lib::Mocks::mock_userenv({ patron => $staff_patron });
471
    t::lib::Mocks::mock_userenv({ patron => $staff_patron });
470
472
471
    my $patron           = $builder->build( { source => 'Borrower' } );
473
    my $hold = $builder->build_object({ class => 'Koha::Holds', value => { borrowernumber => $patron->borrowernumber } });
472
    my $patron_for_sharing = $staff_patron->borrowernumber;
474
    my $modification = $builder->build_object({ class => 'Koha::Patron::Modifications', value => { borrowernumber => $patron->borrowernumber } });
473
    my $retrieved_patron = Koha::Patrons->find( $patron->{borrowernumber} );
474
    my $hold             = $builder->build(
475
        {   source => 'Reserve',
476
            value => { borrowernumber => $patron->{borrowernumber} }
477
        }
478
    );
479
    my $modification = $builder->build_object({ class => 'Koha::Patron::Modifications', value => { borrowernumber => $patron->{borrowernumber} } });
480
    my $private_list = Koha::Virtualshelf->new({
475
    my $private_list = Koha::Virtualshelf->new({
481
            shelfname => "private",
476
        shelfname => "private",
482
            owner => $patron->{borrowernumber},
477
        owner => $patron->borrowernumber,
483
            public => 0,
478
        public => 0,
484
        }
479
    })->store;
485
    )->store;
486
    my $public_list = Koha::Virtualshelf->new({
480
    my $public_list = Koha::Virtualshelf->new({
487
            shelfname => "public",
481
        shelfname => "public",
488
            owner => $patron->{borrowernumber},
482
        owner => $patron->borrowernumber,
489
            public => 1,
483
        public => 1,
490
        }
484
    })->store;
491
    )->store;
492
    my $list_to_share = Koha::Virtualshelf->new({
485
    my $list_to_share = Koha::Virtualshelf->new({
493
            shelfname => "shared",
486
        shelfname => "shared",
494
            owner => $patron->{borrowernumber},
487
        owner => $patron->borrowernumber,
495
            public => 0,
488
        public => 0,
496
        }
489
    })->store;
497
    )->store;
498
490
499
    my $shared_shelf = eval { $list_to_share->share("valid key")->accept("valid key", $patron_for_sharing) };
491
    $list_to_share->share("valid key")->accept( "valid key", $patron_for_sharing->borrowernumber );
500
    my $deleted = $retrieved_patron->delete;
492
    $list_to_share->share("valid key")->accept( "valid key", $staff_patron->borrowernumber ); # this share should be removed at deletion too
493
    my $deleted = $patron->delete;
501
    is( ref($deleted), 'Koha::Patron', 'Koha::Patron->delete should return the deleted patron object if the patron has been correctly deleted' );
494
    is( ref($deleted), 'Koha::Patron', 'Koha::Patron->delete should return the deleted patron object if the patron has been correctly deleted' );
495
    ok( $patron->borrowernumber, 'Still have the deleted borrowernumber' );
502
496
503
    is( Koha::Patrons->find( $patron->{borrowernumber} ), undef, 'Koha::Patron->delete should have deleted the patron' );
497
    is( Koha::Patrons->find( $patron->borrowernumber ), undef, 'Koha::Patron->delete should have deleted the patron' );
504
498
505
    is (Koha::Old::Holds->search( { reserve_id => $hold->{ reserve_id } } )->count, 1, q|Koha::Patron->delete should have cancelled patron's holds| );
499
    is (Koha::Old::Holds->search({ reserve_id => $hold->reserve_id })->count, 1, q|Koha::Patron->delete should have cancelled patron's holds| );
506
500
507
    is( Koha::Holds->search( { borrowernumber => $patron->{borrowernumber} } )->count, 0, q|Koha::Patron->delete should have cancelled patron's holds 2| );
501
    is( Koha::Holds->search( { borrowernumber => $patron->borrowernumber } )->count, 0, q|Koha::Patron->delete should have cancelled patron's holds 2| );
508
502
509
    my $transferred_lists = Koha::Virtualshelves->search({ owner => $patron_for_sharing })->count;
503
    my $transferred_lists = Koha::Virtualshelves->search({ owner => $staff_patron->borrowernumber })->count;
510
    is( $transferred_lists, 2, 'Public and shared lists should stay in database under a different owner with a unique name, while private lists delete, with ListOwnershipPatronDeletion set to Transfer');
504
    is( $transferred_lists, 2, 'Public and shared lists should stay in database under a different owner with a unique name, while private lists delete, with ListOwnershipPatronDeletion set to Transfer');
511
    is( Koha::Virtualshelfshares->search({ borrowernumber => $patron_for_sharing })->count, 0, "New owner of list should have shares removed" );
505
    is( Koha::Virtualshelfshares->search({ borrowernumber => $staff_patron->borrowernumber })->count, 0, "New owner of list should have shares removed" );
512
    is( Koha::Virtualshelves->search({ owner => $patron->{borrowernumber} })->count, 0, q|Koha::Patron->delete should have deleted patron's lists/removed their ownership| );
506
    is( Koha::Virtualshelfshares->search({ borrowernumber => $patron_for_sharing->borrowernumber })->count, 1, "But the other share is still there" );
507
    is( Koha::Virtualshelves->search({ owner => $patron->borrowernumber })->count, 0, q|Koha::Patron->delete should have deleted patron's lists/removed their ownership| );
513
508
514
    is( Koha::Patron::Modifications->search( { borrowernumber => $patron->{borrowernumber} } )->count, 0, q|Koha::Patron->delete should have deleted patron's modifications| );
509
    is( Koha::Patron::Modifications->search( { borrowernumber => $patron->borrowernumber } )->count, 0, q|Koha::Patron->delete should have deleted patron's modifications| );
515
510
516
    my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'DELETE', object => $retrieved_patron->borrowernumber } )->count;
511
    my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'DELETE', object => $patron->borrowernumber } )->count;
517
    is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->delete should have logged' );
512
    is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->delete should have logged' );
518
513
519
    t::lib::Mocks::mock_preference( 'ListOwnershipUponPatronDeletion', 'delete' );
514
    t::lib::Mocks::mock_preference( 'ListOwnershipUponPatronDeletion', 'delete' );
520
    Koha::Virtualshelves->search({})->delete;
515
    Koha::Virtualshelves->delete;
521
    my $patron2           = $builder->build( { source => 'Borrower' } );
516
    my $patron2 = $builder->build_object({ class => 'Koha::Patrons' });
522
    my $retrieved_patron2 = Koha::Patrons->find( $patron2->{borrowernumber} );
523
    my $private_list2 = Koha::Virtualshelf->new({
517
    my $private_list2 = Koha::Virtualshelf->new({
524
            shelfname => "private",
518
         shelfname => "private",
525
            owner => $patron2->{borrowernumber},
519
         owner => $patron2->borrowernumber,
526
            public => 0,
520
         public => 0,
527
        }
521
    })->store;
528
    )->store;
529
    my $public_list2 = Koha::Virtualshelf->new({
522
    my $public_list2 = Koha::Virtualshelf->new({
530
            shelfname => "public",
523
        shelfname => "public",
531
            owner => $patron2->{borrowernumber},
524
        owner => $patron2->borrowernumber,
532
            public => 1,
525
        public => 1,
533
        }
526
    })->store;
534
    )->store;
535
    my $list_to_share2 = Koha::Virtualshelf->new({
527
    my $list_to_share2 = Koha::Virtualshelf->new({
536
            shelfname => "shared",
528
        shelfname => "shared",
537
            owner => $patron2->{borrowernumber},
529
        owner => $patron2->borrowernumber,
538
            public => 0,
530
        public => 0,
539
        }
531
    })->store;
540
    )->store;
532
    $list_to_share2->share("valid key")->accept( "valid key", $patron_for_sharing->borrowernumber );
541
542
    my $shared_shelf2 = eval { $list_to_share2->share("valid key") };
543
    my $deleted2 = $retrieved_patron2->delete;
544
533
545
    is( Koha::Virtualshelves->search( { owner => $patron2->{borrowernumber} } )->count, 0, q|Koha::Patron->delete should have deleted patron's lists| );
534
    # Delete patron2, check if shelves and shares are now empty
546
    $transferred_lists = Koha::Virtualshelves->search({})->count;
535
    $patron2->delete;
547
    is( $transferred_lists, 0, 'All lists should be deleted with ListOwnershipUponPatronDeletion set to Delete');
536
    is( Koha::Virtualshelves->count, 0, 'All lists should be gone now' );
537
    is( Koha::Virtualshelfshares->count, 0, 'All shares should be gone too' );
548
};
538
};
549
539
550
subtest 'Koha::Patrons->delete' => sub {
540
subtest 'Koha::Patrons->delete' => sub {
551
- 

Return to bug 11889