1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
#!/usr/bin/perl -w
#
# Page.t:
# Tests for the Page functions
#
# Copyright (c) 2009 UK Citizens Online Democracy. All rights reserved.
# Email: louise@mysociety.org; WWW: http://www.mysociety.org/
#
# $Id: Page.t,v 1.12 2009-12-09 13:34:36 louise Exp $
#
use strict;
use warnings;
use Test::More tests => 13;
use Test::Exception;
use FindBin;
use lib "$FindBin::Bin";
use lib "$FindBin::Bin/../perllib";
use lib "$FindBin::Bin/../commonlib/perllib";
use Page;
use FixMyStreet::Geocode;
use mySociety::MockQuery;
use mySociety::Locale;
sub mock_query(){
my $q = new MockQuery('mysite');
return $q;
}
sub set_lang($) {
my $lang = shift;
mySociety::Locale::negotiate_language($lang);
mySociety::Locale::gettext_domain('FixMyStreet');
mySociety::Locale::change();
}
sub test_geocode_string() {
my %params = ();
my $q = new MockQuery( 'nosite', \%params );
# geocode a straightforward string, expect success
my ( $latitude, $longitude, $error ) =
FixMyStreet::Geocode::string( 'Buckingham Palace', $q );
is( $latitude, 51.5013639, 'example easting generated' );
is( $longitude, -0.1418898, 'example northing generated' );
is( $error, undef, 'should not generate error for simple example' );
# expect a failure message for Northern Ireland
( $latitude, $longitude, $error ) =
FixMyStreet::Geocode::string( 'Falls Road, Belfast', $q );
is(
$error,
"We do not cover Northern Ireland, I'm afraid, as our licence doesn't "
. "include any maps for the region.",
'error message produced for NI location'
);
}
sub test_header() {
my $q = mock_query();
my $html;
my %params = (title => 'test title');
set_lang('en-gb,English,en_GB');
# Test that param that isn't explicitly allowed raises error
$params{'test-param'} = 'test';
throws_ok { Page::header($q, %params); } qr/bad parameter/, 'bad parameter caught ok';
delete $params{'test-param'};
# Test that template passed is rendered
$params{'template'} = 'test';
$html = Page::template_include('test-header', $q,
'/../t/templates/' . $q->{site} . '/',
title => 'My test title', lang => 'en-gb'
);
like ($html, qr/My test header template/, 'named template rendered ok');
return 1;
}
sub test_footer(){
return 1;
}
sub test_base_url_with_lang {
set_lang('en-gb,English,en_GB');
my $q = mock_query();
my $url = Page::base_url_with_lang($q);
ok($url eq 'http://mysite.example.com', 'Basic url rendered ok');
$q = new MockQuery('emptyhomes');
$url = Page::base_url_with_lang($q);
like($url, qr/http:\/\/en\.emptyhomes\./, 'Empty homes url with lang returned ok');
$url = Page::base_url_with_lang($q, 1);
like($url, qr/http:\/\/cy\.emptyhomes\./, 'Empty homes url with lang reversed returned ok');
}
ok(test_base_url_with_lang() == 1, 'Ran all tests for base_url_with_lang');
ok(test_footer() == 1, 'Ran all tests for the footer function');
ok(test_header() == 1, 'Ran all tests for the header function');
ok(test_geocode_string() == 1, 'Ran all tests for the geocode_string function');
|