Bug 30933: Unit tests
[koha.git] / t / db_dependent / Koha / Virtualshelf.t
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 'transfer_ownership() tests' => sub {
32
33     plan tests => 8;
34
35     $schema->storage->txn_begin;
36
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' });
40
41     my $public_list = $builder->build_object(
42         {
43             class => "Koha::Virtualshelves",
44             value => { owner => $patron_1->id, public => 1 }
45         }
46     );
47
48     my $private_list = $builder->build_object(
49         {
50             class => "Koha::Virtualshelves",
51             value => { owner => $patron_1->id, public => 0 }
52         }
53     );
54
55     throws_ok
56         { $public_list->transfer_ownership }
57         'Koha::Exceptions::MissingParameter',
58         'Exception thrown if missing parameter';
59
60     like( "$@", qr/Mandatory parameter 'patron' missing/, 'Exception string as expected' );
61
62     # add shares
63     $builder->build_object(
64         {
65             class => 'Koha::Virtualshelfshares',
66             value => { shelfnumber => $public_list->id,  invitekey => undef, borrowernumber => $patron_2->id }
67         }
68     );
69     $builder->build_object(
70         {
71             class => 'Koha::Virtualshelfshares',
72             value => { shelfnumber => $private_list->id, invitekey => undef, borrowernumber => $patron_2->id }
73         }
74     );
75     $builder->build_object(
76         {
77             class => 'Koha::Virtualshelfshares',
78             value => { shelfnumber => $private_list->id, invitekey => undef, borrowernumber => $patron_3->id }
79         }
80     );
81
82     $public_list->transfer_ownership( $patron_2->id );
83     $public_list->discard_changes;
84
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" );
89
90     $private_list->transfer_ownership( $patron_2->id );
91     $private_list->discard_changes;
92
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" );
97
98     $schema->storage->txn_rollback;
99 };