From d4cc3f0e24937fa53870132003aec6af460b9b57 Mon Sep 17 00:00:00 2001
From: Carlos Garcia <carlos.garcia@otrs.com>
Date: Fri, 10 May 2019 23:43:54 -0500
Subject: [PATCH] Fixed: Ticket::DefineEmailFrom not used for external notes
 (bug#14491).

---
 CHANGES.md                                    |  2 +
 Kernel/Config/Files/Ticket.xml                | 20 +++++
 Kernel/Modules/CustomerTicketZoom.pm          | 18 +++++
 .../Selenium/Customer/CustomerTicketZoom.t    | 74 +++++++++++++++++++
 4 files changed, 114 insertions(+)

--- a/Kernel/Config/Files/Ticket.xml
+++ b/Kernel/Config/Files/Ticket.xml
@@ -8048,6 +8048,26 @@
             </Hash>
         </Setting>
     </ConfigItem>
+    <ConfigItem Name="Ticket::Frontend::CustomerTicketZoom###DisplayNoteFrom" Required="1" Valid="1">
+        <Description Translatable="1">Defines the displayed style of the From field in notes that are visible for customers. A default agent name can be defined in Ticket::Frontend::CustomerTicketZoom###DefaultAgentName setting.</Description>
+        <Group>Ticket</Group>
+        <SubGroup>Frontend::Customer::Ticket::ViewZoom</SubGroup>
+        <Setting>
+            <Option SelectedID="FromField">
+                <Item Key="FromField" Translatable="1">The value of the From field</Item>
+                <Item Key="DefaultAgentName" Translatable="1">Default agent name</Item>
+            </Option>
+        </Setting>
+    </ConfigItem>
+
+    <ConfigItem Name="Ticket::Frontend::CustomerTicketZoom###DefaultAgentName" Required="1" Valid="1">
+        <Description Translatable="1">Defines the default agent name in the ticket zoom view of the customer interface.</Description>
+        <Group>Ticket</Group>
+        <SubGroup>Frontend::Customer::Ticket::ViewZoom</SubGroup>
+        <Setting>
+            <String Regex="" Translatable="1">Support Agent</String>
+        </Setting>
+    </ConfigItem>
     <ConfigItem Name="Ticket::CustomerTicketSearch::SearchLimit" Required="1" Valid="1">
         <Description Translatable="1">Maximum number of tickets to be displayed in the result of a search in the customer interface.</Description>
         <Group>Ticket</Group>
--- a/Kernel/Modules/CustomerTicketZoom.pm
+++ b/Kernel/Modules/CustomerTicketZoom.pm
@@ -1438,6 +1438,24 @@ sub _Mask {
 
         $LastSenderType = $Article{SenderType};
 
+        # Get screen config for CustomerTicketZoom
+        my $ScreenConfig = $ConfigObject->Get('Ticket::Frontend::CustomerTicketZoom');
+
+        # Define if external notes should show the real name of the agent
+        #   or just a default agent name.
+        if (
+            $ScreenConfig->{DisplayNoteFrom}
+            && $ScreenConfig->{DisplayNoteFrom} eq 'DefaultAgentName'
+            && $Article{SenderType} eq 'agent'
+            && $Article{ArticleType} eq 'note-external'
+            )
+        {
+            my $DefaultAgentName
+                = $LayoutObject->{LanguageObject}->Translate( $ScreenConfig->{DefaultAgentName} || 'Support Agent' );
+            $Article{From}         = $DefaultAgentName;
+            $Article{FromRealname} = $DefaultAgentName;
+        }
+
         $LayoutObject->Block(
             Name => 'Article',
             Data => \%Article,
--- a/scripts/test/Selenium/Customer/CustomerTicketZoom.t
+++ b/scripts/test/Selenium/Customer/CustomerTicketZoom.t
@@ -164,6 +164,80 @@ $Selenium->RunTest(
             "Reply button not found",
         );
 
+        my $TestOriginalFrom = 'Agent Some Agent Some Agent' . $Helper->GetRandomID() . ' <email@example.com>';
+
+        my $ArticleID2 = $TicketObject->ArticleCreate(
+            TicketID       => $TicketID,
+            ArticleType    => 'note-external',
+            SenderType     => 'agent',
+            From           => $TestOriginalFrom,
+            Subject        => $SubjectRandom,
+            Body           => $TextRandom,
+            ContentType    => 'text/html; charset=ISO-8859-15',
+            HistoryType    => 'AddNote',
+            HistoryComment => 'Some free text!',
+            UserID         => 1,
+        );
+        $Self->True(
+            $ArticleID2,
+            "Article is created - $ArticleID",
+        );
+
+        # Use From field value.
+        $Helper->ConfigSettingChange(
+            Valid => 1,
+            Key   => 'Ticket::Frontend::CustomerTicketZoom###DisplayNoteFrom',
+            Value => 'FromField',
+        );
+
+        # Allow apache to pick up the changed SysConfig via Apache::Reload.
+        sleep 2;
+
+        # Refresh the page.
+        $Selenium->VerifiedRefresh();
+
+        # Check From field value.
+        my $FromString = $Selenium->execute_script(
+            "return \$('.MessageBody:eq(1) span:eq(1)').text().trim();"
+        );
+        $Self->Is(
+            $FromString,
+            $TestOriginalFrom,
+            "Test From content",
+        );
+
+        # Use default agent name setting.
+        $Helper->ConfigSettingChange(
+            Valid => 1,
+            Key   => 'Ticket::Frontend::CustomerTicketZoom###DisplayNoteFrom',
+            Value => 'DefaultAgentName',
+        );
+
+        my $TestDefaultAgentName = 'ADefaultValueForAgentName' . $Helper->GetRandomID();
+
+        # Set a default value for agent.
+        $Helper->ConfigSettingChange(
+            Valid => 1,
+            Key   => 'Ticket::Frontend::CustomerTicketZoom###DefaultAgentName',
+            Value => $TestDefaultAgentName,
+        );
+
+        # Allow apache to pick up the changed SysConfig via Apache::Reload.
+        sleep 2;
+
+        # Refresh the page.
+        $Selenium->VerifiedRefresh();
+
+        # Check From field value.
+        $FromString = $Selenium->execute_script(
+            "return \$('.MessageBody:eq(1) span:eq(1)').text().trim();"
+        );
+        $Self->Is(
+            $FromString,
+            $TestDefaultAgentName,
+            "Test From content",
+        );
+
         # check print button
         $Selenium->find_element("//a[contains(\@href, \'Action=CustomerTicketPrint;' )]")->VerifiedClick();
 
