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
15#
16# Tests for setting/changing date and time values of existing DateTime object
17#
18my %OriginalDateTimeValues = (
19    Year     => 2016,
20    Month    => 2,
21    Day      => 29,
22    Hour     => 14,
23    Minute   => 46,
24    Second   => 34,
25    TimeZone => 'Europe/Berlin',
26);
27
28my @TestConfigs = (
29    {
30        Name   => '2017-02-28',
31        Params => {
32            Year      => 2017,
33            Month     => 2,
34            MonthAbbr => 'Feb',
35            Day       => 28,
36            DayOfWeek => 2,
37            DayAbbr   => 'Tue',
38        },
39        SuccessExpected => 1,
40    },
41    {
42        Name   => '2013-12-28 12:59:04',
43        Params => {
44            Year      => 2013,
45            Month     => 12,
46            MonthAbbr => 'Dec',
47            Day       => 28,
48            Hour      => 12,
49            Minute    => 59,
50            Second    => 4,
51            DayOfWeek => 6,
52            DayAbbr   => 'Sat',
53        },
54        SuccessExpected => 1,
55    },
56    {
57        Name   => 'Invalid date 2017-02-29 12:59:04',
58        Params => {
59            Year   => 2017,
60            Month  => 2,
61            Day    => 29,
62            Hour   => 12,
63            Minute => 59,
64            Second => 4,
65        },
66        SuccessExpected => 0,
67    },
68    {
69        Name   => 'No parameters',
70        Params => {
71        },
72        SuccessExpected => 0,
73    },
74);
75
76TESTCONFIG:
77for my $TestConfig (@TestConfigs) {
78
79    my $DateTimeObject = $Kernel::OM->Create(
80        'Kernel::System::DateTime',
81        ObjectParams => \%OriginalDateTimeValues,
82    );
83
84    # Update date and time values
85    my $DateTimeSet = $DateTimeObject->Set( %{ $TestConfig->{Params} } );
86
87    $Self->Is(
88        $DateTimeSet ? 1 : 0,
89        $TestConfig->{SuccessExpected},
90        $TestConfig->{Name} . ': Setting date and time of DateTime object must '
91            . ( $TestConfig->{SuccessExpected} ? '' : 'not ' )
92            . 'succeed.',
93    );
94
95    next TESTCONFIG if !$DateTimeSet;
96
97    # Compare date and time with expected ones
98    my $DateTimeValues        = $DateTimeObject->Get();
99    my $DateTimeValuesCorrect = 1;
100
101    KEY:
102    for my $Key ( sort keys %{$DateTimeValues} ) {
103        my $ExpectedValue;
104        if ( exists $TestConfig->{Params}->{$Key} ) {
105            $ExpectedValue = $TestConfig->{Params}->{$Key};
106        }
107        else {
108            $ExpectedValue = $OriginalDateTimeValues{$Key};
109        }
110
111        next KEY if $DateTimeValues->{$Key} eq $ExpectedValue;
112
113        $DateTimeValuesCorrect = 0;
114        last KEY;
115    }
116
117    $Self->Is(
118        $DateTimeValuesCorrect ? 1 : 0,
119        $TestConfig->{SuccessExpected},
120        $TestConfig->{Name}
121            . ': Set date and time must '
122            . ( $TestConfig->{SuccessExpected} ? '' : 'not ' )
123            . 'match expected ones.',
124    );
125}
126
1271;
128