Koha/t/db_dependent/CourseReserves.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

140 lines
5.4 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 => 26;
use Koha::Database;
use t::lib::TestBuilder;
BEGIN {
use_ok('C4::Biblio', qw( AddBiblio ));
use_ok('C4::CourseReserves', qw( GetCourse ModCourse GetCourses DelCourse GetCourseInstructors ModCourseInstructors GetCourseItem ModCourseItem GetCourseReserve ModCourseReserve GetCourseReserves DelCourseReserve SearchCourses GetItemCourseReservesInfo ));
use_ok('C4::Context');
use_ok('MARC::Field');
use_ok('MARC::Record');
}
my $schema = Koha::Database->schema;
$schema->storage->txn_begin;
my $builder = t::lib::TestBuilder->new;
my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode};
my $itemtype = $builder->build(
{ source => 'Itemtype', value => { notforloan => undef } } )->{itemtype};
# Create 10 sample borrowers
my @borrowers = ();
foreach (1..10) {
push @borrowers, $builder->build({ source => 'Borrower' });
}
# Create the a record with an item
my $record = MARC::Record->new;
my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio($record, '');
my $itemnumber = Koha::Item->new(
{
biblionumber => $biblionumber,
homebranch => $branchcode,
holdingbranch => $branchcode,
itype => $itemtype
},
)->store->itemnumber;
my $course_id = ModCourse(
course_name => "Test Course",
staff_note => "Test staff note",
public_note => "Test public note",
);
ok( $course_id, "ModCourse created course successfully" );
$course_id = ModCourse(
course_id => $course_id,
staff_note => "Test staff note 2",
enabled => 'no',
);
my $course = GetCourse($course_id);
ok( $course->{'course_name'} eq "Test Course", "GetCourse returned correct course" );
ok( $course->{'staff_note'} eq "Test staff note 2", "ModCourse updated course succesfully" );
is( $course->{'enabled'}, 'no', "Test Course is disabled" );
my $courses = GetCourses();
is( ref($courses), 'ARRAY', "GetCourses returns an array" );
my @match = map {$_->{course_name} eq 'Test Course'} @$courses;
ok( scalar(@match) > 0, "GetCourses returns valid array of course data" );
ModCourseInstructors( mode => 'add', course_id => $course_id, borrowernumbers => [ $borrowers[0]->{'borrowernumber'} ] );
$course = GetCourse($course_id);
ok( $course->{'instructors'}->[0]->{'borrowernumber'} == $borrowers[0]->{'borrowernumber'}, "ModCourseInstructors added instructors correctly" );
my $course_instructors = GetCourseInstructors($course_id);
ok( $course_instructors->[0]->{'borrowernumber'} eq $borrowers[0]->{'borrowernumber'}, "GetCourseInstructors returns valid data" );
my $ci_id = ModCourseItem( 'itemnumber' => $itemnumber );
ok( $ci_id, "ModCourseItem returned valid data" );
my $course_item = GetCourseItem( 'ci_id' => $ci_id );
ok( $course_item->{'itemnumber'} eq $itemnumber, "GetCourseItem returns valid data" );
my $cr_id = ModCourseReserve( 'course_id' => $course_id, 'ci_id' => $ci_id );
ok( $cr_id, "ModCourseReserve returns valid data" );
my $course_reserve = GetCourseReserve( 'course_id' => $course_id, 'ci_id' => $ci_id );
ok( $course_reserve->{'cr_id'} eq $cr_id, "GetCourseReserve returns valid data" );
my $course_reserves = GetCourseReserves( 'course_id' => $course_id );
ok( $course_reserves->[0]->{'ci_id'} eq $ci_id, "GetCourseReserves returns valid data." );
## Check for regression of Bug 15530
$course_id = ModCourse(
course_id => $course_id,
enabled => 'yes',
);
$course = GetCourse($course_id);
is( $course->{'enabled'}, 'yes', "Test Course is enabled" );
$course_item = GetCourseItem( 'ci_id' => $ci_id );
is( $course_item->{enabled}, 'yes', "Course item is enabled after modding disabled course" );
my $disabled_course_id = ModCourse(
course_name => "Disabled Course",
enabled => 'no',
);
my $disabled_course = GetCourse( $disabled_course_id );
is( $disabled_course->{'enabled'}, 'no', "Disabled Course is disabled" );
my $cr_id2 = ModCourseReserve( 'course_id' => $disabled_course_id, 'ci_id' => $ci_id );
$course_item = GetCourseItem( 'ci_id' => $ci_id );
is( $course_item->{enabled}, 'yes', "Course item is enabled after modding disabled course" );
## End check for regression of Bug 15530
my $info = GetItemCourseReservesInfo( itemnumber => $itemnumber );
ok( $info->[0]->{'itemnumber'} eq $itemnumber, "GetItemReservesInfo returns valid data." );
DelCourseReserve( 'cr_id' => $cr_id );
$course_reserve = GetCourseReserve( 'cr_id' => $cr_id );
ok( !defined( $course_reserve->{'cr_id'} ), "DelCourseReserve functions correctly" );
DelCourse($course_id);
$course = GetCourse($course_id);
ok( !defined( $course->{'course_id'} ), "DelCourse deleted course successfully" );
$courses = SearchCourses(); # FIXME Lack of tests for SearchCourses
is( ref($courses), 'ARRAY', 'SearchCourses should not crash and return an arrayref' );
$schema->storage->txn_rollback;