diff options
-rw-r--r-- | perllib/Cobrand.pm | 20 | ||||
-rw-r--r-- | perllib/Page.pm | 65 | ||||
-rwxr-xr-x | perllib/t/Cobrand.t | 33 | ||||
-rw-r--r-- | perllib/t/Cobrands/Mysite/Util.pm | 28 | ||||
-rw-r--r-- | perllib/t/MockQuery.pm | 28 | ||||
-rwxr-xr-x | perllib/t/Page.t | 49 | ||||
-rw-r--r-- | perllib/t/templates/mysite/test-header | 1 | ||||
-rwxr-xr-x | web/index.cgi | 6 |
8 files changed, 209 insertions, 21 deletions
diff --git a/perllib/Cobrand.pm b/perllib/Cobrand.pm index 116268f92..fba948868 100644 --- a/perllib/Cobrand.pm +++ b/perllib/Cobrand.pm @@ -7,10 +7,9 @@ # Copyright (c) 2009 UK Citizens Online Democracy. All rights reserved. # Email: louise@mysociety.org. WWW: http://www.mysociety.org # -# $Id: Cobrand.pm,v 1.2 2009-08-20 15:38:48 louise Exp $ +# $Id: Cobrand.pm,v 1.3 2009-08-26 16:52:14 louise Exp $ package Cobrand; - use strict; use Carp; @@ -24,4 +23,21 @@ sub get_allowed_cobrands{ my @allowed_cobrands = split(/\|/, $allowed_cobrand_string); return \@allowed_cobrands; } + +=item cobrand_page QUERY + +Return a string containing the HTML to be rendered for a custom Cobranded page + +=cut +sub cobrand_page{ + my $q = shift; + my $cobrand = $q->{site}; + my $cobrand_class = ucfirst($cobrand); + my $class = "Cobrands::" . $cobrand_class . "::Util"; + eval "use $class"; + my $handle = $class->new; + my ($out, %params) = $handle->page($q); + return ($out, %params); +} + 1; diff --git a/perllib/Page.pm b/perllib/Page.pm index e0523d479..1f2d85ee1 100644 --- a/perllib/Page.pm +++ b/perllib/Page.pm @@ -6,7 +6,7 @@ # Copyright (c) 2006 UK Citizens Online Democracy. All rights reserved. # Email: matthew@mysociety.org; WWW: http://www.mysociety.org/ # -# $Id: Page.pm,v 1.160 2009-08-20 15:41:54 louise Exp $ +# $Id: Page.pm,v 1.161 2009-08-26 16:52:14 louise Exp $ # package Page; @@ -120,7 +120,6 @@ sub microsite { if ($q->{site} eq 'scambs') { Problems::set_site_restriction('scambs'); } - Memcached::set_namespace(mySociety::Config::get('BCI_DB_NAME') . ":"); } @@ -141,6 +140,16 @@ sub base_url_with_lang { return $base; } +=item template_root + +Returns the path from which template files will be read. + +=cut + +sub template_root{ + return '/../templates/website/cobrands/'; +} + =item template_vars QUERY LANG Return a hash of variables that can be substituted into header and footer templates. @@ -168,6 +177,43 @@ sub template_vars ($$){ return \%vars; } +=item template Q [PARAM VALUE ...] + +Return the correct template given PARAMs + +=cut +sub template($%){ + my ($q, %params) = @_; + my $template; + if ($params{template}){ + $template = $params{template}; + }else{ + $template = $q->{site}; + } + return $template; +} + +=item template_header TITLE TEMPLATE Q LANG + +Return HTML for the templated top of a page, given a +title, template name, request, language and template root. + +=cut + +sub template_header{ + + my ($title, $template, $q, $lang, $template_root) = @_; + (my $file = __FILE__) =~ s{/[^/]*?$}{}; + open FP, $file . $template_root . $q->{site} . '/' . $template . '-header'; + my $html = join('', <FP>); + close FP; + my $vars = template_vars($q, $lang); + $vars->{title} = $title; + $html =~ s#{{ ([a-z_]+) }}#$vars->{$1}#g; + return $html; + +} + =item header Q [PARAM VALUE ...] Return HTML for the top of the page, given PARAMs (TITLE is required). @@ -176,7 +222,7 @@ Return HTML for the top of the page, given PARAMs (TITLE is required). sub header ($%) { my ($q, %params) = @_; - my %permitted_params = map { $_ => 1 } qw(title rss js expires lastmodified); + my %permitted_params = map { $_ => 1 } qw(title rss js expires lastmodified template); foreach (keys %params) { croak "bad parameter '$_'" if (!exists($permitted_params{$_})); } @@ -196,13 +242,8 @@ sub header ($%) { my $html; my $lang = $mySociety::Locale::lang; if ($q->{site} ne 'fixmystreet') { - (my $file = __FILE__) =~ s{/[^/]*?$}{}; - open FP, $file . '/../templates/website/cobrands/' . $q->{site} . '/' . $q->{site} . '-header'; - $html = join('', <FP>); - close FP; - my $vars = template_vars($q, $lang); - $vars->{title} = $title; - $html =~ s#{{ ([a-z_]+) }}#$vars->{$1}#g; + my $template = template($q, %params); + $html = template_header($title, $template, $q, $lang, template_root()); } else { my $fixmystreet = _('FixMyStreet'); $html = <<EOF; @@ -247,7 +288,8 @@ sub footer { if ($q->{site} ne 'fixmystreet') { (my $file = __FILE__) =~ s{/[^/]*?$}{}; - open FP, $file . '/../templates/website/cobrands/' . $q->{site} . '/' . $q->{site} . '-footer'; + my $template = template($q, %params); + open FP, $file . template_root() . $q->{site} . '/' . $template . '-footer'; my $html = join('', <FP>); close FP; my $lang = $mySociety::Locale::lang; @@ -702,6 +744,7 @@ sub display_problem_text { if ($problem->{photo}) { my $dims = Image::Size::html_imgsize(\$problem->{photo}); + my $dims = ''; $out .= "<p align='center'><img alt='' $dims src='/photo?id=$problem->{id}'></p>"; } diff --git a/perllib/t/Cobrand.t b/perllib/t/Cobrand.t new file mode 100755 index 000000000..36ec9fefa --- /dev/null +++ b/perllib/t/Cobrand.t @@ -0,0 +1,33 @@ +#!/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.1 2009-08-26 16:52:14 louise Exp $ +# + +use strict; +use warnings; +use Test::More tests => 2; +use Test::Exception; + +use FindBin; +use lib "$FindBin::Bin"; +use lib "$FindBin::Bin/../perllib"; +use lib "$FindBin::Bin/../../perllib"; + +use Cobrand; +use MockQuery; + +sub test_cobrand_page{ + my $q = new MockQuery('mysite'); + my ($html, $params) = Cobrand::cobrand_page($q); + like($html, qr/A cobrand produced page/, 'cobrand_page returns output from cobrand module'); + return 1; +} + + +ok(test_cobrand_page() == 1, 'Ran all tests for the cobrand_page function'); diff --git a/perllib/t/Cobrands/Mysite/Util.pm b/perllib/t/Cobrands/Mysite/Util.pm new file mode 100644 index 000000000..61f6fd831 --- /dev/null +++ b/perllib/t/Cobrands/Mysite/Util.pm @@ -0,0 +1,28 @@ +#!/usr/bin/perl -w +# +# Util.pm: +# Test Cobranding for FixMyStreet. +# +# +# Copyright (c) 2009 UK Citizens Online Democracy. All rights reserved. +# Email: louise@mysociety.org. WWW: http://www.mysociety.org +# +# $Id: Util.pm,v 1.1 2009-08-26 16:52:15 louise Exp $ + +package Cobrands::Mysite::Util; +use Page; +use strict; +use Carp; +use mySociety::Web qw(ent); + +sub new{ + my $class = shift; + return bless {}, $class; +} + +sub page{ + my %params = (); + return ("A cobrand produced page", %params); +} + +1; diff --git a/perllib/t/MockQuery.pm b/perllib/t/MockQuery.pm new file mode 100644 index 000000000..a6cd3ff78 --- /dev/null +++ b/perllib/t/MockQuery.pm @@ -0,0 +1,28 @@ +#!/usr/bin/perl -w +# +# MockQuery.pm: +# Mock query to support 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: MockQuery.pm,v 1.1 2009-08-26 16:52:14 louise Exp $ +# + + +package MockQuery; + +sub new{ + my $class = shift; + my $self = { + site => shift, + }; + bless $self, $class; + return $self; +} + +sub header{ + +} + +1; diff --git a/perllib/t/Page.t b/perllib/t/Page.t index fc01697ed..2890dda1e 100755 --- a/perllib/t/Page.t +++ b/perllib/t/Page.t @@ -6,16 +6,53 @@ # Copyright (c) 2009 UK Citizens Online Democracy. All rights reserved. # Email: louise@mysociety.org; WWW: http://www.mysociety.org/ # -# $Id: Page.t,v 1.1 2009-08-25 10:28:50 louise Exp $ +# $Id: Page.t,v 1.2 2009-08-26 16:52:14 louise Exp $ # use strict; use warnings; - -use Test::More tests => 1; +use Test::More tests => 4; +use Test::Exception; use FindBin; -use lib "$FindBin::Bin/.."; -use lib "$FindBin::Bin/../../../perllib"; +use lib "$FindBin::Bin"; +use lib "$FindBin::Bin/../perllib"; +use lib "$FindBin::Bin/../../perllib"; + +use Page; +use MockQuery; +use mySociety::Locale; + +sub mock_query(){ + my $q = new MockQuery('mysite'); + return $q; +} + +sub test_header(){ + my $q = mock_query(); + my $html; + my %params = (title => 'test title'); + mySociety::Locale::negotiate_language('en-gb,English,en_GB'); + mySociety::Locale::gettext_domain('FixMyStreet'); + mySociety::Locale::change(); + + # 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_header('My test title', 'test', $q, 'en-gb', '/../t/templates/'); + like ($html, qr/My test header template/, 'named template rendered ok'); + + + return 1; +} + +sub test_footer(){ + return 1; +} -BEGIN { use_ok('Page'); }
\ No newline at end of file +ok(test_footer() == 1, 'Ran all tests for the footer function'); +ok(test_header() == 1, 'Ran all tests for the header function'); diff --git a/perllib/t/templates/mysite/test-header b/perllib/t/templates/mysite/test-header new file mode 100644 index 000000000..83d959d3d --- /dev/null +++ b/perllib/t/templates/mysite/test-header @@ -0,0 +1 @@ +My test header template diff --git a/web/index.cgi b/web/index.cgi index a199cf5d1..0319f56a6 100755 --- a/web/index.cgi +++ b/web/index.cgi @@ -6,11 +6,10 @@ # Copyright (c) 2006 UK Citizens Online Democracy. All rights reserved. # Email: matthew@mysociety.org. WWW: http://www.mysociety.org # -# $Id: index.cgi,v 1.272 2009-08-20 17:14:25 matthew Exp $ +# $Id: index.cgi,v 1.273 2009-08-26 16:52:14 louise Exp $ use strict; use Standard; - use Error qw(:try); use File::Slurp; use LWP::Simple; @@ -85,6 +84,8 @@ sub main { } elsif ($q->param('pc') || ($q->param('x') && $q->param('y'))) { ($out, %params) = display_location($q); $params{title} = _('Viewing a location'); + } elsif ($q->param('cobrand_page')) { + ($out, %params) = Cobrand::cobrand_page($q); } else { $out = front_page($q); } @@ -92,6 +93,7 @@ sub main { print $out; my %footerparams; $footerparams{js} = $params{js} if $params{js}; + $footerparams{template} = $params{template} if $params{template}; print Page::footer($q, %footerparams); } Page::do_fastcgi(\&main); |