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
20        my $Helper = $Kernel::OM->Get('Kernel::System::UnitTest::Helper');
21
22        # Disable check email addresses.
23        $Helper->ConfigSettingChange(
24            Key   => 'CheckEmailAddresses',
25            Value => 0,
26        );
27
28        # Create test user.
29        my $TestUserLogin = $Helper->TestUserCreate(
30            Groups => [ 'admin', 'users' ],
31        ) || die "Did not get test user";
32
33        # Get test user ID.
34        my $TestUserID = $Kernel::OM->Get('Kernel::System::User')->UserLookup(
35            UserLogin => $TestUserLogin,
36        );
37
38        # Create test customer user.
39        my $TestCustomerUserLogin = $Helper->TestCustomerUserCreate(
40        ) || die "Did not get test customer user";
41
42        # Get test customer user ID.
43        my @CustomerIDs = $Kernel::OM->Get('Kernel::System::CustomerUser')->CustomerIDs(
44            User => $TestCustomerUserLogin,
45        );
46        my $CustomerID            = $CustomerIDs[0];
47        my $CustomerCompanyObject = $Kernel::OM->Get('Kernel::System::CustomerCompany');
48        my $CustomerUserObject    = $Kernel::OM->Get('Kernel::System::CustomerUser');
49
50        my $RandomID    = $Helper->GetRandomID();
51        my $CompanyRand = 'Customer-Company' . $RandomID;
52
53        my $CustomersID = $CustomerCompanyObject->CustomerCompanyAdd(
54            CustomerID             => $CompanyRand,
55            CustomerCompanyName    => $CompanyRand . ' Inc',
56            CustomerCompanyStreet  => 'Some Street',
57            CustomerCompanyZIP     => '12345',
58            CustomerCompanyCity    => 'Some city',
59            CustomerCompanyCountry => 'USA',
60            CustomerCompanyURL     => 'http://example.com',
61            CustomerCompanyComment => 'some comment',
62            ValidID                => 1,
63            UserID                 => 1,
64        );
65
66        $Self->True(
67            $CustomerID,
68            "CustomerCompanyAdd() - $CustomerID",
69        );
70
71        my @CustomerUserIDs;
72        my @UserEmails;
73        for my $Key ( 1 .. 5 ) {
74            my $UserEmail = 'unittest-' . $Key . $RandomID . '-Email@example.com';
75
76            my $CustomerUserID = $CustomerUserObject->CustomerUserAdd(
77                Source         => 'CustomerUser',
78                UserFirstname  => 'Firstname' . $Key,
79                UserLastname   => 'Lastname' . $Key,
80                UserCustomerID => $CustomerID,
81                UserLogin      => $RandomID . 'CustomerUser' . $Key,
82                UserEmail      => $UserEmail,
83                UserPassword   => 'some_pass',
84                ValidID        => 1,
85                UserID         => 1,
86            );
87
88            $Self->True(
89                $CustomerUserID,
90                "CustomerUserAdd() - $CustomerUserID",
91            );
92
93            push @CustomerUserIDs, $CustomerUserID;
94            push @UserEmails,      $UserEmail;
95
96        }
97
98        my $TicketObject = $Kernel::OM->Get('Kernel::System::Ticket');
99
100        # Create test data parameters.
101        my %TicketData = (
102            'Open' => {
103                TicketState   => 'open',
104                TicketCount   => '',
105                TicketNumbers => [],
106                TicketIDs     => [],
107                TicketLink    => 'Open',
108            },
109            'Closed' => {
110                TicketState   => 'closed successful',
111                TicketCount   => '',
112                TicketNumbers => [],
113                TicketIDs     => [],
114                TicketLink    => 'Closed',
115            },
116        );
117
118        # Create open and closed tickets.
119        for my $TicketCreate ( sort keys %TicketData ) {
120            for my $TestTickets ( 1 .. 5 ) {
121                my $TicketNumber = $TicketObject->TicketCreateNumber();
122                my $TicketID     = $TicketObject->TicketCreate(
123                    TN           => $TicketNumber,
124                    Title        => 'Selenium Test Ticket',
125                    Queue        => 'Raw',
126                    Lock         => 'unlock',
127                    Priority     => '3 normal',
128                    State        => $TicketData{$TicketCreate}->{TicketState},
129                    CustomerID   => $CustomerID,
130                    CustomerUser => $TestCustomerUserLogin,
131                    OwnerID      => $TestUserID,
132                    UserID       => $TestUserID,
133                );
134                $Self->True(
135                    $TicketID,
136                    "$TicketCreate - ticket TicketID $TicketID - created - TN $TicketNumber",
137                );
138                push @{ $TicketData{$TicketCreate}->{TicketIDs} },     $TicketID;
139                push @{ $TicketData{$TicketCreate}->{TicketNumbers} }, $TicketNumber;
140            }
141            my $TicketCount = $TicketObject->TicketSearch(
142                Result     => 'Count',
143                StateType  => $TicketCreate,
144                CustomerID => $CustomerID,
145                UserID     => $TestUserID,
146            );
147            $TicketData{$TicketCreate}->{TicketCount} = $TicketCount;
148        }
149
150        # Login as test user.
151        $Selenium->Login(
152            Type     => 'Agent',
153            User     => $TestUserLogin,
154            Password => $TestUserLogin,
155        );
156
157        my $ScriptAlias = $Kernel::OM->Get('Kernel::Config')->Get('ScriptAlias');
158
159        # Navigate to AdminCustomerInformationCenter screen.
160        $Selenium->VerifiedGet("${ScriptAlias}index.pl?Action=AgentCustomerInformationCenter");
161        $Selenium->WaitFor(
162            JavaScript =>
163                'return typeof($) === "function" && $("#AgentCustomerInformationCenterSearchCustomerID").length;'
164        );
165
166        # Input search parameters for CustomerUser.
167        $Selenium->find_element( "#AgentCustomerInformationCenterSearchCustomerUser", 'css' )
168            ->send_keys( $RandomID . 'CustomerUser' . '*' );
169        sleep 1;
170
171        # Check result of customer user search (there should be 5 matches).
172        $Selenium->WaitFor( JavaScript => "return \$('li a:contains(\"$RandomID\")').length == 5;" );
173
174        $Selenium->find_element( "#AgentCustomerInformationCenterSearchCustomerUser", 'css' )->clear();
175
176        # Input search parameters CustomerID.
177        $Selenium->find_element( "#AgentCustomerInformationCenterSearchCustomerID", 'css' )
178            ->send_keys($TestCustomerUserLogin);
179        $Selenium->WaitFor( JavaScript => 'return typeof($) === "function" && $("li.ui-menu-item:visible").length;' );
180        $Selenium->execute_script("\$('li.ui-menu-item:contains($TestCustomerUserLogin)').click();");
181
182        $Selenium->WaitFor(
183            JavaScript => 'return typeof($) === "function" && $(".ContentColumn .WidgetSimple .Header h2").length;'
184        );
185
186        # Check customer information center page.
187        $Self->True(
188            index( $Selenium->get_page_source(), "Customer Information Center" ) > -1,
189            "Found looked value on page",
190        );
191
192        my @Header = (
193            'Customer Users',
194            'Reminder Tickets',
195            'Escalated Tickets',
196            'New Tickets',
197            'Open Tickets',
198        );
199
200        my $Count = 0;
201        for my $Title (@Header) {
202
203            # Check widget title.
204            $Self->True(
205                index( $Selenium->get_page_source(), $Title ) > -1,
206                "$Title widget found on page",
207            );
208            $Count++;
209        }
210
211        $Self->Is(
212            $Selenium->execute_script("return \$('.SidebarColumn .WidgetSimple .Header h2:eq(0)').text();"),
213            'Settings',
214            "Setting for toggle widgets found on page",
215        );
216
217        # Check if there is link to CIC search modal dialog from heading (name of the company).
218        $Self->True(
219            $Selenium->find_element( "#CustomerInformationCenterHeading", 'css' ),
220            'There is link to customer information center search modal dialog.',
221        );
222
223        # Test links in Company Status widget.
224        for my $TestLinks ( sort keys %TicketData ) {
225
226            # Click on link.
227            $Selenium->find_element(
228                "//a[contains(\@href, \'Subaction=Search;StateType=$TicketData{$TestLinks}->{TicketLink};CustomerIDRaw=$TestCustomerUserLogin' )]"
229            )->VerifiedClick();
230
231            # Wait until page has loaded, if necessary.
232            $Selenium->WaitFor( JavaScript => 'return typeof($) === "function" && $("body").length;' );
233
234            # Check for test ticket numbers on search screen.
235            for my $CheckTicketNumbers ( @{ $TicketData{$TestLinks}->{TicketNumbers} } ) {
236                $Self->True(
237                    index( $Selenium->get_page_source(), $CheckTicketNumbers ) > -1,
238                    "TicketNumber $CheckTicketNumbers - found on screen"
239                );
240            }
241
242            # Click on 'Change search option'.
243            $Selenium->find_element(
244                "//a[contains(\@href, \'AgentTicketSearch;Subaction=LoadProfile' )]"
245            )->click();
246
247            # Wait until search dialog has been loaded.
248            $Selenium->WaitFor( JavaScript => 'return $("#SearchFormSubmit").length;' );
249
250            # Verify state search attributes are shown in search screen, see bug #10853.
251            $Selenium->find_element( "#StateIDs", 'css' );
252
253            # Open CIC again for the next test case.
254            $Selenium->VerifiedGet(
255                "${ScriptAlias}index.pl?Action=AgentCustomerInformationCenter;CustomerID=$TestCustomerUserLogin"
256            );
257
258        }
259
260        # Create new Email ticket.
261        $Selenium->find_element(
262            "//a[contains(\@href, \"Action=AgentTicketEmail;Subaction=StoreNew;ExpandCustomerName=2;CustomerUser=$CustomerUserIDs[0]\" )]"
263        )->VerifiedClick();
264
265        # Confirm I'm on New Email Ticket screen.
266        $Self->True(
267            index( $Selenium->get_page_source(), "Create New Email Ticket" ) > -1,
268            "Found looked value on page",
269        );
270
271        # Select input field and type inside.
272        $Selenium->find_element( "#ToCustomer", 'css' )->send_keys( $CustomerUserIDs[0] );
273
274        # Click on wanted element in dropdown menu.
275        $Selenium->WaitFor( JavaScript => 'return typeof($) === "function" && $("li.ui-menu-item:visible").length;' );
276        $Selenium->execute_script("\$('li.ui-menu-item:contains($CustomerUserIDs[0])').click();");
277        $Selenium->WaitFor( JavaScript => 'return typeof($) === "function" && $(".Dialog.Modal").length;' );
278
279        $Self->Is(
280            $Selenium->execute_script('return $(".Dialog.Modal .Header h1").text().trim();'),
281            'Duplicated entry',
282            "Warning dialog for entry duplication is found",
283        );
284
285        # Close error message.
286        $Selenium->find_element( "#DialogButton1", 'css' )->click();
287        $Selenium->WaitFor( JavaScript => 'return typeof($) === "function" && !$(".Dialog.Modal").length;' );
288
289        # Go to previous.
290        $Selenium->VerifiedGet(
291            "${ScriptAlias}index.pl?Action=AgentCustomerInformationCenter;CustomerID=$TestCustomerUserLogin"
292        );
293
294        # Create new Phone ticket.
295        $Selenium->find_element(
296            "//a[contains(\@href, \"AgentTicketPhone;Subaction=StoreNew;ExpandCustomerName=2;CustomerUser=$CustomerUserIDs[0]\" )]"
297        )->VerifiedClick();
298
299        # Confirm I'm on New Phone Ticket screen.
300        $Self->True(
301            index( $Selenium->get_page_source(), "Create New Phone Ticket" ) > -1,
302            "Found looked value on page",
303        );
304
305        # Select input field and type inside.
306        $Selenium->find_element( "#FromCustomer", 'css' )->send_keys( $CustomerUserIDs[0] );
307
308        $Selenium->WaitFor( JavaScript => 'return typeof($) === "function" && $("li.ui-menu-item:visible").length;' );
309        $Selenium->execute_script("\$('li.ui-menu-item:contains($CustomerUserIDs[0])').click();");
310
311        # Error is expected.
312        $Selenium->WaitFor( JavaScript => 'return typeof($) === "function" && $(".Dialog.Modal").length;' );
313
314        $Self->Is(
315            $Selenium->execute_script('return $(".Dialog.Modal .Header h1").text().trim();'),
316            'Duplicated entry',
317            "Warning dialog for entry duplication is found",
318        );
319
320        # Delete created test tickets.
321        for my $TicketState ( sort keys %TicketData ) {
322            for my $TicketID ( @{ $TicketData{$TicketState}->{TicketIDs} } ) {
323
324                my $Success = $TicketObject->TicketDelete(
325                    TicketID => $TicketID,
326                    UserID   => $TestUserID,
327                );
328
329                # Ticket deletion could fail if apache still writes to ticket history. Try again in this case.
330                if ( !$Success ) {
331                    sleep 3;
332                    $Success = $TicketObject->TicketDelete(
333                        TicketID => $TicketID,
334                        UserID   => $TestUserID,
335                    );
336                }
337                $Self->True(
338                    $Success,
339                    "Delete ticket - $TicketID"
340                );
341            }
342        }
343
344        # Delete created test customer user and customer company.
345        my $DBObject = $Kernel::OM->Get('Kernel::System::DB');
346        for my $CustomerID (@CustomerUserIDs) {
347            my $Success = $DBObject->Do(
348                SQL  => "DELETE FROM customer_user WHERE login = ?",
349                Bind => [ \$CustomerID ],
350            );
351            $Self->True(
352                $Success,
353                "Deleted Customers - $CustomerID",
354            );
355        }
356
357        my $Success = $DBObject->Do(
358            SQL  => "DELETE FROM customer_company WHERE customer_id = ?",
359            Bind => [ \$CompanyRand ],
360        );
361        $Self->True(
362            $Success,
363            "Deleted CustomerUser - $CustomerID",
364        );
365
366        # Make sure cache is correct.
367        $Kernel::OM->Get('Kernel::System::Cache')->CleanUp( Type => 'Ticket' );
368    }
369);
370
3711;
372