diff options
Diffstat (limited to 't/app/model')
-rw-r--r-- | t/app/model/alert_type.t | 2 | ||||
-rw-r--r-- | t/app/model/extra.t | 111 | ||||
-rw-r--r-- | t/app/model/problem.t | 97 |
3 files changed, 157 insertions, 53 deletions
diff --git a/t/app/model/alert_type.t b/t/app/model/alert_type.t index 5b7494a04..528c4d354 100644 --- a/t/app/model/alert_type.t +++ b/t/app/model/alert_type.t @@ -150,7 +150,7 @@ for my $test ( like $body, qr/$msg/, 'email says problem is ' . $test->{state}; if ($to eq $user->email) { - like $body, qr{/M/}, 'contains problem login url'; + like $body, qr{/R/}, 'contains problem login url'; } elsif ($to eq $user3->email) { like $body, qr{/report/$report_id}, 'contains problem url'; } diff --git a/t/app/model/extra.t b/t/app/model/extra.t new file mode 100644 index 000000000..21c37336e --- /dev/null +++ b/t/app/model/extra.t @@ -0,0 +1,111 @@ +use strict; +use warnings; +use Test::More; +use utf8; + +use FixMyStreet::App; +use Data::Dumper; +use DateTime; + +my $c = FixMyStreet::App->new; + +my $db = FixMyStreet::App->model('DB')->schema; +$db->txn_begin; + +my $body = $db->resultset('Body')->create({ name => 'ExtraTestingBody' }); + +my $serial = 1; +sub get_test_contact { + my $extra = shift; + my $contact = $db->resultset('Contact')->create({ + category => "Testing ${serial}", + body => $body, + email => 'test@example.com', + confirmed => 1, + deleted => 0, + editor => 'test script', + note => 'test script', + whenedited => DateTime->now(), + $extra ? ( extra => $extra ) : (), + }); + $serial++; + return $contact; +} + +subtest 'Old list layout transparently upgraded' => sub { + + subtest 'layout' => sub { + my $contact = get_test_contact([]); + + is_deeply $contact->get_extra(), { _fields => [] }, 'transparently upgraded to a hash'; + }; + + subtest 'extra fields' => sub { + my $contact = get_test_contact([]); + + is_deeply $contact->get_extra_fields(), [], 'No extra fields'; + + my @fields = ( { a => 1 }, { b => 2 } ); + $contact->set_extra_fields(@fields); + is_deeply $contact->extra, { _fields => \@fields }, 'extra fields set...'; + $contact->update; + $contact->discard_changes; + is_deeply $contact->extra, { _fields => \@fields }, '...and retrieved'; + is_deeply $contact->get_extra_fields(), \@fields, 'extra fields returned'; + }; + + subtest 'metadata' => sub { + my $contact = get_test_contact([]); + is_deeply $contact->get_extra_metadata_as_hashref(), {}, 'No extra metadata'; + + $contact->set_extra_metadata('foo' => 'bar'); + is $contact->get_extra_metadata('foo'), 'bar', 'extra metadata set...'; + $contact->update; + $contact->discard_changes; + is $contact->get_extra_metadata('foo'), 'bar', '... and retrieved'; + is_deeply $contact->get_extra_metadata_as_hashref(), { foo => 'bar' }, 'No extra metadata'; + }; +}; + +subtest 'Default hash layout' => sub { + subtest 'layout' => sub { + my $contact = get_test_contact(); + + is_deeply $contact->get_extra(), {}, 'default layout is hash'; + }; + + subtest 'extra fields' => sub { + my $contact = get_test_contact(); + + is_deeply $contact->get_extra_fields(), [], 'No extra fields'; + + my @fields = ( { a => 1 }, { b => 2 } ); + $contact->set_extra_fields(@fields); + is_deeply $contact->get_extra_fields, \@fields, 'extra fields set...'; + $contact->update; + $contact->discard_changes; + is_deeply $contact->get_extra_fields(), \@fields, '... and returned'; + is_deeply $contact->extra, { _fields => \@fields }, '(sanity check layout)'; + }; + + subtest 'metadata' => sub { + my $contact = get_test_contact(); + is_deeply $contact->get_extra_metadata_as_hashref(), {}, 'No extra metadata'; + + $contact->set_extra_metadata('foo' => 'bar'); + is $contact->get_extra_metadata('foo'), 'bar', 'extra metadata set...'; + $contact->update; + $contact->discard_changes; + is $contact->get_extra_metadata( 'foo'), 'bar', '... and retrieved'; + is_deeply $contact->get_extra_metadata_as_hashref(), { foo => 'bar' }, 'No extra metadata'; + + $contact->unset_extra_metadata('foo'); + is $contact->get_extra_metadata('foo'), undef, 'extra metadata now unset'; + $contact->update; + $contact->discard_changes; + is $contact->get_extra_metadata('foo'), undef, '... after retrieval'; + }; +}; + +$db->txn_rollback; +done_testing(); diff --git a/t/app/model/problem.t b/t/app/model/problem.t index c40e9e022..ad82a62a5 100644 --- a/t/app/model/problem.t +++ b/t/app/model/problem.t @@ -18,8 +18,8 @@ my $problem_rs = FixMyStreet::App->model('DB::Problem'); my $problem = $problem_rs->new( { postcode => 'EH99 1SP', - latitude => '51.5016605453401', - longitude => '-0.142497580865087', + latitude => '51.5', + longitude => '-0.1', areas => 1, title => '', detail => '', @@ -33,6 +33,23 @@ my $problem = $problem_rs->new( } ); +my $visible_states = $problem->visible_states; +is_deeply $visible_states, { + 'confirmed' => 1, + 'investigating' => 1, + 'in progress' => 1, + 'planned' => 1, + 'action scheduled' => 1, + 'fixed' => 1, + 'fixed - council' => 1, + 'fixed - user' => 1, + 'unable to fix' => 1, + 'not responsible' => 1, + 'duplicate' => 1, + 'closed' => 1, + 'internal referral' => 1, + }, 'visible_states is correct'; + is $problem->confirmed, undef, 'inflating null confirmed ok'; is $problem->whensent, undef, 'inflating null confirmed ok'; is $problem->lastupdate, undef, 'inflating null confirmed ok'; @@ -50,30 +67,6 @@ for my $test ( } }, { - desc => 'name too short', - changed => { - name => 'xx', - }, - errors => { - title => 'Please enter a subject', - detail => 'Please enter some details', - bodies => 'No council selected', - name => 'Please enter your full name, councils need this information – if you do not wish your name to be shown on the site, untick the box below', - } - }, - { - desc => 'name is anonymous', - changed => { - name => 'anonymous', - }, - errors => { - title => 'Please enter a subject', - detail => 'Please enter some details', - bodies => 'No council selected', - name => 'Please enter your full name, councils need this information – if you do not wish your name to be shown on the site, untick the box below', - } - }, - { desc => 'correct name', changed => { name => 'A User', @@ -369,15 +362,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' }, @@ -387,13 +373,19 @@ for my $body ( { area_id => 14279, name => 'Ballymoney Borough Council' }, { area_id => 2636, name => 'Isle of Wight Council' }, { area_id => 2649, name => 'Fife Council' }, + { area_id => 14279, name => 'TransportNI (Western)' }, ) { 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}); + if ($body_ids{$aid}) { + $body_ids{$aid} = [ $body_ids{$aid}, $body->id ]; + } else { + $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', @@ -415,11 +407,11 @@ for my $contact ( { category => 'potholes', email => 'highways@example.com', }, { - body_id => $body_ids{14279}, # Ballymoney + body_id => $body_ids{14279}[1], # TransportNI category => 'Street lighting', email => 'roads.western@drdni.example.org', }, { - body_id => $body_ids{14279}, # Ballymoney + body_id => $body_ids{14279}[0], # Ballymoney category => 'Graffiti', email => 'highways@example.com', }, { @@ -428,9 +420,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 = ( @@ -496,17 +486,19 @@ foreach my $test ( { email_count => 1, dear => qr'Dear Ballymoney Borough Council', to => qr'Ballymoney Borough Council', - body => $body_ids{14279}, + body => $body_ids{14279}[0], category => 'Graffiti', + longitude => -9.5, }, { %common, desc => 'directs NI correctly, 2', unset_whendef => 1, email_count => 1, - dear => qr'Dear Roads Service \(Western\)', - to => qr'Roads Service \(Western\)" <roads', - body => $body_ids{14279}, + dear => qr'Dear TransportNI \(Western\)', + to => qr'TransportNI \(Western\)" <roads', + body => $body_ids{14279}[1], category => 'Street lighting', + longitude => -9.5, }, { %common, desc => 'does not send to unconfirmed contact', @@ -543,6 +535,7 @@ foreach my $test ( { category => $test->{ category } || 'potholes', name => $test->{ name }, cobrand => $test->{ cobrand } || 'fixmystreet', + longitude => $test->{longitude} || '-0.1', } ); FixMyStreet::override_config $override, sub { @@ -558,6 +551,11 @@ foreach my $test ( { like $email->body, qr/A user of FixMyStreet/, 'email body looks a bit like a report'; like $email->body, qr/Subject: A Title/, 'more email body checking'; like $email->body, $test->{ dear }, 'Salutation looks correct'; + if ($test->{longitude}) { + like $email->body, qr{Easting/Northing \(IE\): 95938/28531}; + } else { + like $email->body, qr{Easting/Northing: }; + } if ( $test->{multiple} ) { like $email->body, qr/This email has been sent to several councils /, 'multiple body text correct'; @@ -592,10 +590,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; @@ -756,9 +751,7 @@ END { $problem->delete if $problem; $mech->delete_user( $user ) if $user; - foreach (@contacts) { - $_->delete; - } + $mech->delete_body($_) for @bodies; done_testing(); } |