From f949344d636e170032b91f2dbd746a49906c0fc9 Mon Sep 17 00:00:00 2001 From: Alex Buckley Date: Sat, 18 Mar 2017 04:34:29 +0000 Subject: [PATCH] Bug 18299 - Moved the SQL queries from admin/env_tz_test.pl into the C4/Context.pm in two new subroutines. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Both new subroutines have a successful subtest in the t/db_dependent/Context.t unit test in addition to POD. Signed-off-by: Marc VĂ©ron --- C4/Context.pm | 25 +++++++++++++++++++++++++ admin/env_tz_test.pl | 5 +++-- t/db_dependent/Context.t | 25 ++++++++++++++++++++++++- 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/C4/Context.pm b/C4/Context.pm index a50ddfd..bd40167 100644 --- a/C4/Context.pm +++ b/C4/Context.pm @@ -821,6 +821,31 @@ sub _new_queryparser { return; } + +=head2 get_env_variables +my $tz_sth = C4::Context->get_env_variables(); + +This subroutine retrieves and returns all variable records where variable=time_zone +=cut + +sub get_env_variables { + my $dbh = C4::Context->dbh; + my $tz_sth = $dbh->prepare("SHOW VARIABLES LIKE 'time_zone'"); + return $tz_sth; +} + +=head2 get_current_time +my $now_sth = C4::Context->get_current_time(); + +This subroutine retrieves and returns the current time +=cut + +sub get_current_time { + my $dbh = C4::Context->dbh; + my $current_time = $dbh->prepare("SELECT now()"); + return $current_time; +} + =head2 userenv C4::Context->userenv; diff --git a/admin/env_tz_test.pl b/admin/env_tz_test.pl index ecbb93d..28f15ba 100755 --- a/admin/env_tz_test.pl +++ b/admin/env_tz_test.pl @@ -21,9 +21,10 @@ my ($template, $loggedinuser, $cookie) = get_template_and_user( ); my $dbh = C4::Context->dbh; -my $tz_sth = $dbh->prepare("SHOW VARIABLES LIKE 'time_zone'"); +my $tz_sth = C4::Context->get_env_variables(); $tz_sth->execute(); -my $now_sth = $dbh->prepare("SELECT now()"); + +my $now_sth = C4::Context->get_current_time(); $now_sth->execute(); print $q->header(), diff --git a/t/db_dependent/Context.t b/t/db_dependent/Context.t index bce0cef..d36c106 100755 --- a/t/db_dependent/Context.t +++ b/t/db_dependent/Context.t @@ -7,8 +7,9 @@ use Test::More; use Test::MockModule; use vars qw($debug $koha $dbh $config $ret); use t::lib::Mocks; - +use Test::More tests => 41; use Koha::Database; +use DateTime; BEGIN { $debug = $ENV{DEBUG} || 0; @@ -135,6 +136,28 @@ C4::Context->set_preference('AutoEmailOpacUser', ''); my $yesno_pref = Koha::Config::SysPrefs->find('AutoEmailOpacUser'); is( $yesno_pref->value(), 0, 'set_preference should have set the value to 0, instead of an empty string' ); + +sub get_env_variables { + my $sth = $dbh->prepare("SHOW VARIABLES LIKE 'time_zone'"); + $sth->execute(); + return $sth->rows; +} +my $env_var = get_env_variables(); +is($env_var, 1, "correct number of variables like time_zone"); + +sub get_current_time { + my $sth = $dbh->prepare("SELECT now()"); + $sth->execute(); + return $sth->fetchrow_array;# Return a time value rather than hash value +} +my $datetime = DateTime->now; +my $date = $datetime->ymd; +my $time = $datetime->hms; +my $compardt = "$date $time"; + +my $current_time = get_current_time(); +is($current_time, $compardt, "Correct time"); + done_testing(); sub TransformVersionToNum { -- 2.1.4