diff options
-rw-r--r-- | conf/general.yml-example | 4 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand.pm | 38 | ||||
-rw-r--r-- | t/cobrand/loading.t | 8 |
3 files changed, 34 insertions, 16 deletions
diff --git a/conf/general.yml-example b/conf/general.yml-example index efdb5d007..77319b24b 100644 --- a/conf/general.yml-example +++ b/conf/general.yml-example @@ -51,7 +51,9 @@ IPHONE_URL: '' # Log file (used in test harness, and later in admin scripts) HTTPD_ERROR_LOG: '/var/log/apache/error.log' -ALLOWED_COBRANDS: 'cobrand_one|cobrand_two' +ALLOWED_COBRANDS: + - cobrand_one + - cobrand_two: 'hostname_substring2' # How many items are returned in the GeoRSS feeds by default RSS_LIMIT: '20' diff --git a/perllib/FixMyStreet/Cobrand.pm b/perllib/FixMyStreet/Cobrand.pm index ad21820d7..647261e32 100644 --- a/perllib/FixMyStreet/Cobrand.pm +++ b/perllib/FixMyStreet/Cobrand.pm @@ -18,31 +18,50 @@ my @ALL_COBRAND_CLASSES = __PACKAGE__->_cobrands; =head2 get_allowed_cobrands -Return an array reference of allowed cobrand subdomains +Return an array reference of allowed cobrand monikers and hostname substrings. =cut sub get_allowed_cobrands { - my $allowed_cobrand_string = FixMyStreet->config('ALLOWED_COBRANDS'); - my @allowed_cobrands = split( /\|/, $allowed_cobrand_string ); + my $class = shift; + my @allowed_cobrands = map { + ref $_ ? { moniker => keys %$_, host => values %$_ } + : { moniker => $_, host => $_ } + } @{ $class->_get_allowed_cobrands }; return \@allowed_cobrands; } +=head2 _get_allowed_cobrands + +Simply returns the config variable (so this function can be overridden in test suite). + +=cut + +sub _get_allowed_cobrands { + return FixMyStreet->config('ALLOWED_COBRANDS'); +} + =head2 available_cobrand_classes @available_cobrand_classes = FixMyStreet::Cobrand->available_cobrand_classes(); -Return an array of all the classes that were found and that have monikers that -match the values from get_allowed_cobrands. +Return an array of all the classes that were found and that have monikers +that match the values from get_allowed_cobrands, in the order of +get_allowed_cobrands. =cut sub available_cobrand_classes { my $class = shift; - my %allowed = map { $_ => 1 } @{ $class->get_allowed_cobrands }; - my @avail = grep { $allowed{ $_->moniker } } @ALL_COBRAND_CLASSES; + my %all = map { $_->moniker => $_ } @ALL_COBRAND_CLASSES; + my @avail; + foreach (@{ $class->get_allowed_cobrands }) { + next unless $all{$_->{moniker}}; + $_->{class} = $all{$_->{moniker}}; + push @avail, $_; + } return @avail; } @@ -60,8 +79,7 @@ sub get_class_for_host { my $host = shift; foreach my $avail ( $class->available_cobrand_classes ) { - my $moniker = $avail->moniker; - return $avail if $host =~ m{$moniker}; + return $avail->{class} if $host =~ /$avail->{host}/; } # if none match then use the default @@ -81,7 +99,7 @@ sub get_class_for_moniker { my $moniker = shift; foreach my $avail ( $class->available_cobrand_classes ) { - return $avail if $moniker eq $avail->moniker; + return $avail->{class} if $moniker eq $avail->{moniker}; } # Special case for old blank cobrand entries in fixmystreet.com. diff --git a/t/cobrand/loading.t b/t/cobrand/loading.t index 405ef4761..bd83da07f 100644 --- a/t/cobrand/loading.t +++ b/t/cobrand/loading.t @@ -14,16 +14,14 @@ use_ok 'FixMyStreet::Cobrand'; ok $allowed, "got the allowed_cobrands"; isa_ok $allowed, "ARRAY"; cmp_ok scalar @$allowed, '>', 1, "got more than one"; - is join( '|', @$allowed ), FixMyStreet->config('ALLOWED_COBRANDS'), - "matches config value"; } # fake the allowed cobrands for testing my $override = Sub::Override->new( # - 'FixMyStreet::Cobrand::get_allowed_cobrands' => + 'FixMyStreet::Cobrand::_get_allowed_cobrands' => sub { return ['emptyhomes'] } ); -is_deeply FixMyStreet::Cobrand->get_allowed_cobrands, ['emptyhomes'], +is_deeply FixMyStreet::Cobrand->get_allowed_cobrands, [ { moniker => 'emptyhomes', host => 'emptyhomes' } ], 'overidden get_allowed_cobrands'; sub run_host_tests { @@ -45,7 +43,7 @@ run_host_tests( # now enable barnet too and check that it works $override->replace( # - 'FixMyStreet::Cobrand::get_allowed_cobrands' => + 'FixMyStreet::Cobrand::_get_allowed_cobrands' => sub { return [ 'emptyhomes', 'barnet' ] } ); |