| 1 | package Email::Stuffer::TestLinks; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | our $VERSION = 0.020; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Test::Most; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Mojolicious 6.00; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Mojo::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Email::Stuffer; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Class::Method::Modifiers qw/ install_modifier /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | use Email::Stuffer::TestLinks; | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =head1 NAME | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | Email::Stuffer::TestLinks - validates links in HTML emails sent by | ||||||||||||||||||||||||||||
| 21 | Email::Stuffer>send_or_die() | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | When this module is included in a test, it parses HTML links (<a href="xyz"...) | ||||||||||||||||||||||||||||
| 26 | in every email sent through Email::Stuffer->send_or_die(). Each URI must get a | ||||||||||||||||||||||||||||
| 27 | successful response code (200 range) and the returned pagetitle must not contain | ||||||||||||||||||||||||||||
| 28 | 'error' or 'not found'. | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =cut | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | install_modifier 'Email::Stuffer', after => send_or_die => sub { | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=26.5
| |||||||||||||||||||||||||||||
| 35 | 2 | my $ua = Mojo::UserAgent->new(max_redirects => 10, connect_timeout => 5); | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 2 | my %urls; | |||||||||||||||||||||||||||
| 38 | $self->email->walk_parts( | ||||||||||||||||||||||||||||
| 39 | sub { | ||||||||||||||||||||||||||||
| 40 | 2 | ▶ | my ($part) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.6
| |||||||||||||||||||||||||||||
| 41 | 2 | ▶ | return unless ($part->content_type && $part->content_type =~ /text\/html/i); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$part->content_type and $part->content_type =~ m[text/html]i
MC/DC0%0/2
$part->content_type and $part->content_type =~ m[text/html]i
$part->content_type
$part->content_type =~ m[text/html]i
Truth table33%1/3
A and B
A= $part->content_typeB= $part->content_type =~ m[text/html]i
| |||||||||||||||||||||||||||||
| 42 | 2 | my $dom = Mojo::DOM->new($part->body); | |||||||||||||||||||||||||||
| 43 | 2 | my $links = $dom->find('a')->map(attr => 'href')->compact; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | # Exclude anchors, mailto | ||||||||||||||||||||||||||||
| 46 | 2 | $urls{$_} = 1 for (grep { !/^mailto:/ } @$links); | |||||||||||||||||||||||||||
| 47 | 2 | }); | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 2 | for my $url (sort keys %urls) { | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 1 | my $err = ''; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 1 | ▶ | if ($url =~ /^[#\/]/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 0 | $err = "$url is not a valid URL for an email"; | |||||||||||||||||||||||||||
| 55 | } else { | ||||||||||||||||||||||||||||
| 56 | 1 | my $tx = $ua->get($url); | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 1 | ▶ | if ($tx->success) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 0 | my $res = $tx->result; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 0 | ▶ | if ($res->code !~ /^2\d\d/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 0 | $err = "HTTP code was " . $res->code; | |||||||||||||||||||||||||||
| 63 | } else { | ||||||||||||||||||||||||||||
| 64 | 0 | my $title = $res->dom->at('title')->text; | |||||||||||||||||||||||||||
| 65 | 0 | ▶ | $err = "Page title contains text '$1'" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | if $title =~ /(error|not found)/i; | ||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | } else { | ||||||||||||||||||||||||||||
| 69 | 0 | $err = "Could not retrieve URL: " . $tx->error->{message}; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | 0 | ▶ | ok(!$err, "Link in email works ($url)") or diag($err); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 2 | }; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 2 | 1; | |||||||||||||||||||||||||||