From 12b82c9b0d5d8c24d927f9a106c786dde6e8f05c Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Tue, 19 Jul 2022 14:49:24 -0300 Subject: [PATCH] Bug 30933: Unit tests Content-Type: text/plain; charset=utf-8 Signed-off-by: Tomas Cohen Arazi Signed-off-by: Marcel de Rooy --- t/db_dependent/Koha/Virtualshelf.t | 99 +++++++++++++ t/db_dependent/Koha/Virtualshelves.t | 207 +++++++++++++++++++++++++++ 2 files changed, 306 insertions(+) create mode 100755 t/db_dependent/Koha/Virtualshelf.t create mode 100755 t/db_dependent/Koha/Virtualshelves.t diff --git a/t/db_dependent/Koha/Virtualshelf.t b/t/db_dependent/Koha/Virtualshelf.t new file mode 100755 index 0000000000..af6c2e1b32 --- /dev/null +++ b/t/db_dependent/Koha/Virtualshelf.t @@ -0,0 +1,99 @@ +#!/usr/bin/perl + +# Copyright 2022 Koha Development team +# +# This file is part of Koha +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Test::More tests => 1; +use Test::Exception; + +use t::lib::Mocks; +use t::lib::TestBuilder; + +my $schema = Koha::Database->new->schema; +my $builder = t::lib::TestBuilder->new; + +subtest 'transfer_ownership() tests' => sub { + + plan tests => 8; + + $schema->storage->txn_begin; + + my $patron_1 = $builder->build_object({ class => 'Koha::Patrons' }); + my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' }); + my $patron_3 = $builder->build_object({ class => 'Koha::Patrons' }); + + my $public_list = $builder->build_object( + { + class => "Koha::Virtualshelves", + value => { owner => $patron_1->id, public => 1 } + } + ); + + my $private_list = $builder->build_object( + { + class => "Koha::Virtualshelves", + value => { owner => $patron_1->id, public => 0 } + } + ); + + throws_ok + { $public_list->transfer_ownership } + 'Koha::Exceptions::MissingParameter', + 'Exception thrown if missing parameter'; + + like( "$@", qr/Mandatory parameter 'patron' missing/, 'Exception string as expected' ); + + # add shares + $builder->build_object( + { + class => 'Koha::Virtualshelfshares', + value => { shelfnumber => $public_list->id, invitekey => undef, borrowernumber => $patron_2->id } + } + ); + $builder->build_object( + { + class => 'Koha::Virtualshelfshares', + value => { shelfnumber => $private_list->id, invitekey => undef, borrowernumber => $patron_2->id } + } + ); + $builder->build_object( + { + class => 'Koha::Virtualshelfshares', + value => { shelfnumber => $private_list->id, invitekey => undef, borrowernumber => $patron_3->id } + } + ); + + $public_list->transfer_ownership( $patron_2->id ); + $public_list->discard_changes; + + is( $public_list->owner, $patron_2->id, 'Owner changed correctly' ); + my $public_list_shares = $public_list->get_shares; + is( $public_list_shares->count, 1, 'Count is correct' ); + is( $public_list_shares->next->borrowernumber, $patron_2->id, "Public lists don't get the share removed" ); + + $private_list->transfer_ownership( $patron_2->id ); + $private_list->discard_changes; + + is( $private_list->owner, $patron_2->id ); + my $private_list_shares = $private_list->get_shares; + is( $private_list_shares->count, 1, 'Count is correct' ); + is( $private_list_shares->next->borrowernumber, $patron_3->id, "Private lists get the share for the new owner removed" ); + + $schema->storage->txn_rollback; +}; diff --git a/t/db_dependent/Koha/Virtualshelves.t b/t/db_dependent/Koha/Virtualshelves.t new file mode 100755 index 0000000000..9b39ebd5ae --- /dev/null +++ b/t/db_dependent/Koha/Virtualshelves.t @@ -0,0 +1,207 @@ +#!/usr/bin/perl + +# Copyright 2022 Koha Development team +# +# This file is part of Koha +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Test::More tests => 1; +use Test::Exception; + +use t::lib::Mocks; +use t::lib::TestBuilder; + +my $schema = Koha::Database->new->schema; +my $builder = t::lib::TestBuilder->new; + +subtest 'disown_or_delete() tests' => sub { + + plan tests => 3; + + subtest 'All set cases' => sub { + + plan tests => 6; + + $schema->storage->txn_begin; + + my $patron_1 = $builder->build_object({ class => 'Koha::Patrons' }); + my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' }); + my $patron_3 = $builder->build_object({ class => 'Koha::Patrons' }); + + my $public_list = $builder->build_object( + { + class => "Koha::Virtualshelves", + value => { owner => $patron_1->id, public => 1 } + } + ); + + my $private_list = $builder->build_object( + { + class => "Koha::Virtualshelves", + value => { owner => $patron_1->id, public => 0 } + } + ); + + my $private_list_shared = $builder->build_object( + { + class => "Koha::Virtualshelves", + value => { owner => $patron_1->id, public => 0 } + } + ); + + # add share + $builder->build_object( + { + class => 'Koha::Virtualshelfshares', + value => { shelfnumber => $private_list_shared->id, invitekey => undef, borrowernumber => $patron_3->id } + } + ); + + t::lib::Mocks::mock_preference( 'ListOwnershipUponPatronDeletion', 'transfer' ); + t::lib::Mocks::mock_preference( 'ListOwnerDesignated', $patron_2->id ); + + my $rs = Koha::Virtualshelves->search( { shelfnumber => [ $public_list->id, $private_list->id, $private_list_shared->id ] } ); + + my $result = $rs->disown_or_delete; + is( ref($result), 'Koha::Virtualshelves', 'Return type is correct' ); + $rs->reset; + + is( $rs->count, 2, 'The private/non-shared list was deleted' ); + my $first = $rs->next; + is( $first->id, $public_list->id ); + is( $first->owner, $patron_2->id ); + + my $second = $rs->next; + is( $second->id, $private_list_shared->id ); + is( $second->owner, $patron_2->id ); + + $schema->storage->txn_rollback; + }; + + subtest 'Fallback to userenv' => sub { + + plan tests => 6; + + $schema->storage->txn_begin; + + my $patron_1 = $builder->build_object({ class => 'Koha::Patrons' }); + my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' }); + my $patron_3 = $builder->build_object({ class => 'Koha::Patrons' }); + + my $public_list = $builder->build_object( + { + class => "Koha::Virtualshelves", + value => { owner => $patron_1->id, public => 1 } + } + ); + + my $private_list = $builder->build_object( + { + class => "Koha::Virtualshelves", + value => { owner => $patron_1->id, public => 0 } + } + ); + + my $private_list_shared = $builder->build_object( + { + class => "Koha::Virtualshelves", + value => { owner => $patron_1->id, public => 0 } + } + ); + + # add share + $builder->build_object( + { + class => 'Koha::Virtualshelfshares', + value => { shelfnumber => $private_list_shared->id, invitekey => undef, borrowernumber => $patron_2->id } + } + ); + + t::lib::Mocks::mock_preference( 'ListOwnershipUponPatronDeletion', 'transfer' ); + t::lib::Mocks::mock_preference( 'ListOwnerDesignated', undef ); + t::lib::Mocks::mock_userenv({ patron => $patron_3 }); + + my $rs = Koha::Virtualshelves->search( { shelfnumber => [ $public_list->id, $private_list->id, $private_list_shared->id ] } ); + + my $result = $rs->disown_or_delete; + is( ref($result), 'Koha::Virtualshelves', 'Return type is correct' ); + $rs->reset; + + is( $rs->count, 2, 'The private/non-shared list was deleted' ); + my $first = $rs->next; + is( $first->id, $public_list->id ); + is( $first->owner, $patron_3->id ); + + my $second = $rs->next; + is( $second->id, $private_list_shared->id ); + is( $second->owner, $patron_3->id ); + + $schema->storage->txn_rollback; + }; + + subtest 'ListOwnershipUponPatronDeletion set to delete' => sub { + + plan tests => 2; + + $schema->storage->txn_begin; + + my $patron_1 = $builder->build_object({ class => 'Koha::Patrons' }); + my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' }); + my $patron_3 = $builder->build_object({ class => 'Koha::Patrons' }); + + my $public_list = $builder->build_object( + { + class => "Koha::Virtualshelves", + value => { owner => $patron_1->id, public => 1 } + } + ); + + my $private_list = $builder->build_object( + { + class => "Koha::Virtualshelves", + value => { owner => $patron_1->id, public => 0 } + } + ); + + my $private_list_shared = $builder->build_object( + { + class => "Koha::Virtualshelves", + value => { owner => $patron_1->id, public => 0 } + } + ); + + # add share + $builder->build_object( + { + class => 'Koha::Virtualshelfshares', + value => { shelfnumber => $private_list_shared->id, invitekey => undef, borrowernumber => $patron_2->id } + } + ); + + t::lib::Mocks::mock_preference( 'ListOwnershipUponPatronDeletion', 'delete' ); + + my $rs = Koha::Virtualshelves->search( { shelfnumber => [ $public_list->id, $private_list->id, $private_list_shared->id ] } ); + + my $result = $rs->disown_or_delete; + is( ref($result), 'Koha::Virtualshelves', 'Return type is correct' ); + $rs->reset; + + is( $rs->count, 0, 'All lists deleted' ); + + $schema->storage->txn_rollback; + }; +}; -- 2.20.1