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
15my $Selenium = $Kernel::OM->Get('Kernel::System::UnitTest::Selenium');
16
17$Selenium->RunTest(
18    sub {
19        my $Helper            = $Kernel::OM->Get('Kernel::System::UnitTest::Helper');
20        my $GroupObject       = $Kernel::OM->Get('Kernel::System::Group');
21        my $CalendarObject    = $Kernel::OM->Get('Kernel::System::Calendar');
22        my $AppointmentObject = $Kernel::OM->Get('Kernel::System::Calendar::Appointment');
23
24        # Dashboard widget config key.
25        my $DashboardConfigKey = '0500-AppointmentCalendar';
26
27        # Turn on dashboard widget by default.
28        my $DashboardConfig = $Kernel::OM->Get('Kernel::Config')->Get('DashboardBackend')->{$DashboardConfigKey};
29        $DashboardConfig->{Default} = 1;
30        $Helper->ConfigSettingChange(
31            Valid => 1,
32            Key   => "DashboardBackend###$DashboardConfigKey",
33            Value => $DashboardConfig,
34        );
35
36        my $RandomID = $Helper->GetRandomID();
37
38        # Create test group.
39        my $GroupName = "test-calendar-group-$RandomID";
40        my $GroupID   = $GroupObject->GroupAdd(
41            Name    => $GroupName,
42            ValidID => 1,
43            UserID  => 1,
44        );
45        $Self->True(
46            $GroupID,
47            "Created test group - $GroupID",
48        );
49
50        # Create test user.
51        my ( $TestUserLogin, $UserID ) = $Helper->TestUserCreate(
52            Groups => [$GroupName],
53        );
54        $Self->True(
55            $UserID,
56            "Created test user - $UserID",
57        );
58
59        # Create a test calendar.
60        my %Calendar = $CalendarObject->CalendarCreate(
61            CalendarName => "Calendar $RandomID",
62            Color        => '#3A87AD',
63            GroupID      => $GroupID,
64            UserID       => $UserID,
65            ValidID      => 1,
66        );
67        $Self->True(
68            $Calendar{CalendarID},
69            "Created test calendar - $Calendar{CalendarID}",
70        );
71
72        # Get current time.
73        my $StartTimeObject = $Kernel::OM->Create('Kernel::System::DateTime');
74
75        # Just before midnight today.
76        my $TodayTimeObject = $StartTimeObject->Clone();
77        $TodayTimeObject->Set(
78            Hour   => 23,
79            Minute => 59,
80            Second => 59,
81        );
82
83        my $TomorrowTimeObject = $StartTimeObject->Clone();
84        $TomorrowTimeObject->Add(
85            Days => 1,
86        );
87
88        my $DayAfterTomorrowTimeObject = $StartTimeObject->Clone();
89        $DayAfterTomorrowTimeObject->Add(
90            Days => 2,
91        );
92
93        my $TwoDaysAfterTomorrowTimeObject = $StartTimeObject->Clone();
94        $TwoDaysAfterTomorrowTimeObject->Add(
95            Days => 3,
96        );
97
98        # Sample appointments.
99        my @Appointments = (
100
101            # Today.
102            {
103                CalendarID => $Calendar{CalendarID},
104                StartTime  => $TodayTimeObject->ToString(),
105                EndTime    => $TodayTimeObject->ToString(),
106                Title      => "Today $RandomID",
107                UserID     => $UserID,
108                Filter     => 'Today',
109            },
110
111            # Tomorrow.
112            {
113                CalendarID => $Calendar{CalendarID},
114                StartTime  => $TomorrowTimeObject->ToString(),
115                EndTime    => $TomorrowTimeObject->ToString(),
116                Title      => "Tomorrow $RandomID",
117                UserID     => $UserID,
118                Filter     => 'Tomorrow',
119            },
120
121            # Day after tomorrow.
122            {
123                CalendarID => $Calendar{CalendarID},
124                StartTime  => $DayAfterTomorrowTimeObject->ToString(),
125                EndTime    => $DayAfterTomorrowTimeObject->ToString(),
126                Title      => "Day after tomorrow $RandomID",
127                UserID     => $UserID,
128                Filter     => 'Soon',
129            },
130
131            # Two days after tomorrow.
132            {
133                CalendarID => $Calendar{CalendarID},
134                StartTime  => $TwoDaysAfterTomorrowTimeObject->ToString(),
135                EndTime    => $TwoDaysAfterTomorrowTimeObject->ToString(),
136                Title      => "Two days after tomorrow $RandomID",
137                UserID     => $UserID,
138                Filter     => 'Soon',
139            },
140        );
141
142        # Create appointments.
143        for my $Appointment (@Appointments) {
144            my $AppointmentID = $AppointmentObject->AppointmentCreate(
145                %{$Appointment},
146            );
147            $Self->True(
148                $AppointmentID,
149                "Created test appointment - $AppointmentID",
150            );
151            $Appointment->{AppointmentID} = $AppointmentID;
152        }
153
154        # Login as test user.
155        $Selenium->Login(
156            Type     => 'Agent',
157            User     => $TestUserLogin,
158            Password => $TestUserLogin,
159        );
160
161        # Verify widget is present.
162        $Selenium->find_element( "#Dashboard$DashboardConfigKey", 'css' );
163
164        # Check appointments.
165        my %FilterCount;
166        for my $Appointment (@Appointments) {
167
168            my $AppointmentID = $Appointment->{AppointmentID};
169
170            # Remember filter.
171            $FilterCount{ $Appointment->{Filter} } += 1;
172
173            # Switch filter.
174            $Selenium->find_element("//a[\@id='Dashboard${DashboardConfigKey}$Appointment->{Filter}']")->click();
175
176            # Wait until all AJAX calls finished.
177            $Selenium->WaitFor( JavaScript => "return \$.active == 0" );
178
179            # Verify appointment is visible.
180            $Selenium->find_element("//a[contains(\@href, \'AppointmentID=$Appointment->{AppointmentID}\')]");
181        }
182
183        # Refresh the AgentDashboard screen.
184        $Selenium->VerifiedRefresh();
185
186        # Check filter count.
187        for my $Filter ( sort keys %FilterCount ) {
188
189            # Get filter link.
190            my $FilterLink = $Selenium->find_element( "#Dashboard${DashboardConfigKey}${Filter}", 'css' );
191
192            $Self->Is(
193                $FilterLink->get_text(),
194                "$Filter ($FilterCount{$Filter})",
195                "Filter count - $Filter",
196            );
197        }
198
199        # Delete test appointments.
200        for my $Appointment (@Appointments) {
201            my $Success = $AppointmentObject->AppointmentDelete(
202                AppointmentID => $Appointment->{AppointmentID},
203                UserID        => $UserID,
204            );
205            $Self->True(
206                $Success,
207                "Deleted test appointment - $Appointment->{AppointmentID}",
208            );
209        }
210
211        # Delete test calendar.
212        if ( $Calendar{CalendarID} ) {
213            my $Success = $Kernel::OM->Get('Kernel::System::DB')->Do(
214                SQL  => 'DELETE FROM calendar WHERE id = ?',
215                Bind => [ \$Calendar{CalendarID} ],
216            );
217            $Self->True(
218                $Success,
219                "Deleted test calendar - $Calendar{CalendarID}",
220            );
221        }
222
223        my $CacheObject = $Kernel::OM->Get('Kernel::System::Cache');
224
225        # Make sure cache is correct.
226        for my $Cache (qw(Calendar Appointment)) {
227            $CacheObject->CleanUp( Type => $Cache );
228        }
229    },
230);
231
2321;
233