diff options
Diffstat (limited to 't/app')
-rw-r--r-- | t/app/controller/alert_new.t | 2 | ||||
-rw-r--r-- | t/app/controller/around.t | 2 | ||||
-rw-r--r-- | t/app/controller/auth.t | 4 | ||||
-rw-r--r-- | t/app/controller/contact.t | 37 | ||||
-rw-r--r-- | t/app/controller/dashboard.t | 6 | ||||
-rw-r--r-- | t/app/controller/questionnaire.t | 23 | ||||
-rw-r--r-- | t/app/controller/report_display.t | 24 | ||||
-rw-r--r-- | t/app/controller/report_new.t | 105 | ||||
-rw-r--r-- | t/app/controller/report_new_open311.t | 8 | ||||
-rw-r--r-- | t/app/controller/report_updates.t | 4 | ||||
-rw-r--r-- | t/app/controller/rss.t | 11 | ||||
-rw-r--r-- | t/app/model/alert_type.t | 69 | ||||
-rw-r--r-- | t/app/model/problem.t | 177 | ||||
-rw-r--r-- | t/app/sendreport/email.t | 77 |
14 files changed, 386 insertions, 163 deletions
diff --git a/t/app/controller/alert_new.t b/t/app/controller/alert_new.t index 7ba887824..d66590c57 100644 --- a/t/app/controller/alert_new.t +++ b/t/app/controller/alert_new.t @@ -448,7 +448,7 @@ subtest "Test normal alert signups and that alerts are sent" => sub { $count++ if $_->body =~ /The following updates have been left on this problem:/; $count++ if $_->body =~ /The following new problems have been reported to City of\s*Edinburgh Council:/; $count++ if $_->body =~ /The following nearby problems have been added:/; - $count++ if $_->body =~ /\s+-\s+Testing,\s+EH1\s+1BB/; + $count++ if $_->body =~ /\s+-\s+Testing/; } is $count, 5, 'Five emails with the right things in them'; diff --git a/t/app/controller/around.t b/t/app/controller/around.t index ca1694b49..db03e00f4 100644 --- a/t/app/controller/around.t +++ b/t/app/controller/around.t @@ -72,7 +72,7 @@ foreach my $test ( $mech->get_ok('/'); $mech->submit_form_ok( { with_fields => { pc => $test->{pc} } }, "good location" ); - is_deeply $mech->form_errors, [], "no errors for pc '$test->{pc}'"; + is_deeply $mech->page_errors, [], "no errors for pc '$test->{pc}'"; is_deeply $mech->extract_location, $test, "got expected location for pc '$test->{pc}'"; }; diff --git a/t/app/controller/auth.t b/t/app/controller/auth.t index efc5e60e6..67466e959 100644 --- a/t/app/controller/auth.t +++ b/t/app/controller/auth.t @@ -33,7 +33,7 @@ for my $test ( my ( $email, $error_message ) = @$test; pass "--- testing bad email '$email' gives error '$error_message'"; $mech->get_ok('/auth'); - is_deeply $mech->form_errors, [], 'no errors initially'; + is_deeply $mech->page_errors, [], 'no errors initially'; $mech->submit_form_ok( { form_name => 'general_auth', @@ -43,7 +43,7 @@ for my $test ( "try to create an account with email '$email'" ); is $mech->uri->path, '/auth', "still on auth page"; - is_deeply $mech->form_errors, [ $error_message ], 'no errors initially'; + is_deeply $mech->page_errors, [ $error_message ], 'errors match'; } # create a new account diff --git a/t/app/controller/contact.t b/t/app/controller/contact.t index 970241d12..11e0d30cf 100644 --- a/t/app/controller/contact.t +++ b/t/app/controller/contact.t @@ -131,8 +131,7 @@ for my $test ( message => '', }, page_errors => - [ 'There were problems with your report. Please see below.', ], - field_errors => [ + [ 'There were problems with your report. Please see below.', 'Please enter your name', 'Please enter your email', 'Please enter a subject', @@ -147,8 +146,7 @@ for my $test ( message => '', }, page_errors => - [ 'There were problems with your report. Please see below.', ], - field_errors => [ + [ 'There were problems with your report. Please see below.', 'Please enter your name', 'Please enter a valid email address', 'Please enter a subject', @@ -162,9 +160,10 @@ for my $test ( subject => '', message => '', }, - page_errors => - [ 'There were problems with your report. Please see below.', ], - field_errors => [ 'Please enter a subject', 'Please write a message', ] + page_errors => [ + 'There were problems with your report. Please see below.', + 'Please enter a subject', 'Please write a message', + ] }, { fields => { @@ -173,9 +172,10 @@ for my $test ( subject => 'A subject', message => '', }, - page_errors => - [ 'There were problems with your report. Please see below.', ], - field_errors => [ 'Please write a message', ] + page_errors => [ + 'There were problems with your report. Please see below.', + 'Please write a message', + ] }, { fields => { @@ -184,9 +184,11 @@ for my $test ( subject => ' ', message => '', }, - page_errors => - [ 'There were problems with your report. Please see below.', ], - field_errors => [ 'Please enter a subject', 'Please write a message', ] + page_errors => [ + 'There were problems with your report. Please see below.', + 'Please enter a subject', + 'Please write a message', + ] }, { fields => { @@ -195,9 +197,10 @@ for my $test ( subject => 'A subject', message => ' ', }, - page_errors => - [ 'There were problems with your report. Please see below.', ], - field_errors => [ 'Please write a message', ] + page_errors => [ + 'There were problems with your report. Please see below.', + 'Please write a message', + ] }, { url => '/contact?id=' . $problem_main->id, @@ -209,7 +212,6 @@ for my $test ( id => 'invalid', }, page_errors => [ 'Illegal ID' ], - field_errors => [] }, ) { @@ -217,7 +219,6 @@ for my $test ( $mech->get_ok( $test->{url} ? $test->{url} : '/contact' ); $mech->submit_form_ok( { with_fields => $test->{fields} } ); is_deeply $mech->page_errors, $test->{page_errors}, 'page errors'; - is_deeply $mech->form_errors, $test->{field_errors}, 'field_errors'; # we santise this when we submit so need to remove it delete $test->{fields}->{id} diff --git a/t/app/controller/dashboard.t b/t/app/controller/dashboard.t index 472c6d9bd..47d10ff5c 100644 --- a/t/app/controller/dashboard.t +++ b/t/app/controller/dashboard.t @@ -42,14 +42,14 @@ $mech->submit_form_ok( { with_fields => { email => $test_user, password_sign_in => $test_pass } } ); -$mech->content_contains( 'Summary Statistics for City of Edinburgh' ); +$mech->content_contains( 'City of Edinburgh' ); FixMyStreet::App->model('DB::Contact')->search( { area_id => $test_council } ) ->delete; delete_problems(); -my @cats = qw( Grafitti Litter Potholes ); +my @cats = qw( Grafitti Litter Potholes Other ); for my $contact ( @cats ) { FixMyStreet::App->model('DB::Contact')->create( { @@ -90,7 +90,7 @@ my $categories = scraper { }, }; -my $expected_cats = [ 'All', '-- Pick a category --', @cats, 'Other' ]; +my $expected_cats = [ 'All', '-- Pick a category --', @cats ]; my $res = $categories->scrape( $mech->content ); is_deeply( $res->{cats}, $expected_cats, 'correct list of categories' ); diff --git a/t/app/controller/questionnaire.t b/t/app/controller/questionnaire.t index 2d4fdb711..3475307fb 100644 --- a/t/app/controller/questionnaire.t +++ b/t/app/controller/questionnaire.t @@ -8,6 +8,12 @@ use FixMyStreet::App::Controller::Questionnaire; ok( my $mech = FixMyStreet::TestMech->new, 'Created mech object' ); +# Make sure there's no outstanding questionnaire emails to be sent +FixMyStreet::App->model('DB::Questionnaire')->send_questionnaires( { + site => 'fixmystreet' +} ); +$mech->clear_emails_ok; + # create a test user and report $mech->delete_user('test@example.com'); @@ -273,6 +279,23 @@ foreach my $test ( }; } +my $comment = FixMyStreet::App->model('DB::Comment')->find_or_create( + { + problem_id => $report->id, + user_id => $user->id, + name => 'A User', + mark_fixed => 'false', + text => 'This is some update text', + state => 'confirmed', + confirmed => $sent_time, + anonymous => 'f', + } +); +subtest 'Check updates are shown correctly on questionnaire page' => sub { + $mech->get_ok("/Q/" . $token->token); + $mech->content_contains( 'updates that have been left' ); + $mech->content_contains( 'This is some update text' ); +}; for my $test ( { diff --git a/t/app/controller/report_display.t b/t/app/controller/report_display.t index 548580fe1..6c2733e36 100644 --- a/t/app/controller/report_display.t +++ b/t/app/controller/report_display.t @@ -209,7 +209,7 @@ for my $test ( ), state => 'confirmed', banner_id => 'unknown', - banner_text => 'This problem is old and of unknown status.', + banner_text => 'unknown', fixed => 0 }, { @@ -224,7 +224,7 @@ for my $test ( ), state => 'fixed', banner_id => 'fixed', - banner_text => 'This problem has been fixed.', + banner_text => 'fixed', fixed => 1 }, { @@ -232,7 +232,7 @@ for my $test ( date => DateTime->now, state => 'fixed', banner_id => 'fixed', - banner_text => 'This problem has been fixed.', + banner_text => 'fixed', fixed => 1 }, { @@ -240,7 +240,7 @@ for my $test ( date => DateTime->now, state => 'fixed - user', banner_id => 'fixed', - banner_text => 'This problem has been fixed.', + banner_text => 'fixed', fixed => 1 }, { @@ -248,7 +248,7 @@ for my $test ( date => DateTime->now, state => 'fixed - council', banner_id => 'fixed', - banner_text => 'This problem has been fixed.', + banner_text => 'fixed', fixed => 1 }, { @@ -256,7 +256,7 @@ for my $test ( date => DateTime->now, state => 'closed', banner_id => 'closed', - banner_text => 'This problem has been closed.', + banner_text => 'closed', fixed => 0 }, { @@ -264,7 +264,7 @@ for my $test ( date => DateTime->now, state => 'investigating', banner_id => 'progress', - banner_text => 'This problem is in progress.', + banner_text => 'progress', fixed => 0 }, { @@ -272,7 +272,7 @@ for my $test ( date => DateTime->now, state => 'planned', banner_id => 'progress', - banner_text => 'This problem is in progress.', + banner_text => 'progress', fixed => 0 }, { @@ -280,7 +280,7 @@ for my $test ( date => DateTime->now, state => 'in progress', banner_id => 'progress', - banner_text => 'This problem is in progress.', + banner_text => 'progress', fixed => 0 }, ) { @@ -299,7 +299,11 @@ for my $test ( } is $banner->{id}, $test->{banner_id}, 'banner id'; - is $banner->{text}, $test->{banner_text}, 'banner text'; + if ($test->{banner_text}) { + ok $banner->{text} =~ /$test->{banner_text}/i, 'banner text'; + } else { + is $banner->{text}, $test->{banner_text}, 'banner text'; + } my $update_form = $mech->form_name( 'updateForm' ); if ( $test->{fixed} ) { diff --git a/t/app/controller/report_new.t b/t/app/controller/report_new.t index 29fb650e5..88236a1c3 100644 --- a/t/app/controller/report_new.t +++ b/t/app/controller/report_new.t @@ -34,6 +34,9 @@ my %contact_params = ( note => 'Created for test', ); # Let's make some contacts to send things to! +FixMyStreet::App->model('DB::Contact')->search( { + email => { 'like', '%example.com' }, +} )->delete; my $contact1 = FixMyStreet::App->model('DB::Contact')->find_or_create( { %contact_params, area_id => 2651, # Edinburgh @@ -64,11 +67,25 @@ my $contact5 = FixMyStreet::App->model('DB::Contact')->find_or_create( { category => 'Trees', email => 'trees@example.com', } ); +my $contact6 = FixMyStreet::App->model('DB::Contact')->find_or_create( { + %contact_params, + area_id => 2434, # Lichfield + category => 'Trees', + email => 'trees@example.com', +} ); +my $contact7 = FixMyStreet::App->model('DB::Contact')->find_or_create( { + %contact_params, + area_id => 2240, # Lichfield + category => 'Street lighting', + email => 'highways@example.com', +} ); ok $contact1, "created test contact 1"; ok $contact2, "created test contact 2"; ok $contact3, "created test contact 3"; ok $contact4, "created test contact 4"; ok $contact5, "created test contact 5"; +ok $contact6, "created test contact 6"; +ok $contact7, "created test contact 7"; # test that the various bit of form get filled in and errors correctly # generated. @@ -371,7 +388,7 @@ foreach my $test ( # submit initial pc form $mech->submit_form_ok( { with_fields => { pc => $test->{pc} } }, "submit location" ); - is_deeply $mech->form_errors, [], "no errors for pc '$test->{pc}'"; + is_deeply $mech->page_errors, [], "no errors for pc '$test->{pc}'"; # click through to the report page $mech->follow_link_ok( { text_regex => qr/skip this step/i, }, @@ -382,7 +399,7 @@ foreach my $test ( "submit form" ); # check that we got the errors expected - is_deeply $mech->form_errors, $test->{errors}, "check errors"; + is_deeply $mech->page_errors, $test->{errors}, "check errors"; # check that fields have changed as expected my $new_values = { @@ -463,7 +480,7 @@ foreach my $test ( ); # check that we got the errors expected - is_deeply $mech->form_errors, [], "check there were no errors"; + is_deeply $mech->page_errors, [], "check there were no errors"; # check that the user has been created/ not changed my $user = @@ -573,7 +590,7 @@ subtest "test password errors for a user who is signing in as they report" => su ); # check that we got the errors expected - is_deeply $mech->form_errors, [ + is_deeply $mech->page_errors, [ "There was a problem with your email/password combination. If you cannot remember your password, or do not have one, please fill in the \x{2018}sign in by email\x{2019} section of the form.", ], "check there were errors"; }; @@ -620,7 +637,7 @@ subtest "test report creation for a user who is signing in as they report" => su ); # check that we got the errors expected - is_deeply $mech->form_errors, [ + is_deeply $mech->page_errors, [ 'You have successfully signed in; please check and confirm your details are accurate:', ], "check there were errors"; @@ -798,10 +815,8 @@ subtest "check that a lat/lon off coast leads to /around" => sub { is $mech->uri->path, '/around', "redirected to '/around'"; is_deeply # - $mech->form_errors, - [ 'That spot does not appear to be covered by a council. If you have' - . ' tried to report an issue past the shoreline, for example, please' - . ' specify the closest point on land.' ], # + $mech->page_errors, + [ 'That location does not appear to be covered by a council; perhaps it is offshore or outside the country. Please try again.' ], "Found location error"; }; @@ -859,6 +874,10 @@ for my $test ( ) { subtest $test->{desc} => sub { + if ( $test->{host} =~ /bromley/ && !FixMyStreet::Cobrand->exists('bromley') ) { + plan skip_all => 'Skipping Bromley tests without Bromley cobrand'; + } + $mech->host( $test->{host} ); $mech->log_out_ok; @@ -941,10 +960,78 @@ for my $test ( }; } +SKIP: { + skip( "Need 'lichfielddc' in ALLOWED_COBRANDS config", 100 ) + unless FixMyStreet::Cobrand->exists('lichfielddc'); + + my $test_email = 'test-22@example.com'; + $mech->host( 'http://lichfielddc.fixmystreet.com/' ); + $mech->clear_emails_ok; + $mech->log_out_ok; + + $mech->get_ok('/around'); + $mech->submit_form_ok( { with_fields => { pc => 'WS13 7RD' } }, "submit location" ); + $mech->follow_link_ok( { text_regex => qr/skip this step/i, }, "follow 'skip this step' link" ); + $mech->submit_form_ok( + { + button => 'submit_register', + with_fields => { + title => 'Test Report', + detail => 'Test report details.', + photo => '', + name => 'Joe Bloggs', + may_show_name => '1', + email => $test_email, + phone => '07903 123 456', + category => 'Street lighting', + } + }, + "submit good details" + ); + is_deeply $mech->page_errors, [], "check there were no errors"; + + # check that the user has been created/ not changed + my $user = + FixMyStreet::App->model('DB::User')->find( { email => $test_email } ); + ok $user, "user found"; + + # find the report + my $report = $user->problems->first; + ok $report, "Found the report"; + + # Check the report has been assigned appropriately + is $report->council, 2240; + + # receive token + my $email = $mech->get_email; + ok $email, "got an email"; + like $email->body, qr/confirm the problem/i, "confirm the problem"; + + my ($url) = $email->body =~ m{(http://\S+)}; + ok $url, "extracted confirm url '$url'"; + + # confirm token + $mech->get_ok($url); + $report->discard_changes; + is $report->state, 'confirmed', "Report is now confirmed"; + + # Shouldn't be found, as it was a county problem + is $mech->get( '/report/' . $report->id )->code, 404, "report not found"; + + # But should be on the main site + $mech->host( 'www.fixmystreet.com' ); + $mech->get_ok( '/report/' . $report->id ); + is $report->name, 'Joe Bloggs', 'name updated correctly'; + + $mech->delete_user($user); +} + $contact1->delete; $contact2->delete; $contact3->delete; $contact4->delete; $contact5->delete; +$contact6->delete; +$contact7->delete; done_testing(); diff --git a/t/app/controller/report_new_open311.t b/t/app/controller/report_new_open311.t index dc3583e6b..863571ad0 100644 --- a/t/app/controller/report_new_open311.t +++ b/t/app/controller/report_new_open311.t @@ -113,10 +113,10 @@ foreach my $test ( # submit initial pc form $mech->submit_form_ok( { with_fields => { pc => $test->{pc} } }, "submit location" ); - is_deeply $mech->form_errors, [], "no errors for pc '$test->{pc}'"; + is_deeply $mech->page_errors, [], "no errors for pc '$test->{pc}'"; # click through to the report page - $mech->follow_link_ok( { text => 'skip this step', }, + $mech->follow_link_ok( { text_regex => qr/skip this step/i, }, "follow 'skip this step' link" ); # submit the main form @@ -124,7 +124,7 @@ foreach my $test ( "submit form" ); # check that we got the errors expected - is_deeply $mech->form_errors, $test->{errors}, "check errors"; + is_deeply $mech->page_errors, $test->{errors}, "check errors"; # check that fields have changed as expected my $new_values = { @@ -136,7 +136,7 @@ foreach my $test ( if ( $test->{fields}->{category} eq 'Street lighting' ) { my $result = scraper { - process 'div#category_meta div select#form_type option', 'option[]' => '@value'; + process 'select#form_type option', 'option[]' => '@value'; } ->scrape( $mech->response ); diff --git a/t/app/controller/report_updates.t b/t/app/controller/report_updates.t index 0337a881b..cf6af16cb 100644 --- a/t/app/controller/report_updates.t +++ b/t/app/controller/report_updates.t @@ -271,7 +271,7 @@ for my $test ( $mech->submit_form_ok( { with_fields => $test->{fields} }, 'submit update' ); - is_deeply $mech->form_errors, $test->{field_errors}, 'field errors'; + is_deeply $mech->page_errors, $test->{field_errors}, 'field errors'; my $values = { %{ $test->{fields} }, @@ -640,7 +640,7 @@ for my $test ( 'submit update' ); - is_deeply $mech->form_errors, $test->{field_errors}, 'check there were errors'; + is_deeply $mech->page_errors, $test->{field_errors}, 'check there were errors'; SKIP: { skip( "Incorrect password", 5 ) unless $test->{form_values}{password_sign_in} eq $pw; diff --git a/t/app/controller/rss.t b/t/app/controller/rss.t index c6ab20574..77e2c7ee1 100644 --- a/t/app/controller/rss.t +++ b/t/app/controller/rss.t @@ -41,7 +41,7 @@ my $report = FixMyStreet::App->model('DB::Problem')->find_or_create( { $mech->get_ok("/rss/pc/EH11BB/2"); -$mech->content_contains( "Testing, 10th October, EH1 1BB" ); +$mech->content_contains( "Testing, 10th October" ); $mech->content_lacks( 'Nearest road to the pin' ); $report->geocode( @@ -106,19 +106,12 @@ $report->geocode( 'authenticationResultCode' => 'ValidCredentials' } ); -$report->postcode('eh11bb'); $report->update(); $mech->get_ok("/rss/pc/EH11BB/2"); -$mech->content_contains( "Testing, 10th October, EH1 1BB" ); +$mech->content_contains( "Testing, 10th October" ); $mech->content_contains( '18 North Bridge, Edinburgh' ); -$report->postcode('Princes St, Edinburgh'); -$report->update(); - -$mech->get_ok("/rss/pc/EH11BB/2"); -$mech->content_contains( "Testing, 10th October, Princes St, Edinburgh" ); - $report->delete(); $user1->delete(); diff --git a/t/app/model/alert_type.t b/t/app/model/alert_type.t index c23252de6..7df4c44c0 100644 --- a/t/app/model/alert_type.t +++ b/t/app/model/alert_type.t @@ -155,59 +155,24 @@ my $council_alert = FixMyStreet::App->model('DB::Alert')->find_or_create( } ); -for my $test ( - { - postcode => 'SW1A 1AA', - expected_postcode => 'SW1A 1AA', - }, - { - postcode => 'sw1a 1AA', - expected_postcode => 'SW1A 1AA', - }, - { - postcode => 'SW1A 1aa', - expected_postcode => 'SW1A 1AA', - }, - { - postcode => 'SW1A1AA', - expected_postcode => 'SW1A 1AA', - }, - { - postcode => 'Buckingham Gate', - expected_postcode => 'Buckingham\s+Gate', - }, - { - postcode => 'Buckingham gate', - expected_postcode => 'Buckingham\s+gate', - }, -) { - subtest "correct text for postcode $test->{postcode}" => sub { - $mech->clear_emails_ok; - - my $sent = FixMyStreet::App->model('DB::AlertSent')->search( - { - alert_id => $council_alert->id, - parameter => $report->id, - } - )->delete; - - $report->postcode( $test->{postcode} ); - $report->update; - - FixMyStreet::App->model('DB::AlertType')->email_alerts(); +subtest "correct text for title after URL" => sub { + $mech->clear_emails_ok; - $mech->email_count_is( 1 ); - my $email = $mech->get_email; - my $pc = $test->{expected_postcode}; - (my $title = $report->title) =~ s/ /\\s+/; - my $body = $email->body; + my $sent = FixMyStreet::App->model('DB::AlertSent')->search( + { + alert_id => $council_alert->id, + parameter => $report->id, + } + )->delete; + FixMyStreet::App->model('DB::AlertType')->email_alerts(); - like $body, qr#report/$report_id\s+-\s+$title,\s+$pc#, 'email contains expected postcode'; - }; -} + $mech->email_count_is( 1 ); + my $email = $mech->get_email; + (my $title = $report->title) =~ s/ /\\s+/; + my $body = $email->body; -$report->postcode( 'SW1A 1AA' ); -$report->update; + like $body, qr#report/$report_id\s+-\s+$title#, 'email contains expected title'; +}; $report->geocode( { @@ -448,7 +413,7 @@ subtest "check alerts from cobrand send main site url for alerts for different c my $expected1 = mySociety::Config::get('BASE_URL') . '/report/' . $report_to_county_council->id; my $expected3 = mySociety::Config::get('BASE_URL') . '/report/' . $report_outside_district->id; my $cobrand = FixMyStreet::Cobrand->get_class_for_moniker('lichfielddc')->new(); - my $expected2 = $cobrand->base_url_for_emails . '/report/' . $report_to_council->id; + my $expected2 = $cobrand->base_url . '/report/' . $report_to_council->id; like $body, qr#$expected1#, 'non cobrand area report point to fixmystreet.com'; like $body, qr#$expected2#, 'cobrand area report point to cobrand url'; @@ -485,7 +450,7 @@ subtest "check local alerts from cobrand send main site url for alerts for diffe my $expected1 = mySociety::Config::get('BASE_URL') . '/report/' . $report_to_county_council->id; my $cobrand = FixMyStreet::Cobrand->get_class_for_moniker('lichfielddc')->new(); - my $expected2 = $cobrand->base_url_for_emails . '/report/' . $report_to_council->id; + my $expected2 = $cobrand->base_url . '/report/' . $report_to_council->id; like $body, qr#$expected1#, 'non cobrand area report point to fixmystreet.com'; like $body, qr#$expected2#, 'cobrand area report point to cobrand url'; diff --git a/t/app/model/problem.t b/t/app/model/problem.t index a7415851b..8aabce4f7 100644 --- a/t/app/model/problem.t +++ b/t/app/model/problem.t @@ -348,84 +348,138 @@ for my $test ( my $mech = FixMyStreet::TestMech->new(); -FixMyStreet::App->model('DB::Contact')->find_or_create( - { - area_id => 2651, - category => 'potholes', - email => 'test@example.org', - confirmed => 1, - deleted => 0, - editor => 'test', - whenedited => \'ms_current_timestamp()', - note => '', - } -); - -FixMyStreet::App->model('DB::Contact')->find_or_create( - { - area_id => 2226, - category => 'potholes', - email => '2226@example.org', - confirmed => 1, - deleted => 0, - editor => 'test', - whenedited => \'ms_current_timestamp()', - note => '', - } +my %contact_params = ( + confirmed => 1, + deleted => 0, + editor => 'Test', + whenedited => \'ms_current_timestamp()', + note => 'Created for test', ); +# Let's make some contacts to send things to! +FixMyStreet::App->model('DB::Contact')->search( { + email => { 'like', '%example.com' }, +} )->delete; +my @contacts; +for my $contact ( { + area_id => 2651, # Edinburgh + category => 'potholes', + email => 'test@example.org', +}, { + area_id => 2226, # Gloucestershire + category => 'potholes', + email => '2226@example.org', +}, { + area_id => 2326, # Cheltenham + category => 'potholes', + email => '2326@example.org', +}, { + area_id => 2434, # Lichfield + category => 'potholes', + email => 'trees@example.com', +}, { + area_id => 2240, # Staffordshire + category => 'potholes', + email => 'highways@example.com', +}, { + area_id => 14279, # Ballymoney + category => 'Street lighting', + email => 'roads.western@drdni.example.org', +}, { + area_id => 14279, # Ballymoney + category => 'Graffiti', + email => 'highways@example.com', +}, { + confirmed => 0, + area_id => 2636, # Isle of Wight + category => 'potholes', + email => '2636@example.com', +} ) { + my $new_contact = FixMyStreet::App->model('DB::Contact')->find_or_create( { %contact_params, %$contact } ); + ok $new_contact, "created test contact"; + push @contacts, $new_contact; +} -FixMyStreet::App->model('DB::Contact')->find_or_create( - { - area_id => 2326, - category => 'potholes', - email => '2326@example.org', - confirmed => 1, - deleted => 0, - editor => 'test', - whenedited => \'ms_current_timestamp()', - note => '', - } +my %common = ( + email => 'system_user@example.com', + name => 'Andrew Smith', ); - foreach my $test ( { + %common, desc => 'sends an email', unset_whendef => 1, email_count => 1, - email => 'system_user@example.com', - name => 'Andrew Smith', dear => qr'Dear City of Edinburgh Council', to => qr'City of Edinburgh Council', council => 2651, - }, - { + }, { + %common, desc => 'no email sent if no unsent problems', unset_whendef => 0, email_count => 0, - email => 'system_user@example.com', - name => 'Andrew Smith', council => 2651, - }, - { + }, { + %common, desc => 'email to two tier council', unset_whendef => 1, email_count => 1, - email => 'system_user@example.com', - name => 'Andrew Smith', to => qr'Gloucestershire County Council.*Cheltenham Borough Council', dear => qr'Dear Gloucestershire County Council and Cheltenham Borough', council => '2226,2326', multiple => 1, - }, - { + }, { + %common, desc => 'email to two tier council with one missing details', unset_whendef => 1, email_count => 1, - email => 'system_user@example.com', - name => 'Andrew Smith', - to => qr'Gloucestershire County Council', + to => qr'Gloucestershire County Council" <2226@example', dear => qr'Dear Gloucestershire County Council,', council => '2226|2649', missing => qr'problem might be the responsibility of Fife.*Council'ms, + }, { + %common, + desc => 'email to two tier council that only shows district, district', + unset_whendef => 1, + email_count => 1, + to => qr'Lichfield District Council', + dear => qr'Dear Lichfield District Council,', + council => '2434', + cobrand => 'lichfielddc', + url => 'lichfielddc.', + }, { + %common, + desc => 'email to two tier council that only shows district, county', + unset_whendef => 1, + email_count => 1, + to => qr'Staffordshire County Council" <highways@example', + dear => qr'Dear Staffordshire County Council,', + council => '2240', + cobrand => 'lichfielddc', + url => '', + }, { + %common, + desc => 'directs NI correctly, 1', + unset_whendef => 1, + email_count => 1, + dear => qr'Dear Ballymoney Borough Council', + to => qr'Ballymoney Borough Council', + council => 14279, + category => 'Graffiti', + }, { + %common, + desc => 'directs NI correctly, 2', + unset_whendef => 1, + email_count => 1, + dear => qr'Dear Roads Service \(Western\)', + to => qr'Roads Service \(Western\)" <roads', + council => 14279, + category => 'Street lighting', + }, { + %common, + desc => 'does not send to unconfirmed contact', + unset_whendef => 1, + stays_unsent => 1, + email_count => 0, + council => 2636, }, ) { subtest $test->{ desc } => sub { @@ -437,14 +491,19 @@ foreach my $test ( { } )->update( { whensent => \'ms_current_timestamp()' } ); + if ( $test->{cobrand} && $test->{cobrand} =~ /lichfielddc/ && !FixMyStreet::Cobrand->exists('lichfielddc') ) { + plan skip_all => 'Skipping Lichfield tests without Lichfield cobrand'; + } + $problem->discard_changes; $problem->update( { council => $test->{ council }, state => 'confirmed', confirmed => \'ms_current_timestamp()', whensent => $test->{ unset_whendef } ? undef : \'ms_current_timestamp()', - category => 'potholes', + category => $test->{ category } || 'potholes', name => $test->{ name }, + cobrand => $test->{ cobrand } || 'fixmystreet', } ); FixMyStreet::App->model('DB::Problem')->send_reports(); @@ -465,9 +524,19 @@ foreach my $test ( { like $email->body, $test->{ missing }, 'missing council information correct'; } + if ( $test->{url} ) { + (my $base_url = FixMyStreet->config('BASE_URL')) =~ s{http://}{}; + my $id = $problem->id; + like $email->body, qr[$test->{url}$base_url/report/$id], 'URL present is correct'; + } + $problem->discard_changes; ok defined( $problem->whensent ), 'whensent set'; } + if ( $test->{stays_unsent} ) { + $problem->discard_changes; + ok !defined( $problem->whensent ), 'whensent not set'; + } }; } @@ -475,4 +544,8 @@ $problem->comments->delete; $problem->delete; $user->delete; +foreach (@contacts) { + $_->delete; +} + done_testing(); diff --git a/t/app/sendreport/email.t b/t/app/sendreport/email.t new file mode 100644 index 000000000..f0e1f153a --- /dev/null +++ b/t/app/sendreport/email.t @@ -0,0 +1,77 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More; + +use FixMyStreet; +use FixMyStreet::App; +use FixMyStreet::DB::Result::Contact; +use FixMyStreet::SendReport::Email; +use FixMyStreet::TestMech; +use mySociety::Locale; + +my $e = FixMyStreet::SendReport::Email->new(); + +my $contact = FixMyStreet::App->model('DB::Contact')->find_or_create( + email => 'council@example.com', + area_id => 1000, + category => 'category', + confirmed => 1, + deleted => 0, + editor => 'test suite', + whenedited => DateTime->now, + note => '', +); + +my $row = FixMyStreet::App->model('DB::Problem')->new( { + council => '1000', + category => 'category', +} ); + +ok $e; + +foreach my $test ( { + desc => 'no councils added means no receipients', + count => 0, + add_council => 0, + }, + { + desc => 'adding a council results in receipients', + count => 1, + add_council => 1, + }, + { + desc => 'unconfirmed contact results in no receipients', + count => undef, + add_council => 1, + unconfirmed => 1, + expected_note => 'Council 1000 deleted', + }, + { + desc => 'unconfirmed contact note uses note from contact table', + count => undef, + add_council => 1, + unconfirmed => 1, + note => 'received bounced so unconfirmed', + expected_note => 'received bounced so unconfirmed', + }, +) { + subtest $test->{desc} => sub { + my $e = FixMyStreet::SendReport::Email->new; + $contact->update( { confirmed => 0 } ) if $test->{unconfirmed}; + $contact->update( { note => $test->{note} } ) if $test->{note}; + $e->add_council( 1000, { name => 'test council' } ) if $test->{add_council}; + is $e->build_recipient_list( $row, {} ), $test->{count}, 'correct recipient list count'; + + if ( $test->{unconfirmed} ) { + is_deeply $e->unconfirmed_counts, { 'council@example.com' => { 'category' => 1 } }, 'correct unconfirmed_counts count'; + is_deeply $e->unconfirmed_notes, { 'council@example.com' => { 'category' => $test->{expected_note} } }, 'correct note used'; + } + }; +} + +$contact->delete; + +done_testing(); |