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 27-38 use C4::Biblio; Link Here
27
use C4::Circulation;
27
use C4::Circulation;
28
use C4::Members;
28
use C4::Members;
29
use C4::Circulation;
29
use C4::Circulation;
30
use C4::Context;
30
31
31
use Koha::Holds;
32
use Koha::Holds;
32
use Koha::Patron;
33
use Koha::Patron;
33
use Koha::Patrons;
34
use Koha::Patrons;
34
use Koha::Database;
35
use Koha::Database;
35
use Koha::DateUtils;
36
use Koha::DateUtils;
37
use Koha::Virtualshelf;
36
use Koha::Virtualshelves;
38
use Koha::Virtualshelves;
37
39
38
use t::lib::TestBuilder;
40
use t::lib::TestBuilder;
Lines 65-71 my $new_patron_2 = Koha::Patron->new( Link Here
65
)->store;
67
)->store;
66
68
67
C4::Context->_new_userenv('xxx');
69
C4::Context->_new_userenv('xxx');
68
C4::Context->set_userenv(0,0,0,'firstname','surname', $library->{branchcode}, 'Midway Public Library', '', '', '');
70
C4::Context->set_userenv(1,'userid','usercnum','firstname','surname', $library->{branchcode}, 'Midway Public Library', '', '', '');
69
71
70
is( Koha::Patrons->search->count, $nb_of_patrons + 2, 'The 2 patrons should have been added' );
72
is( Koha::Patrons->search->count, $nb_of_patrons + 2, 'The 2 patrons should have been added' );
71
73
Lines 337-368 subtest "move_to_deleted" => sub { Link Here
337
};
339
};
338
340
339
subtest "delete" => sub {
341
subtest "delete" => sub {
340
    plan tests => 5;
342
    plan tests => 9;
341
    t::lib::Mocks::mock_preference( 'BorrowersLog', 1 );
343
    t::lib::Mocks::mock_preference( 'BorrowersLog', 1 );
344
    t::lib::Mocks::mock_preference( 'ListOwnershipUponPatronDeletion', 'transfer' );
345
    Koha::Virtualshelves->search({})->delete;
346
    my $userenv = C4::Context->userenv();
342
    my $patron           = $builder->build( { source => 'Borrower' } );
347
    my $patron           = $builder->build( { source => 'Borrower' } );
348
    my $patron_for_sharing = (ref($userenv) eq 'HASH' ) ? $userenv->{'number'} : 0;
343
    my $retrieved_patron = Koha::Patrons->find( $patron->{borrowernumber} );
349
    my $retrieved_patron = Koha::Patrons->find( $patron->{borrowernumber} );
344
    my $hold             = $builder->build(
350
    my $hold             = $builder->build(
345
        {   source => 'Reserve',
351
        {   source => 'Reserve',
346
            value  => { borrowernumber => $patron->{borrowernumber} }
352
            value => { borrowernumber => $patron->{borrowernumber} }
347
        }
353
        }
348
    );
354
    );
349
    my $list = $builder->build(
355
    my $private_list = Koha::Virtualshelf->new({
350
        {   source => 'Virtualshelve',
356
            shelfname => "private",
351
            value  => { owner => $patron->{borrowernumber} }
357
            owner => $patron->{borrowernumber},
358
            category => 1
352
        }
359
        }
353
    );
360
    )->store;
361
    my $public_list = Koha::Virtualshelf->new({
362
            shelfname => "public",
363
            owner => $patron->{borrowernumber},
364
            category => 2
365
        }
366
    )->store;
367
    my $list_to_share = Koha::Virtualshelf->new({
368
            shelfname => "shared",
369
            owner => $patron->{borrowernumber},
370
            category => 1
371
        }
372
    )->store;
354
373
374
    my $shared_shelf = eval { $list_to_share->share("valid key")->accept("valid key", $patron_for_sharing) };
355
    my $deleted = $retrieved_patron->delete;
375
    my $deleted = $retrieved_patron->delete;
356
    is( $deleted, 1, 'Koha::Patron->delete should return 1 if the patron has been correctly deleted' );
357
376
377
    is( $deleted, 1, 'Koha::Patron->delete should return 1 if the patron has been correctly deleted' );
358
    is( Koha::Patrons->find( $patron->{borrowernumber} ), undef, 'Koha::Patron->delete should have deleted the patron' );
378
    is( Koha::Patrons->find( $patron->{borrowernumber} ), undef, 'Koha::Patron->delete should have deleted the patron' );
359
360
    is( Koha::Holds->search( { borrowernumber => $patron->{borrowernumber} } )->count, 0, q|Koha::Patron->delete should have deleted patron's holds| );
379
    is( Koha::Holds->search( { borrowernumber => $patron->{borrowernumber} } )->count, 0, q|Koha::Patron->delete should have deleted patron's holds| );
361
380
362
    is( Koha::Virtualshelves->search( { owner => $patron->{borrowernumber} } )->count, 0, q|Koha::Patron->delete should have deleted patron's lists| );
381
    my $transferred_lists = Koha::Virtualshelves->search({ owner => $patron_for_sharing })->count;
382
    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');
383
    is( Koha::Virtualshelfshares->search({ borrowernumber => $patron_for_sharing })->count, 0, "New owner of list should have shares removed" );
384
    is( Koha::Virtualshelves->search({ owner => $patron->{borrowernumber} })->count, 0, q|Koha::Patron->delete should have deleted patron's lists/removed their ownership| );
363
385
364
    my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'DELETE', object => $retrieved_patron->borrowernumber } )->count;
386
    my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'DELETE', object => $patron->{borrowernumber} } )->count;
365
    is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->delete should have logged' );
387
    is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->delete should have logged' );
388
389
    t::lib::Mocks::mock_preference( 'ListOwnershipUponPatronDeletion', 'delete' );
390
    Koha::Virtualshelves->search({})->delete;
391
    my $patron2           = $builder->build( { source => 'Borrower' } );
392
    my $retrieved_patron2 = Koha::Patrons->find( $patron2->{borrowernumber} );
393
    my $private_list2 = Koha::Virtualshelf->new({
394
            shelfname => "private",
395
            owner => $patron2->{borrowernumber},
396
            category => 1
397
        }
398
    )->store;
399
    my $public_list2 = Koha::Virtualshelf->new({
400
            shelfname => "public",
401
            owner => $patron2->{borrowernumber},
402
            category => 2
403
        }
404
    )->store;
405
    my $list_to_share2 = Koha::Virtualshelf->new({
406
            shelfname => "shared",
407
            owner => $patron2->{borrowernumber},
408
            category => 1
409
        }
410
    )->store;
411
412
    my $shared_shelf2 = eval { $list_to_share2->share("valid key") };
413
    my $deleted2 = $retrieved_patron2->delete;
414
415
    is( Koha::Virtualshelves->search( { owner => $patron2->{borrowernumber} } )->count, 0, q|Koha::Patron->delete should have deleted patron's lists| );
416
    $transferred_lists = Koha::Virtualshelves->search({})->count;
417
    is( $transferred_lists, 0, 'All lists should be deleted with ListOwnershipUponPatronDeletion set to Delete');
366
};
418
};
367
419
368
subtest 'add_enrolment_fee_if_needed' => sub {
420
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