aboutsummaryrefslogtreecommitdiffstats
path: root/bin/fixmystreet.com/fixture
diff options
context:
space:
mode:
Diffstat (limited to 'bin/fixmystreet.com/fixture')
-rwxr-xr-xbin/fixmystreet.com/fixture179
1 files changed, 157 insertions, 22 deletions
diff --git a/bin/fixmystreet.com/fixture b/bin/fixmystreet.com/fixture
index 93982af8a..2636ec1cc 100755
--- a/bin/fixmystreet.com/fixture
+++ b/bin/fixmystreet.com/fixture
@@ -1,11 +1,12 @@
#!/usr/bin/env perl
#
-# This script will create a test body and its categories, covering the area of
-# Westminster, and a user associated with that body, which should help testing
+# This script will create a test body and its categories, covering the area
+# provided, and users associated with that body, which should help testing
# of report interactions.
use strict;
use warnings;
+use v5.14;
BEGIN {
use File::Basename qw(dirname);
@@ -14,26 +15,160 @@ BEGIN {
require "$d/../../setenv.pl";
}
-use FixMyStreet::DB;
-
-my $body = FixMyStreet::DB->resultset("Body")->find_or_create({ name => 'Test City Council' });
-$body->body_areas->find_or_create({ area_id => 2504 });
-foreach ("Potholes", "Street lighting", "Graffiti") {
- (my $email = lc $_) =~ s/ /-/g;
- $body->contacts->find_or_create({
- category => $_,
- email => $email . '@example.net',
- confirmed => 't',
- deleted => 'f',
- whenedited => \'current_timestamp',
- editor => 'fixture',
- note => 'Created by fixture'
- });
+use FixMyStreet;
+use FixMyStreet::Cobrand;
+use FixMyStreet::DB::Factories;
+use DateTime::Format::Pg;
+use Getopt::Long::Descriptive;
+
+my ($opt, $usage) = describe_options(
+ '%c %o',
+ [ 'area-id=i', "MapIt area ID to create body for", { required => 1 } ],
+ [ 'name:s', "Name of body to use (defaults to MapIt area name)" ],
+ [ 'empty', "Empty all tables of the database first" ],
+ [ 'commit', "Actually commit changes to the database" ],
+ [ 'coords=s', "Co-ordinates to use instead of example postcode" ],
+ [ 'help', "print usage message and exit", { shortcircuit => 1 } ],
+);
+print($usage->text), exit if $opt->help;
+
+my $db = FixMyStreet::DB->schema->storage;
+$db->txn_begin;
+END {
+ if ($db) {
+ $opt->commit ? $db->txn_commit : $db->txn_rollback;
+ }
+}
+if (!$opt->commit) {
+ say "NOT COMMITTING TO DATABASE";
+}
+
+if ($opt->empty) {
+ $db->dbh->do(q{
+DO
+$func$
+BEGIN
+ EXECUTE
+ (SELECT 'TRUNCATE TABLE ' || string_agg(quote_ident(tablename), ', ') || ' RESTART IDENTITY CASCADE '
+ FROM pg_tables WHERE schemaname='public');
+END
+$func$;
+}) or die $!;
+ $db->dbh->do( scalar FixMyStreet->path_to('db/fixture.sql')->slurp ) or die $!;
+ $db->dbh->do( scalar FixMyStreet->path_to('db/generate_secret.sql')->slurp ) or die $!;
+ say "Emptied database";
}
-FixMyStreet::DB->resultset("User")->find_or_create({
- email => 'council@example.net',
- name => 'Test City Council User',
- from_body => $body,
- password => 'password',
+# Body + categories
+my $categories = ['Potholes', 'Street lighting', 'Graffiti', 'Other'];
+my $body = FixMyStreet::DB::Factory::Body->find_or_create({
+ area_id => $opt->area_id,
+ categories => $categories,
+ $opt->name ? (name => $opt->name) : (),
});
+say "Created body " . $body->name . " for MapIt area ID " . $opt->area_id . ', categories ' . join(', ', @$categories);
+
+FixMyStreet::DB::Factory::ResponseTemplate->create({ body => $body, title => 'Generic' });
+FixMyStreet::DB::Factory::ResponseTemplate->create({ body => $body, title => 'Fixed', state => 'fixed - council' });
+FixMyStreet::DB::Factory::ResponseTemplate->create({ body => $body, title => 'Monitoring', state => 'unable to fix' });
+my $template = FixMyStreet::DB::Factory::ResponseTemplate->create({ body => $body, title => 'Not us', state => 'not responsible' });
+$template->add_to_contacts($body->contacts->first);
+my $priority = FixMyStreet::DB::Factory::ResponsePriority->create_batch(3, { body => $body });
+$priority->[0]->add_to_contacts($body->contacts->first);
+
+# Users
+say "Created users, all with password 'password':";
+my %users;
+my $perms_inspector = ['report_inspect', 'planned_reports'];
+my $perms_cs = [
+ 'contribute_as_body', 'contribute_as_another_user',
+ 'moderate', 'view_body_contribute_details',
+];
+foreach (
+ { name => 'Inspector Gadget', email => 'inspector@example.org', body => $body, permissions => $perms_inspector },
+ { name => 'Harriet Helpful', email => 'cs@example.org', body => $body, permissions => $perms_cs },
+ { name => 'Super User', email => 'super@example.org', body => $body, permissions => [
+ @$perms_cs, @$perms_inspector, 'report_edit',
+ 'category_edit', 'template_edit', 'responsepriority_edit',
+ 'user_assign_body', 'user_manage_permissions', 'user_edit',
+ ] },
+ { name => 'Wizard of Oz', email => 'admin@example.org', is_superuser => 't' },
+ { name => "Norma User", email => 'user@example.org' },
+) {
+ $users{$_->{email}} = FixMyStreet::DB::Factory::User->find_or_create($_);
+ my $perms = $_->{permissions} ? " (" . join(', ', @{$_->{permissions} || []}) . ")" : "";
+ my $su = $_->{is_superuser} ? " (superuser)" : "";
+ say "* $_->{email}$perms$su";
+}
+
+# Problems
+
+my %titles = (
+ 'Potholes' => ['Deep pothole', 'Small pothole', 'Pothole in cycle lane', 'Pothole on busy pavement', 'Large pothole', 'Sinking manhole'],
+ 'Street lighting' => ['Faulty light', 'Street light not working', 'Lights out in tunnel', 'Light not coming on', 'Light not going off'],
+ 'Graffiti' => ['Graffiti', 'Graffiti', 'Offensive graffiti', 'Graffiti on the bridge', 'Remove graffiti'],
+ 'Other' => ['Loose drain cover', 'Flytipping on country lane', 'Vehicle blocking footpath', 'Hedge encroaching pavement', 'Full litter bins'],
+);
+
+my ($location, $lat, $lon);
+if ($opt->coords) {
+ $location = $opt->coords;
+ ($lat, $lon) = split ',', $location;
+} else {
+ my $postcode = mySociety::MaPit::call('area/example_postcode', $opt->area_id);
+ $postcode = mySociety::MaPit::call('postcode', $postcode);
+ ($location, $lat, $lon) = map { $postcode->{$_} } qw/postcode wgs84_lat wgs84_lon/;
+}
+
+my $cobrand = 'default';
+foreach (FixMyStreet::Cobrand->available_cobrand_classes) {
+ my $sub = $_->{class} && $_->{class}->can('council_area_id');
+ if ($sub && &$sub == $opt->area_id) {
+ $cobrand = $_->{class}->moniker;
+ last;
+ }
+}
+
+my $user = $users{'user@example.org'};
+my $num = 20;
+say "Created $num problems around '$location' in cobrand '$cobrand'";
+my $inaccurate_km = 0.01;
+my $confirmed = DateTime->today->subtract(days => 1)->add(hours => 8);
+my $problems = [];
+for (1..$num) {
+ $confirmed->add(seconds => rand(7000));
+ my $category = $categories->[int(rand(@$categories))];
+ my $titles = $titles{$category};
+ push @$problems, FixMyStreet::DB::Factory::Problem->create({
+ body => $body,
+ areas => ',' . $opt->area_id . ',',
+ user => $user,
+ postcode => $location,
+ latitude => $lat + rand(2 * $inaccurate_km) - $inaccurate_km,
+ longitude => $lon + rand(3 * $inaccurate_km) - 1.5 * $inaccurate_km,
+ category => $category,
+ cobrand => $cobrand,
+ title => $titles->[int(rand(@$titles))],
+ detail => 'The details of the report would be here, provided by the user, including exact location and how long it has been here.',
+ confirmed => DateTime::Format::Pg->format_datetime($confirmed),
+ });
+}
+
+for (1..3) {
+ my $p = $problems->[int(rand(@$problems))];
+ $users{'inspector@example.org'}->add_to_planned_reports($p);
+}
+
+# Comment
+
+my $updates = [];
+for (1..$num) {
+ $confirmed->add(seconds => rand(1000));
+ push @$updates, FixMyStreet::DB::Factory::Comment->create({
+ problem => $problems->[int(rand(@$problems))],
+ user => $user,
+ text => 'This is an update on this problem, perhaps providing further relevant information or letting everyone know it is being worked on or fixed.',
+ confirmed => DateTime::Format::Pg->format_datetime($confirmed),
+ });
+}
+say "Created $num updates on problems " . join(', ', map { $_->problem_id } @$updates);