3 # Copyright 2022 Koha Development team
5 # This file is part of Koha
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.
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.
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>.
22 use Test::More tests => 1;
26 use t::lib::TestBuilder;
28 my $schema = Koha::Database->new->schema;
29 my $builder = t::lib::TestBuilder->new;
31 subtest 'transfer_ownership() tests' => sub {
35 $schema->storage->txn_begin;
37 my $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
38 my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
39 my $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
41 my $public_list = $builder->build_object(
43 class => "Koha::Virtualshelves",
44 value => { owner => $patron_1->id, public => 1 }
48 my $private_list = $builder->build_object(
50 class => "Koha::Virtualshelves",
51 value => { owner => $patron_1->id, public => 0 }
56 { $public_list->transfer_ownership }
57 'Koha::Exceptions::MissingParameter',
58 'Exception thrown if missing parameter';
60 like( "$@", qr/Mandatory parameter 'patron' missing/, 'Exception string as expected' );
63 $builder->build_object(
65 class => 'Koha::Virtualshelfshares',
66 value => { shelfnumber => $public_list->id, invitekey => undef, borrowernumber => $patron_2->id }
69 $builder->build_object(
71 class => 'Koha::Virtualshelfshares',
72 value => { shelfnumber => $private_list->id, invitekey => undef, borrowernumber => $patron_2->id }
75 $builder->build_object(
77 class => 'Koha::Virtualshelfshares',
78 value => { shelfnumber => $private_list->id, invitekey => undef, borrowernumber => $patron_3->id }
82 $public_list->transfer_ownership( $patron_2->id );
83 $public_list->discard_changes;
85 is( $public_list->owner, $patron_2->id, 'Owner changed correctly' );
86 my $public_list_shares = $public_list->get_shares;
87 is( $public_list_shares->count, 1, 'Count is correct' );
88 is( $public_list_shares->next->borrowernumber, $patron_2->id, "Public lists don't get the share removed" );
90 $private_list->transfer_ownership( $patron_2->id );
91 $private_list->discard_changes;
93 is( $private_list->owner, $patron_2->id );
94 my $private_list_shares = $private_list->get_shares;
95 is( $private_list_shares->count, 1, 'Count is correct' );
96 is( $private_list_shares->next->borrowernumber, $patron_3->id, "Private lists get the share for the new owner removed" );
98 $schema->storage->txn_rollback;