aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Somerville <matthew@mysociety.org>2016-03-04 18:46:53 +0000
committerMatthew Somerville <matthew@mysociety.org>2016-03-04 18:46:53 +0000
commit38a75cdb259b37e42313e8202eee6bee3f5c4868 (patch)
treeec2f8efa27856d10dfc774e478d3dee4d3c53c1f
parent2fc65ef99e7591f4f570c5102f46583a0ce9a793 (diff)
Remember user's last anonymous state.
If a user is logged in, use their last report/update to set the default anonymity state.
-rw-r--r--perllib/FixMyStreet/DB/Result/User.pm10
-rw-r--r--t/app/model/user.t61
-rw-r--r--templates/web/base/report/new/form_user_loggedin.html2
-rw-r--r--templates/web/base/report/new/form_user_loggedout_by_email.html2
-rw-r--r--templates/web/base/report/update/form_name.html3
-rw-r--r--templates/web/base/user/_anonymity.html11
-rw-r--r--templates/web/eastsussex/report/new/after_name.html4
-rw-r--r--templates/web/eastsussex/report/update-form.html4
8 files changed, 88 insertions, 9 deletions
diff --git a/perllib/FixMyStreet/DB/Result/User.pm b/perllib/FixMyStreet/DB/Result/User.pm
index ddf986c47..6bce415a6 100644
--- a/perllib/FixMyStreet/DB/Result/User.pm
+++ b/perllib/FixMyStreet/DB/Result/User.pm
@@ -104,6 +104,16 @@ __PACKAGE__->add_columns(
use mySociety::EmailUtil;
+sub latest_anonymity {
+ my $self = shift;
+ my $p = $self->problems->search(undef, { order_by => { -desc => 'id' } } )->first;
+ my $c = $self->comments->search(undef, { order_by => { -desc => 'id' } } )->first;
+ my $p_created = $p ? $p->created->epoch : 0;
+ my $c_created = $c ? $c->created->epoch : 0;
+ my $obj = $p_created >= $c_created ? $p : $c;
+ return $obj ? $obj->anonymous : 0;
+}
+
=head2 check_for_errors
$error_hashref = $user->check_for_errors();
diff --git a/t/app/model/user.t b/t/app/model/user.t
new file mode 100644
index 000000000..24e5d2d98
--- /dev/null
+++ b/t/app/model/user.t
@@ -0,0 +1,61 @@
+use strict;
+use warnings;
+
+use Test::More;
+
+use FixMyStreet::TestMech;
+use FixMyStreet::DB;
+
+my $mech = FixMyStreet::TestMech->new();
+$mech->log_in_ok('test@example.com');
+
+my ($problem) = $mech->create_problems_for_body(1, '2504', 'Title', { anonymous => 'f' });
+is $problem->user->latest_anonymity, 0, "User's last report was not anonymous";
+
+FixMyStreet::override_config {
+ ALLOWED_COBRANDS => [ { fixmystreet => '.' } ],
+ MAPIT_URL => 'http://mapit.mysociety.org/',
+}, sub {
+ $mech->get_ok('/around?pc=sw1a1aa');
+ $mech->follow_link_ok( { text_regex => qr/skip this step/i, }, "follow 'skip this step' link" );
+ $mech->content_like(qr/may_show_name[^>]*checked/);
+};
+
+($problem) = $mech->create_problems_for_body(1, '2504', 'Title', { anonymous => 't' });
+is $problem->user->latest_anonymity, 1, "User's last report was anonymous";
+
+create_update($problem, anonymous => 'f');
+is $problem->user->latest_anonymity, 0, "User's last update was not anonyous";
+
+create_update($problem, anonymous => 't');
+is $problem->user->latest_anonymity, 1, "User's last update was anonymous";
+
+FixMyStreet::override_config {
+ ALLOWED_COBRANDS => [ { fixmystreet => '.' } ],
+ MAPIT_URL => 'http://mapit.mysociety.org/',
+}, sub {
+ $mech->get_ok('/around?pc=sw1a1aa');
+ $mech->follow_link_ok( { text_regex => qr/skip this step/i, }, "follow 'skip this step' link" );
+ $mech->content_like(qr/may_show_name[^>c]*>/);
+};
+
+END {
+ $mech->delete_user( $problem->user ) if $problem;
+ done_testing();
+}
+
+sub create_update {
+ my ($problem, %params) = @_;
+ my $dt = DateTime->now()->add(hours => 1);
+ return FixMyStreet::App->model('DB::Comment')->find_or_create({
+ problem_id => $problem->id,
+ user_id => $problem->user_id,
+ name => 'Other User',
+ mark_fixed => 'false',
+ text => 'This is some update text',
+ state => 'confirmed',
+ anonymous => 'f',
+ created => $dt->ymd . ' ' . $dt->hms,
+ %params,
+ });
+}
diff --git a/templates/web/base/report/new/form_user_loggedin.html b/templates/web/base/report/new/form_user_loggedin.html
index 848d331d4..a39200c72 100644
--- a/templates/web/base/report/new/form_user_loggedin.html
+++ b/templates/web/base/report/new/form_user_loggedin.html
@@ -4,8 +4,8 @@
<input disabled type="text" value="[% c.user.email | html %]">
[% INCLUDE 'report/new/extra_name.html' %]
+ [% PROCESS 'user/_anonymity.html' anonymous = report.anonymous %]
- [% name_public = report.anonymous==0 OR (c.cobrand.default_show_name AND report.anonymous=='') %]
<label for="form_name">[% loc('Name') %]
[% TRY %]
[% INCLUDE 'report/new/after_name.html' %]
diff --git a/templates/web/base/report/new/form_user_loggedout_by_email.html b/templates/web/base/report/new/form_user_loggedout_by_email.html
index a3e36dfa3..1ad07c540 100644
--- a/templates/web/base/report/new/form_user_loggedout_by_email.html
+++ b/templates/web/base/report/new/form_user_loggedout_by_email.html
@@ -2,8 +2,8 @@
<h5>[% loc('<strong>No</strong> Let me confirm my report by email') %]</h5>
[% INCLUDE 'report/new/extra_name.html' %]
+ [% PROCESS 'user/_anonymity.html' anonymous = report.anonymous %]
- [% name_public = report.anonymous==0 OR (c.cobrand.default_show_name AND report.anonymous=='') %]
<label for="form_name">[% loc('Name') %]
[% TRY %]
[% INCLUDE 'report/new/after_name.html' %]
diff --git a/templates/web/base/report/update/form_name.html b/templates/web/base/report/update/form_name.html
index 8311922e3..4cb3e516c 100644
--- a/templates/web/base/report/update/form_name.html
+++ b/templates/web/base/report/update/form_name.html
@@ -1,6 +1,7 @@
[% INCLUDE 'report/new/extra_name.html' %]
-[% name_public = update.anonymous==0 OR (c.cobrand.default_show_name AND update.anonymous=='') %]
+[% PROCESS 'user/_anonymity.html' anonymous = update.anonymous %]
+
<label for="form_name">[% loc('Name') %]</label>
[% IF field_errors.name %]
<p class='form-error'>[% field_errors.name %]</p>
diff --git a/templates/web/base/user/_anonymity.html b/templates/web/base/user/_anonymity.html
new file mode 100644
index 000000000..cc3630f16
--- /dev/null
+++ b/templates/web/base/user/_anonymity.html
@@ -0,0 +1,11 @@
+[%
+ IF c.cobrand.default_show_name AND anonymous=='';
+ IF c.user_exists;
+ SET name_public = NOT c.user.latest_anonymity;
+ ELSE;
+ SET name_public = 1;
+ END;
+ ELSE;
+ SET name_public = anonymous==0;
+ END
+%]
diff --git a/templates/web/eastsussex/report/new/after_name.html b/templates/web/eastsussex/report/new/after_name.html
index 26c213c1d..8b0fc9816 100644
--- a/templates/web/eastsussex/report/new/after_name.html
+++ b/templates/web/eastsussex/report/new/after_name.html
@@ -1,5 +1,3 @@
- <span id="name-public-warning" class="label-warning public-warning name-warning"
- [% 'style="display:none"' UNLESS name_public %]
- >
+ <span id="name-public-warning" class="label-warning public-warning name-warning">
[% loc('public') %]
</span>
diff --git a/templates/web/eastsussex/report/update-form.html b/templates/web/eastsussex/report/update-form.html
index c2e445f22..e071ff8ce 100644
--- a/templates/web/eastsussex/report/update-form.html
+++ b/templates/web/eastsussex/report/update-form.html
@@ -193,9 +193,7 @@
[% name_public = update.anonymous==0 OR (c.cobrand.default_show_name AND update.anonymous=='') %]
<label for="form_name">[% loc('Name') %]
- <span id="name-public-warning" class="label-warning public-warning name-warning"
- [% 'style="display:none"' UNLESS name_public %]
- >
+ <span id="name-public-warning" class="label-warning public-warning name-warning">
[% loc('public') %]
</span>
</label>