Skip to content

Commit

Permalink
Merge branch 'rel-10_1' into issue-#737-filter_content
Browse files Browse the repository at this point in the history
  • Loading branch information
bschmalhofer committed Jan 21, 2021
2 parents 343b155 + 8e399d8 commit 730dcdd
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 39 deletions.
79 changes: 65 additions & 14 deletions Kernel/System/Package.pm
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ use strict;
use warnings;
use utf8;

# core modules
use MIME::Base64;
use File::Copy;
use File::Copy qw(copy move);

# CPAN modules

# OTOBO modules
use Kernel::Config;
use Kernel::System::SysConfig;
use Kernel::System::WebUserAgent;

use Kernel::System::VariableCheck qw(:all);
use Kernel::Language qw(Translatable);

Expand Down Expand Up @@ -71,7 +74,8 @@ create an object
=cut

sub new {
my ( $Type, %Param ) = @_;
my $Type = shift;
my %Param = @_;

# allocate new hash for object
my $Self = {};
Expand Down Expand Up @@ -3528,10 +3532,6 @@ sub PackageUpgradeAllIsRunning {
);
}

=begin Internal:
=cut

sub _Download {
my ( $Self, %Param ) = @_;

Expand Down Expand Up @@ -4023,8 +4023,41 @@ sub _PackageFileCheck {
return 1;
}

=head2 _FileInstall()
Update or create files below the OTOBO home dir or below a specified dir.
Additionally this method creates a backup if needed.
There is also special support for notifying the webserver about new modules
in the F<Custom/Kernel> folder. These files may override core modules in F<Kernel>,
but module refreshers like M<Module::Refresh> won't catch this. Therefore
_FileInstall() will touch the core module when it exists.
Return undef on failure, 1 on success.
my $File = {
Location => 'Custom/Kernel/System/MyExtension/MyFeature.pm'
Content => $MyFeatureCode,
Permission => '644', # unix file permissions
};
# File install below the OTOBO home dir
my $FileInstallOk = $PackageObject->_FileInstall(
File => $File,
);
# File install below a specified dir
my $FileInstallOk = $PackageObject->_FileInstall(
File => $File,
Home => $ExportDir
);
=cut

sub _FileInstall {
my ( $Self, %Param ) = @_;
my $Self = shift;
my %Param = @_;

# check needed stuff
for my $Needed (qw(File)) {
Expand All @@ -4033,6 +4066,7 @@ sub _FileInstall {
Priority => 'error',
Message => "$Needed not defined!",
);

return;
}
}
Expand All @@ -4042,18 +4076,20 @@ sub _FileInstall {
Priority => 'error',
Message => "$Item not defined in File!",
);

return;
}
}

my $Home = $Param{Home} || $Self->{Home};

# check Home
if ( !-e $Home ) {
if ( ! -e $Home ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => "No such home directory: $Home!",
);

return;
}

Expand Down Expand Up @@ -4126,13 +4162,32 @@ sub _FileInstall {
}

# write file
return if !$MainObject->FileWrite(
my $FileWriteOk = $MainObject->FileWrite(
Location => $RealFile,
Content => \$Param{File}->{Content},
Mode => 'binmode',
Permission => $Param{File}->{Permission},
);

if ( ! $FileWriteOk ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => "Sorry, can't install package because the file $RealFile can't be created.",
);

return;
}

# trigger Module::Refresh when a custom module overrides a core module
if ( $RealFile =~ m!^\Q$Home\E/Custom/Kernel/.*\.pm! ) {
my $CoreModuleFn = $RealFile =~ s!^\Q$Home/Custom/!$Home/!r;

# touch the original module, ignore errors
if ( -f $CoreModuleFn ) {
utime undef, undef, $CoreModuleFn;
}
}

print STDERR "Notice: Install $RealFile ($Param{File}->{Permission})!\n";

return 1;
Expand Down Expand Up @@ -5346,8 +5401,4 @@ sub DESTROY {
return 1;
}

=end Internal:
=cut

1;
11 changes: 11 additions & 0 deletions bin/otobo.CheckModules.pl
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,17 @@
ports => 'devel/p5-Try-Tiny',
},
},
{
Module => 'URI',
Required => 1,
Comment => 'for generating properly escaped URLs',
InstTypes => {
aptget => 'liburi-perl',
emerge => 'dev-perl/URI',
zypper => 'perl-URI',
ports => 'devel/p5-URI',
},
},
{
Module => 'XML::LibXML',
Required => 1,
Expand Down
3 changes: 3 additions & 0 deletions cpanfile
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ requires 'Time::HiRes';

requires 'Try::Tiny';

# for generating properly escaped URLs
requires 'URI';

# Required for XML processing.
requires 'XML::LibXML';

Expand Down
3 changes: 3 additions & 0 deletions cpanfile.docker
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ requires 'Time::HiRes';

requires 'Try::Tiny';

# for generating properly escaped URLs
requires 'URI';

# Required for XML processing.
requires 'XML::LibXML';

Expand Down
58 changes: 33 additions & 25 deletions scripts/test/Package.t
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,20 @@

use strict;
use warnings;
use v5.24;
use utf8;

# Set up the test driver $Self when we are running as a standalone script.
use Kernel::System::UnitTest::RegisterDriver;
# core modules
use File::Copy qw(copy move);
use File::stat;

use vars (qw($Self));
# CPAN modules
use Test2::V0;

use File::Copy;
# OTOBO modules
use Kernel::System::UnitTest::RegisterDriver; # Set up $Self and $Kernel::System::OM

our $Self;

# get needed objects
my $ConfigObject = $Kernel::OM->Get('Kernel::Config');
Expand All @@ -31,14 +37,6 @@ my $CacheObject = $Kernel::OM->Get('Kernel::System::Cache');
my $DBObject = $Kernel::OM->Get('Kernel::System::DB');
my $MainObject = $Kernel::OM->Get('Kernel::System::Main');

# get helper object
$Kernel::OM->ObjectParamAdd(
'Kernel::System::UnitTest::Helper' => {
RestoreDatabase => 1,
},
);
my $Helper = $Kernel::OM->Get('Kernel::System::UnitTest::Helper');

my $Home = $ConfigObject->Get('Home');

my $CachePopulate = sub {
Expand Down Expand Up @@ -116,6 +114,7 @@ my $String = '<?xml version="1.0" encoding="utf-8" ?>
<Filelist>
<File Location="Test" Permission="644" Encode="Base64">aGVsbG8K</File>
<File Location="var/Test" Permission="644" Encode="Base64">aGVsbG8K</File>
<File Location="Custom/Kernel/Modules/Test.pm" Permission="644" Encode="Base64">aGVsbG8K</File>
</Filelist>
</otobo_package>
';
Expand Down Expand Up @@ -195,30 +194,44 @@ $Self->True(

$CachePopulate->();

my $PackageInstall = $PackageObject->PackageInstall( String => $String );
# The package declared in $String contains Custom/Kernel/Modules/Test.pm.
# For supporting the loading Custom/Kernel/Modules/Test.pm by the webserver,
# there is a touch on Kernel/Modules/Test.pm.
# Remember the current time for testing whether the touch worked.
my $CoreTestModule = 'Kernel/Modules/Test.pm';
my $TimeBeforeInstall = time;

# The core module Kernel/Modules/Test.pm should be old.
ok( stat($CoreTestModule)->mtime < $TimeBeforeInstall, 'core Test.pm is old' );

my $FirstPackageInstallOk = $PackageObject->PackageInstall( String => $String );

$Self->True(
$PackageInstall,
$FirstPackageInstallOk,
'#1 PackageInstall()',
);

$PackageInstall = $PackageObject->PackageInstall( String => $StringSecond );
# PackageInstall() should have touched the core module
ok( stat($CoreTestModule)->mtime >= $TimeBeforeInstall, 'core Test.pm has been touched' );

# overwriting the just install files
my $SecondPackageInstallOk = $PackageObject->PackageInstall( String => $StringSecond );

$Self->True(
$PackageInstall,
$SecondPackageInstallOk,
'#1 PackageInstall() 2',
);

$CacheClearedCheck->();

# check if the package is already installed - check by name
# check whether the package has been installed - check by name
$PackageIsInstalledByName = $PackageObject->PackageIsInstalled( Name => 'Test' );
$Self->True(
$PackageIsInstalledByName,
'#1 PackageIsInstalled() - check if the package is already installed - check by name',
);

# check if the package is already installed - check by XML string
# check whether the package has been installed - check by XML string
$PackageIsInstalledByString = $PackageObject->PackageIsInstalled( String => $String );
$Self->True(
$PackageIsInstalledByString,
Expand Down Expand Up @@ -399,7 +412,7 @@ $String = '<?xml version="1.0" encoding="utf-8" ?>
</Filelist>
</otobo_package>
';
$PackageInstall = $PackageObject->PackageInstall( String => $String );
my $PackageInstall = $PackageObject->PackageInstall( String => $String );

$Self->True(
!$PackageInstall || 0,
Expand Down Expand Up @@ -1140,9 +1153,4 @@ if ( !$DeveloperSystem ) {
chmod 0755, $Home . '/' . 'bin/otobo.CheckSum.pl';
}

# cleanup cache is done by RestoreDatabase


$Self->DoneTesting();


done_testing();

0 comments on commit 730dcdd

Please sign in to comment.