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

(-)a/Koha/Patron.pm (-3 / +8 lines)
Lines 388-405 sub delete { Link Here
388
                $hold->cancel;
388
                $hold->cancel;
389
            }
389
            }
390
390
391
            # FIXME Could be $patron->get_lists
391
            # If ListOwnershipUponPatronDeletion = transfer, change ownership of all
392
            # If ListOwnershipUponPatronDeletion = transfer, change ownership of all
392
            # public and shared lists to the user who deleted them.
393
            # public and shared lists to the user who deleted them.
393
            if ( C4::Context->preference('ListOwnershipUponPatronDeletion') eq 'transfer' ) {
394
            if ( C4::Context->preference('ListOwnershipUponPatronDeletion') eq 'transfer' ) {
394
                my $userenv = C4::Context->userenv();
395
                my $userenv = C4::Context->userenv();
395
                my $usernumber = (ref($userenv) eq 'HASH') ? $userenv->{'number'} : 0;
396
                my $usernumber = (ref($userenv) eq 'HASH') ? $userenv->{'number'} : 0;
396
                my @publiclists = Koha::Virtualshelves->get_public_shelves;
397
                my @publiclists = Koha::Virtualshelves->get_public_shelves;
397
                my @sharedlists = Koha::Virtualshelves->search({ 'me.owner' => $self->borrowernumber, 'me.shelfnumber' => { -ident => 'virtualshelfshares.shelfnumber' }  }, { prefetch => 'virtualshelfshares' });
398
                my @sharedlists = Koha::Virtualshelves->get_shared_shelves({ borrowernumber => $self->borrowernumber });
398
                foreach my $plist ( @publiclists ) {
399
                foreach my $plist ( @publiclists ) {
399
                    $plist->set({ owner => $usernumber })->store;
400
                    if ( $plist->owner == $self->borrowernumber ) {
401
                        my $unique_name = $plist->shelfname . '_' . $self->borrowernumber;
402
                        $plist->set({ owner => $usernumber, shelfname => $unique_name })->store;
403
                    }
400
                }
404
                }
401
                foreach my $slist ( @sharedlists ) {
405
                foreach my $slist ( @sharedlists ) {
402
                    $slist->set({ owner => $usernumber })->store;
406
                    my $unique_name = $slist->shelfname . '_' . $self->borrowernumber;
407
                    $slist->set({ owner => $usernumber, shelfname => $unique_name })->store;
403
                    # if staff member had a share, remove it
408
                    # if staff member had a share, remove it
404
                    $slist->remove_share( $usernumber );
409
                    $slist->remove_share( $usernumber );
405
                }
410
                }
(-)a/Koha/Virtualshelves.pm (+13 lines)
Lines 180-185 sub get_shelves_containing_record { Link Here
180
    );
180
    );
181
}
181
}
182
182
183
sub get_shared_shelves {
184
    my ( $self, $params ) = @_;
185
    my $borrowernumber = $params->{borrowernumber} || 0;
186
187
    $self->search(
188
        {
189
            'me.owner' => $borrowernumber,
190
            'me.shelfnumber' => { -ident => 'virtualshelfshares.shelfnumber' }
191
        },
192
        { prefetch => 'virtualshelfshares' }
193
    );
194
}
195
183
sub _type {
196
sub _type {
184
    return 'Virtualshelve';
197
    return 'Virtualshelve';
185
}
198
}
(-)a/t/db_dependent/Koha/Patrons.t (-8 / +60 lines)
Lines 42-47 use Koha::Patron::Categories; Link Here
42
use Koha::Patron::Relationship;
42
use Koha::Patron::Relationship;
43
use Koha::Database;
43
use Koha::Database;
44
use Koha::DateUtils qw( dt_from_string output_pref );
44
use Koha::DateUtils qw( dt_from_string output_pref );
45
use Koha::Virtualshelf;
45
use Koha::Virtualshelves;
46
use Koha::Virtualshelves;
46
use Koha::Notice::Messages;
47
use Koha::Notice::Messages;
47
48
Lines 459-480 subtest "move_to_deleted" => sub { Link Here
459
};
460
};
460
461
461
subtest "delete" => sub {
462
subtest "delete" => sub {
462
    plan tests => 7;
463
    plan tests => 11;
463
    t::lib::Mocks::mock_preference( 'BorrowersLog', 1 );
464
    t::lib::Mocks::mock_preference( 'BorrowersLog', 1 );
465
    t::lib::Mocks::mock_preference( 'ListOwnershipUponPatronDeletion', 'transfer' );
466
    Koha::Virtualshelves->search({})->delete;
467
    my $userenv = C4::Context->userenv();
464
    my $patron           = $builder->build( { source => 'Borrower' } );
468
    my $patron           = $builder->build( { source => 'Borrower' } );
469
    my $patron_for_sharing = (ref($userenv) eq 'HASH' ) ? $userenv->{'number'} : 0;
465
    my $retrieved_patron = Koha::Patrons->find( $patron->{borrowernumber} );
470
    my $retrieved_patron = Koha::Patrons->find( $patron->{borrowernumber} );
466
    my $hold             = $builder->build(
471
    my $hold             = $builder->build(
467
        {   source => 'Reserve',
472
        {   source => 'Reserve',
468
            value  => { borrowernumber => $patron->{borrowernumber} }
473
            value => { borrowernumber => $patron->{borrowernumber} }
469
        }
470
    );
471
    my $list = $builder->build(
472
        {   source => 'Virtualshelve',
473
            value  => { owner => $patron->{borrowernumber} }
474
        }
474
        }
475
    );
475
    );
476
    my $modification = $builder->build_object({ class => 'Koha::Patron::Modifications', value => { borrowernumber => $patron->{borrowernumber} } });
476
    my $modification = $builder->build_object({ class => 'Koha::Patron::Modifications', value => { borrowernumber => $patron->{borrowernumber} } });
477
    my $private_list = Koha::Virtualshelf->new({
478
            shelfname => "private",
479
            owner => $patron->{borrowernumber},
480
            category => 1
481
        }
482
    )->store;
483
    my $public_list = Koha::Virtualshelf->new({
484
            shelfname => "public",
485
            owner => $patron->{borrowernumber},
486
            category => 2
487
        }
488
    )->store;
489
    my $list_to_share = Koha::Virtualshelf->new({
490
            shelfname => "shared",
491
            owner => $patron->{borrowernumber},
492
            category => 1
493
        }
494
    )->store;
477
495
496
    my $shared_shelf = eval { $list_to_share->share("valid key")->accept("valid key", $patron_for_sharing) };
478
    my $deleted = $retrieved_patron->delete;
497
    my $deleted = $retrieved_patron->delete;
479
    is( ref($deleted), 'Koha::Patron', 'Koha::Patron->delete should return the deleted patron object if the patron has been correctly deleted' );
498
    is( ref($deleted), 'Koha::Patron', 'Koha::Patron->delete should return the deleted patron object if the patron has been correctly deleted' );
480
499
Lines 484-495 subtest "delete" => sub { Link Here
484
503
485
    is( Koha::Holds->search( { borrowernumber => $patron->{borrowernumber} } )->count, 0, q|Koha::Patron->delete should have cancelled patron's holds 2| );
504
    is( Koha::Holds->search( { borrowernumber => $patron->{borrowernumber} } )->count, 0, q|Koha::Patron->delete should have cancelled patron's holds 2| );
486
505
487
    is( Koha::Virtualshelves->search( { owner => $patron->{borrowernumber} } )->count, 0, q|Koha::Patron->delete should have deleted patron's lists| );
506
    my $transferred_lists = Koha::Virtualshelves->search({ owner => $patron_for_sharing })->count;
507
    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');
508
    is( Koha::Virtualshelfshares->search({ borrowernumber => $patron_for_sharing })->count, 0, "New owner of list should have shares removed" );
509
    is( Koha::Virtualshelves->search({ owner => $patron->{borrowernumber} })->count, 0, q|Koha::Patron->delete should have deleted patron's lists/removed their ownership| );
488
510
489
    is( Koha::Patron::Modifications->search( { borrowernumber => $patron->{borrowernumber} } )->count, 0, q|Koha::Patron->delete should have deleted patron's modifications| );
511
    is( Koha::Patron::Modifications->search( { borrowernumber => $patron->{borrowernumber} } )->count, 0, q|Koha::Patron->delete should have deleted patron's modifications| );
490
512
491
    my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'DELETE', object => $retrieved_patron->borrowernumber } )->count;
513
    my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'DELETE', object => $retrieved_patron->borrowernumber } )->count;
492
    is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->delete should have logged' );
514
    is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->delete should have logged' );
515
516
    t::lib::Mocks::mock_preference( 'ListOwnershipUponPatronDeletion', 'delete' );
517
    Koha::Virtualshelves->search({})->delete;
518
    my $patron2           = $builder->build( { source => 'Borrower' } );
519
    my $retrieved_patron2 = Koha::Patrons->find( $patron2->{borrowernumber} );
520
    my $private_list2 = Koha::Virtualshelf->new({
521
            shelfname => "private",
522
            owner => $patron2->{borrowernumber},
523
            category => 1
524
        }
525
    )->store;
526
    my $public_list2 = Koha::Virtualshelf->new({
527
            shelfname => "public",
528
            owner => $patron2->{borrowernumber},
529
            category => 2
530
        }
531
    )->store;
532
    my $list_to_share2 = Koha::Virtualshelf->new({
533
            shelfname => "shared",
534
            owner => $patron2->{borrowernumber},
535
            category => 1
536
        }
537
    )->store;
538
539
    my $shared_shelf2 = eval { $list_to_share2->share("valid key") };
540
    my $deleted2 = $retrieved_patron2->delete;
541
542
    is( Koha::Virtualshelves->search( { owner => $patron2->{borrowernumber} } )->count, 0, q|Koha::Patron->delete should have deleted patron's lists| );
543
    $transferred_lists = Koha::Virtualshelves->search({})->count;
544
    is( $transferred_lists, 0, 'All lists should be deleted with ListOwnershipUponPatronDeletion set to Delete');
493
};
545
};
494
546
495
subtest 'Koha::Patrons->delete' => sub {
547
subtest 'Koha::Patrons->delete' => sub {
(-)a/t/db_dependent/Virtualshelves.t (-4 / +13 lines)
Lines 439-445 subtest 'Shelf permissions' => sub { Link Here
439
};
439
};
440
440
441
subtest 'Get shelves' => sub {
441
subtest 'Get shelves' => sub {
442
    plan tests => 4;
442
    plan tests => 5;
443
    my $patron1 = $builder->build({
443
    my $patron1 = $builder->build({
444
        source => 'Borrower',
444
        source => 'Borrower',
445
    });
445
    });
Lines 477-495 subtest 'Get shelves' => sub { Link Here
477
            public => 1,
477
            public => 1,
478
        }
478
        }
479
    )->store;
479
    )->store;
480
    my $shelf_to_share = Koha::Virtualshelf->new({
481
            shelfname => "shared shelf",
482
            owner => $patron1->{borrowernumber},
483
            category => 1,
484
        }
485
    )->store;
480
486
481
    my $private_shelves = Koha::Virtualshelves->get_private_shelves;
487
    my $private_shelves = Koha::Virtualshelves->get_private_shelves;
482
    is( $private_shelves->count, 0, 'Without borrowernumber given, get_private_shelves should not return any shelf' );
488
    is( $private_shelves->count, 0, 'Without borrowernumber given, get_private_shelves should not return any shelf' );
483
    $private_shelves = Koha::Virtualshelves->get_private_shelves({ borrowernumber => $patron1->{borrowernumber} });
489
    $private_shelves = Koha::Virtualshelves->get_private_shelves({ borrowernumber => $patron1->{borrowernumber} });
484
    is( $private_shelves->count, 2, 'get_private_shelves should return all shelves for a given patron' );
490
    is( $private_shelves->count, 3, 'get_private_shelves should return all shelves for a given patron' );
485
491
486
    $private_shelf2_1->share('a key')->accept('a key', $patron1->{borrowernumber});
492
    $private_shelf2_1->share('a key')->accept('a key', $patron1->{borrowernumber});
487
    $private_shelves = Koha::Virtualshelves->get_private_shelves({ borrowernumber => $patron1->{borrowernumber} });
493
    $private_shelves = Koha::Virtualshelves->get_private_shelves({ borrowernumber => $patron1->{borrowernumber} });
488
    is( $private_shelves->count, 3, 'get_private_shelves should return all shelves for a given patron, even the shared ones' );
494
    is( $private_shelves->count, 4, 'get_private_shelves should return all shelves for a given patron, even the shared ones' );
489
495
490
    my $public_shelves = Koha::Virtualshelves->get_public_shelves;
496
    my $public_shelves = Koha::Virtualshelves->get_public_shelves;
491
    is( $public_shelves->count, 2, 'get_public_shelves should return all public shelves, no matter who is the owner' );
497
    is( $public_shelves->count, 2, 'get_public_shelves should return all public shelves, no matter who is the owner' );
492
498
499
    my $shared_shelf = eval { $shelf_to_share->share("valid key") };
500
    my $shared_shelves = Koha::Virtualshelves->get_shared_shelves({ borrowernumber => $patron1->{borrowernumber} });
501
    is( $shared_shelves->count, 1, 'get_shared_shelves should return shared shelves' );
502
493
    teardown();
503
    teardown();
494
};
504
};
495
505
496
- 

Return to bug 11889