aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--perllib/FixMyStreet/App/Controller/Report/Update.pm29
-rw-r--r--t/app/controller/report_updates.t73
2 files changed, 77 insertions, 25 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Report/Update.pm b/perllib/FixMyStreet/App/Controller/Report/Update.pm
index d9136698d..e1e90bb93 100644
--- a/perllib/FixMyStreet/App/Controller/Report/Update.pm
+++ b/perllib/FixMyStreet/App/Controller/Report/Update.pm
@@ -228,6 +228,8 @@ sub save_update : Private {
} else {
$update->insert;
}
+
+ return 1;
}
=head2 redirect_or_confirm_creation
@@ -243,6 +245,7 @@ sub redirect_or_confirm_creation : Private {
# If confirmed send the user straight there.
if ( $update->confirmed ) {
+ $c->forward( 'signup_for_alerts' );
my $report_uri = $c->uri_for( '/report', $update->problem_id );
$c->res->redirect($report_uri);
$c->detach;
@@ -268,6 +271,32 @@ sub redirect_or_confirm_creation : Private {
return 1;
}
+=head2 signup_for_alerts
+
+If the user has selected to be signed up for alerts then create a
+new_updates alert.
+
+NB: this does not check if they are a registered user so that should
+happen before calling this.
+
+=cut
+
+sub signup_for_alerts : Private {
+ my ( $self, $c ) = @_;
+
+ if ( $c->req->param( 'add_alert' ) ) {
+ my $alert = $c->model( 'DB::Alert' )->find_or_create(
+ user => $c->stash->{update_user},
+ alert_type => 'new_updates',
+ parameter => $c->stash->{problem}->id
+ );
+
+ $alert->update;
+ }
+
+ return 1;
+}
+
__PACKAGE__->meta->make_immutable;
1;
diff --git a/t/app/controller/report_updates.t b/t/app/controller/report_updates.t
index 8f64f1109..182f3a244 100644
--- a/t/app/controller/report_updates.t
+++ b/t/app/controller/report_updates.t
@@ -262,37 +262,60 @@ subtest "submit an update for a non registered user" => sub {
is $add_alerts, 0, 'do not sign up for alerts';
};
-subtest "submit an update for a registered user" => sub {
- # clear out comments for this problem to make
- # checking details easier later
- ok( $_->delete, 'deleted comment ' . $_->id )
- for $report->comments;
+for my $test (
+ {
+ desc => 'submit update for register user',
+ fields => {
+ rznvy => 'test@example.com',
+ update => 'update from a registered user'
+ },
+ alert => 0,
+ },
+ {
+ desc => 'submit update for register user and sign up',
+ fields => {
+ rznvy => 'test@example.com',
+ update => 'update from a registered user',
+ add_alert => 1,
+ },
+ alert => 1,
+ },
+) {
+ subtest $test->{desc} => sub {
+ # clear out comments for this problem to make
+ # checking details easier later
+ ok( $_->delete, 'deleted comment ' . $_->id )
+ for $report->comments;
- $mech->clear_emails_ok();
+ $mech->clear_emails_ok();
- $mech->log_in_ok( $user->email );
- $mech->get_ok("/report/$report_id");
+ $mech->log_in_ok( $user->email );
+ $mech->get_ok("/report/$report_id");
- $mech->submit_form_ok(
- {
- with_fields => {
- rznvy => 'test@example.com',
- update => 'update from a registered user'
- }
- },
- 'submit update'
- );
+ $mech->submit_form_ok(
+ {
+ with_fields => $test->{fields},
+ },
+ 'submit update'
+ );
- is $mech->uri->path, "/report/" . $report_id, "redirected to report page";
+ is $mech->uri->path, "/report/" . $report_id, "redirected to report page";
- $mech->email_count_is(0);
+ $mech->email_count_is(0);
- my $update = $report->comments->first;
- ok $update, 'found update';
- is $update->text, 'update from a registered user', 'update text';
- is $update->user->email, 'test@example.com', 'update user';
- is $update->state, 'confirmed', 'update confirmed';
-};
+ my $update = $report->comments->first;
+ ok $update, 'found update';
+ is $update->text, $test->{fields}->{update}, 'update text';
+ is $update->user->email, 'test@example.com', 'update user';
+ is $update->state, 'confirmed', 'update confirmed';
+
+ my $alert =
+ FixMyStreet::App->model('DB::Alert')
+ ->find( { user => $user, alert_type => 'new_updates' } );
+
+ ok $test->{alert} ? $alert : !$alert, 'not signed up for alerts';
+ };
+}
ok $comment->delete, 'deleted comment';
$mech->delete_user('commenter@example.com');