# --
# Copyright (C) 2001-2021 OTRS AG, https://otrs.com/
# Copyright (C) 2021 Znuny GmbH, https://znuny.org/
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (GPL). If you
# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
# --

use strict;
use warnings;
use utf8;

use vars (qw($Self));

use Kernel::Language;

# get selenium object
my $Selenium = $Kernel::OM->Get('Kernel::System::UnitTest::Selenium');

$Selenium->RunTest(
    sub {

        my $HelperObject = $Kernel::OM->Get('Kernel::System::UnitTest::Helper');
        my $ConfigObject = $Kernel::OM->Get('Kernel::Config');

        # do not check RichText
        $HelperObject->ConfigSettingChange(
            Valid => 1,
            Key   => 'Frontend::RichText',
            Value => 1,
        );

        # do not check RichText enhanced mode
        $HelperObject->ConfigSettingChange(
            Valid => 1,
            Key   => 'Frontend::RichText::EnhancedMode',
            Value => 0,
        );

        my $Language      = 'de';
        my $TestUserLogin = $HelperObject->TestUserCreate(
            Language => $Language,
            Groups   => ['admin'],
        ) || die "Did not get test user";

        $Selenium->Login(
            Type     => 'Agent',
            User     => $TestUserLogin,
            Password => $TestUserLogin,
        );

        # remember to escape the strings correctly for Perl AND Javascript!
        my @TestCasesBasic = (
            {
                'Name'  => '1: Basic text with formatting tags which should be changed to be semantically correct',
                'Input' =>
                    'This is a test text with <b>some</b> <i>formatting</i> and <a href=\"http://www.test.de\">a link</a>. Also, there is a list: <ul><li>Listitem 1</li><li>Listitem 2</li></ul>.',
                'Expected' =>
                    '<p>This is a test text with <strong>some</strong> <i>formatting</i> and <a href="http://www.test.de">a link</a>. Also, there is a list:</p><ul><li>Listitem 1</li><li>Listitem 2</li></ul><p>.</p>'
            },
            {
                'Name'  => '2: Remove invalid/forbidden tags',
                'Input' =>
                    "This text contains a script tag: <script>alert(\'bla\');</script> and a table, though the enhanced mode is not enabled: <table><tr><td>One cell</td></tr></table>.",
                'Expected' =>
                    "<p>This text contains a script tag: alert(\'bla\'); and a table, though the enhanced mode is not enabled:</p><figure class=\"table\"><table><tbody><tr><td>One cell</td></tr></tbody></table></figure><p>.</p>",
            },
            {
                'Name'  => '3: Remove invalid/forbidden attributes',
                'Input' =>
                    'Here is an allowed element with a forbidden attribute: <strong data-uri=\"foo\">Strong text</strong>.',
                'Expected' =>
                    '<p>Here is an allowed element with a forbidden attribute: <strong>Strong text</strong>.</p>',
            },
        );

        # get script alias
        my $ScriptAlias = $ConfigObject->Get('ScriptAlias');

        # navigate to AgentDashboard screen
        $Selenium->VerifiedGet("${ScriptAlias}index.pl?Action=AgentTicketPhone");

        # wait for the CKE to load
        $Selenium->WaitFor(
            JavaScript =>
                "return typeof(\$) === 'function' && \$('.ck-editor__editable_inline').contents().length == 1"
        );

       # send some text to the CKE's textarea (we cant do it with Selenium directly because the textarea is not visible)
        my $SetCKEContent = 1;
        eval {
            $SetCKEContent = $Selenium->execute_script(
                q{
                    return window.editor.setData('This is a test text');
                }
            );
        };

        # if the result is undef, the command succeeded (o_O)
        $Self->Is(
            $SetCKEContent,
            undef,
            "Successfully sent data to the CKE instance."
        );

        # Wait until CKEditor content is updated.
        $Selenium->WaitFor(
            JavaScript => "return window.editor.getData() ",
        );

        $Self->Is(
            $Selenium->execute_script('return window.editor.getData();'),
            '<p>This is a test text</p>',
            'Check plain text content.'
        );

        # now go through the test cases
        for my $TestCase (@TestCasesBasic) {

            # wait for the CKE to load
            $Selenium->WaitFor(
                JavaScript =>
                    "return typeof(\$) === 'function' && \$('.ck-editor__editable').contents().length >= 1"
            );

            $Selenium->execute_script( 'window.editor.setData("' . $TestCase->{Input} . '");' );

            # Wait until CKEditor content is updated.
            $Selenium->WaitFor(
                JavaScript => "return window.editor.getData();",
            );

            $Self->Is(
                $Selenium->execute_script("return window.editor.getData();"),
                $TestCase->{Expected},
                $TestCase->{Name}
            );
        }
    }
);

1;
