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# get helper object
16$Kernel::OM->ObjectParamAdd(
17    'Kernel::System::UnitTest::Helper' => {
18        RestoreDatabase => 1,
19    },
20);
21my $Helper = $Kernel::OM->Get('Kernel::System::UnitTest::Helper');
22
23# get needed objects
24my $ConfigObject      = $Kernel::OM->Get('Kernel::Config');
25my $CalendarObject    = $Kernel::OM->Get('Kernel::System::Calendar');
26my $AppointmentObject = $Kernel::OM->Get('Kernel::System::Calendar::Appointment');
27my $PluginObject      = $Kernel::OM->Get('Kernel::System::Calendar::Plugin');
28my $MainObject        = $Kernel::OM->Get('Kernel::System::Main');
29my $GroupObject       = $Kernel::OM->Get('Kernel::System::Group');
30my $UserObject        = $Kernel::OM->Get('Kernel::System::User');
31my $TicketObject      = $Kernel::OM->Get('Kernel::System::Ticket');
32
33# get registered plugin modules
34my $PluginConfig = $ConfigObject->Get("AppointmentCalendar::Plugin");
35my $PluginCount  = scalar keys %{$PluginConfig};
36
37my $PluginList = $PluginObject->PluginList();
38
39$Self->True(
40    $PluginList,
41    'Plugin list loaded',
42);
43
44$Self->Is(
45    scalar keys %{$PluginList},
46    $PluginCount,
47    'Registered plugin count',
48);
49
50my $PluginKeyTicket;
51
52# check all registered plugin modules
53PLUGIN:
54for my $PluginKey ( sort keys %{$PluginConfig} ) {
55
56    my $GenericModule = $PluginConfig->{$PluginKey}->{Module};
57    next PLUGIN if !$GenericModule;
58
59    if ( $GenericModule eq 'Kernel::System::Calendar::Plugin::Ticket' ) {
60        $PluginKeyTicket = $PluginKey;
61    }
62
63    # check if module can be required
64    $Self->True(
65        $MainObject->Require($GenericModule),
66        "Required '$GenericModule' plugin module",
67    );
68
69    my $PluginModule = $GenericModule->new( %{$Self} );
70
71    # check if module has been loaded
72    $Self->True(
73        $PluginModule,
74        "Plugin module loaded successfully",
75    );
76
77    # check class name
78    $Self->True(
79        $PluginModule->isa($GenericModule),
80        'Plugin module has correct package name',
81    );
82
83    # check required methods
84    for my $MethodName (qw(LinkAdd LinkList Search)) {
85        $Self->True(
86            $PluginModule->can($MethodName),
87            "Plugin module implements $MethodName()",
88        );
89    }
90
91    my $PluginURL = $PluginConfig->{$PluginKey}->{URL};
92
93    # check if URL contains ID placeholder
94    $Self->True(
95        scalar $PluginURL =~ /%s/,
96        'Plugin module URL contains ID placeholder',
97    );
98}
99
100# check ticket plugin if registered
101if ($PluginKeyTicket) {
102
103    # create test group
104    my $GroupName = 'test-calendar-group-' . $Helper->GetRandomID();
105    my $GroupID   = $GroupObject->GroupAdd(
106        Name    => $GroupName,
107        ValidID => 1,
108        UserID  => 1,
109    );
110
111    $Self->True(
112        $GroupID,
113        "Test group $GroupID created",
114    );
115
116    # create test user
117    my ( $UserLogin, $UserID ) = $Helper->TestUserCreate(
118        Groups => [ 'users', $GroupName ],
119    );
120
121    $Self->True(
122        $UserID,
123        "Test user $UserID created",
124    );
125
126    my $RandomID = $Helper->GetRandomID();
127
128    # create a test ticket
129    my $TicketID = $TicketObject->TicketCreate(
130        Title        => 'Test Ticket ' . $RandomID,
131        Queue        => 'Raw',
132        Lock         => 'unlock',
133        Priority     => '3 normal',
134        State        => 'open',
135        CustomerNo   => '123465',
136        CustomerUser => 'customer@example.com',
137        OwnerID      => $UserID,
138        UserID       => $UserID,
139    );
140    $Self->True(
141        $TicketID,
142        "TicketCreate() - $TicketID",
143    );
144    my $TicketNumber = $TicketObject->TicketNumberLookup(
145        TicketID => $TicketID,
146        UserID   => $UserID,
147    );
148    $Self->True(
149        $TicketNumber,
150        "TicketNumberLookup() - $TicketNumber",
151    );
152
153    # crete test calendar
154    my %Calendar = $CalendarObject->CalendarCreate(
155        CalendarName => 'Test Calendar ' . $RandomID,
156        Color        => '#3A87AD',
157        GroupID      => $GroupID,
158        UserID       => $UserID,
159    );
160
161    $Self->True(
162        $Calendar{CalendarID},
163        "CalendarCreate() - $Calendar{CalendarID}",
164    );
165
166    # create test appointment
167    my $AppointmentID = $AppointmentObject->AppointmentCreate(
168        CalendarID => $Calendar{CalendarID},
169        Title      => 'Test appointment ' . $RandomID,
170        StartTime  => '2016-01-01 12:00:00',
171        EndTime    => '2016-01-01 13:00:00',
172        TimezoneID => 0,
173        UserID     => $UserID,
174    );
175
176    $Self->True(
177        $AppointmentID,
178        "AppointmentCreate() - $AppointmentID",
179    );
180
181    # search the ticket via ticket number
182    my $ResultList = $PluginObject->PluginSearch(
183        Search    => $TicketNumber,
184        PluginKey => $PluginKeyTicket,
185        UserID    => $UserID,
186    );
187
188    $Self->IsDeeply(
189        $ResultList,
190        {
191            $TicketID => "$TicketNumber Test Ticket $RandomID",
192        },
193        'PluginSearch() - Search results (by ticket number)'
194    );
195
196    # search the ticket via ticket id
197    $ResultList = $PluginObject->PluginSearch(
198        ObjectID  => $TicketID,
199        PluginKey => $PluginKeyTicket,
200        UserID    => $UserID,
201    );
202
203    $Self->IsDeeply(
204        $ResultList,
205        {
206            $TicketID => "$TicketNumber Test Ticket $RandomID",
207        },
208        'PluginSearch() - Search results (by ticket ID)'
209    );
210
211    # link appointment with the ticket
212    my $Success = $PluginObject->PluginLinkAdd(
213        AppointmentID => $AppointmentID,
214        PluginKey     => $PluginKeyTicket,
215        PluginData    => $TicketID,
216        UserID        => $UserID,
217    );
218
219    $Self->True(
220        $Success,
221        'PluginLinkAdd() - Link appointment to the ticket',
222    );
223
224    # verify link
225    my $LinkList = $PluginObject->PluginLinkList(
226        AppointmentID => $AppointmentID,
227        PluginKey     => $PluginKeyTicket,
228        UserID        => $UserID,
229    );
230
231    $Self->True(
232        $LinkList->{$TicketID},
233        'PluginLinkList() - Verify link to the ticket'
234    );
235
236    $Self->Is(
237        $LinkList->{$TicketID}->{LinkID},
238        $TicketID,
239        'TicketID',
240    );
241
242    # check URL
243    $Self->True(
244        $LinkList->{$TicketID}->{LinkURL} =~ /TicketID=$TicketID/,
245        'Ticket URL contains ticket ID'
246    );
247
248    # link name
249    $Self->Is(
250        $LinkList->{$TicketID}->{LinkName},
251        "$TicketNumber Test Ticket $RandomID",
252        'Link name'
253    );
254
255    # delete links
256    $Success = $PluginObject->PluginLinkDelete(
257        AppointmentID => $AppointmentID,
258        UserID        => $UserID,
259    );
260
261    $Self->True(
262        $Success,
263        'PluginLinkDelete() - Links deleted'
264    );
265
266    # verify links have been deleted
267    $LinkList = $PluginObject->PluginLinkList(
268        AppointmentID => $AppointmentID,
269        PluginKey     => $PluginKeyTicket,
270        UserID        => $UserID,
271    );
272
273    $Self->IsDeeply(
274        $LinkList,
275        {},
276        'PluginLinkList() - Empty link list',
277    );
278}
279
2801;
281