aboutsummaryrefslogtreecommitdiffstats
path: root/t/Cobrand.t
blob: 1f38ec605ded009c32a548fdf9110529e8902fc8 (plain)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/usr/bin/perl -w
#
# Cobrand.t:
# Tests for the cobranding functions
#
#  Copyright (c) 2009 UK Citizens Online Democracy. All rights reserved.
# Email: louise@mysociety.org; WWW: http://www.mysociety.org/
#
# $Id: Cobrand.t,v 1.25 2009-12-16 12:43:12 matthew Exp $
#

use strict;
use warnings;
use Test::More tests => 59;
use Test::Exception;
use Error qw(:try);

use FindBin;
use lib "$FindBin::Bin";
use lib "$FindBin::Bin/../perllib";
use lib "$FindBin::Bin/../commonlib/perllib";

use Cobrand;
use mySociety::MockQuery;

sub test_site_restriction { 
    my ($site_restriction, $site_id) = Cobrand::site_restriction('mysite', 'test');
    like($site_restriction, qr/ and council = 1 /, 'should return result of cobrand module site_restriction function');
    ok($site_id == 99, 'should return result of cobrand module site_restriction function');    
    
    ($site_restriction, $site_id) = Cobrand::site_restriction('nosite', 'test');
    ok($site_restriction eq '', 'should return "" and zero if no module exists' );
    ok($site_id == 0, 'should return "" and zero if no module exists');
}

sub test_form_elements {
    my $q  = new MockQuery('mysite');
    my $element_html = Cobrand::form_elements('mysite', 'postcodeForm', $q);
    ok($element_html eq 'Extra html', 'should return result of cobrand module element_html function') or diag("Got $element_html");

    $element_html = Cobrand::form_elements('nosite', 'postcodeForm', $q);
    ok($element_html eq '', 'should return an empty string if no cobrand module exists') or diag("Got $element_html");
}

sub test_disambiguate_location {
    my $q  = new MockQuery('mysite');
    my $s = 'London Road';
    $s = Cobrand::disambiguate_location('mysite', $s, $q);
    ok($s eq 'Specific Location', 'should return result of cobrand module disambiguate_location function') or diag("Got $s");;
    
    $q = new MockQuery('nosite');
    $s = 'London Road';
    $s = Cobrand::disambiguate_location('nosite', $s, $q);
    ok($s eq 'London Road&gl=uk', 'should return location string as passed if no cobrand module exists') or diag("Got $s");
  
}

sub test_cobrand_handle {
    my $cobrand = 'mysite';
    my $handle = Cobrand::cobrand_handle($cobrand);
    like($handle->site_name(), qr/mysite/, 'should get a module handle if Util module exists for cobrand');
    $cobrand = 'nosite';    
    $handle = Cobrand::cobrand_handle($cobrand);
    ok($handle == 0, 'should return zero if no module exists');
}

sub test_extra_problem_data {
    my $cobrand = 'mysite'; 
    my $q = new MockQuery($cobrand);
   
    # should get the result of the page function in the cobrand module if one exists
    my $cobrand_data = Cobrand::extra_problem_data($cobrand, $q);
    ok($cobrand_data eq 'Cobrand problem data', 'extra_problem_data should return data from cobrand module') or diag("Got $cobrand_data");

    # should return an empty string if no cobrand module exists
    $q = new MockQuery('nosite');
    $cobrand_data = Cobrand::extra_problem_data('nosite', $q);
    ok($cobrand_data eq '', 'extra_problem_data should return an empty string if there is no cobrand module') or diag("Got $cobrand_data");
}

sub test_extra_update_data {
    my $cobrand = 'mysite';
    my $q = new MockQuery($cobrand);
   
    # should get the result of the page function in the cobrand module if one exists
    my $cobrand_data = Cobrand::extra_update_data($cobrand, $q);
    ok($cobrand_data eq 'Cobrand update data', 'extra_update_data should return data from cobrand module') or diag("Got $cobrand_data");

    # should return an empty string if no cobrand module exists
    $q = new MockQuery('nosite');
    $cobrand_data = Cobrand::extra_update_data('nosite', $q);
    ok($cobrand_data eq '', 'extra_update_data should return an empty string if there is no cobrand module') or diag("Got $cobrand_data");
}


sub test_extra_alert_data {
    my $cobrand = 'mysite';
    my $q = new MockQuery($cobrand);

    # should get the result of the page function in the cobrand module if one exists
    my $cobrand_data = Cobrand::extra_alert_data($cobrand, $q);
    ok($cobrand_data eq 'Cobrand alert data', 'extra_alert_data should return data from cobrand module') or diag("Got $cobrand_data");

    # should return an empty string if no cobrand module exists
    $q = new MockQuery('nosite');
    $cobrand_data = Cobrand::extra_alert_data('nosite', $q);
    ok($cobrand_data eq '', 'extra_alert_data should return an empty string if there is no cobrand module') or diag("Got $cobrand_data");
}

sub test_base_url {
    my $cobrand = 'mysite';

    # should get the result of the page function in the cobrand module if one exists
    my $base_url = Cobrand::base_url($cobrand);
    is('http://mysite.example.com', $base_url, 'base_url returns output from cobrand module');

    # should return the base url from the config if there is no cobrand module
    $cobrand = 'nosite';
    $base_url = Cobrand::base_url($cobrand);
    is(mySociety::Config::get('BASE_URL'), $base_url, 'base_url returns config base url if no cobrand module');

}

sub test_base_url_for_emails {
    my $cobrand = 'mysite';    

    # should get the results of the base_url_for_emails function in the cobrand module if one exists
    my $base_url = Cobrand::base_url_for_emails($cobrand);
    is('http://mysite.foremails.example.com', $base_url, 'base_url_for_emails returns output from cobrand module') ;

    # should return the result of Cobrand::base_url otherwise
    $cobrand = 'nosite';
    $base_url = Cobrand::base_url_for_emails($cobrand);
    is(mySociety::Config::get('BASE_URL'), $base_url, 'base_url_for_emails returns config base url if no cobrand module');

}

sub test_extra_params { 
    my $cobrand = 'mysite';    
    my $q = new MockQuery($cobrand);

    # should get the results of the extra_params function in the cobrand module if one exists
    my $extra_params = Cobrand::extra_params($cobrand, $q);
    is($extra_params, 'key=value', 'extra_params returns output from cobrand module') ;

    # should return an empty string otherwise
    $cobrand = 'nosite';
    $extra_params = Cobrand::extra_params($cobrand, $q);
    is($extra_params, '', 'extra_params returns an empty string if no cobrand module');
    
}

sub test_header_params {
    my $cobrand = 'mysite';
    my $q = new MockQuery($cobrand);

    # should get the results of the header_params function in the cobrand module if one exists
    my $header_params = Cobrand::header_params($cobrand, $q);
    is_deeply($header_params, {'key' => 'value'}, 'header_params returns output from cobrand module') ;

    # should return an empty string otherwise
    $cobrand = 'nosite';
    $header_params = Cobrand::header_params($cobrand, $q);
    is_deeply($header_params, {}, 'header_params returns an empty hash ref if no cobrand module');
}

sub test_root_path_js {
    my $cobrand = 'mysite';
    my $root_path_js = Cobrand::root_path_js($cobrand);
 
    # should get the results of the root_path_js function in the cobrand module if one exists
    is($root_path_js, 'root path js', 'root_path_js returns output from cobrand module');

    # should return a js string setting the root path to an empty string otherwise
    $cobrand = 'nosite';
    $root_path_js = Cobrand::root_path_js($cobrand);
    is($root_path_js, 'var root_path = "";', 'root_path_pattern returns a string setting the root path to an empty string if no cobrand module');
}

sub test_site_title {
    my $cobrand = 'mysite';
    my $site_title = Cobrand::site_title($cobrand);

    # should get the results of the site_title function in the cobrand module if one exists
    is($site_title,  'Mysite Title', 'site_title returns output from cobrand module');

    # should return an empty string otherwise
    $cobrand = 'nosite';
    $site_title = Cobrand::site_title($cobrand);
    is($site_title, '', 'site_title returns an empty string if no site title');
}

sub test_on_map_list_limit {
    my $cobrand = 'mysite';
    my $limit = Cobrand::on_map_list_limit($cobrand);
   
    is($limit, 30, 'on_map_list_limit returns output from cobrand module');

    $cobrand = 'nosite';
    $limit = Cobrand::on_map_list_limit($cobrand);
    is($limit, undef, 'on_map_list_limit returns undef if there is no limit defined by the cobrand');

}

sub test_url {
    my $cobrand = 'mysite';
    my $q = new MockQuery('mysite');
    my $url = Cobrand::url($cobrand, '/xyz', $q);
    is($url, '/transformed_url', 'url returns output from cobrand module');
    
    $cobrand = 'nosite';
    $url = Cobrand::url($cobrand, '/xyz', $q);
    is($url, '/xyz', 'url returns passed url if there is no url function defined by the cobrand'); 
}

sub test_allow_photo_upload {
    my $cobrand = 'mysite';
    my $photo_upload = Cobrand::allow_photo_upload($cobrand);
    is($photo_upload, 0, 'allow_photo_upload returns output from cobrand module');

    $cobrand = 'nosite';
    $photo_upload = Cobrand::allow_photo_upload($cobrand);
    is($photo_upload, 1, 'allow_photo_upload returns 1 if there is no allow_photo_upload function defined by the cobrand');
}

sub test_allow_photo_display {
    my $cobrand = 'mysite';
    my $photo_display = Cobrand::allow_photo_display($cobrand);
    is($photo_display, 0, 'allow_photo_display returns output from cobrand module');

    $cobrand = 'nosite';
    $photo_display = Cobrand::allow_photo_display($cobrand);
    is($photo_display, 1, 'allow_photo_display returns 1 if there is no allow_photo_display function defined by the cobrand');
}

sub test_council_check {
    my $cobrand = 'mysite';
    my $councils = {};
    my $query = new MockQuery('mysite');
    my ($check_result, $error) = Cobrand::council_check($cobrand, $councils, $query);
    is($check_result, 0, 'council_check returns output from cobrand module');
    
    $cobrand = 'nosite';
    ($check_result, $error) = Cobrand::council_check($cobrand, $councils, $query);
    is($check_result, 1, 'council_check returns 1 if there is no council_check function defined by the cobrand');
}

sub test_recent {
    my $cobrand = 'mysite';
    my $check_result = Cobrand::recent($cobrand);
    is_deeply($check_result, [ { id => 1, title => 'Title 1' }, { id => 2, title => 'Title 2' } ], 'recent returns output from cobrand module');

    # Can't test default here as calls database. So test it throws a db not configured error :)
    my $error;
    $cobrand = 'nosite';
    try {
        $check_result = Cobrand::recent($cobrand);
    } catch Error with {
        $error = shift;
    };
    ok($error =~ /^configure not yet called in new_dbh/, 'Default throws a database error');
}

ok(test_cobrand_handle() == 1, 'Ran all tests for the cobrand_handle function');
ok(test_site_restriction() == 1, 'Ran all tests for the site_restriction function');
ok(test_base_url() == 1, 'Ran all tests for the base url');
ok(test_disambiguate_location() == 1, 'Ran all tests for disambiguate location');
ok(test_form_elements() == 1, 'Ran all tests for form_elements');
ok(test_base_url_for_emails() == 1, 'Ran all tests for base_url_for_emails');
ok(test_extra_problem_data() == 1, 'Ran all tests for extra_problem_data');
ok(test_extra_update_data() == 1, 'Ran all tests for extra_update_data');
ok(test_extra_alert_data() == 1, 'Ran all tests for extra_alert_data');
ok(test_extra_params() == 1, 'Ran all tests for extra_params');
ok(test_header_params() == 1, 'Ran all tests for header_params');
ok(test_root_path_js() == 1, 'Ran all tests for root_js');
ok(test_site_title() == 1, 'Ran all tests for site_title');
ok(test_on_map_list_limit() == 1, 'Ran all tests for on_map_list_limit');
ok(test_url() == 1, 'Ran all tests for url');
ok(test_allow_photo_upload() == 1, 'Ran all tests for allow_photo_upload');
ok(test_allow_photo_display() == 1, 'Ran all tests for allow_photo_display');
ok(test_council_check() == 1, 'Ran all tests for council_check');
ok(test_recent() == 1, 'Ran all tests for recent');