diff options
-rwxr-xr-x | conf/Vagrantfile.example | 8 | ||||
-rw-r--r-- | conf/general.yml-example | 24 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand.pm | 15 | ||||
-rw-r--r-- | t/app/controller/questionnaire.t | 2 | ||||
-rw-r--r-- | t/cobrand/loading.t | 103 |
5 files changed, 104 insertions, 48 deletions
diff --git a/conf/Vagrantfile.example b/conf/Vagrantfile.example index 8c2a32c47..98b77dcc4 100755 --- a/conf/Vagrantfile.example +++ b/conf/Vagrantfile.example @@ -38,14 +38,6 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| apt-get install -qq -y curl >/dev/null curl -s -O https://raw.github.com/mysociety/commonlib/master/bin/install-site.sh sh install-site.sh --dev fixmystreet vagrant 127.0.0.1.xip.io - # Assume a developer will want to run the tests. - # TODO The tests should be further altered to work regardless of - # configuration file settings - sed -i -r \ - -e "s,(MAPIT_URL:) '',\\1 'http://mapit.mysociety.org/'," \ - -e "s, - cobrand_one, - barnet\\n - bromley\\n - emptyhomes\\n - fiksgatami\\n - fixmybarangay\\n - lichfielddc\\n - reading\\n - seesomething\\n - southampton\\n - zurich," \ - -e "s, - cobrand_two: 'hostname_substring2', - fixmystreet: '.'," \ - fixmystreet/conf/general.yml # All done echo "****************" echo "You can now ssh into your vagrant box: vagrant ssh" diff --git a/conf/general.yml-example b/conf/general.yml-example index 2f78de986..f5fb89b64 100644 --- a/conf/general.yml-example +++ b/conf/general.yml-example @@ -121,16 +121,28 @@ GEOCODING_DISAMBIGUATION: '' MAP_TYPE: 'OSM' # FixMyStreet uses a templating cobrand system to provide different looks for -# different installations. If your site was at moon.example.org, and your templates -# were in the templates/web/moon directory, you would use: +# different installations. In the common case, if your templates are in the +# templates/web/moon directory and CSS in web/cobrands/moon, you just specify: # ALLOWED_COBRANDS: -# - moon: 'moon.example.org' +# - moon +# If you wish to use multiple cobrands, specify them in a list, optionally with +# hostname-matching regular expressions if the name of the cobrand is not +# enough. For example: +# ALLOWED_COBRANDS: +# - moon +# - venus +# Any hostname with 'moon' in it will use the moon cobrand, any with 'venus' +# the venus cobrand (any other the Default cobrand). Whereas: +# ALLOWED_COBRANDS: +# - moon: 'orbital' +# - venus +# Any hostname with 'orbital' in it will use the moon cobrand. # This also allows development servers to map to different cobrands if needed, # using DNS subdomains for example. ALLOWED_COBRANDS: - - cobrand_one - - cobrand_two: 'hostname_substring2' - - cobrand_three + - cobrand1 + - cobrand2: 'hostname_substring2' + - cobrand3 # This is only used in "offensive report" emails to provide a link directly to # the admin interface. If wanted, set to the full URL of your admin interface. diff --git a/perllib/FixMyStreet/Cobrand.pm b/perllib/FixMyStreet/Cobrand.pm index 881183463..ff7d7f943 100644 --- a/perllib/FixMyStreet/Cobrand.pm +++ b/perllib/FixMyStreet/Cobrand.pm @@ -8,6 +8,7 @@ use warnings; use FixMyStreet; use Carp; +use Moose; use Module::Pluggable sub_name => '_cobrands', @@ -38,7 +39,10 @@ Simply returns the config variable (so this function can be overridden in test s =cut sub _get_allowed_cobrands { - return FixMyStreet->config('ALLOWED_COBRANDS') || []; + my $allowed = FixMyStreet->config('ALLOWED_COBRANDS') || []; + # If the user has supplied a string, convert to an arrayref + $allowed = [ $allowed ] unless ref $allowed; + return $allowed; } =head2 available_cobrand_classes @@ -92,7 +96,14 @@ sub get_class_for_host { my $class = shift; my $host = shift; - foreach my $avail ( $class->available_cobrand_classes ) { + my @available = $class->available_cobrand_classes; + + # If only one entry, always use it + return class($available[0]) if 1 == @available; + + # If more than one entry, pick first whose regex (or + # name by default) matches hostname + foreach my $avail ( @available ) { return class($avail) if $host =~ /$avail->{host}/; } diff --git a/t/app/controller/questionnaire.t b/t/app/controller/questionnaire.t index 8e553a1a5..3468909b9 100644 --- a/t/app/controller/questionnaire.t +++ b/t/app/controller/questionnaire.t @@ -384,7 +384,7 @@ for my $test ( } FixMyStreet::override_config { - ALLOWED_COBRANDS => [ 'emptyhomes' ], + ALLOWED_COBRANDS => [ 'emptyhomes', 'fixmystreet' ], }, sub { # EHA extra checking ok $mech->host("reportemptyhomes.com"), 'change host to reportemptyhomes'; diff --git a/t/cobrand/loading.t b/t/cobrand/loading.t index bd83da07f..48a10293e 100644 --- a/t/cobrand/loading.t +++ b/t/cobrand/loading.t @@ -9,51 +9,92 @@ use FixMyStreet; use_ok 'FixMyStreet::Cobrand'; # check that the allowed cobrands is correctly loaded from config -{ +sub check_allowed_cobrands { + my $should = shift; + $should = [ map { { moniker => $_, host => $_ } } @$should ]; my $allowed = FixMyStreet::Cobrand->get_allowed_cobrands; - ok $allowed, "got the allowed_cobrands"; + ok $allowed, "got the allowed_cobrands"; isa_ok $allowed, "ARRAY"; - cmp_ok scalar @$allowed, '>', 1, "got more than one"; + is_deeply $allowed, $should, "allowed_cobrands matched"; } -# fake the allowed cobrands for testing -my $override = Sub::Override->new( # - 'FixMyStreet::Cobrand::_get_allowed_cobrands' => - sub { return ['emptyhomes'] } -); -is_deeply FixMyStreet::Cobrand->get_allowed_cobrands, [ { moniker => 'emptyhomes', host => 'emptyhomes' } ], - 'overidden get_allowed_cobrands'; +FixMyStreet::override_config { ALLOWED_COBRANDS => 'fixmyhouse' }, + sub { check_allowed_cobrands([ 'fixmyhouse' ]); }; +FixMyStreet::override_config { ALLOWED_COBRANDS => [ 'fixmyhouse' ] }, + sub { check_allowed_cobrands([ 'fixmyhouse' ]); }; +FixMyStreet::override_config { ALLOWED_COBRANDS => [ 'fixmyhouse', 'fixmyshed' ] }, + sub { check_allowed_cobrands([ 'fixmyhouse', 'fixmyshed' ]); }; sub run_host_tests { my %host_tests = @_; for my $host ( sort keys %host_tests ) { - is FixMyStreet::Cobrand->get_class_for_host($host), - "FixMyStreet::Cobrand::$host_tests{$host}", - "does $host -> F::C::$host_tests{$host}"; + # get the cobrand class by host + my $cobrand = FixMyStreet::Cobrand->get_class_for_host($host); + my $test_class = $host_tests{$host}; + my $test_moniker = lc $test_class; + is $cobrand, "FixMyStreet::Cobrand::$test_class", "does $host -> F::C::$test_class"; + my $c = $cobrand->new(); + is $c->moniker, $test_moniker; } } -# get the cobrand class by host -run_host_tests( - 'www.fixmystreet.com' => 'Default', - 'reportemptyhomes.com' => 'EmptyHomes', - 'barnet.fixmystreet.com' => 'Default', # not in the allowed_cobrands list - 'some.odd.site.com' => 'Default', -); +# Only one cobrand, always use it +FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'fixmystreet' ], +}, sub { + run_host_tests( + 'www.fixmystreet.com' => 'FixMyStreet', + 'reportemptyhomes.com' => 'FixMyStreet', + 'barnet.fixmystreet.com' => 'FixMyStreet', + 'some.odd.site.com' => 'FixMyStreet', + ); +}; + +# Only one cobrand, no .pm file, should still work +FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'nopmfile' ], +}, sub { + run_host_tests( + 'www.fixmystreet.com' => 'nopmfile', + 'some.odd.site.com' => 'nopmfile', + ); +}; + +# Couple of cobrands, hostname checking and default fallback +FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'emptyhomes', 'fixmystreet' ], +}, sub { + run_host_tests( + 'www.fixmystreet.com' => 'FixMyStreet', + 'reportemptyhomes.com' => 'EmptyHomes', + 'barnet.fixmystreet.com' => 'FixMyStreet', # not in the allowed_cobrands list + 'some.odd.site.com' => 'Default', + ); +}; # now enable barnet too and check that it works -$override->replace( # - 'FixMyStreet::Cobrand::_get_allowed_cobrands' => - sub { return [ 'emptyhomes', 'barnet' ] } -); +FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'emptyhomes', 'barnet', 'fixmystreet' ], +}, sub { + run_host_tests( + 'www.fixmystreet.com' => 'FixMyStreet', + 'reportemptyhomes.com' => 'EmptyHomes', + 'barnet.fixmystreet.com' => 'Barnet', # found now it is in allowed_cobrands + 'some.odd.site.com' => 'Default', + ); +}; -# get the cobrand class by host -run_host_tests( - 'www.fixmystreet.com' => 'Default', - 'reportemptyhomes.com' => 'EmptyHomes', - 'barnet.fixmystreet.com' => 'Barnet', # found now it is in allowed_cobrands - 'some.odd.site.com' => 'Default', -); +# And a check with some regex matching +FixMyStreet::override_config { + ALLOWED_COBRANDS => [ { 'fixmystreet' => 'empty' }, 'barnet', { 'testing' => 'fixmystreet' } ], +}, sub { + run_host_tests( + 'www.fixmystreet.com' => 'testing', + 'reportemptyhomes.com' => 'FixMyStreet', + 'barnet.fixmystreet.com' => 'Barnet', + 'some.odd.site.com' => 'Default', + ); +}; # check that the moniker works as expected both on class and object. is FixMyStreet::Cobrand::EmptyHomes->moniker, 'emptyhomes', |