[Rt-commit] rt branch, 4.4/attach-from-transactions, created. rt-4.2.11-142-gbd700c1

? sunnavy sunnavy at bestpractical.com
Sun Aug 9 13:18:47 EDT 2015


The branch, 4.4/attach-from-transactions has been created
        at  bd700c1f1f2302217c9f974fb134732e1d53e65a (commit)

- Log -----------------------------------------------------------------
commit a7bc597650f96c07f7b4aab98ad40779a89b566b
Author: Thomas Sibley <trs at bestpractical.com>
Date:   Wed Aug 10 18:05:34 2011 -0400

    Add the ability to include existing attachments when responding to a transaction
    
    The ticket update page (and any other place which uses
    /Ticket/Elements/AddAttachments) will offer up the named attachments
    from the transaction being quoted (if any).  Each selected attachment
    causes an RT-Attach: id header to be added to the update.
    
    The RT-Attach headers are inspected to actually add the attachments to
    outgoing mail in RT::Action::SendEmail, as long as the creator of the
    transaction with the RT-Attach headers can see the requested
    attachments.  If the outgoing template has a false RT-Attach-Message
    value, then RT-Attach headers are skipped just like attachments directly
    on the transaction.
    
    Any RT-Attach headers in the template are merged with the ones from the
    transaction.  This makes it possible, if permissions are correct, to use
    a template-level RT-Attach header to attach arbitrary content.
    
    This feature is accessible via the Perl API by passing in the
    AttachExisting option (as an arrayref) to RT::Ticket->Correspond and
    RT::Ticket->Comment, or manually setting RT-Attach headers on
    transaction content.

diff --git a/lib/RT/Action/SendEmail.pm b/lib/RT/Action/SendEmail.pm
index cee612e..b71f43e 100644
--- a/lib/RT/Action/SendEmail.pm
+++ b/lib/RT/Action/SendEmail.pm
@@ -58,6 +58,7 @@ use base qw(RT::Action);
 use RT::EmailParser;
 use RT::Interface::Email;
 use Email::Address;
+use List::MoreUtils qw(uniq);
 our @EMAIL_RECIPIENT_HEADERS = qw(To Cc Bcc);
 
 
@@ -377,6 +378,51 @@ sub AddAttachments {
         }
         $self->AddAttachment($attach);
     }
+
+    # attach any attachments requested by the transaction or template
+    # that aren't part of the transaction itself
+    $self->AddAttachmentsFromHeaders;
+}
+
+=head2 AddAttachmentsFromHeaders
+
+Add attachments requested by the transaction or template that aren't part of
+the transaction itself.
+
+This inspects C<RT-Attach> headers, which are expected to contain an
+L<RT::Attachment> ID that the transaction's creator can See.
+
+L<RT::Ticket->_RecordNote> accepts an C<AttachExisting> argument which sets
+C<RT-Attach> headers appropriately on Comment/Correspond.
+
+=cut
+
+sub AddAttachmentsFromHeaders {
+    my $self  = shift;
+    my $orig  = $self->TransactionObj->Attachments->First;
+    my $email = $self->TemplateObj->MIMEObj;
+
+    # Add the RT-Attach headers from the transaction to the email
+    if ($orig and $orig->GetHeader('RT-Attach')) {
+        for my $id ($orig->ContentAsMIME(Children => 0)->head->get_all('RT-Attach')) {
+            $email->head->add('RT-Attach' => $id);
+        }
+    }
+
+    # Take all RT-Attach headers and add the attachments to the outgoing mail
+    my $seen_attachment = 0;
+    for my $id (uniq $email->head->get_all('RT-Attach')) {
+        my $attach = RT::Attachment->new( $self->TransactionObj->CreatorObj );
+        $attach->Load($id);
+        next unless $attach->Id
+                and $attach->TransactionObj->CurrentUserCanSee;
+
+        if ( !$seen_attachment ) {
+            $email->make_multipart( 'mixed', Force => 1 );
+            $seen_attachment = 1;
+        }
+        $self->AddAttachment($attach, $email);
+    }
 }
 
 =head2 AddAttachment $attachment
diff --git a/lib/RT/Interface/Web.pm b/lib/RT/Interface/Web.pm
index 193b2c2..97c79fe 100644
--- a/lib/RT/Interface/Web.pm
+++ b/lib/RT/Interface/Web.pm
@@ -2327,7 +2327,8 @@ sub ProcessUpdateMessage {
         Sign         => $args{ARGSRef}->{'Sign'},
         Encrypt      => $args{ARGSRef}->{'Encrypt'},
         MIMEObj      => $Message,
-        TimeTaken    => $args{ARGSRef}->{'UpdateTimeWorked'}
+        TimeTaken    => $args{ARGSRef}->{'UpdateTimeWorked'},
+        AttachExisting => $args{ARGSRef}->{'AttachExisting'},
     );
 
     _ProcessUpdateMessageRecipients(
diff --git a/lib/RT/Ticket.pm b/lib/RT/Ticket.pm
index 742c562..8b75fb8 100644
--- a/lib/RT/Ticket.pm
+++ b/lib/RT/Ticket.pm
@@ -1541,6 +1541,7 @@ sub _RecordNote {
         NoteType     => 'Correspond',
         TimeTaken    => 0,
         SquelchMailTo => undef,
+        AttachExisting => [],
         @_
     );
 
@@ -1563,6 +1564,17 @@ sub _RecordNote {
     # convert text parts into utf-8
     RT::I18N::SetMIMEEntityToUTF8( $args{'MIMEObj'} );
 
+    # Set the magic RT headers which include existing attachments on this note
+    if ($args{'AttachExisting'}) {
+        $args{'AttachExisting'} = [$args{'AttachExisting'}]
+            if not ref $args{'AttachExisting'} eq 'ARRAY';
+
+        for my $attach (@{$args{'AttachExisting'}}) {
+            next if $attach =~ /\D/;
+            $args{'MIMEObj'}->head->add( 'RT-Attach' => $attach );
+        }
+    }
+
     # If we've been passed in CcMessageTo and BccMessageTo fields,
     # add them to the mime object for passing on to the transaction handler
     # The "NotifyOtherRecipients" scripAction will look for RT-Send-Cc: and
diff --git a/share/html/Ticket/Elements/AddAttachments b/share/html/Ticket/Elements/AddAttachments
index 769b691..05d1e63 100644
--- a/share/html/Ticket/Elements/AddAttachments
+++ b/share/html/Ticket/Elements/AddAttachments
@@ -60,13 +60,40 @@
 
 <tr><td class="label"><&|/l&>Attach</&>:</td><td><input name="Attach" type="file" /><input type="submit" class="button" name="AddMoreAttach" value="<&|/l&>Add More Files</&>" /><input type="hidden" class="hidden" name="UpdateAttach" value="1" />
 </td></tr>
+% if (@quoted_attachments) {
+<tr>
+  <td class="label" valign="top"><&|/l&>Include attachments</&>:</td>
+  <td>
+%     for my $attach (@quoted_attachments) {
+    <label>
+      <input type="checkbox" class="checkbox" name="AttachExisting" value="<% $attach->Id %>" \
+             <% (grep { $attach->Id == $_ } @AttachExisting) ? 'checked' : '' %> />
+      <% $attach->Filename %>
+    </label><br />
+%     }
+  </td>
+</tr>
+% }
 % $m->callback( %ARGS, CallbackName => 'End' );
 <%ARGS>
 $Token => ''
+ at AttachExisting => ()
+$QuoteTransaction => ''
 </%ARGS>
 <%INIT>
 my $attachments;
 if ( exists $session{'Attachments'}{ $Token } && keys %{ $session{'Attachments'}{ $Token } } ) {
     $attachments = $session{'Attachments'}{ $Token };
 }
+
+my @quoted_attachments;
+if ($QuoteTransaction) {
+    my $txn = RT::Transaction->new( $session{'CurrentUser'} );
+    $txn->Load($QuoteTransaction);
+    if ($txn->Id and $txn->CurrentUserCanSee) {
+        @quoted_attachments = sort { lc($a->Filename) cmp lc($b->Filename) }
+                              grep { defined $_->Filename and length $_->Filename }
+                                  @{$txn->Attachments->ItemsArrayRef};
+    }
+}
 </%INIT>

commit 0da0f88e68fc2be419076764cbe5f99c612d4b1a
Author: Thomas Sibley <trs at bestpractical.com>
Date:   Thu Aug 11 12:40:51 2011 -0400

    Simplify the copying of RT-Attach headers from the txn into the mail
    
    By adding an RT::Attachment->GetAllHeaders method comparable to
    MIME::Head->get_all.

diff --git a/lib/RT/Action/SendEmail.pm b/lib/RT/Action/SendEmail.pm
index b71f43e..3b72323 100644
--- a/lib/RT/Action/SendEmail.pm
+++ b/lib/RT/Action/SendEmail.pm
@@ -399,12 +399,11 @@ C<RT-Attach> headers appropriately on Comment/Correspond.
 
 sub AddAttachmentsFromHeaders {
     my $self  = shift;
-    my $orig  = $self->TransactionObj->Attachments->First;
     my $email = $self->TemplateObj->MIMEObj;
 
     # Add the RT-Attach headers from the transaction to the email
-    if ($orig and $orig->GetHeader('RT-Attach')) {
-        for my $id ($orig->ContentAsMIME(Children => 0)->head->get_all('RT-Attach')) {
+    if (my $attachment = $self->TransactionObj->Attachments->First) {
+        for my $id ($attachment->GetAllHeaders('RT-Attach')) {
             $email->head->add('RT-Attach' => $id);
         }
     }
diff --git a/lib/RT/Attachment.pm b/lib/RT/Attachment.pm
index 8b5e2d6..a0cb1a4 100644
--- a/lib/RT/Attachment.pm
+++ b/lib/RT/Attachment.pm
@@ -642,8 +642,8 @@ sub EncodedHeaders {
 
 =head2 GetHeader $TAG
 
-Returns the value of the header Tag as a string. This bypasses the weeding out
-done in Headers() above.
+Returns the value of the B<first> header Tag as a string. This bypasses the
+weeding out done in Headers() above.
 
 =cut
 
@@ -661,6 +661,24 @@ sub GetHeader {
     return undef;
 }
 
+=head2 GetAllHeaders $TAG
+
+Returns a list of all values for the the given header tag, in the order they
+appear.
+
+=cut
+
+sub GetAllHeaders {
+    my $self = shift;
+    my $tag = shift;
+    my @values = ();
+    foreach my $line ($self->_SplitHeaders) {
+        next unless $line =~ /^\Q$tag\E:\s+(.*)$/si;
+        push @values, $1;
+    }
+    return @values;
+}
+
 =head2 DelHeader $TAG
 
 Delete a field from the attachment's headers.

commit 01348d6d087bfe9782f96fb25ebe401c1d698b09
Author: Thomas Sibley <trs at bestpractical.com>
Date:   Thu Aug 11 13:17:25 2011 -0400

    Display RT-Attach headers in history by linking the filename
    
    It is possible recipients of the transaction saw the attachment via
    email but can't see it via the web.  This should be improved in the
    future, but it's an acceptable edge case at the moment since we avoid
    storing the attachment twice.

diff --git a/share/html/Elements/ShowMessageHeaders b/share/html/Elements/ShowMessageHeaders
index 27c67bd..bb7218b 100644
--- a/share/html/Elements/ShowMessageHeaders
+++ b/share/html/Elements/ShowMessageHeaders
@@ -80,6 +80,17 @@ unless ( $display_headers{'_all'} ) {
 my $object = $Message->TransactionObj->Object;
 foreach my $f (@headers) {
     $m->comp('/Elements/MakeClicky', content => \$f->{'Value'}, object => $object, %ARGS);
+    if ($f->{'Tag'} eq 'RT-Attach') {
+        # Blat in the filename and linkify
+        my $att = RT::Attachment->new( $session{'CurrentUser'} );
+        $att->Load($f->{'Value'});
+        next unless $att->Id and $att->TransactionObj->CurrentUserCanSee;
+
+        $f->{'Value'} = sprintf '<a href="%s/Ticket/Attachment/%d/%d/%s">%s</a>',
+                                RT->Config->Get('WebPath'), $att->TransactionObj->Id, $att->Id,
+                                $m->interp->apply_escapes($att->Filename, qw(u h)),
+                                $m->interp->apply_escapes($att->Filename, 'h');
+    }
 }
 
 unshift @headers, $m->comp( 'CryptStatus', Message => $Message, WarnUnsigned => $WarnUnsigned );
diff --git a/share/html/Elements/ShowTransactionAttachments b/share/html/Elements/ShowTransactionAttachments
index dcc28f2..24b191f 100644
--- a/share/html/Elements/ShowTransactionAttachments
+++ b/share/html/Elements/ShowTransactionAttachments
@@ -118,7 +118,7 @@ if ( $Transaction->Type =~ /EmailRecord$/ ) {
 
 # If the transaction has anything attached to it at all
 elsif (!$ShowHeaders)  {
-    @DisplayHeaders = qw(To From RT-Send-Cc Cc Bcc Date Subject);
+    @DisplayHeaders = qw(To From RT-Send-Cc Cc Bcc RT-Attach Date Subject);
     push @DisplayHeaders, 'RT-Send-Bcc' if RT->Config->Get('ShowBccHeader');
 }
 

commit 1ef7d367178d6f8a6359d7ae0578a59cee33be56
Author: Thomas Sibley <trs at bestpractical.com>
Date:   Mon Sep 19 13:43:41 2011 -0400

    Headers often come with leading spaces, which make SQLite choke on comparisons
    
    This was noticed because of test failures, but does affect functionality
    on SQLite.  Pg and MySQL can Do The Right Thing since they know they're
    dealing with numeric types.

diff --git a/lib/RT/Action/SendEmail.pm b/lib/RT/Action/SendEmail.pm
index 3b72323..f7ca3f8 100644
--- a/lib/RT/Action/SendEmail.pm
+++ b/lib/RT/Action/SendEmail.pm
@@ -411,6 +411,8 @@ sub AddAttachmentsFromHeaders {
     # Take all RT-Attach headers and add the attachments to the outgoing mail
     my $seen_attachment = 0;
     for my $id (uniq $email->head->get_all('RT-Attach')) {
+        $id =~ s/(?:^\s*|\s*$)//g;
+
         my $attach = RT::Attachment->new( $self->TransactionObj->CreatorObj );
         $attach->Load($id);
         next unless $attach->Id
diff --git a/share/html/Elements/ShowMessageHeaders b/share/html/Elements/ShowMessageHeaders
index bb7218b..bcf5253 100644
--- a/share/html/Elements/ShowMessageHeaders
+++ b/share/html/Elements/ShowMessageHeaders
@@ -81,6 +81,8 @@ my $object = $Message->TransactionObj->Object;
 foreach my $f (@headers) {
     $m->comp('/Elements/MakeClicky', content => \$f->{'Value'}, object => $object, %ARGS);
     if ($f->{'Tag'} eq 'RT-Attach') {
+        $f->{'Value'} =~ s/(?:^\s*|\s*$)//g;
+
         # Blat in the filename and linkify
         my $att = RT::Attachment->new( $session{'CurrentUser'} );
         $att->Load($f->{'Value'});

commit 66db6d3aa9fb2a8912418598594f1d0b4a04dc70
Author: Thomas Sibley <trs at bestpractical.com>
Date:   Tue Oct 18 13:25:06 2011 -0400

    Link to the download page for existing attachments
    
    This lets people quickly verify the attachments they're including.
    I'm not thrilled by the (View) solution, but I'm at a loss for something
    better at the moment.  The filename _should_ be the checkbox label, and
    clicking it should tick the checkbox, but not necessarily open the
    attachment.

diff --git a/share/html/Ticket/Elements/AddAttachments b/share/html/Ticket/Elements/AddAttachments
index 05d1e63..b14e3b3 100644
--- a/share/html/Ticket/Elements/AddAttachments
+++ b/share/html/Ticket/Elements/AddAttachments
@@ -69,7 +69,14 @@
       <input type="checkbox" class="checkbox" name="AttachExisting" value="<% $attach->Id %>" \
              <% (grep { $attach->Id == $_ } @AttachExisting) ? 'checked' : '' %> />
       <% $attach->Filename %>
-    </label><br />
+    </label>
+<%perl>
+my $url = sprintf '%s/Ticket/Attachment/%d/%d/%s',
+            RT->Config->Get('WebPath'), $attach->TransactionObj->Id, $attach->Id,
+            $m->interp->apply_escapes($attach->Filename, 'u');
+</%perl>
+      (<a href="<% $url %>" target="_blank"><&|/l&>View</&></a>)
+      <br />
 %     }
   </td>
 </tr>

commit d1982c24cab94802fee17f9cf63cb1866c1f47d1
Author: Thomas Sibley <trs at bestpractical.com>
Date:   Fri Dec 9 10:46:55 2011 -0500

    Don't present transaction attachments when we don't have an existing ticket
    
    QuoteTransaction may be used on Ticket/Create.html, but we don't
    currently handle including attachments from a transaction on create,
    only on update.

diff --git a/share/html/Ticket/Elements/AddAttachments b/share/html/Ticket/Elements/AddAttachments
index b14e3b3..36a4437 100644
--- a/share/html/Ticket/Elements/AddAttachments
+++ b/share/html/Ticket/Elements/AddAttachments
@@ -86,6 +86,7 @@ my $url = sprintf '%s/Ticket/Attachment/%d/%d/%s',
 $Token => ''
 @AttachExisting => ()
 $QuoteTransaction => ''
+$TicketObj => undef
 </%ARGS>
 <%INIT>
 my $attachments;
@@ -94,7 +95,7 @@ if ( exists $session{'Attachments'}{ $Token } && keys %{ $session{'Attachments'}
 }
 
 my @quoted_attachments;
-if ($QuoteTransaction) {
+if ($QuoteTransaction and $TicketObj and $TicketObj->id) {
     my $txn = RT::Transaction->new( $session{'CurrentUser'} );
     $txn->Load($QuoteTransaction);
     if ($txn->Id and $txn->CurrentUserCanSee) {

commit 216627fcd610c7c2deb8f80142520385a062345b
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Wed Feb 8 16:05:47 2012 +0800

    email could be multipart already before calling AddAttachmentsFromHeaders
    
    the bug's behavior is: when people select attachments and upload some too at
    the same time, the uploaded attachments will leak to EmailRecord txn, which
    results in duplicates for uploaded ones.
    
    this is because in RT::Action::SendEmail::RecordOutgoingMailTransaction, we
    filter top parts of email by checking their "RT-Attachment" headers, so we
    can't make multipart again if the email is already multipart.

diff --git a/lib/RT/Action/SendEmail.pm b/lib/RT/Action/SendEmail.pm
index f7ca3f8..b5df720 100644
--- a/lib/RT/Action/SendEmail.pm
+++ b/lib/RT/Action/SendEmail.pm
@@ -409,7 +409,6 @@ sub AddAttachmentsFromHeaders {
     }
 
     # Take all RT-Attach headers and add the attachments to the outgoing mail
-    my $seen_attachment = 0;
     for my $id (uniq $email->head->get_all('RT-Attach')) {
         $id =~ s/(?:^\s*|\s*$)//g;
 
@@ -418,9 +417,8 @@ sub AddAttachmentsFromHeaders {
         next unless $attach->Id
                 and $attach->TransactionObj->CurrentUserCanSee;
 
-        if ( !$seen_attachment ) {
+        if ( !$email->is_multipart ) {
             $email->make_multipart( 'mixed', Force => 1 );
-            $seen_attachment = 1;
         }
         $self->AddAttachment($attach, $email);
     }

commit bd700c1f1f2302217c9f974fb134732e1d53e65a
Author: Thomas Sibley <trs at bestpractical.com>
Date:   Thu Aug 11 18:26:20 2011 -0400

    Tests for the new RT-Attach functionality

diff --git a/t/data/image.png b/t/data/image.png
new file mode 100644
index 0000000..8a87374
Binary files /dev/null and b/t/data/image.png differ
diff --git a/t/data/owls.jpg b/t/data/owls.jpg
new file mode 100644
index 0000000..33c6558
Binary files /dev/null and b/t/data/owls.jpg differ
diff --git a/t/web/attach-from-txn.t b/t/web/attach-from-txn.t
new file mode 100644
index 0000000..dd1ebda
--- /dev/null
+++ b/t/web/attach-from-txn.t
@@ -0,0 +1,184 @@
+use strict;
+use warnings;
+
+use RT::Test tests => 70;
+
+my $LogoName    = 'image.png';
+my $ImageName   = 'owls.jpg';
+my $LogoFile    = RT::Test::get_relocatable_file($LogoName, '..', 'data');
+my $ImageFile   = RT::Test::get_relocatable_file($ImageName, '..', 'data');
+
+# reply to ticket = nothing
+# reply to correspond = getting the right list
+# maintaining the checked ones
+# storing the header
+# getting attached to mail
+# showing up in the web history
+
+my ($baseurl, $m) = RT::Test->started_ok;
+ok $m->login, 'logged in';
+
+my $queue = RT::Queue->new(RT->Nobody);
+my $qid = $queue->Load('General');
+ok( $qid, "Loaded General queue" );
+
+# Create ticket
+$m->form_name('CreateTicketInQueue');
+$m->field('Queue', $qid);
+$m->field('Requestors', 'owls at localhost');
+$m->submit;
+is($m->status, 200, "request successful");
+$m->content_contains("Create a new ticket", 'ticket create page');
+
+$m->form_name('TicketCreate');
+$m->field('Subject', 'Attachments test');
+$m->field('Content', 'Some content');
+$m->submit;
+is($m->status, 200, "request successful");
+
+$m->content_contains('Attachments test', 'we have subject on the page');
+$m->content_contains('Some content', 'and content');
+
+# Reply with uploaded attachments
+$m->follow_link_ok({text => 'Reply'}, "reply to the ticket");
+$m->content_lacks('AttachExisting');
+$m->form_name('TicketUpdate');
+$m->field('Attach', $LogoFile);
+$m->click('AddMoreAttach');
+is($m->status, 200, "request successful");
+
+$m->form_name('TicketUpdate');
+$m->field('Attach', $ImageFile);
+$m->field('UpdateContent', 'Message');
+$m->click('SubmitTicket');
+is($m->status, 200, "request successful");
+
+$m->content_contains("Download $LogoName", 'page has file name');
+$m->content_contains("Download $ImageName", 'page has file name');
+
+# clear mail catcher
+RT::Test->fetch_caught_mails;
+
+# Reply to first correspondence, including an attachment
+$m->follow_link_ok({text => 'Reply', n => 3}, "reply to the reply");
+$m->content_contains('AttachExisting');
+$m->content_contains($LogoName);
+$m->content_contains($ImageName);
+# check stuff
+$m->form_name('TicketUpdate');
+$m->current_form->find_input('AttachExisting', 'checkbox', 2)->check; # owls.jpg
+$m->click('AddMoreAttach');
+is($m->status, 200, "request successful");
+
+# ensure it's still checked
+$m->form_name('TicketUpdate');
+ok $m->current_form->find_input('AttachExisting', 'checkbox', 2)->value, 'still checked';
+$m->field('UpdateContent', 'Here are some attachments');
+$m->click('SubmitTicket');
+is($m->status, 200, "request successful");
+
+# yep, we got it and processed the header!
+$m->content_contains('Here are some attachments');
+$m->content_like(qr/RT-Attach:.+?\Q$ImageName\E/s, 'found rt attach header');
+
+# outgoing looks good
+$m->follow_link_ok({text => 'Show', n => 3}, "found show link");
+$m->content_like(qr/RT-Attach: \d+/, "found RT-Attach header");
+$m->content_like(qr/RT-Attachment: \d+\/\d+\/\d+/, "found RT-Attachment header");
+$m->content_lacks($ImageName);
+$m->back;
+
+# check that it got into mail
+my @mails = RT::Test->fetch_caught_mails;
+is scalar @mails, 1, "got one outgoing email";
+my $mail = shift @mails;
+like $mail, qr/To: owls\@localhost/, 'got To';
+like $mail, qr/RT-Attach: \d+/, "found attachment we expected";
+like $mail, qr/RT-Attachment: \d+\/\d+\/\d+/, "found RT-Attachment header";
+like $mail, qr/filename=.?\Q$ImageName\E.?/, "found filename";
+
+# Reply to first correspondence, including an attachment with an uploaded one
+$m->follow_link_ok({text => 'Reply', n => 3}, "reply to the reply");
+$m->form_name('TicketUpdate');
+$m->current_form->find_input('AttachExisting', 'checkbox', 2)->check; # owls.jpg
+$m->field( 'UpdateContent', 'attachments from both list and upload' );
+$m->field('Attach', $LogoFile);
+$m->click('SubmitTicket');
+is($m->status, 200, "request successful");
+
+# yep, we got it and processed the header!
+$m->content_contains('attachments from both list and upload');
+$m->content_like(qr/(RT-Attach:.+?\Q$ImageName\E).*\1/s, 'found rt attach header');
+$m->content_like(qr/Subject:.+?\Q$LogoName\E/s, 'found rt attach header');
+
+# outgoing looks good
+$m->follow_link_ok({text => 'Show', n => 4}, "found show link");
+$m->content_like(qr/RT-Attach: \d+/, "found RT-Attach header");
+$m->content_like(qr/RT-Attachment: \d+\/\d+\/\d+/, "found RT-Attachment header");
+$m->content_lacks($ImageName);
+$m->content_lacks($LogoName);
+$m->back;
+
+# check that it got into mail
+ at mails = RT::Test->fetch_caught_mails;
+is scalar @mails, 1, "got one outgoing email";
+$mail = shift @mails;
+like $mail, qr/To: owls\@localhost/, 'got To';
+like $mail, qr/RT-Attach: \d+/, "found attachment we expected";
+like $mail, qr/RT-Attachment: \d+\/\d+\/\d+/, "found RT-Attachment header";
+like $mail, qr/filename=.?\Q$ImageName\E.?/, "found selected filename";
+like $mail, qr/filename=.?\Q$LogoName\E.?/, "found uploaded filename";
+
+# add header to template, make a normal reply, and see that it worked
+my $link = $m->find_link(text_regex => qr/\Q$LogoName\E/, url_regex => qr/Attachment/);
+ok $link;
+my ($LogoId) = $link->url =~ /Attachment\/\d+\/(\d+)/;
+ok $LogoId;
+my $template = RT::Template->new( RT->SystemUser );
+$template->LoadGlobalTemplate('Correspondence in HTML');
+ok $template->Id;
+my $old_template = $template->Content;
+$template->SetContent( "RT-Attach: $LogoId\n" . $template->Content );
+like $template->Content, qr/RT-Attach:/, "updated template";
+
+# reply...
+$m->follow_link_ok({text => 'Reply'}, "reply to the ticket");
+$m->form_name('TicketUpdate');
+$m->field('UpdateContent', 'who gives a hoot');
+$m->click('SubmitTicket');
+is($m->status, 200, "request successful");
+$m->content_contains('who gives a hoot');
+
+# then see if we got the right mail
+ at mails = RT::Test->fetch_caught_mails;
+is scalar @mails, 1, "got one outgoing email";
+$mail = shift @mails;
+like $mail, qr/To: owls\@localhost/, 'got To';
+like $mail, qr/RT-Attach: $LogoId/, "found attachment we expected";
+like $mail, qr/RT-Attachment: \d+\/\d+\/$LogoId/, "found RT-Attachment header";
+like $mail, qr/filename=.?\Q$LogoName\E.?/, "found filename";
+
+# create a new, privileged user who can't see this ticket but can reply
+$template->SetContent($old_template);
+unlike $template->Content, qr/RT-Attach:/, "no header in template anymore";
+
+my $peter = RT::Test->load_or_create_user(
+    Name            => 'peter',
+    EmailAddress    => 'peter at localhost',
+);
+ok( RT::Test->add_rights({ Principal => 'Everyone', Right => [qw(ReplyToTicket)] }), 'add ReplyToTicket rights');
+
+my $ticket = RT::Ticket->new($peter);
+$ticket->Load(1);
+ok $ticket->Id, "loaded ticket";
+
+my ($ok, $msg, $txn) = $ticket->Correspond( AttachExisting => $LogoId, Content => 'Hi' );
+ok $ok, $msg;
+
+# check mail that went out doesn't contain the logo
+ at mails = RT::Test->fetch_caught_mails;
+is scalar @mails, 1, "got one outgoing email";
+$mail = shift @mails;
+like $mail, qr/RT-Attach: $LogoId/, "found header we expected";
+unlike $mail, qr/RT-Attachment: \d+\/\d+\/$LogoId/, "lacks RT-Attachment header";
+unlike $mail, qr/filename=.?\Q$LogoName\E.?/, "lacks filename";

-----------------------------------------------------------------------


More information about the rt-commit mailing list