diff options
-rw-r--r-- | perllib/FixMyStreet/DB/ResultSet/AlertType.pm | 18 | ||||
-rw-r--r-- | t/app/controller/alert_new.t | 18 | ||||
-rw-r--r-- | t/app/model/alert_type.t | 47 |
3 files changed, 63 insertions, 20 deletions
diff --git a/perllib/FixMyStreet/DB/ResultSet/AlertType.pm b/perllib/FixMyStreet/DB/ResultSet/AlertType.pm index 2d206d84e..468df2654 100644 --- a/perllib/FixMyStreet/DB/ResultSet/AlertType.pm +++ b/perllib/FixMyStreet/DB/ResultSet/AlertType.pm @@ -94,7 +94,23 @@ sub email_alerts ($) { } # this is currently only for new_updates if ($row->{item_text}) { - $data{problem_url} = $url . "/report/" . $row->{id}; + if ( $row->{alert_user_id} == $row->{user_id} ) { + # This is an alert to the same user who made the report - make this a login link + my $user = FixMyStreet::App->model('DB::User')->find( { + id => $row->{alert_user_id} + } ); + $data{alert_email} = $user->email; + my $token_obj = FixMyStreet::App->model('DB::Token')->create( { + scope => 'email_sign_in', + data => { + email => $user->email, + r => 'report/' . $row->{id}, + } + } ); + $data{problem_url} = $url . "/M/" . $token_obj->token; + } else { + $data{problem_url} = $url . "/report/" . $row->{id}; + } $data{data} .= $row->{item_name} . ' : ' if $row->{item_name} && !$row->{item_anonymous}; $data{data} .= $row->{item_text} . "\n\n------\n\n"; # this is ward and council problems diff --git a/t/app/controller/alert_new.t b/t/app/controller/alert_new.t index cb787d959..c89f37028 100644 --- a/t/app/controller/alert_new.t +++ b/t/app/controller/alert_new.t @@ -390,7 +390,7 @@ subtest "Test normal alert signups and that alerts are sent" => sub { used_map => 1, name => $user1->name, anonymous => 0, - state => 'confirmed', + state => 'fixed - user', confirmed => $dt, lastupdate => $dt, whensent => $dt->clone->add( minutes => 5 ), @@ -430,7 +430,7 @@ subtest "Test normal alert signups and that alerts are sent" => sub { problem_id => $report_id, user_id => $user2->id, name => 'Anonymous User', - mark_fixed => 'false', + mark_fixed => 'true', text => 'This is some more update text', state => 'confirmed', confirmed => $dt->clone->add( hours => 8 ), @@ -450,13 +450,23 @@ subtest "Test normal alert signups and that alerts are sent" => sub { $count++ if $_->body =~ /The following nearby problems have been added:/; $count++ if $_->body =~ /\s+-\s+Testing/; } - is $count, 5, 'Five emails with the right things in them'; + is $count, 5, 'Three emails, with five matching lines in them'; my $email = $emails[0]; like $email->body, qr/Other User/, 'Update name given'; unlike $email->body, qr/Anonymous User/, 'Update name not given'; - my ( $url, $url_token ) = $emails[0]->body =~ m{http://\S+(/A/(\S+))}; + # The update alert was to the problem reporter, so has a login update URL + $mech->get_ok( "/report/$report_id" ); + $mech->content_lacks( 'has not been fixed' ); + my ($url) = $email->body =~ m{(http://\S+/M/\S+)}; + ok $url, "extracted update url '$url'"; + $mech->get_ok( $url ); + is $mech->uri->path, "/report/" . $report_id, "redirected to report page"; + $mech->content_contains( 'has not been fixed' ); + $mech->logged_in_ok; + + ($url) = $emails[0]->body =~ m{http://\S+(/A/\S+)}; $mech->get_ok( $url ); $mech->content_contains('successfully deleted'); diff --git a/t/app/model/alert_type.t b/t/app/model/alert_type.t index 7df4c44c0..c592e9d3f 100644 --- a/t/app/model/alert_type.t +++ b/t/app/model/alert_type.t @@ -22,6 +22,10 @@ my $user2 = ->find_or_create( { email => 'commenter@example.com', name => 'Commenter' } ); ok $user2, "created comment user"; +my $user3 = + FixMyStreet::App->model('DB::User') + ->find_or_create( { email => 'bystander@example.com', name => 'Bystander' } ); +ok $user3, "created bystander"; my $dt = DateTime->new( year => 2011, @@ -96,6 +100,16 @@ my $alert = FixMyStreet::App->model('DB::Alert')->find_or_create( } ); +my $alert3 = FixMyStreet::App->model('DB::Alert')->find_or_create( + { + user => $user3, + parameter => $report_id, + alert_type => 'new_updates', + whensubscribed => $dt->ymd . ' ' . $dt->hms, + confirmed => 1, + } +); + for my $test ( { state => 'closed', @@ -115,7 +129,7 @@ for my $test ( my $sent = FixMyStreet::App->model('DB::AlertSent')->search( { - alert_id => $alert->id, + alert_id => [ $alert->id, $alert3->id ], parameter => $comment->id, } )->delete; @@ -125,18 +139,25 @@ for my $test ( FixMyStreet::App->model('DB::AlertType')->email_alerts(); - $mech->email_count_is( 1 ); - my $email = $mech->get_email; + $mech->email_count_is( 2 ); + my @emails = $mech->get_email; my $msg = $test->{msg}; - my $body = $email->body; - - like $body, qr/$msg/, 'email says problem is ' . $test->{state}; - like $body, qr{report/$report_id}, 'contains problem url'; - like $body, qr/This is some update text/, 'contains update text'; - unlike $body, qr/This is other update text/, 'does not contains other update text'; + for my $email (@emails) { + my $body = $email->body; + my $to = $email->header('To'); + + like $body, qr/$msg/, 'email says problem is ' . $test->{state}; + if ($to eq $user->email) { + like $body, qr{/M/}, 'contains problem login url'; + } elsif ($to eq $user3->email) { + like $body, qr{/report/$report_id}, 'contains problem url'; + } + like $body, qr/This is some update text/, 'contains update text'; + unlike $body, qr/This is other update text/, 'does not contains other update text'; - my $comments = $body =~ s/(------)//gs; - is $comments, 1, 'only 1 update'; + my $comments = $body =~ s/(------)//gs; + is $comments, 1, 'only 1 update'; + } }; } @@ -166,7 +187,6 @@ subtest "correct text for title after URL" => sub { )->delete; FixMyStreet::App->model('DB::AlertType')->email_alerts(); - $mech->email_count_is( 1 ); my $email = $mech->get_email; (my $title = $report->title) =~ s/ /\\s+/; my $body = $email->body; @@ -300,7 +320,6 @@ foreach my $test ( FixMyStreet::App->model('DB::AlertType')->email_alerts(); - $mech->email_count_is( 1 ); my $email = $mech->get_email; my $body = $email->body; @@ -406,7 +425,6 @@ subtest "check alerts from cobrand send main site url for alerts for different c FixMyStreet::App->model('DB::AlertType')->email_alerts(); - $mech->email_count_is( 1 ); my $email = $mech->get_email; my $body = $email->body; @@ -444,7 +462,6 @@ subtest "check local alerts from cobrand send main site url for alerts for diffe FixMyStreet::App->model('DB::AlertType')->email_alerts(); - $mech->email_count_is( 1 ); my $email = $mech->get_email; my $body = $email->body; |