Koha/t/db_dependent/RotatingCollections.t
Jonathan Druart 9d6d641d1f Bug 17600: Standardize our EXPORT_OK
On bug 17591 we discovered that there was something weird going on with
the way we export and use subroutines/modules.
This patch tries to standardize our EXPORT to use EXPORT_OK only.

That way we will need to explicitely define the subroutine we want to
use from a module.

This patch is a squashed version of:
Bug 17600: After export.pl
Bug 17600: After perlimport
Bug 17600: Manual changes
Bug 17600: Other manual changes after second perlimports run
Bug 17600: Fix tests

And a lot of other manual changes.

export.pl is a dirty script that can be found on bug 17600.

"perlimport" is:
git clone https://github.com/oalders/App-perlimports.git
cd App-perlimports/
cpanm --installdeps .
export PERL5LIB="$PERL5LIB:/kohadevbox/koha/App-perlimports/lib"
find . \( -name "*.pl" -o -name "*.pm" \) -exec perl App-perlimports/script/perlimports --inplace-edit --no-preserve-unused --filename {} \;

The ideas of this patch are to:
* use EXPORT_OK instead of EXPORT
* perltidy the EXPORT_OK list
* remove '&' before the subroutine names
* remove some uneeded use statements
* explicitely import the subroutines we need within the controllers or
modules

Note that the private subroutines (starting with _) should not be
exported (and not used from outside of the module except from tests).

EXPORT vs EXPORT_OK (from
https://www.thegeekstuff.com/2010/06/perl-exporter-examples/)
"""
Export allows to export the functions and variables of modules to user’s namespace using the standard import method. This way, we don’t need to create the objects for the modules to access it’s members.

@EXPORT and @EXPORT_OK are the two main variables used during export operation.

@EXPORT contains list of symbols (subroutines and variables) of the module to be exported into the caller namespace.

@EXPORT_OK does export of symbols on demand basis.
"""

If this patch caused a conflict with a patch you wrote prior to its
push:
* Make sure you are not reintroducing a "use" statement that has been
removed
* "$subroutine" is not exported by the C4::$MODULE module
means that you need to add the subroutine to the @EXPORT_OK list
* Bareword "$subroutine" not allowed while "strict subs"
means that you didn't imported the subroutine from the module:
  - use $MODULE qw( $subroutine list );
You can also use the fully qualified namespace: C4::$MODULE::$subroutine

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
2021-07-16 08:58:47 +02:00

347 lines
12 KiB
Perl
Executable file

#!/usr/bin/perl
# 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 <http://www.gnu.org/licenses>.
use Modern::Perl;
use Test::More tests => 53;
use C4::Context;
use C4::RotatingCollections qw( AddItemToCollection CreateCollection DeleteCollection GetCollection GetCollectionItemBranches GetCollections GetItemsInCollection RemoveItemFromCollection TransferCollection UpdateCollection isItemInAnyCollection isItemInThisCollection );
use C4::Biblio qw( AddBiblio );
use Koha::Database;
use Koha::Library;
use t::lib::TestBuilder;
BEGIN {
}
can_ok(
'C4::RotatingCollections',
qw(
AddItemToCollection
CreateCollection
DeleteCollection
GetCollection
GetCollectionItemBranches
GetCollections
GetItemsInCollection
RemoveItemFromCollection
TransferCollection
UpdateCollection
isItemInAnyCollection
isItemInThisCollection
)
);
my $schema = Koha::Database->new->schema;
$schema->storage->txn_begin;
my $dbh = C4::Context->dbh;
#Start Tests
$dbh->do(q|DELETE FROM issues |);
$dbh->do(q|DELETE FROM borrowers |);
$dbh->do(q|DELETE FROM items |);
$dbh->do(q|DELETE FROM collections_tracking |);
$dbh->do(q|DELETE FROM collections |);
$dbh->do(q|DELETE FROM branches |);
$dbh->do(q|DELETE FROM categories|);
my $builder = t::lib::TestBuilder->new;
#Test CreateCollection
my $collections = GetCollections();
my $countcollection = scalar(@$collections);
my ($success,$errorCode,$errorMessage);
($success,$errorCode,$errorMessage) = CreateCollection( 'Collection1', 'Description1' );
is( $success, 1, "All parameters have been given - Collection 1 added" );
ok( !defined $errorCode && !defined $errorMessage,
"Collection added, no error code or message");
my $collection_id1 = $dbh->last_insert_id( undef, undef, 'collections', undef );
($success,$errorCode,$errorMessage) = CreateCollection( 'Collection2', 'Description2' );
is( $success, 1, "All parameters have been given - Collection 2 added" );
ok( !defined $errorCode && !defined $errorMessage,
"Collection added, no error code or message");
my $collection_id2 = $dbh->last_insert_id( undef, undef, 'collections', undef );
$collections = GetCollections();
is( scalar(@$collections), $countcollection + 2,
"Collection1 and Collection2 have been added" );
($success,$errorCode,$errorMessage) = CreateCollection('Collection3');
is( $success, 1, "Collections can be created without description" );
ok( !defined $errorCode && !defined $errorMessage,
"Collection added, no error code or message");
my $collection_id3 = $dbh->last_insert_id( undef, undef, 'collections', undef );
($success,$errorCode,$errorMessage) = CreateCollection();
is( $success, 0, "Title missing, fails to create collection" );
is( $errorCode, 1, "Title missing, error code is 1" );
is( $errorMessage, 'NO_TITLE', "Title missing, error message is NO_TITLE" );
$collections = GetCollections();
is( scalar(@$collections), $countcollection + 3, "Only one collection added" );
#FIXME, as the id is auto incremented, two similar Collections (same title /same description) can be created
#$collection1 = CreateCollection('Collection1','Description1');
#Test GetCollections
my $collection = GetCollections();
is_deeply(
$collections,
[
{
colId => $collection_id1,
colTitle => 'Collection1',
colDesc => 'Description1',
colBranchcode => undef
},
{
colId => $collection_id2,
colTitle => 'Collection2',
colDesc => 'Description2',
colBranchcode => undef
},
{
colId => $collection_id3,
colTitle => 'Collection3',
colDesc => '',
colBranchcode => undef
}
],
'All Collections'
);
#Test UpdateCollection
($success,$errorCode,$errorMessage) =
UpdateCollection( $collection_id2, 'Collection2bis', undef );
is( $success, 1, "UpdateCollection succeeds without description");
($success,$errorCode,$errorMessage) =
UpdateCollection( $collection_id2, 'Collection2 modified', 'Description2 modified' );
is( $success, 1, "Collection2 has been modified" );
ok( !defined $errorCode && !defined $errorMessage,
"Collection2 modified, no error code or message");
($success,$errorCode,$errorMessage) =
UpdateCollection( $collection_id2, undef, 'Description' ),
ok( !$success, "UpdateCollection fails without title" );
is( $errorCode, 2, "Title missing, error code is 2");
is( $errorMessage, 'NO_TITLE', "Title missing, error message is NO_TITLE");
is( UpdateCollection(), 'NO_ID', "UpdateCollection without params" );
#Test GetCollection
my @collection1 = GetCollection($collection_id1);
is_deeply(
\@collection1,
[ $collection_id1, 'Collection1', 'Description1', undef ],
"Collection1's informations"
);
my @collection2 = GetCollection($collection_id2);
is_deeply(
\@collection2,
[ $collection_id2, 'Collection2 modified', 'Description2 modified', undef ],
"Collection2's informations"
);
my @undef_collection = GetCollection();
is_deeply(
\@undef_collection,
[ undef, undef, undef, undef ],
"GetCollection without id given"
);
@undef_collection = GetCollection(-1);
is_deeply(
\@undef_collection,
[ undef, undef, undef, undef ],
"GetCollection with a wrong id"
);
#Test TransferCollection
my $samplebranch = {
branchcode => 'SAB',
branchname => 'Sample Branch',
branchaddress1 => 'sample adr1',
branchaddress2 => 'sample adr2',
branchaddress3 => 'sample adr3',
branchzip => 'sample zip',
branchcity => 'sample city',
branchstate => 'sample state',
branchcountry => 'sample country',
branchphone => 'sample phone',
branchfax => 'sample fax',
branchemail => 'sample email',
branchurl => 'sample url',
branchip => 'sample ip',
branchnotes => 'sample note',
opac_info => 'sample opac',
};
Koha::Library->new($samplebranch)->store;
my ( $transferred, $messages ) = TransferCollection( $collection_id1, $samplebranch->{branchcode} );
is( $transferred,
1, "Collection1 has been transfered in the branch SAB" );
@collection1 = GetCollection($collection_id1);
is_deeply(
\@collection1,
[
$collection_id1, 'Collection1',
'Description1', $samplebranch->{branchcode}
],
"Collection1 belongs to the sample branch (SAB)"
);
( $transferred, $messages ) = TransferCollection();
is( $messages->[0]->{code}, "NO_ID", "TransferCollection without ID" );
( $transferred, $messages ) = TransferCollection($collection_id1);
is( $messages->[0]->{code},
'NO_BRANCHCODE', "TransferCollection without branchcode" );
#Test AddItemToCollection
my $record = MARC::Record->new();
$record->append_fields(
MARC::Field->new(
'952', '0', '0',
a => $samplebranch->{branchcode},
b => $samplebranch->{branchcode}
)
);
my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio( $record, '', );
my $item_id1 = $builder->build_sample_item(
{
biblionumber => $biblionumber,
library => $samplebranch->{branchcode},
barcode => 1, # FIXME This must not be hardcoded!
itemcallnumber => 'callnumber1',
}
)->itemnumber;
my $item_id2 = $builder->build_sample_item(
{
biblionumber => $biblionumber,
library => $samplebranch->{branchcode},
barcode => 2, # FIXME This must not be hardcoded!
itemcallnumber => 'callnumber2',
}
)->itemnumber;
is( AddItemToCollection( $collection_id1, $item_id1 ),
1, "Sampleitem1 has been added to Collection1" );
is( AddItemToCollection( $collection_id1, $item_id2 ),
1, "Sampleitem2 has been added to Collection1" );
#Test GetItemsInCollection
my $itemsincollection1;
($itemsincollection1,$success,$errorCode,$errorMessage) = GetItemsInCollection($collection_id1);
is( scalar @$itemsincollection1, 2, "Collection1 has 2 items" );
is_deeply(
$itemsincollection1,
[
{
title => undef,
itemcallnumber => 'callnumber1',
biblionumber => $biblionumber,
barcode => 1
},
{
title => undef,
itemcallnumber => 'callnumber2',
biblionumber => $biblionumber,
barcode => 2
}
],
"Collection1 has Item1 and Item2"
);
($itemsincollection1,$success,$errorCode,$errorMessage) = GetItemsInCollection();
ok( !$success, "GetItemsInCollection fails without a collection ID" );
is( $errorCode, 1, "Title missing, error code is 2");
is( $errorMessage, 'NO_ID', "Collection ID missing, error message is NO_ID");
#Test RemoveItemFromCollection
is( RemoveItemFromCollection( $collection_id1, $item_id2 ),
1, "Item2 has been removed from collection 1" );
$itemsincollection1 = GetItemsInCollection($collection_id1);
is( scalar @$itemsincollection1, 1, "Collection1 has 1 items" );
#Test isItemInAnyCollection
is( C4::RotatingCollections::isItemInAnyCollection($item_id1),
1, "Item1 is in a collection" );
is( C4::RotatingCollections::isItemInAnyCollection($item_id2),
0, "Item2 is not in a collection" );
is( C4::RotatingCollections::isItemInAnyCollection(),
0, "isItemInAnyCollection returns 0 if no itemid given " );
is( C4::RotatingCollections::isItemInAnyCollection(-1),
0, "isItemInAnyCollection returns 0 if a wrong id is given" );
#Test isItemInThisCollection
is(
C4::RotatingCollections::isItemInThisCollection(
$item_id1, $collection_id1
),
1,
"Item1 is in the Collection1"
);
is(
C4::RotatingCollections::isItemInThisCollection(
$item_id1, $collection_id2
),
0,
"Item1 is not in the Collection2"
);
is(
C4::RotatingCollections::isItemInThisCollection(
$item_id2, $collection_id2
),
0,
"Item2 is not in the Collection2"
);
is( C4::RotatingCollections::isItemInThisCollection($collection_id1),
0, "isItemInThisCollection returns 0 is ItemId is missing" );
is( C4::RotatingCollections::isItemInThisCollection($item_id1),
0, "isItemInThisCollection returns 0 is Collectionid if missing" );
is( C4::RotatingCollections::isItemInThisCollection(),
0, "isItemInThisCollection returns 0 if no params given" );
#Re-add item to test deletion of collection
AddItemToCollection( $collection_id1, $item_id1 );
#Test DeleteCollection
is( DeleteCollection($collection_id2), 1, "Collection2 deleted" );
is( DeleteCollection($collection_id1), 1, "Collection1 deleted" );
is(
DeleteCollection(),
'NO_ID',
"DeleteCollection without id"
);
$collections = GetCollections();
is(
scalar(@$collections),
$countcollection + 1,
"Two Collections have been deleted"
);
is( C4::RotatingCollections::isItemInAnyCollection($item_id1),
0, "Item1 is no longer in a collection after it is deleted" );
is(
C4::RotatingCollections::isItemInThisCollection(
$item_id1, $collection_id1
),
0,
"Item1 is not in the deleted Collection1"
);
$schema->storage->txn_rollback;