Skip to content

Commit a9e89e4

Browse files
committed
Issue #7: migrated to Test2::V0, use Kernel::System::UnitTest::MockTime
Do not reuse the variable @tests
1 parent 354669f commit a9e89e4

File tree

1 file changed

+46
-104
lines changed

1 file changed

+46
-104
lines changed

scripts/test/FAQSearch.t

+46-104
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ use warnings;
2121
# core modules
2222

2323
# CPAN modules
24+
use Test2::V0;
2425

2526
# OTOBO modules
26-
use Kernel::System::UnitTest::RegisterDriver; # Set up $Self and $Kernel::OM
27-
28-
our $Self;
27+
use Kernel::System::UnitTest::RegisterDriver; # Set up $Kernel::OM
28+
use Kernel::System::UnitTest::MockTime qw(:all);
2929

3030
# get helper object
3131
$Kernel::OM->ObjectParamAdd(
@@ -72,7 +72,8 @@ my %FAQAddTemplate = (
7272
);
7373

7474
# freeze time
75-
$Helper->FixedTimeSet();
75+
FixedTimeSet(); # t=0m
76+
FixedTimeAddSeconds(10*60);
7677

7778
# get FAQ object
7879
my $FAQObject = $Kernel::OM->Get('Kernel::System::FAQ');
@@ -83,17 +84,12 @@ for my $Counter ( 1 .. 2 ) {
8384
UserID => $AddedUsers[ $Counter - 1 ],
8485
);
8586

86-
$Self->IsNot(
87-
undef,
88-
$ItemID,
89-
"FAQAdd() ItemID:'$ItemID' for FAQSearch()",
90-
);
91-
87+
ok( defined $ItemID, "FAQAdd() $Counter ItemID:'$ItemID' for FAQSearch()",);
9288
push @AddedFAQs, $ItemID;
9389

9490
# add 1 minute to frozen time
95-
$Helper->FixedTimeAddSeconds(60);
96-
}
91+
FixedTimeAddSeconds(60);
92+
} # t=2m
9793

9894
# add some votes
9995
my @VotesToAdd = (
@@ -142,23 +138,20 @@ my @VotesToAdd = (
142138
for my $Vote (@VotesToAdd) {
143139
my $Success = $FAQObject->VoteAdd( %{$Vote} );
144140

145-
$Self->True(
146-
$Success,
147-
"VoteAdd(): ItemID:'$Vote->{ItemID}' IP:'$Vote->{IP}' Rate:'$Vote->{Rate}' with true",
148-
);
141+
ok( $Success, "VoteAdd(): ItemID:'$Vote->{ItemID}' IP:'$Vote->{IP}' Rate:'$Vote->{Rate}' with true" );
149142
}
150143

151144
# do vote search tests
152145
my %SearchConfigTemplate = (
153-
Keyword => "$RandomID",
146+
Keyword => $RandomID,
154147
States => [ 'public', 'internal' ],
155148
OrderBy => ['FAQID'],
156149
OrderByDirection => ['Up'],
157150
Limit => 150,
158151
UserID => 1,
159152

160153
);
161-
my @Tests = (
154+
my @VotesTests = (
162155

163156
# votes tests
164157
{
@@ -490,18 +483,14 @@ my @Tests = (
490483
);
491484

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

496-
$Self->IsDeeply(
497-
\@ItemIDs,
498-
$Test->{ExpectedResults},
499-
"$Test->{Name} FAQSearch()",
500-
);
489+
is( \@ItemIDs, $Test->{ExpectedResults}, "$Test->{Name} FAQSearch()" );
501490
}
502491

503492
# other tests
504-
@Tests = (
493+
my @OtherTests = (
505494
{
506495
Name => 'States Hash Correct IDs',
507496
Config => {
@@ -542,14 +531,10 @@ for my $Test (@Tests) {
542531
);
543532

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

548-
$Self->IsDeeply(
549-
\@ItemIDs,
550-
$Test->{ExpectedResults},
551-
"$Test->{Name} FAQSearch()",
552-
);
537+
is( \@ItemIDs, $Test->{ExpectedResults}, "$Test->{Name} FAQSearch()" );
553538
}
554539

555540
# time based tests
@@ -568,34 +553,28 @@ my %FAQUpdateTemplate = (
568553
);
569554

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

573558
my $Success = $FAQObject->FAQUpdate(
574559
%FAQUpdateTemplate,
575560
ItemID => $AddedFAQs[0],
576561
UserID => $AddedUsers[2],
577562
);
578563

579-
$Self->True(
580-
$Success,
581-
"FAQUpdate() ItemID:'$AddedFAQs[0]' for FAQSearch()",
582-
);
564+
ok( $Success, "FAQUpdate() ItemID:'$AddedFAQs[0]' for FAQSearch()" );
583565

584-
$Helper->FixedTimeAddSeconds(60);
566+
FixedTimeAddSeconds(60); # t=4m
585567

586568
$Success = $FAQObject->FAQUpdate(
587569
%FAQUpdateTemplate,
588570
ItemID => $AddedFAQs[1],
589571
UserID => $AddedUsers[3],
590572
);
591573

592-
$Self->True(
593-
$Success,
594-
"FAQUpdate() ItemID:'$AddedFAQs[1]' for FAQSearch()",
595-
);
574+
ok( $Success, "FAQUpdate() ItemID:'$AddedFAQs[1]' for FAQSearch()" );
596575

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

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

@@ -611,7 +590,8 @@ my $DateMinus5Mins = $DateTime->ToString();
611590
$DateTime->Subtract( Seconds => 60 );
612591
my $DateMinus6Mins = $DateTime->ToString();
613592

614-
@Tests = (
593+
# Two FAQs were added. One 6 minutes ago, the other 5 minutes ago
594+
my @TimeBasedTests = (
615595
{
616596
Name => 'CreateTimeOlderMinutes 3 min',
617597
Config => {
@@ -728,19 +708,14 @@ my $DateMinus6Mins = $DateTime->ToString();
728708
);
729709

730710
# execute the tests
731-
for my $Test (@Tests) {
732-
711+
for my $Test (@TimeBasedTests) {
733712
my @ItemIDs = $FAQObject->FAQSearch( %{ $Test->{Config} } );
734713

735-
$Self->IsDeeply(
736-
\@ItemIDs,
737-
$Test->{ExpectedResults},
738-
"$Test->{Name} FAQSearch()",
739-
);
714+
is( \@ItemIDs, $Test->{ExpectedResults}, "$Test->{Name} FAQSearch()" );
740715
}
741716

742717
# created user tests
743-
@Tests = (
718+
my @CreatedUserTests = (
744719
{
745720
Name => 'CreatedUserIDs 1',
746721
Config => {
@@ -786,7 +761,7 @@ for my $Test (@Tests) {
786761
);
787762

788763
# last changed user tests
789-
@Tests = (
764+
my @LastChangedUserTests = (
790765
{
791766
Name => 'LastChangedUserIDs 3',
792767
Config => {
@@ -829,15 +804,10 @@ for my $Test (@Tests) {
829804
);
830805

831806
# execute the tests
832-
for my $Test (@Tests) {
833-
807+
for my $Test (@LastChangedUserTests) {
834808
my @ItemIDs = $FAQObject->FAQSearch( %{ $Test->{Config} } );
835809

836-
$Self->IsDeeply(
837-
\@ItemIDs,
838-
$Test->{ExpectedResults},
839-
"$Test->{Name} FAQSearch()",
840-
);
810+
is( \@ItemIDs, $Test->{ExpectedResults}, "$Test->{Name} FAQSearch()" );
841811
}
842812

843813
# approval tests
@@ -853,7 +823,7 @@ return if !$Kernel::OM->Get('Kernel::System::DB')->Do(
853823
],
854824
);
855825

856-
@Tests = (
826+
my @ApprovalTests = (
857827
{
858828
Name => 'Approved 1',
859829
Config => {
@@ -877,22 +847,14 @@ return if !$Kernel::OM->Get('Kernel::System::DB')->Do(
877847
);
878848

879849
# execute the tests
880-
for my $Test (@Tests) {
881-
850+
for my $Test (@ApprovalTests) {
882851
my @ItemIDs = $FAQObject->FAQSearch( %{ $Test->{Config} } );
883852

884-
$Self->IsDeeply(
885-
\@ItemIDs,
886-
$Test->{ExpectedResults},
887-
"$Test->{Name} FAQSearch()",
888-
);
853+
is( \@ItemIDs, $Test->{ExpectedResults}, "$Test->{Name} FAQSearch()" );
889854
}
890855

891856
# execute old tests
892-
$Self->True(
893-
1,
894-
"--Execute Former Tests--",
895-
);
857+
diag( "Execute Former Tests" );
896858
{
897859
my $ItemID1 = $FAQObject->FAQAdd(
898860
CategoryID => 1,
@@ -906,13 +868,10 @@ $Self->True(
906868
UserID => 1,
907869
ContentType => 'text/html',
908870
);
909-
$Self->True(
910-
$ItemID1,
911-
"FAQAdd() - 1",
912-
);
871+
ok( $ItemID1, "FAQAdd() - 1" );
913872

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

917876
my $ItemID2 = $FAQObject->FAQAdd(
918877
Title => 'Title' . $RandomID,
@@ -925,13 +884,10 @@ $Self->True(
925884
UserID => 1,
926885
ContentType => 'text/html',
927886
);
928-
$Self->True(
929-
$ItemID2,
930-
"FAQAdd() - 2",
931-
);
887+
ok( $ItemID2, "FAQAdd() - 2");
932888

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

936892
my %Keywords = (
937893
Keyword1 => "some1$RandomID",
@@ -950,13 +906,10 @@ $Self->True(
950906
UserID => 1,
951907
ContentType => 'text/html',
952908
);
953-
$Self->True(
954-
$ItemID3,
955-
"FAQAdd() - 3",
956-
);
909+
ok( $ItemID3, "FAQAdd() - 3");
957910

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

961914
my $ItemID4 = $FAQObject->FAQAdd(
962915
Title => 'Test FAQ-4',
@@ -969,13 +922,10 @@ $Self->True(
969922
ContentType => 'text/html',
970923
);
971924

972-
$Self->True(
973-
$ItemID4,
974-
"FAQAdd() - 4",
975-
);
925+
ok( $ItemID4, "FAQAdd() - 4");
976926

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

980930
my $ItemID5 = $FAQObject->FAQAdd(
981931
Title => 'Test FAQ-5',
@@ -988,15 +938,12 @@ $Self->True(
988938
ContentType => 'text/html',
989939
);
990940

991-
$Self->True(
992-
$ItemID5,
993-
"FAQAdd() - 4",
994-
);
941+
ok( $ItemID5, "FAQAdd() - 4");
995942

996943
# restore time
997-
$Helper->FixedTimeUnset();
944+
FixedTimeUnset();
998945

999-
@Tests = (
946+
my @Tests = (
1000947
{
1001948
Name => 'Keywords',
1002949
Config => {
@@ -1172,7 +1119,6 @@ $Self->True(
11721119
);
11731120

11741121
for my $Test (@Tests) {
1175-
11761122
my @ItemIDs = $FAQObject->FAQSearch(
11771123
Number => '*',
11781124
States => [ 'public', 'internal' ],
@@ -1182,12 +1128,8 @@ $Self->True(
11821128
%{ $Test->{Config} },
11831129
);
11841130

1185-
$Self->IsDeeply(
1186-
\@ItemIDs,
1187-
$Test->{ExpectedResults},
1188-
"$Test->{Name}, FAQSearch()",
1189-
);
1131+
is( \@ItemIDs, $Test->{ExpectedResults}, "$Test->{Name}, FAQSearch()");
11901132
}
11911133
}
11921134

1193-
$Self->DoneTesting();
1135+
done_testing();

0 commit comments

Comments
 (0)