|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
| 3 |
# Copyright 2022 Koha Development team |
| 4 |
# |
| 5 |
# This file is part of Koha |
| 6 |
# |
| 7 |
# Koha is free software; you can redistribute it and/or modify it |
| 8 |
# under the terms of the GNU General Public License as published by |
| 9 |
# the Free Software Foundation; either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# Koha is distributed in the hope that it will be useful, but |
| 13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
| 19 |
|
| 20 |
use Modern::Perl; |
| 21 |
|
| 22 |
use Test::More tests => 1; |
| 23 |
use Test::Exception; |
| 24 |
|
| 25 |
use t::lib::Mocks; |
| 26 |
use t::lib::TestBuilder; |
| 27 |
|
| 28 |
my $schema = Koha::Database->new->schema; |
| 29 |
my $builder = t::lib::TestBuilder->new; |
| 30 |
|
| 31 |
subtest 'disown_or_delete() tests' => sub { |
| 32 |
|
| 33 |
plan tests => 3; |
| 34 |
|
| 35 |
subtest 'All set cases' => sub { |
| 36 |
|
| 37 |
plan tests => 6; |
| 38 |
|
| 39 |
$schema->storage->txn_begin; |
| 40 |
|
| 41 |
my $patron_1 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 42 |
my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 43 |
my $patron_3 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 44 |
|
| 45 |
my $public_list = $builder->build_object( |
| 46 |
{ |
| 47 |
class => "Koha::Virtualshelves", |
| 48 |
value => { owner => $patron_1->id, public => 1 } |
| 49 |
} |
| 50 |
); |
| 51 |
|
| 52 |
my $private_list = $builder->build_object( |
| 53 |
{ |
| 54 |
class => "Koha::Virtualshelves", |
| 55 |
value => { owner => $patron_1->id, public => 0 } |
| 56 |
} |
| 57 |
); |
| 58 |
|
| 59 |
my $private_list_shared = $builder->build_object( |
| 60 |
{ |
| 61 |
class => "Koha::Virtualshelves", |
| 62 |
value => { owner => $patron_1->id, public => 0 } |
| 63 |
} |
| 64 |
); |
| 65 |
|
| 66 |
# add share |
| 67 |
$builder->build_object( |
| 68 |
{ |
| 69 |
class => 'Koha::Virtualshelfshares', |
| 70 |
value => { shelfnumber => $private_list_shared->id, invitekey => undef, borrowernumber => $patron_3->id } |
| 71 |
} |
| 72 |
); |
| 73 |
|
| 74 |
t::lib::Mocks::mock_preference( 'ListOwnershipUponPatronDeletion', 'transfer' ); |
| 75 |
t::lib::Mocks::mock_preference( 'ListOwnerDesignated', $patron_2->id ); |
| 76 |
|
| 77 |
my $rs = Koha::Virtualshelves->search( { shelfnumber => [ $public_list->id, $private_list->id, $private_list_shared->id ] } ); |
| 78 |
|
| 79 |
my $result = $rs->disown_or_delete; |
| 80 |
is( ref($result), 'Koha::Virtualshelves', 'Return type is correct' ); |
| 81 |
$rs->reset; |
| 82 |
|
| 83 |
is( $rs->count, 2, 'The private/non-shared list was deleted' ); |
| 84 |
my $first = $rs->next; |
| 85 |
is( $first->id, $public_list->id ); |
| 86 |
is( $first->owner, $patron_2->id ); |
| 87 |
|
| 88 |
my $second = $rs->next; |
| 89 |
is( $second->id, $private_list_shared->id ); |
| 90 |
is( $second->owner, $patron_2->id ); |
| 91 |
|
| 92 |
$schema->storage->txn_rollback; |
| 93 |
}; |
| 94 |
|
| 95 |
subtest 'Fallback to userenv' => sub { |
| 96 |
|
| 97 |
plan tests => 6; |
| 98 |
|
| 99 |
$schema->storage->txn_begin; |
| 100 |
|
| 101 |
my $patron_1 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 102 |
my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 103 |
my $patron_3 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 104 |
|
| 105 |
my $public_list = $builder->build_object( |
| 106 |
{ |
| 107 |
class => "Koha::Virtualshelves", |
| 108 |
value => { owner => $patron_1->id, public => 1 } |
| 109 |
} |
| 110 |
); |
| 111 |
|
| 112 |
my $private_list = $builder->build_object( |
| 113 |
{ |
| 114 |
class => "Koha::Virtualshelves", |
| 115 |
value => { owner => $patron_1->id, public => 0 } |
| 116 |
} |
| 117 |
); |
| 118 |
|
| 119 |
my $private_list_shared = $builder->build_object( |
| 120 |
{ |
| 121 |
class => "Koha::Virtualshelves", |
| 122 |
value => { owner => $patron_1->id, public => 0 } |
| 123 |
} |
| 124 |
); |
| 125 |
|
| 126 |
# add share |
| 127 |
$builder->build_object( |
| 128 |
{ |
| 129 |
class => 'Koha::Virtualshelfshares', |
| 130 |
value => { shelfnumber => $private_list_shared->id, invitekey => undef, borrowernumber => $patron_2->id } |
| 131 |
} |
| 132 |
); |
| 133 |
|
| 134 |
t::lib::Mocks::mock_preference( 'ListOwnershipUponPatronDeletion', 'transfer' ); |
| 135 |
t::lib::Mocks::mock_preference( 'ListOwnerDesignated', undef ); |
| 136 |
t::lib::Mocks::mock_userenv({ patron => $patron_3 }); |
| 137 |
|
| 138 |
my $rs = Koha::Virtualshelves->search( { shelfnumber => [ $public_list->id, $private_list->id, $private_list_shared->id ] } ); |
| 139 |
|
| 140 |
my $result = $rs->disown_or_delete; |
| 141 |
is( ref($result), 'Koha::Virtualshelves', 'Return type is correct' ); |
| 142 |
$rs->reset; |
| 143 |
|
| 144 |
is( $rs->count, 2, 'The private/non-shared list was deleted' ); |
| 145 |
my $first = $rs->next; |
| 146 |
is( $first->id, $public_list->id ); |
| 147 |
is( $first->owner, $patron_3->id ); |
| 148 |
|
| 149 |
my $second = $rs->next; |
| 150 |
is( $second->id, $private_list_shared->id ); |
| 151 |
is( $second->owner, $patron_3->id ); |
| 152 |
|
| 153 |
$schema->storage->txn_rollback; |
| 154 |
}; |
| 155 |
|
| 156 |
subtest 'ListOwnershipUponPatronDeletion set to delete' => sub { |
| 157 |
|
| 158 |
plan tests => 2; |
| 159 |
|
| 160 |
$schema->storage->txn_begin; |
| 161 |
|
| 162 |
my $patron_1 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 163 |
my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 164 |
my $patron_3 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 165 |
|
| 166 |
my $public_list = $builder->build_object( |
| 167 |
{ |
| 168 |
class => "Koha::Virtualshelves", |
| 169 |
value => { owner => $patron_1->id, public => 1 } |
| 170 |
} |
| 171 |
); |
| 172 |
|
| 173 |
my $private_list = $builder->build_object( |
| 174 |
{ |
| 175 |
class => "Koha::Virtualshelves", |
| 176 |
value => { owner => $patron_1->id, public => 0 } |
| 177 |
} |
| 178 |
); |
| 179 |
|
| 180 |
my $private_list_shared = $builder->build_object( |
| 181 |
{ |
| 182 |
class => "Koha::Virtualshelves", |
| 183 |
value => { owner => $patron_1->id, public => 0 } |
| 184 |
} |
| 185 |
); |
| 186 |
|
| 187 |
# add share |
| 188 |
$builder->build_object( |
| 189 |
{ |
| 190 |
class => 'Koha::Virtualshelfshares', |
| 191 |
value => { shelfnumber => $private_list_shared->id, invitekey => undef, borrowernumber => $patron_2->id } |
| 192 |
} |
| 193 |
); |
| 194 |
|
| 195 |
t::lib::Mocks::mock_preference( 'ListOwnershipUponPatronDeletion', 'delete' ); |
| 196 |
|
| 197 |
my $rs = Koha::Virtualshelves->search( { shelfnumber => [ $public_list->id, $private_list->id, $private_list_shared->id ] } ); |
| 198 |
|
| 199 |
my $result = $rs->disown_or_delete; |
| 200 |
is( ref($result), 'Koha::Virtualshelves', 'Return type is correct' ); |
| 201 |
$rs->reset; |
| 202 |
|
| 203 |
is( $rs->count, 0, 'All lists deleted' ); |
| 204 |
|
| 205 |
$schema->storage->txn_rollback; |
| 206 |
}; |
| 207 |
}; |