diff options
author | Dave Arter <davea@mysociety.org> | 2016-08-01 12:03:45 +0100 |
---|---|---|
committer | Dave Arter <davea@mysociety.org> | 2016-08-01 12:03:45 +0100 |
commit | 1af9684e54aedb7f9935cdc4d38a28c61ec7d1f5 (patch) | |
tree | d6b044f54e070f8dbf265083534e849eb9ad6ead /perllib/FixMyStreet/TestMech.pm | |
parent | 14aaf6fafaa9aa8736f49851e95fa2c3c566c056 (diff) | |
parent | 27e0c74321f48f9997745bf00647e3958f34d8e2 (diff) |
Merge branch '1281-html-emails'
Diffstat (limited to 'perllib/FixMyStreet/TestMech.pm')
-rw-r--r-- | perllib/FixMyStreet/TestMech.pm | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/perllib/FixMyStreet/TestMech.pm b/perllib/FixMyStreet/TestMech.pm index 937780a31..5f4a6ceed 100644 --- a/perllib/FixMyStreet/TestMech.pm +++ b/perllib/FixMyStreet/TestMech.pm @@ -221,6 +221,48 @@ sub get_email { return $emails[0]; } +sub get_text_body_from_email { + my ($mech, $email, $obj) = @_; + unless ($email) { + $email = $mech->get_email; + $mech->clear_emails_ok; + } + + my $body; + $email->walk_parts(sub { + my $part = shift; + return if $part->subparts; + return if $part->content_type !~ m{text/plain}; + $body = $obj ? $part : $part->body; + ok $body, "Found text body"; + }); + return $body; +} + +sub get_link_from_email { + my ($mech, $email, $multiple) = @_; + unless ($email) { + $email = $mech->get_email; + $mech->clear_emails_ok; + } + + my @links; + $email->walk_parts(sub { + my $part = shift; + return if $part->subparts; + return if $part->content_type !~ m{text/}; + if (@links) { + # Must be an HTML part now, first two links are in header + my @html_links = $part->body =~ m{https?://[^"]+}g; + is $links[0], $html_links[2], 'HTML link matches text link'; + } else { + @links = $part->body =~ m{https?://\S+}g; + ok @links, "Found links in email '@links'"; + } + }); + return $multiple ? @links : $links[0]; +} + =head2 get_first_email $email = $mech->get_first_email(@emails); |