1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
|
# --
# Copyright (C) 2021 Znuny GmbH, https://znuny.org/
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (AGPL). If you
# did not receive this file, see http://www.gnu.org/licenses/agpl.txt.
# --
## nofilter(TidyAll::Plugin::Znuny::Perl::PerlCritic)
use strict;
use warnings;
use utf8;
use Data::Dumper;
use vars (qw($Self));
use List::Util qw{shuffle};
use Storable;
use Kernel::System::VariableCheck qw(:all);
$Kernel::OM->ObjectParamAdd(
'Kernel::System::UnitTest::Helper' => {
RestoreDatabase => 1,
UseTmpArticleDir => 1,
},
);
my $HelperObject = $Kernel::OM->Get('Kernel::System::UnitTest::Helper');
my $ZnunyHelperObject = $Kernel::OM->Get('Kernel::System::ZnunyHelper');
my $TicketObject = $Kernel::OM->Get('Kernel::System::Ticket');
my $MainObject = $Kernel::OM->Get('Kernel::System::Main');
my $ConfigObject = $Kernel::OM->Get('Kernel::Config');
my $ArticleObject = $Kernel::OM->Get('Kernel::System::Ticket::Article');
my $DynamicFieldObject = $Kernel::OM->Get('Kernel::System::DynamicField');
my $DynamicFieldBackendObject = $Kernel::OM->Get('Kernel::System::DynamicField::Backend');
my $ModuleName = 'TicketCreate';
my $RandomID = $HelperObject->GetRandomID();
my $MinimumSamples = 2;
my $UserID = 1;
my %FileTypeMap = (
bin => 'application/octet-stream',
txt => 'text/plain',
pdf => 'application/pdf',
);
$ConfigObject->Set(
Key => 'CheckEmailAddresses',
Value => '0',
);
my $TicketID = $HelperObject->TicketCreate(
UserID => $UserID,
);
my $AttachmentsDynamicFieldName = $ConfigObject->Get('Process::DynamicFieldProcessManagementAttachment')
// 'ProcessManagementAttachment';
my @DynamicFields = (
{
Name => $AttachmentsDynamicFieldName,
Label => $AttachmentsDynamicFieldName,
ObjectType => 'Ticket',
FieldType => 'TextArea',
Config => {
DefaultValue => '',
},
},
);
$ZnunyHelperObject->_DynamicFieldsCreateIfNotExists(@DynamicFields);
# Create a bunch of articles and attachments
for my $ChannelName (qw(Email Phone Internal)) {
my $ArticleID = $ArticleObject->ArticleCreate(
ChannelName => $ChannelName,
TicketID => $TicketID,
SenderType => 'agent',
IsVisibleForCustomer => 1,
From => 'Some Agent <email@example.com>',
To => 'Some Customer A <customer-a@example.com>',
Cc => 'Some Customer B <customer-b@example.com>',
Bcc => 'Some Customer C <customer-c@example.com>',
Subject => 'Origin article Subject',
Body => 'Lorem ipsum',
MessageID => '<' . $RandomID . '@example.com>',
Charset => 'ISO-8859-15',
MimeType => 'text/plain',
HistoryType => 'AddNote',
HistoryComment => 'Some free text!',
UserID => 1,
UnlockOnAway => 1,
FromRealname => 'Some Agent',
ToRealname => 'Some Customer A',
CcRealname => 'Some Customer B',
);
$Self->True(
$ArticleID,
"ArticleCreate - Added article $ArticleID",
);
my @File = (
qw(
Ticket-Article-Test1.txt
Ticket-Article-Test1.pdf
Ticket-Article-Test-utf8-1.txt
Ticket-Article-Test-utf8-1.bin
Ticket-Article-Test-empty.txt
)
);
for my $File (@File) {
my $Location = $ConfigObject->Get('Home') . "/scripts/test/sample/Ticket/$File";
my $ContentRef = $MainObject->FileRead(
Location => $Location,
Mode => 'binmode',
);
my @FileNames = ( '_Attachment1_', '_Attachment2_', '_Attachment3_' );
for my $FileName (@FileNames) {
my $Content = ${$ContentRef};
my $FileDisposition = $ChannelName . $FileName . $File;
my $ContentType = $File;
$ContentType =~ m{.*\.(.*)\z};
$ContentType = $FileTypeMap{$1};
my $ArticleWroteAttachment = $ArticleObject->ArticleWriteAttachment(
TicketID => $TicketID,
Content => $Content,
Filename => $FileDisposition,
ContentType => $ContentType,
ArticleID => $ArticleID,
UserID => 1,
);
$Self->True(
scalar $ArticleWroteAttachment,
"ArticleWriteAttachment() - $FileDisposition",
);
}
}
}
my @OriginalArticles = $ArticleObject->ArticleList(
TicketID => $TicketID,
);
# Enriching each article in the OriginalArticle array with the attachment (meta information)
# and storing some selected attachment IDs in the TestDataArrayMatrix,
# which will also be included in each article, to be used in the later verification process.
for my $Article (@OriginalArticles) {
my %FullArticle = $ArticleObject->ArticleGet(
TicketID => $TicketID,
ArticleID => $Article->{ArticleID},
DynamicFields => 1,
);
my %Article = ( %{$Article}, %FullArticle );
my %AttachmentIndex = $ArticleObject->ArticleAttachmentIndex(
TicketID => $TicketID,
ArticleID => $Article->{ArticleID},
ExcludePlainText => 1,
ExcludeHTMLBody => 1,
ExcludeInline => 0,
);
for my $AttachmentID ( sort keys %AttachmentIndex ) {
my %Attachment = $ArticleObject->ArticleAttachment(
TicketID => $TicketID,
ArticleID => $Article->{ArticleID},
FileID => $AttachmentID,
);
# Don't store/keep the raw content, instead this test stores just the MD5 sum in the
# TestDataRecord - for later comparison, as described.
$Attachment{Content} = $MainObject->MD5sum(
String => $Attachment{Content},
);
$AttachmentIndex{$AttachmentID} = \%Attachment;
}
my $AttachmentCount = keys %AttachmentIndex;
my $SampleCount = int( rand($AttachmentCount) ); ## no-critic
$SampleCount = $MinimumSamples if $SampleCount < $MinimumSamples;
my @RandomAttachmentIndex = shuffle keys %AttachmentIndex;
@RandomAttachmentIndex = @RandomAttachmentIndex[ 0 .. $SampleCount - 1 ];
# store/assign it to the Article Test Data
$Article{RandomAttachmentIndex} = \@RandomAttachmentIndex;
# store/assign it to the Article Test Data for later use
$Article{AttachmentIndex} = \%AttachmentIndex;
$Article = \%Article;
}
# create a stringified structure to use data as key in html selection
# we need this information to get the correct attachment from backend
#
# stringified structure
# $ValueString = 'ObjectType=Article;ObjectID=1;ID=5'
# $ValueString = 'ObjectType=Article;ObjectID=1;ID=5,ObjectType=Article;ObjectID=3;ID=2'
#
# parsed stringified structure
# Data = {
# ObjectType => 'Article',
# ObjectID => '1',
# ID => '4',
# }
my $ValueString;
for my $Article (@OriginalArticles) {
my @ArticleAttachmentArray
= map {"ObjectType=Article;ObjectID=$Article->{ArticleID};ID=$_"} @{ $Article->{RandomAttachmentIndex} };
$ValueString = join( ',', @ArticleAttachmentArray );
}
# prepare the attachments{Filename} hash for easy verification at a later stage, see below
# Example:
# 'Internal_Attachment3_Ticket-Article-Test-utf8-1.txt' => {
# ContentType => 'text/plain',
# Content => 'md5sum....',
# },
my %ArticleAttachmentsToTest;
for my $Article (@OriginalArticles) {
for my $AttachmentID ( @{ $Article->{RandomAttachmentIndex} } ) {
my $Attachment = $Article->{AttachmentIndex}{$AttachmentID};
$ArticleAttachmentsToTest{ $Attachment->{Filename} }->{ContentType} = $Attachment->{ContentType};
$ArticleAttachmentsToTest{ $Attachment->{Filename} }->{Content} = $Attachment->{Content};
$ArticleAttachmentsToTest{ $Attachment->{Filename} }->{FilesizeRaw} = $Attachment->{FilesizeRaw};
$ArticleAttachmentsToTest{ $Attachment->{Filename} }->{ArticleID} = $Article->{ArticleID};
}
}
my $NewCreatedSubTicketConfigData = {
Title => 'New Subticket',
Subject => 'New Subticket',
StateID => 1,
Priority => '3 normal',
Lock => 'unlock',
QueueID => 1,
IsVisibleForCustomer => 0,
HistoryType => 'AddNote',
HistoryComment => 'Subticket created',
From => 'znuny',
LinkAs => 'Child',
SenderType => 'agent',
CommunicationChannel => 'Internal',
ContentType => 'text/html; charset=UTF-8',
OwnerID => $UserID,
};
my $AttachmentsDynamicFieldConfig = $DynamicFieldObject->DynamicFieldGet(
Name => $AttachmentsDynamicFieldName,
);
my $ValidConfig = IsHashRefWithData($AttachmentsDynamicFieldConfig);
$Self->True(
IsHashRefWithData($AttachmentsDynamicFieldConfig),
"Got dynamic field config for $AttachmentsDynamicFieldName.",
);
# Test config collection
my @Tests = (
{
Name => 'Empty AttachmentsReuse',
Config => {
UserID => $UserID,
Config => {
%{$NewCreatedSubTicketConfigData},
AttachmentsReuse => '',
},
},
ExpectedSuccess => 1,
ExpectedSuccessAttachment => 0,
},
{
Name => 'Empty AttachmentsReuse with 0',
Config => {
UserID => $UserID,
Config => {
%{$NewCreatedSubTicketConfigData},
AttachmentsReuse => '0',
},
},
ExpectedSuccess => 1,
ExpectedSuccessAttachment => 0,
},
{
Name => 'AttachmentsReuse with 1',
Config => {
UserID => $UserID,
Config => {
%{$NewCreatedSubTicketConfigData},
AttachmentsReuse => '1',
},
},
ExpectedSuccess => 1,
ExpectedSuccessAttachment => 1,
},
{
Name => 'AttachmentsReuse with y',
Config => {
UserID => $UserID,
Config => {
%{$NewCreatedSubTicketConfigData},
AttachmentsReuse => 'y',
},
},
ExpectedSuccess => 1,
ExpectedSuccessAttachment => 1,
},
{
Name => 'AttachmentsReuse with yes',
Config => {
UserID => $UserID,
Config => {
%{$NewCreatedSubTicketConfigData},
AttachmentsReuse => 'yes',
},
},
ExpectedSuccess => 1,
ExpectedSuccessAttachment => 1,
},
);
for my $Test (@Tests) {
# make a deep copy to avoid changing the definition
my $OrigTest = Storable::dclone($Test);
my $ValueWasSet = $DynamicFieldBackendObject->ValueSet(
DynamicFieldConfig => $AttachmentsDynamicFieldConfig,
ObjectID => $TicketID,
Value => $ValueString,
UserID => 1,
);
# Check if value was set
$Self->True(
scalar $ValueWasSet,
"Dynamic field ValueSet() - $TicketID $AttachmentsDynamicFieldConfig->{Name}"
);
my %Ticket = $TicketObject->TicketGet(
TicketID => $TicketID,
UserID => $UserID,
DynamicFields => 1,
);
# Pre-set a ticket number to work with
my $TicketNumber = $TicketObject->TicketCreateNumber();
$Test->{Config}->{Ticket} = \%Ticket;
$Test->{Config}->{Config}->{TN} = $TicketNumber;
my $TransitionActionObject = $Kernel::OM->Get(
'Kernel::System::ProcessManagement::TransitionAction::TicketCreate'
);
my $Success = $TransitionActionObject->Run(
%{ $Test->{Config} },
ProcessEntityID => 'PEID1',
ActivityEntityID => 'AEID1',
TransitionEntityID => 'TEID1',
TransitionActionEntityID => 'TAEID1',
);
if ( !$Test->{ExpectedSuccess} ) {
$Self->False(
scalar $Success,
"$ModuleName Run(): '$Test->{Name}' must fail.",
);
}
else {
$Self->True(
scalar $Success,
"$ModuleName Run(): '$Test->{Name}' must succeed.",
);
}
my $ChildTicketID = $TicketObject->TicketIDLookup(
TicketNumber => $TicketNumber,
);
my %ChildTicket = $TicketObject->TicketGet(
TicketID => $ChildTicketID,
);
$Self->True(
IsHashRefWithData( \%ChildTicket ),
"$ModuleName Run(): '$Test->{Name}' Child ticket must exist."
);
my @ChildArticles = $ArticleObject->ArticleList(
TicketID => $ChildTicketID,
OnlyFirst => 1,
);
my $FirstChildArticleID = $ChildArticles[0]->{ArticleID};
my %Article = $ArticleObject->ArticleGet(
TicketID => $ChildTicketID,
ArticleID => $FirstChildArticleID,
);
my %AttachmentIndex = $ArticleObject->ArticleAttachmentIndex(
TicketID => $ChildTicketID,
ArticleID => $FirstChildArticleID,
ExcludePlainText => 1,
ExcludeHTMLBody => 1,
ExcludeInline => 0,
);
if ( $Test->{ExpectedSuccessAttachment} ) {
$Self->True(
scalar %AttachmentIndex,
"$ModuleName Run(): '$Test->{Name}' Child ticket article must have attachments.",
);
}
else {
$Self->False(
scalar %AttachmentIndex,
"$ModuleName Run(): '$Test->{Name}' Child ticket article must not have attachments.",
);
}
for my $AttachmentID ( sort keys %AttachmentIndex ) {
my %Attachment = $ArticleObject->ArticleAttachment(
TicketID => $ChildTicketID,
ArticleID => $FirstChildArticleID,
FileID => $AttachmentID,
);
# raw content should NOT match the MD5 sum from the origin Attachment,
# which is stored in the AttachmentsToTest hash
$Self->IsNot(
$Attachment{Content},
$ArticleAttachmentsToTest{ $Attachment{Filename} }->{Content},
"$ModuleName Run(): '$Test->{Name} $Attachment{Filename} content MD5 sum must not match.",
);
$Self->Is(
$MainObject->MD5sum(
String => $Attachment{Content},
),
$ArticleAttachmentsToTest{ $Attachment{Filename} }->{Content},
"$ModuleName Run(): '$Test->{Name} $Attachment{Filename} content MD5 sum must match.",
);
$Self->Is(
$Attachment{FilesizeRaw},
$ArticleAttachmentsToTest{ $Attachment{Filename} }->{FilesizeRaw},
"$ModuleName Run(): '$Test->{Name} $Attachment{Filename} content size must match.",
);
$Self->Is(
$Attachment{ContentType},
$ArticleAttachmentsToTest{ $Attachment{Filename} }->{ContentType},
"$ModuleName Run(): '$Test->{Name} $Attachment{Filename} content type must match.",
);
}
}
1;
|