@@ -, +, @@ convenience function C4::Context->setCommandlineEnvironment(); --- C4/Context.pm | 35 +++++++++++++++++++++++++++++++++++ t/db_dependent/Context_commandline.t | 30 ++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 t/db_dependent/Context_commandline.t --- a/C4/Context.pm +++ a/C4/Context.pm @@ -102,6 +102,7 @@ use ZOOM; use XML::Simple; use C4::Boolean; use C4::Debug; +use C4::Members; use POSIX (); use DateTime::TimeZone; use Module::Load::Conditional qw(can_load); @@ -1293,6 +1294,40 @@ sub IsSuperLibrarian { return ($userenv->{flags}//0) % 2; } +=head2 setCommandlineEnvironment + +Sets the Koha environment for command line scripts. + +=cut + +sub setCommandlineEnvironment { + my ($class) = @_; + + C4::Context->interface('commandline'); #Set interface for logger and friends + C4::Context->_new_userenv('commandline'); + my $clisu = _enforceCommandlineSuperuserBorrowerExists(); + C4::Context::set_userenv($clisu->{borrowernumber},$clisu->{userid},$clisu->{cardnumber},$clisu->{firstname},$clisu->{surname}, $clisu->{branchcode}, '', {}, '', '', ''); +} + +sub _enforceCommandlineSuperuserBorrowerExists { + my $commandlineSuperuser = C4::Members::GetMember(userid => 'commandlineadmin'); + unless ($commandlineSuperuser) { + my $dbh = C4::Context->dbh(); + my $ctgr = $dbh->selectrow_array("SELECT categorycode FROM categories WHERE category_type = 'I' LIMIT 1;"); + my $brnchcd = $dbh->selectrow_array("SELECT branchcode FROM borrowers GROUP BY branchcode ORDER BY COUNT(*) DESC LIMIT 1;"); + C4::Members::AddMember(cardnumber => 'commandlineadmin', + userid => 'commandlineadmin', + surname => 'Admin', + firstname => 'Koha', + dateexpiry => '2099-12-31', + categorycode => $ctgr, + branchcode => $brnchcd, + ); + $commandlineSuperuser = C4::Members::GetMember(userid => 'commandlineadmin'); + } + return $commandlineSuperuser; +} + =head2 interface Sets the current interface for later retrieval in any Perl module --- a/t/db_dependent/Context_commandline.t +++ a/t/db_dependent/Context_commandline.t @@ -0,0 +1,30 @@ +# Copyright 2016 KohaSuomi +# +# 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, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; +use Test::More; + +use C4::Context; + +my $commandlineSuperuser = C4::Context::_enforceCommandlineSuperuserBorrowerExists(); +is($commandlineSuperuser->{cardnumber}, "commandlineadmin", "_enforceCommandlineSuperuserBorrowerExists() enforced"); + +C4::Context->setCommandlineEnvironment(); +my $env = C4::Context->userenv(); +is($env->{id}, $commandlineSuperuser->{userid}, "setCommandlineEnvironment userenv set with 'commandlineadmin'"); + +done_testing(); --