diff options
Diffstat (limited to 't/cobrand')
-rw-r--r-- | t/cobrand/bromley.t | 42 | ||||
-rw-r--r-- | t/cobrand/closest.t | 6 | ||||
-rw-r--r-- | t/cobrand/councils.t | 10 | ||||
-rw-r--r-- | t/cobrand/fixmybarangay.t | 140 | ||||
-rw-r--r-- | t/cobrand/get_body_sender.t | 43 | ||||
-rw-r--r-- | t/cobrand/get_council_sender.t | 30 | ||||
-rw-r--r-- | t/cobrand/hart.t | 16 | ||||
-rw-r--r-- | t/cobrand/loading.t | 103 | ||||
-rw-r--r-- | t/cobrand/zurich.t | 685 |
9 files changed, 1008 insertions, 67 deletions
diff --git a/t/cobrand/bromley.t b/t/cobrand/bromley.t new file mode 100644 index 000000000..91a7038ec --- /dev/null +++ b/t/cobrand/bromley.t @@ -0,0 +1,42 @@ +use strict; +use warnings; +use Test::More; + +use FixMyStreet::TestMech; +my $mech = FixMyStreet::TestMech->new; + +# Create test data +my $user = $mech->create_user_ok( 'bromley@example.com' ); +my $body = $mech->create_body_ok( 2482, 'Bromley' ); + +my @reports = $mech->create_problems_for_body( 1, 2482, 'Test', { + cobrand => 'bromley', + user => $user, +}); +my $report = $reports[0]; + +for my $update ('in progress', 'unable to fix') { + FixMyStreet::App->model('DB::Comment')->find_or_create( { + problem_state => $update, + problem_id => $report->id, + user_id => $user->id, + name => 'User', + mark_fixed => 'f', + text => "This update marks it as $update", + state => 'confirmed', + confirmed => 'now()', + anonymous => 'f', + } ); +} + +# Test Bromley special casing of 'unable to fix' +$mech->get_ok( '/report/' . $report->id ); +$mech->content_contains( 'marks it as in progress' ); +$mech->content_contains( 'marked as in progress' ); +$mech->content_contains( 'marks it as unable to fix' ); +$mech->content_contains( 'marked as no further action' ); + +# Clean up +$mech->delete_user($user); +$mech->delete_problems_for_body( 2482 ); +done_testing(); diff --git a/t/cobrand/closest.t b/t/cobrand/closest.t index 464c95e67..d06f7e9a0 100644 --- a/t/cobrand/closest.t +++ b/t/cobrand/closest.t @@ -5,6 +5,9 @@ use Test::More; use mySociety::Locale; use FixMyStreet::App; +use FixMyStreet::TestMech; + +my $mech = FixMyStreet::TestMech->new; use_ok 'FixMyStreet::Cobrand'; @@ -29,7 +32,7 @@ my $dt = DateTime->new( my $report = FixMyStreet::App->model('DB::Problem')->find_or_create( { postcode => 'SW1A 1AA', - council => '2504', + bodies_str => '2504', areas => ',105255,11806,11828,2247,2504,', category => 'Other', title => 'Test 2', @@ -81,4 +84,5 @@ SKIP: { } # all done +$mech->delete_user( $user ); done_testing(); diff --git a/t/cobrand/councils.t b/t/cobrand/councils.t index 8fb10cfbe..0cf25ebdf 100644 --- a/t/cobrand/councils.t +++ b/t/cobrand/councils.t @@ -6,13 +6,13 @@ use FixMyStreet::TestMech; my $mech = FixMyStreet::TestMech->new; foreach my $council (qw/southampton reading bromley/) { - SKIP: { - skip( "Need '$council' in ALLOWED_COBRANDS config", 3 ) - unless FixMyStreet::Cobrand->exists($council); + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ $council ], + }, sub { ok $mech->host("$council.fixmystreet.com"), "change host to $council"; $mech->get_ok('/'); - $mech->content_like( qr/$council/i ); - } + $mech->content_like( qr/\u$council/ ); + }; } done_testing(); diff --git a/t/cobrand/fixmybarangay.t b/t/cobrand/fixmybarangay.t new file mode 100644 index 000000000..b996fa0a6 --- /dev/null +++ b/t/cobrand/fixmybarangay.t @@ -0,0 +1,140 @@ +use strict; +use warnings; +use Test::More; + +BEGIN { + use FixMyStreet; + FixMyStreet->test_mode(1); +} + +use FixMyStreet::TestMech; +my $mech = FixMyStreet::TestMech->new; + +# Front page test + +ok $mech->host("www.fixmybarangay.com"), "change host to FixMyBarangay"; +FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'fixmybarangay' ], +}, sub { + $mech->get_ok('/'); +}; +$mech->content_like( qr/FixMyBarangay/ ); + +# Set up bodies + +my $luz = $mech->create_body_ok( 1, 'Bgy Luz' ); +$luz->update( { send_method => 'Email' } ); + +my $bsn = $mech->create_body_ok( 2, 'Bgy BSN' ); +$bsn->update( { send_method => 'Email' } ); + +my $dps = $mech->create_body_ok( 3, 'DPS' ); +$dps->update( { send_method => 'Open311', endpoint => 'http://dps.endpoint.example.com', jurisdiction => 'FMB', api_key => 'test' } ); +FixMyStreet::App->model('DB::BodyArea')->find_or_create({ area_id => 1, body_id => 3 }); +FixMyStreet::App->model('DB::BodyArea')->find_or_create({ area_id => 2, body_id => 3 }); + +# Create contacts for these bodies +# TODO: log in as a Bgy user, and create a report using the front end, +# testing that the drop-down has the right things in it, and so on. + +my %contact_params = ( + confirmed => 1, + deleted => 0, + editor => 'Test', + whenedited => \'current_timestamp', + note => 'Created for test', +); +FixMyStreet::App->model('DB::Contact')->search( { + email => { 'like', '%example.com' }, +} )->delete; +my $contact1 = FixMyStreet::App->model('DB::Contact')->find_or_create( { + %contact_params, + body_id => 1, + category => 'Streetlight (BGY)', + email => 'bgy@example.com', +} ); +my $contact2 = FixMyStreet::App->model('DB::Contact')->find_or_create( { + %contact_params, + body_id => 3, + category => 'Streetlight (DPS)', + email => 'LIGHT', +} ); + +# Create a couple of reports + +my @reports = $mech->create_problems_for_body( 1, 1, 'Test', { + cobrand => 'fixmybarangay', + category => 'Streetlight (BGY)', +}); +my $luz_report = $reports[0]; + +@reports = $mech->create_problems_for_body( 1, 3, 'Test', { + cobrand => 'fixmybarangay', + category => 'Streetlight (DPS)', +}); +my $dps_report = $reports[0]; + +$mech->get_ok( '/report/' . $luz_report->id ); +$mech->get_ok( '/report/' . $dps_report->id ); + +# Send the reports + +$mech->email_count_is(0); + +FixMyStreet::override_config { + SEND_REPORTS_ON_STAGING => 1, +}, sub { + FixMyStreet::App->model('DB::Problem')->send_reports('fixmybarangay'); +}; + +# Check BGY one sent by email +my $email = $mech->get_email; +like $email->header('Subject'), qr/Problem Report: Test Test/, 'subject looks okay'; +like $email->header('To'), qr/bgy\@example.com/, 'to line looks correct'; +$mech->clear_emails_ok; + +$luz_report->discard_changes; +$dps_report->discard_changes; +ok $luz_report->whensent, 'Luz report marked as sent'; +ok $dps_report->whensent, 'DPS report marked as sent'; +is $dps_report->send_method_used, 'Open311', 'DPS report sent via Open311'; +is $dps_report->external_id, 248, 'DPS report has right external ID'; + +my $fmb_test_email = 'luz_test_user@example.com'; +my $user = FixMyStreet::App->model('DB::User')->find_or_create( { email => $fmb_test_email, from_body => 1, password => 'fmbsecret' } ); +ok $user, "test user does exist"; + +$mech->log_out_ok; +$mech->get_ok( '/report/' . $luz_report->id ); +$mech->content_lacks( "Remove from site" ); +$mech->content_contains( "Report abuse" ); + +$mech->post_ok('/report/delete/' . $luz_report->id); +is $mech->uri->path, '/report/' . $luz_report->id, "should redirect to report page, deletion ignored"; +$luz_report->discard_changes; +is $luz_report->state, 'confirmed', 'should be confirmed'; + +$user = $mech->log_in_ok($fmb_test_email); + +FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'fixmybarangay' ], +}, sub { + $mech->get_ok( '/report/' . $luz_report->id ); +}; +$mech->content_contains( "Remove from site" ); +$mech->content_lacks( "Report abuse" ); + +$mech->form_id('remove-from-site-form'); +$mech->submit_form(); +is $mech->uri->path, '/report/' . $luz_report->id, "should redirect to report page, deletion successful"; +$luz_report->discard_changes; +is $luz_report->state, 'hidden', 'should be hidden'; + +$mech->delete_user($fmb_test_email); + +$mech->delete_problems_for_body( 1 ); +$mech->delete_problems_for_body( 3 ); + +ok $mech->host("www.fixmystreet.com"), "change host back"; + +done_testing(); diff --git a/t/cobrand/get_body_sender.t b/t/cobrand/get_body_sender.t new file mode 100644 index 000000000..964f5c0e4 --- /dev/null +++ b/t/cobrand/get_body_sender.t @@ -0,0 +1,43 @@ +use strict; +use warnings; + +use Test::More; + +use mySociety::Locale; +use FixMyStreet::App; + +use_ok 'FixMyStreet::Cobrand'; + +mySociety::Locale::gettext_domain( 'FixMyStreet' ); + +my $c = FixMyStreet::Cobrand::FixMyStreet->new(); + +FixMyStreet::App->model('DB::BodyArea')->search( { body_id => 1000 } )->delete; +FixMyStreet::App->model('DB::Body')->search( { name => 'Body of a Thousand' } )->delete; + +my $body = FixMyStreet::App->model('DB::Body')->find_or_create({ + id => 1000, + name => 'Body of a Thousand', +}); +my $body_area = $body->body_areas->find_or_create({ area_id => 1000 }); + +FixMyStreet::override_config { + MAPIT_TYPES => [ 'LBO' ], + MAPIT_URL => 'http://mapit.mysociety.org/', +}, sub { + is_deeply $c->get_body_sender( $body ), { method => 'Email' }, 'defaults to email'; + + $body_area->update({ area_id => 2481 }); # Croydon LBO + is_deeply $c->get_body_sender( $body ), { method => 'London' }, 'returns london report it if London borough'; +}; + +$body->send_method( 'TestMethod' ); +is $c->get_body_sender( $body )->{ method }, 'TestMethod', 'uses send_method in preference to London'; + +$body_area->update({ area_id => 1000 }); # Nothing +is $c->get_body_sender( $body )->{ method }, 'TestMethod', 'uses send_method in preference to Email'; + +$body_area->delete; +$body->delete; + +done_testing(); diff --git a/t/cobrand/get_council_sender.t b/t/cobrand/get_council_sender.t deleted file mode 100644 index e61f36370..000000000 --- a/t/cobrand/get_council_sender.t +++ /dev/null @@ -1,30 +0,0 @@ -use strict; -use warnings; - -use Test::More; - -use mySociety::Locale; -use FixMyStreet::App; - -use_ok 'FixMyStreet::Cobrand'; - -mySociety::Locale::gettext_domain( 'FixMyStreet' ); - -my $c = FixMyStreet::Cobrand::FixMyStreet->new(); - - -is_deeply $c->get_council_sender( '1000', { type => 'DIS' } ), { method => 'Email' }, 'defaults to email'; -is_deeply $c->get_council_sender( '1000', { type => 'LBO' } ), { method=> 'London' }, 'returns london report it if London borough'; - -my $conf = FixMyStreet::App->model('DB::Open311Conf')->find_or_create( - area_id => 1000, - endpoint => '', - send_method => 'TestMethod' -); - -is $c->get_council_sender( '1000', { type => 'LBO' } )->{ method }, 'TestMethod', 'uses send_method in preference to London'; -is $c->get_council_sender( '1000', { type => 'DIS' } )->{ method }, 'TestMethod', 'uses send_method in preference to Email'; - -$conf->delete; - -done_testing(); diff --git a/t/cobrand/hart.t b/t/cobrand/hart.t new file mode 100644 index 000000000..f4a2473eb --- /dev/null +++ b/t/cobrand/hart.t @@ -0,0 +1,16 @@ +use strict; +use warnings; +use Test::More; + +use FixMyStreet::TestMech; +my $mech = FixMyStreet::TestMech->new; + +FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'hart' ], +}, sub { + ok $mech->host("hart.fixmystreet.com"), "change host to hart"; + $mech->get_ok('/'); + $mech->content_like( qr/Hart\b/ ); +}; + +done_testing(); diff --git a/t/cobrand/loading.t b/t/cobrand/loading.t index bd83da07f..48a10293e 100644 --- a/t/cobrand/loading.t +++ b/t/cobrand/loading.t @@ -9,51 +9,92 @@ use FixMyStreet; use_ok 'FixMyStreet::Cobrand'; # check that the allowed cobrands is correctly loaded from config -{ +sub check_allowed_cobrands { + my $should = shift; + $should = [ map { { moniker => $_, host => $_ } } @$should ]; my $allowed = FixMyStreet::Cobrand->get_allowed_cobrands; - ok $allowed, "got the allowed_cobrands"; + ok $allowed, "got the allowed_cobrands"; isa_ok $allowed, "ARRAY"; - cmp_ok scalar @$allowed, '>', 1, "got more than one"; + is_deeply $allowed, $should, "allowed_cobrands matched"; } -# fake the allowed cobrands for testing -my $override = Sub::Override->new( # - 'FixMyStreet::Cobrand::_get_allowed_cobrands' => - sub { return ['emptyhomes'] } -); -is_deeply FixMyStreet::Cobrand->get_allowed_cobrands, [ { moniker => 'emptyhomes', host => 'emptyhomes' } ], - 'overidden get_allowed_cobrands'; +FixMyStreet::override_config { ALLOWED_COBRANDS => 'fixmyhouse' }, + sub { check_allowed_cobrands([ 'fixmyhouse' ]); }; +FixMyStreet::override_config { ALLOWED_COBRANDS => [ 'fixmyhouse' ] }, + sub { check_allowed_cobrands([ 'fixmyhouse' ]); }; +FixMyStreet::override_config { ALLOWED_COBRANDS => [ 'fixmyhouse', 'fixmyshed' ] }, + sub { check_allowed_cobrands([ 'fixmyhouse', 'fixmyshed' ]); }; sub run_host_tests { my %host_tests = @_; for my $host ( sort keys %host_tests ) { - is FixMyStreet::Cobrand->get_class_for_host($host), - "FixMyStreet::Cobrand::$host_tests{$host}", - "does $host -> F::C::$host_tests{$host}"; + # get the cobrand class by host + my $cobrand = FixMyStreet::Cobrand->get_class_for_host($host); + my $test_class = $host_tests{$host}; + my $test_moniker = lc $test_class; + is $cobrand, "FixMyStreet::Cobrand::$test_class", "does $host -> F::C::$test_class"; + my $c = $cobrand->new(); + is $c->moniker, $test_moniker; } } -# get the cobrand class by host -run_host_tests( - 'www.fixmystreet.com' => 'Default', - 'reportemptyhomes.com' => 'EmptyHomes', - 'barnet.fixmystreet.com' => 'Default', # not in the allowed_cobrands list - 'some.odd.site.com' => 'Default', -); +# Only one cobrand, always use it +FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'fixmystreet' ], +}, sub { + run_host_tests( + 'www.fixmystreet.com' => 'FixMyStreet', + 'reportemptyhomes.com' => 'FixMyStreet', + 'barnet.fixmystreet.com' => 'FixMyStreet', + 'some.odd.site.com' => 'FixMyStreet', + ); +}; + +# Only one cobrand, no .pm file, should still work +FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'nopmfile' ], +}, sub { + run_host_tests( + 'www.fixmystreet.com' => 'nopmfile', + 'some.odd.site.com' => 'nopmfile', + ); +}; + +# Couple of cobrands, hostname checking and default fallback +FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'emptyhomes', 'fixmystreet' ], +}, sub { + run_host_tests( + 'www.fixmystreet.com' => 'FixMyStreet', + 'reportemptyhomes.com' => 'EmptyHomes', + 'barnet.fixmystreet.com' => 'FixMyStreet', # not in the allowed_cobrands list + 'some.odd.site.com' => 'Default', + ); +}; # now enable barnet too and check that it works -$override->replace( # - 'FixMyStreet::Cobrand::_get_allowed_cobrands' => - sub { return [ 'emptyhomes', 'barnet' ] } -); +FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'emptyhomes', 'barnet', 'fixmystreet' ], +}, sub { + run_host_tests( + 'www.fixmystreet.com' => 'FixMyStreet', + 'reportemptyhomes.com' => 'EmptyHomes', + 'barnet.fixmystreet.com' => 'Barnet', # found now it is in allowed_cobrands + 'some.odd.site.com' => 'Default', + ); +}; -# get the cobrand class by host -run_host_tests( - 'www.fixmystreet.com' => 'Default', - 'reportemptyhomes.com' => 'EmptyHomes', - 'barnet.fixmystreet.com' => 'Barnet', # found now it is in allowed_cobrands - 'some.odd.site.com' => 'Default', -); +# And a check with some regex matching +FixMyStreet::override_config { + ALLOWED_COBRANDS => [ { 'fixmystreet' => 'empty' }, 'barnet', { 'testing' => 'fixmystreet' } ], +}, sub { + run_host_tests( + 'www.fixmystreet.com' => 'testing', + 'reportemptyhomes.com' => 'FixMyStreet', + 'barnet.fixmystreet.com' => 'Barnet', + 'some.odd.site.com' => 'Default', + ); +}; # check that the moniker works as expected both on class and object. is FixMyStreet::Cobrand::EmptyHomes->moniker, 'emptyhomes', diff --git a/t/cobrand/zurich.t b/t/cobrand/zurich.t new file mode 100644 index 000000000..e97891b91 --- /dev/null +++ b/t/cobrand/zurich.t @@ -0,0 +1,685 @@ +# TODO +# Overdue alerts + +use strict; +use warnings; +use DateTime; +use Test::More; +use JSON; + +# Check that you have the required locale installed - the following +# should return a line with de_CH.utf8 in. If not install that locale. +# +# locale -a | grep de_CH +# +# To generate the translations use: +# +# commonlib/bin/gettext-makemo FixMyStreet + +use FixMyStreet; + +# This is a helper method that will send the reports but with the config +# correctly set - notably SEND_REPORTS_ON_STAGING needs to be true. +sub send_reports_for_zurich { + FixMyStreet::override_config { SEND_REPORTS_ON_STAGING => 1 }, sub { + # Actually send the report + 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; + +# Front page test +ok $mech->host("zurich.example.com"), "change host to Zurich"; +FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'zurich' ], +}, sub { + $mech->get_ok('/'); +}; +$mech->content_like( qr/zurich/i ); + +# Set up bodies +my $zurich = $mech->create_body_ok( 1, 'Zurich' ); +$zurich->parent( undef ); +$zurich->update; +my $division = $mech->create_body_ok( 2, 'Division 1' ); +$division->parent( $zurich->id ); +$division->send_method( 'Zurich' ); +$division->endpoint( 'division@example.org' ); +$division->update; +my $subdivision = $mech->create_body_ok( 3, 'Subdivision A' ); +$subdivision->parent( $division->id ); +$subdivision->send_method( 'Zurich' ); +$subdivision->endpoint( 'subdivision@example.org' ); +$subdivision->update; +my $external_body = $mech->create_body_ok( 4, 'External Body' ); +$external_body->send_method( 'Zurich' ); +$external_body->endpoint( 'external_body@example.org' ); +$external_body->update; + +sub cleanup { + $mech->delete_problems_for_body( $division->id ); + $mech->delete_problems_for_body( $subdivision->id ); + $mech->delete_user( 'dm1@example.org' ); + $mech->delete_user( 'sdm1@example.org' ); +} + +sub get_export_rows_count { + my $mech = shift; + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'zurich' ], + }, sub { + $mech->get_ok( '/admin/stats?export=1' ); + }; + is $mech->res->code, 200, 'csv retrieved ok'; + is $mech->content_type, 'text/csv', 'content_type correct' and do { + my @lines = split /\n/, $mech->content; + return @lines - 1; + }; + return; +} + +cleanup(); + +my $EXISTING_REPORT_COUNT = 0; + +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 }); + $EXISTING_REPORT_COUNT = get_export_rows_count($mech); + $mech->log_out_ok; +}; + +my @reports = $mech->create_problems_for_body( 1, 2, 'Test', { + state => 'unconfirmed', + confirmed => undef, + cobrand => 'zurich', +}); +my $report = $reports[0]; + +FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'zurich' ], +}, sub { + $mech->get_ok( '/report/' . $report->id ); +}; +$mech->content_contains('Überprüfung ausstehend'); + +# Check logging in to deal with this report +FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'zurich' ], +}, sub { + $mech->get_ok( '/admin' ); + is $mech->uri->path, '/auth', "got sent to the sign in page"; + + my $user = $mech->log_in_ok( 'dm1@example.org') ; + $user->from_body( undef ); + $user->update; + $mech->get_ok( '/admin' ); + is $mech->uri->path, '/my', "got sent to /my"; + $user->from_body( 2 ); + $user->update; + + $mech->get_ok( '/admin' ); +}; +is $mech->uri->path, '/admin', "am logged in"; + +$mech->content_contains( 'report_edit/' . $report->id ); +$mech->content_contains( DateTime->now->strftime("%d.%m.%Y") ); +$mech->content_contains( 'Erfasst' ); + + +subtest "changing of categories" => sub { + # create a few categories (which are actually contacts) + foreach my $name ( qw/Cat1 Cat2/ ) { + FixMyStreet::App->model('DB::Contact')->find_or_create({ + body => $division, + category => $name, + email => "$name\@example.org", + confirmed => 1, + deleted => 0, + editor => "editor", + whenedited => DateTime->now(), + note => "note for $name", + }); + } + + # put report into known category + my $original_category = $report->category; + $report->update({ category => 'Cat1' }); + is( $report->category, "Cat1", "Category set to Cat1" ); + + # get the latest comment + my $comments_rs = $report->comments->search({},{ order_by => { -desc => "created" } }); + ok ( !$comments_rs->first, "There are no comments yet" ); + + # change the category via the web interface + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'zurich' ], + }, sub { + $mech->get_ok( '/admin/report_edit/' . $report->id ); + $mech->submit_form_ok( { with_fields => { category => 'Cat2' } } ); + }; + + # check changes correctly saved + $report->discard_changes(); + is( $report->category, "Cat2", "Category changed to Cat2 as expected" ); + + # Check that a new comment has been created. + my $new_comment = $comments_rs->first(); + is( $new_comment->text, "Weitergeleitet von Cat1 an Cat2", "category change comment created" ); + + # restore report to original category. + $report->update({category => $original_category }); +}; + +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; + + # use a separate mech to avoid stomping on test state + my $mech = FixMyStreet::TestMech->new; + my $user = $mech->log_in_ok( 'super@example.org' ); + + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'zurich' ], + }, sub { + $mech->get( '/admin/stats' ); + }; + if ($mech->content =~/Innerhalb eines Arbeitstages moderiert: (\d+)/) { + return $1; + } + else { + fail sprintf "Could not get moderation results (%d)", $mech->status; + return undef; + } +} + +subtest "report_edit" => sub { + + ok ( ! exists ${$report->extra}{moderated_overdue}, 'Report currently unmoderated' ); + + is get_moderated_count(), 0; + + 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; + + is (FixMyStreet::Cobrand::Zurich->new->get_or_check_overdue($report), 0, 'sanity check'); + $report->update({ created => $created->clone->subtract(days => 10) }); + is (FixMyStreet::Cobrand::Zurich->new->get_or_check_overdue($report), 0, 'overdue call not increased'); + + reset_report_state($report, $created); +}; + +FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'zurich' ], +}, sub { + # Photo publishing + $mech->get_ok( '/admin/report_edit/' . $report->id ); + $mech->submit_form_ok( { with_fields => { state => 'confirmed', publish_photo => 1 } } ); + $mech->get_ok( '/report/' . $report->id ); + $mech->content_contains('photo/' . $report->id . '.jpeg'); + + # Internal notes + $mech->get_ok( '/admin/report_edit/' . $report->id ); + $mech->submit_form_ok( { with_fields => { new_internal_note => 'Initial internal note.' } } ); + $mech->submit_form_ok( { with_fields => { new_internal_note => 'Another internal note.' } } ); + $mech->content_contains( 'Initial internal note.' ); + $mech->content_contains( 'Another internal note.' ); + + # Original description + $mech->submit_form_ok( { with_fields => { detail => 'Edited details text.' } } ); + $mech->content_contains( 'Edited details text.' ); + $mech->content_contains( 'Originaltext: “Test Test 1 for 2 Detail”' ); + + $mech->get_ok( '/admin/report_edit/' . $report->id ); + $mech->submit_form_ok( { with_fields => { body_subdivision => 3, send_rejected_email => 1 } } ); + + $mech->get_ok( '/report/' . $report->id ); + $mech->content_contains('In Bearbeitung'); + $mech->content_contains('Test Test'); +}; + +send_reports_for_zurich(); +my $email = $mech->get_email; +like $email->header('Subject'), qr/Neue Meldung/, 'subject looks okay'; +like $email->header('To'), qr/subdivision\@example.org/, 'to line looks correct'; +$mech->clear_emails_ok; + +$mech->log_out_ok; + +my $user = $mech->log_in_ok( 'sdm1@example.org') ; +$user->update({ from_body => undef }); +FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'zurich' ], +}, sub { + $mech->get_ok( '/admin' ); +}; +is $mech->uri->path, '/my', "got sent to /my"; +$user->from_body( 3 ); +$user->update; + +FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'zurich' ], +}, sub { + $mech->get_ok( '/admin' ); +}; +is $mech->uri->path, '/admin', "am logged in"; + +$mech->content_contains( 'report_edit/' . $report->id ); +$mech->content_contains( DateTime->now->strftime("%d.%m.%Y") ); +$mech->content_contains( 'In Bearbeitung' ); + +FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'zurich' ], +}, sub { + $mech->get_ok( '/admin/report_edit/' . $report->id ); + $mech->content_contains( 'Initial internal note' ); + + $mech->submit_form_ok( { with_fields => { status_update => 'This is an update.' } } ); + is $mech->uri->path, '/admin/report_edit/' . $report->id, "still on edit page"; + $mech->content_contains('This is an update'); + ok $mech->form_with_fields( 'status_update' ); + $mech->submit_form_ok( { button => 'no_more_updates' } ); + is $mech->uri->path, '/admin/summary', "redirected now finished with report."; + + $mech->get_ok( '/report/' . $report->id ); + $mech->content_contains('In Bearbeitung'); + $mech->content_contains('Test Test'); +}; + +send_reports_for_zurich(); +$email = $mech->get_email; +like $email->header('Subject'), qr/Feedback/, 'subject looks okay'; +like $email->header('To'), qr/division\@example.org/, 'to line looks correct'; +$mech->clear_emails_ok; + +$report->discard_changes; +is $report->state, 'planned', 'Report now in planned state'; + +$mech->log_out_ok; +$user = $mech->log_in_ok( 'dm1@example.org') ; +FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'zurich' ], +}, sub { + $mech->get_ok( '/admin' ); +}; + +$mech->content_contains( 'report_edit/' . $report->id ); +$mech->content_contains( DateTime->now->strftime("%d.%m.%Y") ); + +# User confirms their email address +my $extra = $report->extra; +$extra->{email_confirmed} = 1; +$report->extra ( { %$extra } ); +$report->update; + +FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'zurich' ], +}, sub { + $mech->get_ok( '/admin/report_edit/' . $report->id ); + $mech->content_lacks( 'Unbestätigt' ); # Confirmed email + $mech->submit_form_ok( { with_fields => { status_update => 'FINAL UPDATE' } } ); + $mech->form_with_fields( 'status_update' ); + $mech->submit_form_ok( { button => 'publish_response' } ); + + $mech->get_ok( '/report/' . $report->id ); +}; +$mech->content_contains('Beantwortet'); +$mech->content_contains('Test Test'); +$mech->content_contains('FINAL UPDATE'); + +$email = $mech->get_email; +like $email->header('To'), qr/test\@example.com/, 'to line looks correct'; +like $email->header('From'), qr/division\@example.org/, 'from line looks correct'; +like $email->body, qr/FINAL UPDATE/, 'body looks correct'; +$mech->clear_emails_ok; + +# Assign directly to planned, don't confirm email +@reports = $mech->create_problems_for_body( 1, 2, 'Second', { + state => 'unconfirmed', + confirmed => undef, + cobrand => 'zurich', +}); +$report = $reports[0]; + +FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'zurich' ], +}, sub { + $mech->get_ok( '/admin/report_edit/' . $report->id ); + $mech->submit_form_ok( { with_fields => { state => 'planned' } } ); + $mech->get_ok( '/report/' . $report->id ); +}; +$mech->content_contains('In Bearbeitung'); +$mech->content_contains('Second Test'); + +FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'zurich' ], +}, sub { + $mech->get_ok( '/admin/report_edit/' . $report->id ); + $mech->content_contains( 'Unbestätigt' ); + $mech->submit_form_ok( { button => 'publish_response', with_fields => { status_update => 'FINAL UPDATE' } } ); + + $mech->get_ok( '/report/' . $report->id ); +}; +$mech->content_contains('Beantwortet'); +$mech->content_contains('Second Test'); +$mech->content_contains('FINAL UPDATE'); + +$mech->email_count_is(0); + +# Report assigned to third party + +@reports = $mech->create_problems_for_body( 1, 2, 'Third', { + state => 'unconfirmed', + confirmed => undef, + cobrand => 'zurich', +}); +$report = $reports[0]; + +FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'zurich' ], +}, sub { + $mech->get_ok( '/admin/report_edit/' . $report->id ); + $mech->submit_form_ok( { with_fields => { body_external => 4 } } ); + $mech->get_ok( '/report/' . $report->id ); +}; +$mech->content_contains('Beantwortet'); +$mech->content_contains('Third Test'); +$mech->content_contains('Wir haben Ihr Anliegen an External Body weitergeleitet'); +send_reports_for_zurich(); +$email = $mech->get_email; +like $email->header('Subject'), qr/Weitergeleitete Meldung/, 'subject looks okay'; +like $email->header('To'), qr/external_body\@example.org/, 'to line looks correct'; +like $email->body, qr/External Body/, 'body has right name'; +unlike $email->body, qr/test\@example.com/, 'body does not contain email address'; +$mech->clear_emails_ok; + +# Test calling back, and third_personal boolean setting +FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'zurich' ], +}, sub { + $mech->get_ok( '/admin' ); + is $mech->uri->path, '/admin', "am logged in"; + $mech->content_contains( 'report_edit/' . $report->id ); + $mech->get_ok( '/admin/report_edit/' . $report->id ); + $mech->submit_form_ok( { with_fields => { state => 'unconfirmed' } } ); + $mech->submit_form_ok( { with_fields => { body_external => 4, third_personal => 1 } } ); + $mech->get_ok( '/report/' . $report->id ); +}; +$mech->content_contains('Beantwortet'); +$mech->content_contains('Third Test'); +$mech->content_contains('Wir haben Ihr Anliegen an External Body weitergeleitet'); +send_reports_for_zurich(); +$email = $mech->get_email; +like $email->header('Subject'), qr/Weitergeleitete Meldung/, 'subject looks okay'; +like $email->header('To'), qr/external_body\@example.org/, 'to line looks correct'; +like $email->body, qr/External Body/, 'body has right name'; +like $email->body, qr/test\@example.com/, 'body does contain email address'; +$mech->clear_emails_ok; +$mech->log_out_ok; + +subtest "only superuser can see stats" => sub { + $user = $mech->log_in_ok( 'super@example.org' ); + + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'zurich' ], + }, sub { + $mech->get( '/admin/stats' ); + }; + is $mech->res->code, 200, "superuser should be able to see stats page"; + $mech->log_out_ok; + + $user = $mech->log_in_ok( 'dm1@example.org' ); + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'zurich' ], + }, sub { + $mech->get( '/admin/stats' ); + }; + is $mech->res->code, 404, "only superuser should be able to see stats page"; + $mech->log_out_ok; +}; + +subtest "only superuser can edit bodies" => sub { + $user = $mech->log_in_ok( 'dm1@example.org' ); + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'zurich' ], + }, sub { + $mech->get( '/admin/body/' . $zurich->id ); + }; + is $mech->res->code, 404, "only superuser should be able to edit bodies"; + $mech->log_out_ok; +}; + +subtest "only superuser can see 'Add body' form" => sub { + $user = $mech->log_in_ok( 'dm1@example.org' ); + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'zurich' ], + MAPIT_URL => 'http://global.mapit.mysociety.org/', + MAPIT_TYPES => [ 'O08' ], + MAPIT_ID_WHITELIST => [ 423017 ], + }, sub { + $mech->get_ok( '/admin/bodies' ); + }; + $mech->content_lacks( '<form method="post" action="bodies"' ); + $mech->log_out_ok; +}; + +subtest "phone number is mandatory" => sub { + FixMyStreet::override_config { + MAPIT_TYPES => [ 'O08' ], + MAPIT_URL => 'http://global.mapit.mysociety.org/', + ALLOWED_COBRANDS => [ 'zurich' ], + MAPIT_ID_WHITELIST => [ 423017 ], + }, sub { + $user = $mech->log_in_ok( 'dm1@example.org' ); + $mech->get_ok( '/report/new?lat=47.381817&lon=8.529156' ); + $mech->submit_form( with_fields => { phone => "" } ); + $mech->content_contains( 'Diese Information wird benötigt' ); + $mech->log_out_ok; + }; +}; + +subtest "phone number is not mandatory for reports from mobile apps" => sub { + FixMyStreet::override_config { + MAPIT_TYPES => [ 'O08' ], + MAPIT_URL => 'http://global.mapit.mysociety.org/', + ALLOWED_COBRANDS => [ 'zurich' ], + MAPIT_ID_WHITELIST => [ 423017 ], + }, sub { + $mech->post_ok( '/report/new/mobile?lat=47.381817&lon=8.529156' , { + service => 'iPhone', + detail => 'Problem-Bericht', + lat => 47.381817, + lon => 8.529156, + email => 'user@example.org', + pc => '', + name => '', + category => 'bad category', + }); + my $res = $mech->response; + ok $res->header('Content-Type') =~ m{^application/json\b}, 'response should be json'; + unlike $res->content, qr/Diese Information wird benötigt/, 'response should not contain phone error'; + # Clear out the mailq + $mech->clear_emails_ok; + }; +}; + +subtest "problems can't be assigned to deleted bodies" => sub { + $user = $mech->log_in_ok( 'dm1@example.org' ); + $user->from_body( 1 ); + $user->update; + $report->state( 'confirmed' ); + $report->update; + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'zurich' ], + MAPIT_URL => 'http://global.mapit.mysociety.org/', + MAPIT_TYPES => [ 'O08' ], + MAPIT_ID_WHITELIST => [ 423017 ], + }, sub { + $mech->get_ok( '/admin/body/' . $external_body->id ); + $mech->submit_form_ok( { with_fields => { deleted => 1 } } ); + $mech->get_ok( '/admin/report_edit/' . $report->id ); + $mech->content_lacks( $external_body->name ); + }; + $user->from_body( 2 ); + $user->update; + $mech->log_out_ok; +}; + +subtest "hidden report email are only sent when requested" => sub { + $user = $mech->log_in_ok( 'dm1@example.org') ; + $extra = $report->extra; + $extra->{email_confirmed} = 1; + $report->extra ( { %$extra } ); + $report->update; + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'zurich' ], + }, sub { + $mech->get_ok( '/admin/report_edit/' . $report->id ); + $mech->submit_form_ok( { with_fields => { state => 'hidden', send_rejected_email => 1 } } ); + $mech->email_count_is(1); + $mech->clear_emails_ok; + $mech->get_ok( '/admin/report_edit/' . $report->id ); + $mech->submit_form_ok( { with_fields => { state => 'hidden', send_rejected_email => undef } } ); + $mech->email_count_is(0); + $mech->clear_emails_ok; + $mech->log_out_ok; + }; +}; + +subtest "test stats" => sub { + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'zurich' ], + }, sub { + $user = $mech->log_in_ok( 'super@example.org' ); + + $mech->get_ok( '/admin/stats' ); + 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: 3'); + # my @data = $mech->content =~ /(?:moderiert|abgeschlossen): \d+/g; + # diag Dumper(\@data); use Data::Dumper; + + my $export_count = get_export_rows_count($mech); + if (defined $export_count) { + is $export_count - $EXISTING_REPORT_COUNT, 3, 'Correct number of reports'; + $mech->content_contains(',fixed - council,'); + $mech->content_contains(',hidden,'); + } + + $mech->log_out_ok; + }; +}; + +subtest "test admin_log" => sub { + diag $report->id; + my @entries = FixMyStreet::App->model('DB::AdminLog')->search({ + object_type => 'problem', + object_id => $report->id, + }); + is scalar @entries, 4, 'State changes logged'; + is $entries[-1]->action, 'state change to hidden', 'State change logged as expected'; +}; + +cleanup(); + +ok $mech->host("www.fixmystreet.com"), "change host back"; + +done_testing(); |