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

84 lines
2.7 KiB
Perl
Executable file

#!/usr/bin/perl
#
# This module will excercise pdf creation routines
#
# When run with KEEP_PDF enviroment variable it will keep
# test.pdf for manual inspection. This can be used to verify
# that ttf font configuration is complete like:
#
# KEEP_PDF=1 KOHA_CONF=/etc/koha/sites/srvgit/koha-conf.xml prove t/Creators.t
#
# sample of utf-8 text, font name and type will be on bottom of second page
use strict;
use warnings;
use File::Temp qw/ tempfile /;
use Test::More tests => 41;
BEGIN {
use_ok('C4::Creators');
use_ok('C4::Creators::PDF', qw( Init Add Bookmark Compress Font FontSize Page StrWidth Text End ));
}
my $pdf_creator = C4::Creators::PDF->new(InitVars => 0);
ok($pdf_creator, "testing new() works");
if (-e $pdf_creator->{filename}) {
pass('testing pdf file created');
}
else {
fail('testing pdf file created');
}
ok($pdf_creator->Add(""), "testing Add() works");
ok($pdf_creator->Bookmark({}), "testing Bookmark() works");
ok($pdf_creator->Compress(1), "testing Compress() works");
is($pdf_creator->Font("H"), "Ft1", "testing Font() works");
is($pdf_creator->FontSize(), '12', "testing FontSize() is set to 12 by default");
my @result = $pdf_creator->FontSize(14);
is($result[0], '14', "testing FontSize() can be set to a different value");
$pdf_creator->FontSize(); # Reset font size before testing text width etc below
ok($pdf_creator->Page(), "testing Page() works");
my $expected_width;
my $expected_offset;
if (C4::Context->config('ttf')) {
$expected_width = '23.044921875';
$expected_offset = '33.044921875';
} else {
$expected_width = '19.344';
$expected_offset = '29.344';
}
is($pdf_creator->StrWidth("test", "H", 12), $expected_width, "testing StrWidth() returns correct point width");
@result = $pdf_creator->Text(10, 10, "test");
is($result[0], '10', "testing Text() writes from a given x-value");
is($result[1], $expected_offset, "testing Text() writes to the correct x-value");
my $font_types = C4::Creators::Lib::get_font_types();
isa_ok( $font_types, 'ARRAY', 'get_font_types' );
my $y = 50;
foreach my $font ( @$font_types ) {
ok( $pdf_creator->Font( $font->{type} ), 'Font ' . $font->{type} );
ok( $pdf_creator->Text(10, $y, "\x{10C}evap\x{10D}i\x{107} " . $font->{name} . ' - ' . $font->{type} ), 'Text ' . $font->{name});
$y += $pdf_creator->FontSize() * 1.2;
}
SKIP: {
skip "Skipping because without proper fonts these two tests fail",
2 if ! $ENV{KOHA_CONF};
my ($fh, $filename) = tempfile();
open( $fh, '>', $filename );
select $fh;
ok($pdf_creator->End(), "testing End() works");
close($fh);
ok( -s $filename , "test file $filename created OK" );
unlink $filename unless $ENV{KEEP_PDF};
}