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

(-)a/Koha/Patron.pm (-3 / +8 lines)
Lines 68-85 sub delete { Link Here
68
            # Delete Patron's holds
68
            # Delete Patron's holds
69
            $self->holds->delete;
69
            $self->holds->delete;
70
70
71
            # FIXME Could be $patron->get_lists
71
            # If ListOwnershipUponPatronDeletion = transfer, change ownership of all
72
            # If ListOwnershipUponPatronDeletion = transfer, change ownership of all
72
            # public and shared lists to the user who deleted them.
73
            # public and shared lists to the user who deleted them.
73
            if ( C4::Context->preference('ListOwnershipUponPatronDeletion') eq 'transfer' ) {
74
            if ( C4::Context->preference('ListOwnershipUponPatronDeletion') eq 'transfer' ) {
74
                my $userenv = C4::Context->userenv();
75
                my $userenv = C4::Context->userenv();
75
                my $usernumber = (ref($userenv) eq 'HASH') ? $userenv->{'number'} : 0;
76
                my $usernumber = (ref($userenv) eq 'HASH') ? $userenv->{'number'} : 0;
76
                my @publiclists = Koha::Virtualshelves->get_public_shelves;
77
                my @publiclists = Koha::Virtualshelves->get_public_shelves;
77
                my @sharedlists = Koha::Virtualshelves->search({ 'me.owner' => $self->borrowernumber, 'me.shelfnumber' => { -ident => 'virtualshelfshares.shelfnumber' }  }, { prefetch => 'virtualshelfshares' });
78
                my @sharedlists = Koha::Virtualshelves->get_shared_shelves({ borrowernumber => $self->borrowernumber });
78
                foreach my $plist ( @publiclists ) {
79
                foreach my $plist ( @publiclists ) {
79
                    $plist->set({ owner => $usernumber })->store;
80
                    if ( $plist->owner == $self->borrowernumber ) {
81
                        my $unique_name = $plist->shelfname . '_' . $self->borrowernumber;
82
                        $plist->set({ owner => $usernumber, shelfname => $unique_name })->store;
83
                    }
80
                }
84
                }
81
                foreach my $slist ( @sharedlists ) {
85
                foreach my $slist ( @sharedlists ) {
82
                    $slist->set({ owner => $usernumber })->store;
86
                    my $unique_name = $slist->shelfname . '_' . $self->borrowernumber;
87
                    $slist->set({ owner => $usernumber, shelfname => $unique_name })->store;
83
                    # if staff member had a share, remove it
88
                    # if staff member had a share, remove it
84
                    $slist->remove_share( $usernumber );
89
                    $slist->remove_share( $usernumber );
85
                }
90
                }
(-)a/Koha/Virtualshelves.pm (+13 lines)
Lines 158-163 sub get_shelves_containing_record { Link Here
158
    );
158
    );
159
}
159
}
160
160
161
sub get_shared_shelves {
162
    my ( $self, $params ) = @_;
163
    my $borrowernumber = $params->{borrowernumber} || 0;
164
165
    $self->search(
166
        {
167
            'me.owner' => $borrowernumber,
168
            'me.shelfnumber' => { -ident => 'virtualshelfshares.shelfnumber' }
169
        },
170
        { prefetch => 'virtualshelfshares' }
171
    );
172
}
173
161
sub _type {
174
sub _type {
162
    return 'Virtualshelve';
175
    return 'Virtualshelve';
163
}
176
}
(-)a/t/db_dependent/Koha/Patrons.t (-11 / +63 lines)
Lines 26-37 use DateTime; Link Here
26
use C4::Biblio;
26
use C4::Biblio;
27
use C4::Circulation;
27
use C4::Circulation;
28
use C4::Members;
28
use C4::Members;
29
use C4::Context;
29
30
30
use Koha::Holds;
31
use Koha::Holds;
31
use Koha::Patron;
32
use Koha::Patron;
32
use Koha::Patrons;
33
use Koha::Patrons;
33
use Koha::Database;
34
use Koha::Database;
34
use Koha::DateUtils;
35
use Koha::DateUtils;
36
use Koha::Virtualshelf;
35
use Koha::Virtualshelves;
37
use Koha::Virtualshelves;
36
38
37
use t::lib::TestBuilder;
39
use t::lib::TestBuilder;
Lines 64-70 my $new_patron_2 = Koha::Patron->new( Link Here
64
)->store;
66
)->store;
65
67
66
C4::Context->_new_userenv('xxx');
68
C4::Context->_new_userenv('xxx');
67
C4::Context->set_userenv(0,0,0,'firstname','surname', $library->{branchcode}, 'Midway Public Library', '', '', '');
69
C4::Context->set_userenv(1,'userid','usercnum','firstname','surname', $library->{branchcode}, 'Midway Public Library', '', '', '');
68
70
69
is( Koha::Patrons->search->count, $nb_of_patrons + 2, 'The 2 patrons should have been added' );
71
is( Koha::Patrons->search->count, $nb_of_patrons + 2, 'The 2 patrons should have been added' );
70
72
Lines 336-367 subtest "move_to_deleted" => sub { Link Here
336
};
338
};
337
339
338
subtest "delete" => sub {
340
subtest "delete" => sub {
339
    plan tests => 5;
341
    plan tests => 9;
340
    t::lib::Mocks::mock_preference( 'BorrowersLog', 1 );
342
    t::lib::Mocks::mock_preference( 'BorrowersLog', 1 );
343
    t::lib::Mocks::mock_preference( 'ListOwnershipUponPatronDeletion', 'transfer' );
344
    Koha::Virtualshelves->search({})->delete;
345
    my $userenv = C4::Context->userenv();
341
    my $patron           = $builder->build( { source => 'Borrower' } );
346
    my $patron           = $builder->build( { source => 'Borrower' } );
347
    my $patron_for_sharing = (ref($userenv) eq 'HASH' ) ? $userenv->{'number'} : 0;
342
    my $retrieved_patron = Koha::Patrons->find( $patron->{borrowernumber} );
348
    my $retrieved_patron = Koha::Patrons->find( $patron->{borrowernumber} );
343
    my $hold             = $builder->build(
349
    my $hold             = $builder->build(
344
        {   source => 'Reserve',
350
        {   source => 'Reserve',
345
            value  => { borrowernumber => $patron->{borrowernumber} }
351
            value => { borrowernumber => $patron->{borrowernumber} }
346
        }
352
        }
347
    );
353
    );
348
    my $list = $builder->build(
354
    my $private_list = Koha::Virtualshelf->new({
349
        {   source => 'Virtualshelve',
355
            shelfname => "private",
350
            value  => { owner => $patron->{borrowernumber} }
356
            owner => $patron->{borrowernumber},
357
            category => 1
351
        }
358
        }
352
    );
359
    )->store;
360
    my $public_list = Koha::Virtualshelf->new({
361
            shelfname => "public",
362
            owner => $patron->{borrowernumber},
363
            category => 2
364
        }
365
    )->store;
366
    my $list_to_share = Koha::Virtualshelf->new({
367
            shelfname => "shared",
368
            owner => $patron->{borrowernumber},
369
            category => 1
370
        }
371
    )->store;
353
372
373
    my $shared_shelf = eval { $list_to_share->share("valid key")->accept("valid key", $patron_for_sharing) };
354
    my $deleted = $retrieved_patron->delete;
374
    my $deleted = $retrieved_patron->delete;
355
    is( $deleted, 1, 'Koha::Patron->delete should return 1 if the patron has been correctly deleted' );
356
375
376
    is( $deleted, 1, 'Koha::Patron->delete should return 1 if the patron has been correctly deleted' );
357
    is( Koha::Patrons->find( $patron->{borrowernumber} ), undef, 'Koha::Patron->delete should have deleted the patron' );
377
    is( Koha::Patrons->find( $patron->{borrowernumber} ), undef, 'Koha::Patron->delete should have deleted the patron' );
358
359
    is( Koha::Holds->search( { borrowernumber => $patron->{borrowernumber} } )->count, 0, q|Koha::Patron->delete should have deleted patron's holds| );
378
    is( Koha::Holds->search( { borrowernumber => $patron->{borrowernumber} } )->count, 0, q|Koha::Patron->delete should have deleted patron's holds| );
360
379
361
    is( Koha::Virtualshelves->search( { owner => $patron->{borrowernumber} } )->count, 0, q|Koha::Patron->delete should have deleted patron's lists| );
380
    my $transferred_lists = Koha::Virtualshelves->search({ owner => $patron_for_sharing })->count;
381
    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');
382
    is( Koha::Virtualshelfshares->search({ borrowernumber => $patron_for_sharing })->count, 0, "New owner of list should have shares removed" );
383
    is( Koha::Virtualshelves->search({ owner => $patron->{borrowernumber} })->count, 0, q|Koha::Patron->delete should have deleted patron's lists/removed their ownership| );
362
384
363
    my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'DELETE', object => $retrieved_patron->borrowernumber } )->count;
385
    my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'DELETE', object => $patron->{borrowernumber} } )->count;
364
    is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->delete should have logged' );
386
    is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->delete should have logged' );
387
388
    t::lib::Mocks::mock_preference( 'ListOwnershipUponPatronDeletion', 'delete' );
389
    Koha::Virtualshelves->search({})->delete;
390
    my $patron2           = $builder->build( { source => 'Borrower' } );
391
    my $retrieved_patron2 = Koha::Patrons->find( $patron2->{borrowernumber} );
392
    my $private_list2 = Koha::Virtualshelf->new({
393
            shelfname => "private",
394
            owner => $patron2->{borrowernumber},
395
            category => 1
396
        }
397
    )->store;
398
    my $public_list2 = Koha::Virtualshelf->new({
399
            shelfname => "public",
400
            owner => $patron2->{borrowernumber},
401
            category => 2
402
        }
403
    )->store;
404
    my $list_to_share2 = Koha::Virtualshelf->new({
405
            shelfname => "shared",
406
            owner => $patron2->{borrowernumber},
407
            category => 1
408
        }
409
    )->store;
410
411
    my $shared_shelf2 = eval { $list_to_share2->share("valid key") };
412
    my $deleted2 = $retrieved_patron2->delete;
413
414
    is( Koha::Virtualshelves->search( { owner => $patron2->{borrowernumber} } )->count, 0, q|Koha::Patron->delete should have deleted patron's lists| );
415
    $transferred_lists = Koha::Virtualshelves->search({})->count;
416
    is( $transferred_lists, 0, 'All lists should be deleted with ListOwnershipUponPatronDeletion set to Delete');
365
};
417
};
366
418
367
subtest 'add_enrolment_fee_if_needed' => sub {
419
subtest 'add_enrolment_fee_if_needed' => sub {
(-)a/t/db_dependent/Virtualshelves.t (-4 / +13 lines)
Lines 350-356 subtest 'Shelf permissions' => sub { Link Here
350
};
350
};
351
351
352
subtest 'Get shelves' => sub {
352
subtest 'Get shelves' => sub {
353
    plan tests => 4;
353
    plan tests => 5;
354
    my $patron1 = $builder->build({
354
    my $patron1 = $builder->build({
355
        source => 'Borrower',
355
        source => 'Borrower',
356
    });
356
    });
Lines 388-406 subtest 'Get shelves' => sub { Link Here
388
            category => 2,
388
            category => 2,
389
        }
389
        }
390
    )->store;
390
    )->store;
391
    my $shelf_to_share = Koha::Virtualshelf->new({
392
            shelfname => "shared shelf",
393
            owner => $patron1->{borrowernumber},
394
            category => 1,
395
        }
396
    )->store;
391
397
392
    my $private_shelves = Koha::Virtualshelves->get_private_shelves;
398
    my $private_shelves = Koha::Virtualshelves->get_private_shelves;
393
    is( $private_shelves->count, 0, 'Without borrowernumber given, get_private_shelves should not return any shelf' );
399
    is( $private_shelves->count, 0, 'Without borrowernumber given, get_private_shelves should not return any shelf' );
394
    $private_shelves = Koha::Virtualshelves->get_private_shelves({ borrowernumber => $patron1->{borrowernumber} });
400
    $private_shelves = Koha::Virtualshelves->get_private_shelves({ borrowernumber => $patron1->{borrowernumber} });
395
    is( $private_shelves->count, 2, 'get_private_shelves should return all shelves for a given patron' );
401
    is( $private_shelves->count, 3, 'get_private_shelves should return all shelves for a given patron' );
396
402
397
    $private_shelf2_1->share('a key')->accept('a key', $patron1->{borrowernumber});
403
    $private_shelf2_1->share('a key')->accept('a key', $patron1->{borrowernumber});
398
    $private_shelves = Koha::Virtualshelves->get_private_shelves({ borrowernumber => $patron1->{borrowernumber} });
404
    $private_shelves = Koha::Virtualshelves->get_private_shelves({ borrowernumber => $patron1->{borrowernumber} });
399
    is( $private_shelves->count, 3, 'get_private_shelves should return all shelves for a given patron, even the shared ones' );
405
    is( $private_shelves->count, 4, 'get_private_shelves should return all shelves for a given patron, even the shared ones' );
400
406
401
    my $public_shelves = Koha::Virtualshelves->get_public_shelves;
407
    my $public_shelves = Koha::Virtualshelves->get_public_shelves;
402
    is( $public_shelves->count, 2, 'get_public_shelves should return all public shelves, no matter who is the owner' );
408
    is( $public_shelves->count, 2, 'get_public_shelves should return all public shelves, no matter who is the owner' );
403
409
410
    my $shared_shelf = eval { $shelf_to_share->share("valid key") };
411
    my $shared_shelves = Koha::Virtualshelves->get_shared_shelves({ borrowernumber => $patron1->{borrowernumber} });
412
    is( $shared_shelves->count, 1, 'get_shared_shelves should return shared shelves' );
413
404
    teardown();
414
    teardown();
405
};
415
};
406
416
407
- 

Return to bug 11889