aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Somerville <matthew@mysociety.org>2015-03-13 18:04:55 +0000
committerMatthew Somerville <matthew@mysociety.org>2015-03-13 21:54:29 +0000
commit76e533bd318cb4208faa1bf5cd0c00bde9e08e6c (patch)
tree7ada0b46c3e75b4d3977ca27960638c191354cc7
parente22563a5c8fde74a8a6f1de29948eb45487e9597 (diff)
Factor out test contact creation to function.
-rw-r--r--perllib/FixMyStreet/TestMech.pm35
-rw-r--r--t/app/controller/my.t3
-rw-r--r--t/app/controller/report_import.t4
-rw-r--r--t/app/controller/report_new.t73
-rw-r--r--t/app/controller/report_new_open311.t26
-rw-r--r--t/app/controller/report_updates.t1
-rw-r--r--t/app/model/problem.t27
-rw-r--r--t/app/sendreport/email.t15
-rw-r--r--t/cobrand/fixamingata.t9
-rw-r--r--t/cobrand/fixmybarangay.t20
-rw-r--r--t/cobrand/zurich.t9
11 files changed, 83 insertions, 139 deletions
diff --git a/perllib/FixMyStreet/TestMech.pm b/perllib/FixMyStreet/TestMech.pm
index 72a6810bc..cd846dcd8 100644
--- a/perllib/FixMyStreet/TestMech.pm
+++ b/perllib/FixMyStreet/TestMech.pm
@@ -151,23 +151,21 @@ sub delete_user {
->find( { email => $email_or_user } );
# If no user found we can't delete them
- if ( !$user ) {
- ok( 1, "No user found to delete" );
- return 1;
- }
+ return 1 unless $user;
+
+ $mech->get('/auth/sign_out');
- $mech->log_out_ok;
for my $p ( $user->problems ) {
- ok( $_->delete, "delete comment " . $_->text ) for $p->comments;
- ok( $_->delete, "delete questionnaire " . $_->id ) for $p->questionnaires;
- ok( $p->delete, "delete problem " . $p->title );
+ $p->comments->delete;
+ $p->questionnaires->delete;
+ $p->delete;
}
for my $a ( $user->alerts ) {
$a->alerts_sent->delete;
- ok( $a->delete, "delete alert " . $a->alert_type );
+ $a->delete;
}
- ok( $_->delete, "delete comment " . $_->text ) for $user->comments;
- ok $user->delete, "delete test user " . $user->email;
+ $_->delete for $user->comments;
+ $user->delete;
return 1;
}
@@ -559,6 +557,21 @@ sub delete_problems_for_body {
}
}
+sub create_contact_ok {
+ my $self = shift;
+ my %contact_params = (
+ confirmed => 1,
+ deleted => 0,
+ editor => 'Test',
+ whenedited => \'current_timestamp',
+ note => 'Created for test',
+ @_
+ );
+ my $contact = FixMyStreet::App->model('DB::Contact')->find_or_create( \%contact_params );
+ ok $contact, 'found/created contact ' . $contact->category;;
+ return $contact;
+}
+
sub create_body_ok {
my $self = shift;
my ( $area_id, $name, %extra ) = @_;
diff --git a/t/app/controller/my.t b/t/app/controller/my.t
index da509e8ed..b723e537e 100644
--- a/t/app/controller/my.t
+++ b/t/app/controller/my.t
@@ -1,7 +1,7 @@
use strict;
use warnings;
-use Test::More tests => 11;
+use Test::More;
use FixMyStreet::TestMech;
my $mech = FixMyStreet::TestMech->new;
@@ -17,3 +17,4 @@ is $mech->uri->path, '/my', "stayed on '/my/' page";
# cleanup
$mech->delete_user( $user );
+done_testing();
diff --git a/t/app/controller/report_import.t b/t/app/controller/report_import.t
index e5e9e2899..0631b4209 100644
--- a/t/app/controller/report_import.t
+++ b/t/app/controller/report_import.t
@@ -372,3 +372,7 @@ subtest "Submit a correct entry (with location) to cobrand" => sub {
};
done_testing();
+
+END {
+ $mech->delete_body($body);
+}
diff --git a/t/app/controller/report_new.t b/t/app/controller/report_new.t
index 49ff7b69e..761fdad30 100644
--- a/t/app/controller/report_new.t
+++ b/t/app/controller/report_new.t
@@ -2,7 +2,6 @@ use strict;
use utf8; # sign in error message has &ndash; in it
use warnings;
use Test::More;
-use utf8;
use FixMyStreet::TestMech;
use FixMyStreet::App;
@@ -35,15 +34,8 @@ subtest "test that bare requests to /report/new get redirected" => sub {
"pc correctly transferred";
};
-my %contact_params = (
- confirmed => 1,
- deleted => 0,
- editor => 'Test',
- whenedited => \'current_timestamp',
- note => 'Created for test',
-);
-
my %body_ids;
+my @bodies;
for my $body (
{ area_id => 2651, name => 'City of Edinburgh Council' },
{ area_id => 2226, name => 'Gloucestershire County Council' },
@@ -56,62 +48,46 @@ for my $body (
{ area_id => 2333, name => 'Hart Council', id => 2333 },
) {
my $body_obj = $mech->create_body_ok($body->{area_id}, $body->{name}, id => $body->{id});
+ push @bodies, $body_obj;
$body_ids{$body->{area_id}} = $body_obj->id;
}
# 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,
+my $contact1 = $mech->create_contact_ok(
body_id => $body_ids{2651}, # Edinburgh
category => 'Street lighting',
email => 'highways@example.com',
-} );
-my $contact2 = FixMyStreet::App->model('DB::Contact')->find_or_create( {
- %contact_params,
+);
+my $contact2 = $mech->create_contact_ok(
body_id => $body_ids{2226}, # Gloucestershire
category => 'Potholes',
email => 'potholes@example.com',
-} );
-my $contact3 = FixMyStreet::App->model('DB::Contact')->find_or_create( {
- %contact_params,
+);
+my $contact3 = $mech->create_contact_ok(
body_id => $body_ids{2326}, # Cheltenham
category => 'Trees',
email => 'trees@example.com',
-} );
-my $contact4 = FixMyStreet::App->model('DB::Contact')->find_or_create( {
- %contact_params,
+);
+my $contact4 = $mech->create_contact_ok(
body_id => $body_ids{2482}, # Bromley
category => 'Trees',
email => 'trees@example.com',
-} );
-my $contact5 = FixMyStreet::App->model('DB::Contact')->find_or_create( {
- %contact_params,
+);
+my $contact5 = $mech->create_contact_ok(
body_id => $body_ids{2651}, # Edinburgh
category => 'Trees',
email => 'trees@example.com',
-} );
-my $contact6 = FixMyStreet::App->model('DB::Contact')->find_or_create( {
- %contact_params,
+);
+my $contact6 = $mech->create_contact_ok(
body_id => $body_ids{2333}, # Hart
category => 'Trees',
email => 'trees@example.com',
-} );
-my $contact7 = FixMyStreet::App->model('DB::Contact')->find_or_create( {
- %contact_params,
+);
+my $contact7 = $mech->create_contact_ok(
body_id => $body_ids{2227}, # Hampshire
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.
@@ -1356,13 +1332,12 @@ subtest "test SeeSomething" => sub {
my $cobrand = FixMyStreet::Cobrand::SeeSomething->new();
my $body_ss = $mech->create_body_ok(2535, 'Sandwell Borough Council', id => 2535);
- my $bus_contact = FixMyStreet::App->model('DB::Contact')->find_or_create( {
- %contact_params,
+ my $bus_contact = $mech->create_contact_ok(
body_id => $body_ss->id,
category => 'Bus',
email => 'bus@example.com',
non_public => 1,
- } );
+ );
for my $test ( {
desc => 'report with no user details works',
@@ -1597,12 +1572,8 @@ subtest "extra google analytics code displayed on email confirmation problem cre
};
};
-$contact1->delete;
-$contact2->delete;
-$contact3->delete;
-$contact4->delete;
-$contact5->delete;
-$contact6->delete;
-$contact7->delete;
-
done_testing();
+
+END {
+ $mech->delete_body($_) foreach @bodies;
+}
diff --git a/t/app/controller/report_new_open311.t b/t/app/controller/report_new_open311.t
index a274d918b..15ccef493 100644
--- a/t/app/controller/report_new_open311.t
+++ b/t/app/controller/report_new_open311.t
@@ -19,16 +19,8 @@ $body->update({
api_key => 'apikey',
});
-my %contact_params = (
- confirmed => 1,
- deleted => 0,
- editor => 'Test',
- whenedited => \'current_timestamp',
- note => 'Created for test',
-);
# Let's make some contacts to send things to!
-my $contact1 = FixMyStreet::App->model('DB::Contact')->find_or_create( {
- %contact_params,
+my $contact1 = $mech->create_contact_ok(
body_id => $body->id, # Edinburgh
category => 'Street lighting',
email => '100',
@@ -37,15 +29,12 @@ my $contact1 = FixMyStreet::App->model('DB::Contact')->find_or_create( {
{ value => [ { name => ['Gas'], key => ['old'] }, { name => [ 'Yellow' ], key => [ 'modern' ] } ] }
}
],
-} );
-my $contact2 = FixMyStreet::App->model('DB::Contact')->find_or_create( {
- %contact_params,
+);
+my $contact2 = $mech->create_contact_ok(
body_id => $body->id, # Edinburgh
category => 'Graffiti Removal',
email => '101',
-} );
-ok $contact1, "created test contact 1";
-ok $contact2, "created test contact 2";
+);
# test that the various bit of form get filled in and errors correctly
# generated.
@@ -176,7 +165,8 @@ foreach my $test (
};
}
-$contact1->delete;
-$contact2->delete;
-
done_testing();
+
+END {
+ $mech->delete_body($body);
+}
diff --git a/t/app/controller/report_updates.t b/t/app/controller/report_updates.t
index 58d9eff86..fa6c44292 100644
--- a/t/app/controller/report_updates.t
+++ b/t/app/controller/report_updates.t
@@ -789,6 +789,7 @@ subtest "check comment with no status change has not status in meta" => sub {
},
'submit update'
);
+ $mech->get_ok("/report/$report_id");
$report->discard_changes;
my @updates = $report->comments->all;
diff --git a/t/app/model/problem.t b/t/app/model/problem.t
index 94ac153d2..f88dc09b8 100644
--- a/t/app/model/problem.t
+++ b/t/app/model/problem.t
@@ -369,15 +369,8 @@ for my $test (
my $mech = FixMyStreet::TestMech->new();
-my %contact_params = (
- confirmed => 1,
- deleted => 0,
- editor => 'Test',
- whenedited => \'ms_current_timestamp()',
- note => 'Created for test',
-);
-
my %body_ids;
+my @bodies;
for my $body (
{ area_id => 2651, name => 'City of Edinburgh Council' },
{ area_id => 2226, name => 'Gloucestershire County Council' },
@@ -389,11 +382,12 @@ for my $body (
{ area_id => 2649, name => 'Fife Council' },
) {
my $aid = $body->{area_id};
- $body_ids{$aid} = $mech->create_body_ok($aid, $body->{name}, id => $body->{id})->id;
+ my $body = $mech->create_body_ok($aid, $body->{name});
+ $body_ids{$aid} = $body->id;
+ push @bodies, $body;
}
# Let's make some contacts to send things to!
-my @contacts;
for my $contact ( {
body_id => $body_ids{2651}, # Edinburgh
category => 'potholes',
@@ -428,9 +422,7 @@ for my $contact ( {
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;
+ $mech->create_contact_ok( %$contact );
}
my %common = (
@@ -600,10 +592,7 @@ subtest 'check can set mutiple emails as a single contact' => sub {
category => 'trees',
email => '2636@example.com,2636-2@example.com',
};
- my $new_contact = FixMyStreet::App->model('DB::Contact')->find_or_create( {
- %contact_params,
- %$contact } );
- ok $new_contact, "created multiple email test contact";
+ $mech->create_contact_ok( %$contact );
$mech->clear_emails_ok;
@@ -764,9 +753,7 @@ END {
$problem->delete if $problem;
$mech->delete_user( $user ) if $user;
- foreach (@contacts) {
- $_->delete;
- }
+ $mech->delete_body($_) for @bodies;
done_testing();
}
diff --git a/t/app/sendreport/email.t b/t/app/sendreport/email.t
index b2cab42ed..65cd7bfa8 100644
--- a/t/app/sendreport/email.t
+++ b/t/app/sendreport/email.t
@@ -7,11 +7,12 @@ use Test::More;
use FixMyStreet;
use FixMyStreet::App;
-use FixMyStreet::DB::Result::Contact;
use FixMyStreet::SendReport::Email;
use FixMyStreet::TestMech;
use mySociety::Locale;
+ok( my $mech = FixMyStreet::TestMech->new, 'Created mech object' );
+
my $e = FixMyStreet::SendReport::Email->new();
# area id 1000
@@ -19,14 +20,10 @@ my $params = { id => 1000, name => 'Council of the Thousand' };
my $body = FixMyStreet::App->model('DB::Body')->find_or_create($params);
ok $body, "found/created body";
-my $contact = FixMyStreet::App->model('DB::Contact')->find_or_create(
+my $contact = $mech->create_contact_ok(
email => 'council@example.com',
body_id => 1000,
category => 'category',
- confirmed => 1,
- deleted => 0,
- editor => 'test suite',
- whenedited => DateTime->now,
note => '',
);
@@ -78,6 +75,8 @@ foreach my $test ( {
};
}
-$contact->delete;
-
done_testing();
+
+END {
+ $mech->delete_body($body);
+}
diff --git a/t/cobrand/fixamingata.t b/t/cobrand/fixamingata.t
index 714d475e1..3c818474d 100644
--- a/t/cobrand/fixamingata.t
+++ b/t/cobrand/fixamingata.t
@@ -25,16 +25,11 @@ FixMyStreet::override_config {
$mech->content_like( qr/FixaMinGata/ );
my $body = $mech->create_body_ok( 1, 'Body' );
-FixMyStreet::App->model('DB::Contact')->find_or_create({
+$mech->create_contact_ok(
body => $body,
category => "Other",
email => "other\@example.org",
- confirmed => 1,
- deleted => 0,
- editor => "Editor",
- whenedited => \'now()',
- note => 'Note',
-});
+);
my @reports = $mech->create_problems_for_body( 1, $body->id, 'Test', {
cobrand => 'fixamingata',
diff --git a/t/cobrand/fixmybarangay.t b/t/cobrand/fixmybarangay.t
index edb14fe3d..316739dfa 100644
--- a/t/cobrand/fixmybarangay.t
+++ b/t/cobrand/fixmybarangay.t
@@ -37,28 +37,16 @@ FixMyStreet::App->model('DB::BodyArea')->find_or_create({ area_id => 2, body_id
# 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,
+$mech->create_contact_ok(
body_id => $luz->id,
category => 'Streetlight (BGY)',
email => 'bgy@example.com',
-} );
-my $contact2 = FixMyStreet::App->model('DB::Contact')->find_or_create( {
- %contact_params,
+);
+$mech->create_contact_ok(
body_id => $dps->id,
category => 'Streetlight (DPS)',
email => 'LIGHT',
-} );
+);
# Create a couple of reports
diff --git a/t/cobrand/zurich.t b/t/cobrand/zurich.t
index 8dbc43464..203bbc0f8 100644
--- a/t/cobrand/zurich.t
+++ b/t/cobrand/zurich.t
@@ -137,16 +137,11 @@ $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({
+ $mech->create_contact_ok(
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