From 02ef3db41eba92dbb3d058bf31b7833c0a9c774d Mon Sep 17 00:00:00 2001 From: Hakim Cassimally Date: Tue, 10 Dec 2013 13:03:56 +0000 Subject: [Zurich] fix moderated statistics to include hidden As per comments after testing mysociety/FixMyStreet-Commercial#459, the business logic of the actual state changes is fine, but the stats do also then need to include hidden records. --- t/cobrand/zurich.t | 180 +++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 127 insertions(+), 53 deletions(-) (limited to 't/cobrand') diff --git a/t/cobrand/zurich.t b/t/cobrand/zurich.t index a03099aac..8dde27b02 100644 --- a/t/cobrand/zurich.t +++ b/t/cobrand/zurich.t @@ -26,6 +26,20 @@ sub send_reports_for_zurich { FixMyStreet::App->model('DB::Problem')->send_reports('zurich'); }; } +sub reset_report_state { + my ($report, $created) = @_; + $report->discard_changes; + my $extra = $report->extra; + delete $extra->{moderated_overdue}; + delete $extra->{subdiv_overdue}; + delete $extra->{closed_overdue}; + $report->update({ + extra => { %$extra }, + state => 'unconfirmed', + $created ? ( created => $created ) : (), + }); +} + use FixMyStreet::TestMech; my $mech = FixMyStreet::TestMech->new; @@ -143,73 +157,131 @@ subtest "changing of categories" => sub { my $new_comment = $comments_rs->first(); is( $new_comment->text, "Weitergeleitet von Cat1 an Cat2", "category change comment created" ); - # restore report to original state. + # restore report to original category. $report->update({category => $original_category }); }; -ok ( ! exists ${$report->extra}{moderated_overdue}, 'Report currently unmoderated' ); +sub get_moderated_count { + # my %date_params = ( ); + # my $moderated = FixMyStreet::App->model('DB::Problem')->search({ + # extra => { like => '%moderated_overdue,I1:0%' }, %date_params } )->count; + # return $moderated; -FixMyStreet::override_config { - ALLOWED_COBRANDS => [ 'zurich' ], -}, sub { - $mech->get_ok( '/admin/report_edit/' . $report->id ); - $mech->content_contains( 'Unbestätigt' ); # Unconfirmed email - $mech->submit_form_ok( { with_fields => { state => 'confirmed' } } ); - $mech->get_ok( '/report/' . $report->id ); -}; -$mech->content_contains('Aufgenommen'); -$mech->content_contains('Test Test'); -$mech->content_lacks('photo/' . $report->id . '.jpeg'); -$mech->email_count_is(0); + # use a separate mech to avoid stomping on test state + my $mech = FixMyStreet::TestMech->new; + my $user = $mech->log_in_ok( 'super@example.org' ); -$report->discard_changes; -is ( $report->extra->{moderated_overdue}, 0, 'Report now marked moderated' ); + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'zurich' ], + }, sub { + $mech->get( '/admin/stats' ); + }; + if ($mech->content =~/Innerhalb eines Arbeitstages moderiert: (\d+)/) { + return $1; + } + else { + fail "Could not get moderation results"; + return undef; + } +} -# Set state back to 10 days ago so that report is overdue -my $created = $report->created; -$report->update({ - state => 'unconfirmed', - created => $created->subtract(days => 10), -}); +subtest "report_edit" => sub { -FixMyStreet::override_config { - ALLOWED_COBRANDS => [ 'zurich' ], -}, sub { - $mech->get_ok( '/admin/report_edit/' . $report->id ); - $mech->submit_form_ok( { with_fields => { state => 'confirmed' } } ); - $mech->get_ok( '/report/' . $report->id ); -}; -$report->discard_changes; -is ( $report->extra->{moderated_overdue}, 0, 'Report still not overdue (subsequent time)' ); + ok ( ! exists ${$report->extra}{moderated_overdue}, 'Report currently unmoderated' ); -$report->update({ created => $created }); # reset - -# delete the {moderated_overdue} so that changing status will now reset moderation -{ - my $extra = $report->extra; - delete $extra->{moderated_overdue}; - $report->update({ - extra => { %$extra }, - state => 'unconfirmed', - }); -} + is get_moderated_count(), 0; -FixMyStreet::override_config { - ALLOWED_COBRANDS => [ 'zurich' ], -}, sub { - $mech->get_ok( '/admin/report_edit/' . $report->id ); - $mech->submit_form_ok( { with_fields => { state => 'confirmed' } } ); - $mech->get_ok( '/report/' . $report->id ); + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'zurich' ], + }, sub { + $mech->get_ok( '/admin/report_edit/' . $report->id ); + $mech->content_contains( 'Unbestätigt' ); # Unconfirmed email + $mech->submit_form_ok( { with_fields => { state => 'confirmed' } } ); + $mech->get_ok( '/report/' . $report->id ); + }; + + $mech->content_contains('Aufgenommen'); + $mech->content_contains('Test Test'); + $mech->content_lacks('photo/' . $report->id . '.jpeg'); + $mech->email_count_is(0); + + $report->discard_changes; + + is ( $report->extra->{moderated_overdue}, 0, 'Report now marked moderated' ); + is get_moderated_count(), 1; + + + # Set state back to 10 days ago so that report is overdue + my $created = $report->created; + reset_report_state($report, $created->clone->subtract(days => 10)); + + is get_moderated_count(), 0; + + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'zurich' ], + }, sub { + $mech->get_ok( '/admin/report_edit/' . $report->id ); + $mech->submit_form_ok( { with_fields => { state => 'confirmed' } } ); + $mech->get_ok( '/report/' . $report->id ); + }; + $report->discard_changes; + is ( $report->extra->{moderated_overdue}, 1, 'moderated_overdue set correctly when overdue' ); + is get_moderated_count(), 0, 'Moderated count not increased when overdue'; + + reset_report_state($report, $created); + + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'zurich' ], + }, sub { + $mech->get_ok( '/admin/report_edit/' . $report->id ); + $mech->submit_form_ok( { with_fields => { state => 'confirmed' } } ); + $mech->get_ok( '/report/' . $report->id ); + }; + $report->discard_changes; + is ( $report->extra->{moderated_overdue}, 0, 'Marking confirmed sets moderated_overdue' ); + is ( $report->extra->{closed_overdue}, undef, 'Marking confirmed does NOT set closed_overdue' ); + is get_moderated_count(), 1; + + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'zurich' ], + }, sub { + $mech->get_ok( '/admin/report_edit/' . $report->id ); + $mech->submit_form_ok( { with_fields => { state => 'hidden' } } ); + $mech->get_ok( '/admin/report_edit/' . $report->id ); + }; + $report->discard_changes; + is ( $report->extra->{moderated_overdue}, 0, 'Still marked moderated_overdue' ); + is ( $report->extra->{closed_overdue}, 0, 'Marking hidden also set closed_overdue' ); + is get_moderated_count(), 1, 'Check still counted moderated' + or diag $report->get_column('extra'); + + reset_report_state($report); + + is ( $report->extra->{moderated_overdue}, undef, 'Sanity check' ); + is get_moderated_count(), 0; + + # Check that setting to 'hidden' also triggers moderation + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'zurich' ], + }, sub { + $mech->get_ok( '/admin/report_edit/' . $report->id ); + $mech->submit_form_ok( { with_fields => { state => 'hidden' } } ); + $mech->get_ok( '/admin/report_edit/' . $report->id ); + }; + $report->discard_changes; + is ( $report->extra->{moderated_overdue}, 0, 'Marking hidden from scratch sets moderated_overdue' ); + is ( $report->extra->{closed_overdue}, 0, 'Marking hidden from scratch also set closed_overdue' ); + is get_moderated_count(), 1; + + reset_report_state($report); }; -$report->discard_changes; -is ( $report->extra->{moderated_overdue}, 1, 'Report marked as moderated_overdue' ); FixMyStreet::override_config { ALLOWED_COBRANDS => [ 'zurich' ], }, sub { # Photo publishing $mech->get_ok( '/admin/report_edit/' . $report->id ); - $mech->submit_form_ok( { with_fields => { publish_photo => 1 } } ); + $mech->submit_form_ok( { with_fields => { state => 'confirmed', publish_photo => 1 } } ); $mech->get_ok( '/report/' . $report->id ); $mech->content_contains('photo/' . $report->id . '.jpeg'); @@ -549,8 +621,10 @@ subtest "test stats" => sub { }; is $mech->res->code, 200, "superuser should be able to see stats page"; - $mech->content_contains('Innerhalb eines Arbeitstages moderiert: 1'); - $mech->content_contains('Innerhalb von fünf Arbeitstagen abgeschlossen: 1'); + $mech->content_contains('Innerhalb eines Arbeitstages moderiert: 2'); # now including hidden + $mech->content_contains('Innerhalb von fünf Arbeitstagen abgeschlossen: 2'); + # my @data = $mech->content =~ /(?:moderiert|abgeschlossen): \d+/g; + # diag Dumper(\@data); use Data::Dumper; $mech->log_out_ok; }; -- cgit v1.2.3 From 730abb82c55b7fe15aa9fe8c05ed1af6b522dc0c Mon Sep 17 00:00:00 2001 From: Hakim Cassimally Date: Tue, 10 Dec 2013 16:25:10 +0000 Subject: [Zurich] also allow hidden reports for closed_overdue --- t/cobrand/zurich.t | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 't/cobrand') diff --git a/t/cobrand/zurich.t b/t/cobrand/zurich.t index 8dde27b02..6fd6677f5 100644 --- a/t/cobrand/zurich.t +++ b/t/cobrand/zurich.t @@ -180,7 +180,7 @@ sub get_moderated_count { return $1; } else { - fail "Could not get moderation results"; + fail sprintf "Could not get moderation results (%d)", $mech->status; return undef; } } @@ -622,7 +622,7 @@ subtest "test stats" => sub { is $mech->res->code, 200, "superuser should be able to see stats page"; $mech->content_contains('Innerhalb eines Arbeitstages moderiert: 2'); # now including hidden - $mech->content_contains('Innerhalb von fünf Arbeitstagen abgeschlossen: 2'); + $mech->content_contains('Innerhalb von fünf Arbeitstagen abgeschlossen: 3'); # my @data = $mech->content =~ /(?:moderiert|abgeschlossen): \d+/g; # diag Dumper(\@data); use Data::Dumper; -- cgit v1.2.3 From 52fcc8addf58e5f1cb5ce87d1ec2d15ccfc4cd81 Mon Sep 17 00:00:00 2001 From: Hakim Cassimally Date: Tue, 10 Dec 2013 16:59:56 +0000 Subject: [Zurich] testing - create superuser earlier So that tests pass on clean dataset such as travis... --- t/cobrand/zurich.t | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 't/cobrand') diff --git a/t/cobrand/zurich.t b/t/cobrand/zurich.t index 6fd6677f5..fb6f172fe 100644 --- a/t/cobrand/zurich.t +++ b/t/cobrand/zurich.t @@ -79,6 +79,13 @@ $external_body->send_method( 'Zurich' ); $external_body->endpoint( 'external_body@example.org' ); $external_body->update; +subtest "set up superuser" => sub { + my $superuser = $mech->log_in_ok( 'super@example.org' ); + # a user from body $zurich is a superuser, as $zurich has no parent id! + $superuser->update({ from_body => $zurich->id }); + $mech->log_out_ok; +}; + my @reports = $mech->create_problems_for_body( 1, 2, 'Test', { state => 'unconfirmed', confirmed => undef, @@ -486,8 +493,6 @@ $mech->log_out_ok; subtest "only superuser can see stats" => sub { $user = $mech->log_in_ok( 'super@example.org' ); - # a user from body $zurich is a superuser, as $zurich has no parent id! - $user->update({ from_body => $zurich->id }); FixMyStreet::override_config { ALLOWED_COBRANDS => [ 'zurich' ], -- cgit v1.2.3