1# --
2# Copyright (C) 2001-2020 OTRS AG, https://otrs.com/
3# --
4# This software comes with ABSOLUTELY NO WARRANTY. For details, see
5# the enclosed file COPYING for license information (GPL). If you
6# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
7# --
8
9use strict;
10use warnings;
11use utf8;
12
13use vars (qw($Self));
14
15use Digest::MD5 qw(md5_hex);
16
17# get needed objects
18my $ConfigObject = $Kernel::OM->Get('Kernel::Config');
19my $EncodeObject = $Kernel::OM->Get('Kernel::System::Encode');
20my $MainObject   = $Kernel::OM->Get('Kernel::System::Main');
21
22# get command object
23my $CommandObject = $Kernel::OM->Get('Kernel::System::Console::Command::Maint::WebUploadCache::Cleanup');
24
25my ( $Result, $ExitCode );
26
27my $Helper = $Kernel::OM->Get('Kernel::System::UnitTest::Helper');
28
29for my $Module (qw(DB FS)) {
30
31    # make sure that the $UploadCacheObject gets recreated for each loop.
32    $Kernel::OM->ObjectsDiscard( Objects => ['Kernel::System::Web::UploadCache'] );
33
34    $ConfigObject->Set(
35        Key   => 'WebUploadCacheModule',
36        Value => "Kernel::System::Web::UploadCache::$Module",
37    );
38
39    # get a new upload cache object
40    my $UploadCacheObject = $Kernel::OM->Get('Kernel::System::Web::UploadCache');
41
42    $Self->True(
43        $UploadCacheObject->{Backend}->isa("Kernel::System::Web::UploadCache::$Module"),
44        "Upload cache created with correct object",
45    );
46
47    my $FormID = $UploadCacheObject->FormIDCreate();
48
49    $Self->True(
50        $FormID,
51        "#$Module - FormIDCreate()",
52    );
53
54    my $Location = $ConfigObject->Get('Home')
55        . "/scripts/test/sample/WebUploadCache/WebUploadCache-Test1.txt";
56
57    my $ContentRef = $MainObject->FileRead(
58        Location => $Location,
59        Mode     => 'binmode',
60    );
61    my $Content = ${$ContentRef};
62    $EncodeObject->EncodeOutput( \$Content );
63
64    my $MD5         = md5_hex($Content);
65    my $ContentID   = undef;
66    my $Disposition = 'attachment';
67
68    my $Add = $UploadCacheObject->FormIDAddFile(
69        FormID      => $FormID,
70        Filename    => 'UploadCache Test1äöüß.txt',
71        Content     => $Content,
72        ContentType => 'text/html',
73        ContentID   => $ContentID,
74        Disposition => $Disposition,
75    );
76
77    $Self->True(
78        $Add || '',
79        "#$Module - FormIDAddFile()",
80    );
81
82    # delete upload cache - should not remove cached form
83    $ExitCode = $CommandObject->Execute();
84    $Self->Is(
85        $ExitCode,
86        0,
87        "#$Module - delete upload cache",
88    );
89
90    my @Data = $UploadCacheObject->FormIDGetAllFilesData(
91        FormID => $FormID,
92    );
93
94    $Self->True(
95        scalar @Data,
96        "#$Module - FormIDGetAllFilesData() check if formid is present",
97    );
98
99    @Data = $UploadCacheObject->FormIDGetAllFilesMeta( FormID => $FormID );
100
101    $Self->True(
102        scalar @Data,
103        "#$Module - FormIDGetAllFilesMeta() check if formid is present",
104    );
105
106    # set fixed time
107    $Helper->FixedTimeSet();
108
109    # wait 24h+1s to expire upload cache
110    $Helper->FixedTimeAddSeconds(86401);
111
112    # delete upload cache - should remove cached form
113    $ExitCode = $CommandObject->Execute();
114    $Self->Is(
115        $ExitCode,
116        0,
117        "#$Module - delete upload cache",
118    );
119
120    @Data = $UploadCacheObject->FormIDGetAllFilesData(
121        FormID => $FormID,
122    );
123
124    $Self->False(
125        scalar @Data,
126        "#$Module - FormIDGetAllFilesData() check if formid is absent",
127    );
128
129    @Data = $UploadCacheObject->FormIDGetAllFilesMeta(
130        FormID => $FormID,
131    );
132
133    $Self->False(
134        scalar @Data,
135        "#$Module - FormIDGetAllFilesMeta() check if formid is absent",
136    );
137
138    # unset fixed time
139    $Helper->FixedTimeUnset();
140
141}
142
1431;
144