Skip to content

Commit

Permalink
Merge pull request #21 from RotherOSS/issue-#7-unit_tests_take_2
Browse files Browse the repository at this point in the history
Issue #7 unit tests take 2
  • Loading branch information
bschmalhofer authored Mar 18, 2022
2 parents 354669f + 88b79a7 commit c2260dd
Showing 1 changed file with 63 additions and 111 deletions.
174 changes: 63 additions & 111 deletions scripts/test/FAQSearch.t
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ use warnings;
# core modules

# CPAN modules
use Test2::V0;

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

our $Self;
use Kernel::System::UnitTest::RegisterDriver; # Set up $Kernel::OM
use Kernel::System::UnitTest::MockTime qw(:all);

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

# set config options
Expand Down Expand Up @@ -65,35 +65,33 @@ my %FAQAddTemplate = (
StateID => 1,
LanguageID => 1,
Keywords => $RandomID,
Field1 => 'Problem...',
Field2 => 'Solution...',
UserID => 1,
ContentType => 'text/html',
);

# freeze time
$Helper->FixedTimeSet();
FixedTimeSet(); # t=0m

# get FAQ object
my $FAQObject = $Kernel::OM->Get('Kernel::System::FAQ');

# add two FAQs with creation time 60 s apart
my @Field1;
for my $Counter ( 1 .. 2 ) {
push @Field1, sprintf 'Field1 Counter: %d, time: %d', $Counter, time;
my $ItemID = $FAQObject->FAQAdd(
%FAQAddTemplate,
UserID => $AddedUsers[ $Counter - 1 ],
Field1 => $Field1[-1],
);

$Self->IsNot(
undef,
$ItemID,
"FAQAdd() ItemID:'$ItemID' for FAQSearch()",
);

ok( defined $ItemID, "FAQAdd() $Counter ItemID:'$ItemID' for FAQSearch()", );
push @AddedFAQs, $ItemID;

# add 1 minute to frozen time
$Helper->FixedTimeAddSeconds(60);
}
FixedTimeAddSeconds(60);
} # t=2m

# add some votes
my @VotesToAdd = (
Expand Down Expand Up @@ -142,23 +140,20 @@ my @VotesToAdd = (
for my $Vote (@VotesToAdd) {
my $Success = $FAQObject->VoteAdd( %{$Vote} );

$Self->True(
$Success,
"VoteAdd(): ItemID:'$Vote->{ItemID}' IP:'$Vote->{IP}' Rate:'$Vote->{Rate}' with true",
);
ok( $Success, "VoteAdd(): ItemID:'$Vote->{ItemID}' IP:'$Vote->{IP}' Rate:'$Vote->{Rate}' with true" );
}

# do vote search tests
my %SearchConfigTemplate = (
Keyword => "$RandomID",
Keyword => $RandomID,
States => [ 'public', 'internal' ],
OrderBy => ['FAQID'],
OrderByDirection => ['Up'],
Limit => 150,
UserID => 1,

);
my @Tests = (
my @VotesTests = (

# votes tests
{
Expand Down Expand Up @@ -490,18 +485,14 @@ my @Tests = (
);

# execute the tests
for my $Test (@Tests) {
for my $Test (@VotesTests) {
my @ItemIDs = $FAQObject->FAQSearch( %{ $Test->{Config} } );

$Self->IsDeeply(
\@ItemIDs,
$Test->{ExpectedResults},
"$Test->{Name} FAQSearch()",
);
is( \@ItemIDs, $Test->{ExpectedResults}, "$Test->{Name} FAQSearch()" );
}

# other tests
@Tests = (
my @OtherTests = (
{
Name => 'States Hash Correct IDs',
Config => {
Expand Down Expand Up @@ -542,14 +533,10 @@ for my $Test (@Tests) {
);

# execute the tests
for my $Test (@Tests) {
for my $Test (@OtherTests) {
my @ItemIDs = $FAQObject->FAQSearch( %{ $Test->{Config} } );

$Self->IsDeeply(
\@ItemIDs,
$Test->{ExpectedResults},
"$Test->{Name} FAQSearch()",
);
is( \@ItemIDs, $Test->{ExpectedResults}, "$Test->{Name} FAQSearch()" );
}

# time based tests
Expand All @@ -561,41 +548,36 @@ my %FAQUpdateTemplate = (
StateID => 1,
LanguageID => 1,
Keywords => $RandomID,
Field1 => 'Problem...',
Field2 => 'Solution...',
UserID => 1,
ContentType => 'text/html',
);

# add 1 minute to frozen time
$Helper->FixedTimeAddSeconds(60);
FixedTimeAddSeconds(60); # t=3m

my $Success = $FAQObject->FAQUpdate(
%FAQUpdateTemplate,
ItemID => $AddedFAQs[0],
Field1 => "Updated $Field1[0]",
UserID => $AddedUsers[2],
);

$Self->True(
$Success,
"FAQUpdate() ItemID:'$AddedFAQs[0]' for FAQSearch()",
);
ok( $Success, "FAQUpdate() ItemID:'$AddedFAQs[0]' for FAQSearch()" );

$Helper->FixedTimeAddSeconds(60);
FixedTimeAddSeconds(60); # t=4m

$Success = $FAQObject->FAQUpdate(
%FAQUpdateTemplate,
ItemID => $AddedFAQs[1],
Field1 => "Updated $Field1[1]",
UserID => $AddedUsers[3],
);

$Self->True(
$Success,
"FAQUpdate() ItemID:'$AddedFAQs[1]' for FAQSearch()",
);
ok( $Success, "FAQUpdate() ItemID:'$AddedFAQs[1]' for FAQSearch()" );

# add 2 minutes to frozen time
$Helper->FixedTimeAddSeconds(120);
FixedTimeAddSeconds(120); # t=6m

my $DateTime = $Kernel::OM->Create('Kernel::System::DateTime');

Expand All @@ -611,7 +593,8 @@ my $DateMinus5Mins = $DateTime->ToString();
$DateTime->Subtract( Seconds => 60 );
my $DateMinus6Mins = $DateTime->ToString();

@Tests = (
# Two FAQs were added. One 6 minutes ago, the other 5 minutes ago
my @TimeBasedTests = (
{
Name => 'CreateTimeOlderMinutes 3 min',
Config => {
Expand Down Expand Up @@ -728,19 +711,14 @@ my $DateMinus6Mins = $DateTime->ToString();
);

# execute the tests
for my $Test (@Tests) {

for my $Test (@TimeBasedTests) {
my @ItemIDs = $FAQObject->FAQSearch( %{ $Test->{Config} } );

$Self->IsDeeply(
\@ItemIDs,
$Test->{ExpectedResults},
"$Test->{Name} FAQSearch()",
);
is( \@ItemIDs, $Test->{ExpectedResults}, "$Test->{Name} FAQSearch()" );
}

# created user tests
@Tests = (
my @CreatedUserTests = (
{
Name => 'CreatedUserIDs 1',
Config => {
Expand Down Expand Up @@ -785,8 +763,15 @@ for my $Test (@Tests) {
},
);

# execute the tests
for my $Test (@CreatedUserTests) {
my @ItemIDs = $FAQObject->FAQSearch( %{ $Test->{Config} } );

is( \@ItemIDs, $Test->{ExpectedResults}, "$Test->{Name} FAQSearch()" );
}

# last changed user tests
@Tests = (
my @LastChangedUserTests = (
{
Name => 'LastChangedUserIDs 3',
Config => {
Expand Down Expand Up @@ -829,15 +814,10 @@ for my $Test (@Tests) {
);

# execute the tests
for my $Test (@Tests) {

for my $Test (@LastChangedUserTests) {
my @ItemIDs = $FAQObject->FAQSearch( %{ $Test->{Config} } );

$Self->IsDeeply(
\@ItemIDs,
$Test->{ExpectedResults},
"$Test->{Name} FAQSearch()",
);
is( \@ItemIDs, $Test->{ExpectedResults}, "$Test->{Name} FAQSearch()" );
}

# approval tests
Expand All @@ -853,7 +833,7 @@ return if !$Kernel::OM->Get('Kernel::System::DB')->Do(
],
);

@Tests = (
my @ApprovalTests = (
{
Name => 'Approved 1',
Config => {
Expand All @@ -877,22 +857,14 @@ return if !$Kernel::OM->Get('Kernel::System::DB')->Do(
);

# execute the tests
for my $Test (@Tests) {

for my $Test (@ApprovalTests) {
my @ItemIDs = $FAQObject->FAQSearch( %{ $Test->{Config} } );

$Self->IsDeeply(
\@ItemIDs,
$Test->{ExpectedResults},
"$Test->{Name} FAQSearch()",
);
is( \@ItemIDs, $Test->{ExpectedResults}, "$Test->{Name} FAQSearch()" );
}

# execute old tests
$Self->True(
1,
"--Execute Former Tests--",
);
diag("Execute Former Tests");
{
my $ItemID1 = $FAQObject->FAQAdd(
CategoryID => 1,
Expand All @@ -906,13 +878,10 @@ $Self->True(
UserID => 1,
ContentType => 'text/html',
);
$Self->True(
$ItemID1,
"FAQAdd() - 1",
);
ok( $ItemID1, "FAQAdd() - 1" );

# add 1 minute to frozen time
$Helper->FixedTimeAddSeconds(60);
FixedTimeAddSeconds(60); # t=7m

my $ItemID2 = $FAQObject->FAQAdd(
Title => 'Title' . $RandomID,
Expand All @@ -925,13 +894,10 @@ $Self->True(
UserID => 1,
ContentType => 'text/html',
);
$Self->True(
$ItemID2,
"FAQAdd() - 2",
);
ok( $ItemID2, "FAQAdd() - 2" );

# add 1 minute to frozen time
$Helper->FixedTimeAddSeconds(60);
FixedTimeAddSeconds(60); # t=8m

my %Keywords = (
Keyword1 => "some1$RandomID",
Expand All @@ -950,13 +916,10 @@ $Self->True(
UserID => 1,
ContentType => 'text/html',
);
$Self->True(
$ItemID3,
"FAQAdd() - 3",
);
ok( $ItemID3, "FAQAdd() - 3" );

# add 1 minute to frozen time
$Helper->FixedTimeAddSeconds(60);
FixedTimeAddSeconds(60); # t=9m

my $ItemID4 = $FAQObject->FAQAdd(
Title => 'Test FAQ-4',
Expand All @@ -969,13 +932,10 @@ $Self->True(
ContentType => 'text/html',
);

$Self->True(
$ItemID4,
"FAQAdd() - 4",
);
ok( $ItemID4, "FAQAdd() - 4" );

# add 1 minute to frozen time
$Helper->FixedTimeAddSeconds(60);
FixedTimeAddSeconds(60); # t=10m

my $ItemID5 = $FAQObject->FAQAdd(
Title => 'Test FAQ-5',
Expand All @@ -988,15 +948,12 @@ $Self->True(
ContentType => 'text/html',
);

$Self->True(
$ItemID5,
"FAQAdd() - 4",
);
ok( $ItemID5, "FAQAdd() - 4" );

# restore time
$Helper->FixedTimeUnset();
FixedTimeUnset();

@Tests = (
my @Tests = (
{
Name => 'Keywords',
Config => {
Expand Down Expand Up @@ -1172,7 +1129,6 @@ $Self->True(
);

for my $Test (@Tests) {

my @ItemIDs = $FAQObject->FAQSearch(
Number => '*',
States => [ 'public', 'internal' ],
Expand All @@ -1182,12 +1138,8 @@ $Self->True(
%{ $Test->{Config} },
);

$Self->IsDeeply(
\@ItemIDs,
$Test->{ExpectedResults},
"$Test->{Name}, FAQSearch()",
);
is( \@ItemIDs, $Test->{ExpectedResults}, "$Test->{Name}, FAQSearch()" );
}
}

$Self->DoneTesting();
done_testing();

0 comments on commit c2260dd

Please sign in to comment.