From a1bc9b2168e1c173e6446e87ec27446cab78589f Mon Sep 17 00:00:00 2001 From: Mark Tompsett Date: Wed, 18 Jan 2017 15:38:31 +0000 Subject: [PATCH] [SIGNED-OFF] Bug 17934: Make t/db_dependent/www/auth_values_input_www.t awesome There were two lines of code which declared unused variables. They were removed. Next, the issue of double planning encountered on bug 17540 (https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=17540#c3) was encountered. So this corrects that and adds a little more error handling into the whole C4::Context using process so that perlcritic -4 is achieved. Also typo on Korean in a comment. TEST PLAN --------- 2) follow these commands: $ mv C4/Context.pm C4/Context_backup.pm $ sudo koha-shell -c "prove t/db_dependent/www/auth_values_input_www.t" kohadev -- Nasty fail $ mv C4/Context_backup.pm C4/Context.pm $ sudo koha-shell -c "export PERL5LIB=\"\";prove t/db_dependent/www/auth_values_input_www.t" kohadev -- Nasty fail $ sudo koha-shell -c "export KOHA_CONF=\"\";prove t/db_dependent/www/auth_values_input_www.t" kohadev -- Nasty fail $ sudo koha-shell -c "export KOHA_INTRANET_URL=\"\";prove t/db_dependent/www/auth_values_input_www.t" kohadev $ sudo koha-shell -c "prove t/db_dependent/www/auth_values_input_www.t" kohadev -- Nasty fail $ sudo koha-shell -c "export KOHA_INTRANET_URL=\"http://localhost:8080/\";prove t/db_dependent/www/auth_values_input_www.t" kohadev -- This should work. $ perlcritic -4 t/db_dependent/www/auth_values_input_www.t -- This should be OK 2) apply patch 3) follow these commands: $ mv C4/Context.pm C4/Context_backup.pm $ sudo koha-shell -c "prove t/db_dependent/www/auth_values_input_www.t" kohadev -- This should trigger the C4::Context not working message $ mv C4/Context_backup.pm C4/Context.pm $ sudo koha-shell -c "export PERL5LIB=\"\";prove t/db_dependent/www/auth_values_input_www.t" kohadev -- This should trigger the C4::Contxt not loading message $ sudo koha-shell -c "export KOHA_CONF=\"\";prove t/db_dependent/www/auth_values_input_www.t" kohadev -- This should trigger the KOHA_CONF not set message $ sudo koha-shell -c "export KOHA_INTRANET_URL=\"\";prove t/db_dependent/www/auth_values_input_www.t" kohadev $ sudo koha-shell -c "prove t/db_dependent/www/auth_values_input_www.t" kohadev -- These should both trigger the KOHA_INTRANET_URL not set message $ sudo koha-shell -c "export KOHA_INTRANET_URL=\"http://localhost:8080/\";prove t/db_dependent/www/auth_values_input_www.t" kohadev -- This should work. $ perlcritic -4 t/db_dependent/www/auth_values_input_www.t -- This should be OK 3) run koha qa test tools Signed-off-by: Josef Moravec --- t/db_dependent/www/auth_values_input_www.t | 106 ++++++++++++++++------------- 1 file changed, 58 insertions(+), 48 deletions(-) diff --git a/t/db_dependent/www/auth_values_input_www.t b/t/db_dependent/www/auth_values_input_www.t index 265865c..d0a3c93 100644 --- a/t/db_dependent/www/auth_values_input_www.t +++ b/t/db_dependent/www/auth_values_input_www.t @@ -18,7 +18,7 @@ use Modern::Perl; use utf8; -use Test::More tests => 33; +use Test::More tests => 34; use Test::WWW::Mechanize; use XML::Simple; use JSON; @@ -27,47 +27,57 @@ use File::Spec; use POSIX; use URI::Escape; use Encode; +use Carp; +use Module::Load::Conditional qw/check_install can_load/; -my $testdir = File::Spec->rel2abs( dirname(__FILE__) ); +my $skip_all = q{}; +if ( ! check_install( module => 'C4::Context' ) ) { + $skip_all = "Tests skipped. You must have a working C4::Context.\n"; +} +elsif ( ! can_load( modules => { 'C4::Context' => undef } ) ) { + $skip_all = "Tests skipped. You must be able to load C4::Context.\n"; +} my $koha_conf = $ENV{KOHA_CONF}; -my $xml = XMLin($koha_conf); - -eval{ - use C4::Context; -}; -if ($@) { - plan skip_all => "Tests skip. You must have a working Context\n"; +my ($xml, $user, $password); +if ($koha_conf) { + $xml = XMLin($koha_conf); + $user = $ENV{KOHA_USER} || $xml->{config}->{user}; + $password = $ENV{KOHA_PASS} || $xml->{config}->{pass}; +} +elsif ($skip_all eq q{}) { + $skip_all = "Tests skipped. You must set env. variable KOHA_CONF to run tests.\n"; } -my $user = $ENV{KOHA_USER} || $xml->{config}->{user}; -my $password = $ENV{KOHA_PASS} || $xml->{config}->{pass}; my $intranet = $ENV{KOHA_INTRANET_URL}; -if (not defined $intranet) { - plan skip_all => "Tests skip. You must set env. variable KOHA_INTRANET_URL to do tests\n"; +if ($skip_all eq q{} && ! $intranet) { + $skip_all = "Tests skipped. You must set env. variable KOHA_INTRANET_URL to run tests\n"; } +if (length $skip_all > 0) { + croak $skip_all; # to keep Test Summary output +} + +use_ok('C4::Context'); my $dbh = C4::Context->dbh; -$intranet =~ s#/$##; +$intranet =~ s/\/$//xsm; my $agent = Test::WWW::Mechanize->new( autocheck => 1 ); -my $jsonresponse; my ($category, $expected_base, $add_form_link_exists, $delete_form_link_exists); # -------------------------------------------------- LOGIN - $agent->get_ok( "$intranet/cgi-bin/koha/mainpage.pl", 'connect to intranet' ); $agent->form_name('loginform'); $agent->field( 'password', $password ); $agent->field( 'userid', $user ); -$agent->field( 'branch', '' ); -$agent->click_ok( '', 'login to staff client' ); +$agent->field( 'branch', q{} ); +$agent->click_ok( q{}, 'login to staff client' ); $agent->get_ok( "$intranet/cgi-bin/koha/mainpage.pl", 'load main page' ); -#--------------------------------------------------- Test with corean and greek chars +#--------------------------------------------------- Test with Korean and greek chars $category = '学協会μμ'; $dbh->do(q|DELETE FROM authorised_values WHERE category = ?|, undef, $category); @@ -78,12 +88,12 @@ $agent->get_ok( "$intranet/cgi-bin/koha/admin/authorised_values.pl", 'Connect to $agent->get_ok( "$intranet/cgi-bin/koha/admin/authorised_values.pl?op=add_form", 'Open to create a new category' ); $agent->form_name('Aform'); $agent->field('category', $category); -$agent->click_ok( '', "Create new AV category " ); +$agent->click_ok( q{}, 'Create new AV category ' ); -$agent->base_like(qr|$expected_base|, "check base"); +$agent->base_like(qr/$expected_base/xsm, 'check base'); $add_form_link_exists = 0; for my $link ( $agent->links() ) { - if ( $link->url =~ m|authorised_values.pl\?op=add_form&category=$category| ) { + if ( $link->url =~ m/authorised_values.pl\x3Fop=add_form&category=$category/xsm ) { $add_form_link_exists = 1; } } @@ -94,34 +104,34 @@ $agent->form_name('Aform'); $agent->field('authorised_value', 'επιμεq'); $agent->field('lib_opac', 'autdesc2'); $agent->field('lib', 'desc1'); -$agent->field('branches', ''); -$agent->click_ok( '', "Create a new value for the category" ); +$agent->field('branches', q{}); +$agent->click_ok( q{}, 'Create a new value for the category' ); -$agent->base_like(qr|$expected_base|, "check base"); +$agent->base_like(qr/$expected_base/xsm, 'check base'); $add_form_link_exists = 0; $delete_form_link_exists = 0; for my $link ( $agent->links() ) { - if ( $link->url =~ m|authorised_values.pl\?op=add_form&category=$category| ) { + if ( $link->url =~ m/authorised_values.pl\x3Fop=add_form&category=$category/xsm ) { $add_form_link_exists = 1; - }elsif( $link->url =~ m|authorised_values.pl\?op=delete&searchfield=$category| ) { + }elsif( $link->url =~ m/authorised_values.pl\x3Fop=delete&searchfield=$category/xsm ) { $delete_form_link_exists = 1; } } is( $add_form_link_exists, 1, 'Add a new category button should be displayed'); -is( $delete_form_link_exists, 1, ''); +is( $delete_form_link_exists, 1, q{}); $agent->get_ok( "$intranet/cgi-bin/koha/admin/authorised_values.pl", 'Return to Authorized values page' ); $agent->get_ok( "$intranet/cgi-bin/koha/admin/authorised_values.pl?searchfield=学協会μμ&offset=0", 'Search the values inserted' ); my $text = $agent->text() ; #Tests on UTF-8 -ok ( ( length(Encode::encode('UTF-8', $text)) != length($text) ) , 'UTF-8 are multi-byte. Good') ; -ok ($text =~ m/学協会μμ/, 'UTF-8 (Asia) chars are correctly present. Good'); -ok ($text =~ m/επιμεq/, 'UTF-8 (Greek) chars are correctly present. Good'); +ok ( ( length Encode::encode('UTF-8', $text) != length $text ) , 'UTF-8 are multi-byte. Good') ; +ok ($text =~ m/学協会μμ/xsm, 'UTF-8 (Asia) chars are correctly present. Good'); +ok ($text =~ m/επιμεq/xsm, 'UTF-8 (Greek) chars are correctly present. Good'); my @links = $agent->links; -my $id_to_del =''; +my $id_to_del =q{}; foreach my $dato (@links){ my $link = $dato->url; - if ($link =~ m/op=delete\&searchfield=学協会μμ\&id=(\d+)/){ + if ($link =~ m/op=delete\&searchfield=学協会μμ\&id=(\d+)/xsm){ $id_to_del = $1; last; } @@ -129,7 +139,7 @@ foreach my $dato (@links){ if ($id_to_del) { $agent->get_ok( "$intranet/cgi-bin/koha/admin/authorised_values.pl?op=delete&searchfield=学協会μμ&id=$id_to_del", 'UTF_8 auth. value deleted' ); }else{ - ok($id_to_del ne undef, "error, link to delete not working"); + ok($id_to_del ne undef, 'error, link to delete not working'); } #---------------------------------------- Test with only latin utf-8 (could be taken as Latin-1/ISO 8859-1) @@ -142,42 +152,42 @@ $agent->get_ok( "$intranet/cgi-bin/koha/admin/authorised_values.pl", 'Connect to $agent->get_ok( "$intranet/cgi-bin/koha/admin/authorised_values.pl?op=add_form", 'Open to create a new category' ); $agent->form_name('Aform'); $agent->field('category', $category); -$agent->click_ok( '', "Create new AV category" ); +$agent->click_ok( q{}, 'Create new AV category' ); $agent->get_ok( "$intranet/cgi-bin/koha/admin/authorised_values.pl?op=add_form&category=$category", 'Open to create a new AV for this category' ); $agent->form_name('Aform'); $agent->field('authorised_value', 'ràmen'); $agent->field('lib_opac', 'autdesc2'); $agent->field('lib', 'desc1'); -$agent->field('branches', ''); -$agent->click_ok( '', "Create a new value for the category" ); +$agent->field('branches', q{}); +$agent->click_ok( q{}, 'Create a new value for the category' ); $expected_base = q|authorised_values.pl|; -$agent->base_like(qr|$expected_base|, "check base"); +$agent->base_like(qr/$expected_base/xsm, 'check base'); $add_form_link_exists = 0; $delete_form_link_exists = 0; for my $link ( $agent->links() ) { - if ( $link->url =~ m|authorised_values.pl\?op=add_form&category=$category| ) { + if ( $link->url =~ m/authorised_values.pl\x3Fop=add_form&category=$category/xsm ) { $add_form_link_exists = 1; - }elsif( $link->url =~ m|authorised_values.pl\?op=delete&searchfield=$category| ) { + }elsif( $link->url =~ m/authorised_values.pl\x3Fop=delete&searchfield=$category/xsm ) { $delete_form_link_exists = 1; } } -is( $add_form_link_exists, 1, ); -is( $delete_form_link_exists, 1, ); +is( $add_form_link_exists, 1, 'Add form link exists'); +is( $delete_form_link_exists, 1, 'Delete form link exists'); $agent->get_ok( "$intranet/cgi-bin/koha/admin/authorised_values.pl", 'Return to Authorized values page' ); $agent->get_ok( "$intranet/cgi-bin/koha/admin/authorised_values.pl?searchfield=tòmas&offset=0", 'Search the values inserted' ); my $text2 = $agent->text() ; #Tests on UTF-8 -ok ( ( length(Encode::encode('UTF-8', $text)) != length($text) ) , 'UTF-8 are multi-byte. Good') ; -ok ($text2 =~ m/tòmas/, 'UTF-8 not Latin-1 first test is OK. Good'); -ok ($text2=~ m/ràmen/, 'UTF-8 not Latin-1 second test is OK. Good'); +ok ( ( length Encode::encode('UTF-8', $text) != length $text ) , 'UTF-8 are multi-byte. Good') ; +ok ($text2 =~ m/tòmas/xsm, 'UTF-8 not Latin-1 first test is OK. Good'); +ok ($text2=~ m/ràmen/xsm, 'UTF-8 not Latin-1 second test is OK. Good'); my @links2 = $agent->links; -my $id_to_del2 =''; +my $id_to_del2 =q{}; foreach my $dato (@links2){ my $link = $dato->url; - if ($link =~ m/op=delete\&searchfield=tòmas\&id=(\d+)/){ + if ($link =~ m/op=delete\&searchfield=tòmas\&id=(\d+)/xsm){ $id_to_del2 = $1; last; } @@ -185,7 +195,7 @@ foreach my $dato (@links2){ if ($id_to_del2) { $agent->get_ok( "$intranet/cgi-bin/koha/admin/authorised_values.pl?op=delete&searchfield=tòmas&id=$id_to_del2", 'UTF_8 auth. value deleted' ); }else{ - ok($id_to_del2 ne undef, "error, link to delete not working"); + ok($id_to_del2 ne undef, 'error, link to delete not working'); } 1; -- 2.1.4