Skip to content

Commit

Permalink
Issue mozfr#604: clean up code and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalchevrel committed Feb 23, 2016
1 parent a189fcc commit 1d439b2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 28 deletions.
41 changes: 20 additions & 21 deletions app/classes/Transvision/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -459,31 +459,30 @@ public static function ago($datetime, $ref_time = '')
* This is used on views which also exist in our public API
* https://github.com/mozfr/transvision/wiki/JSON-API
*
* @param boolean $revert if true, invert the locale and the source_locale in the API generated link
* @param boolean $swap_locales Defaults to False. If set to True, swap the
* values of locale and source_locale parameters.
* @return string URL with 'json' appended as part of the query string
*/
public static function redirectToAPI($revert = false)
public static function redirectToAPI($swap_locales = false)
{
if (! is_null($_SERVER['QUERY_STRING'])) {
// Allow to revert the locale and the sourcelocale when redirecting to the API
if ($revert) {
$arg = [];
parse_str($_SERVER['QUERY_STRING'], $arg);
$sourcelocale = $arg['sourcelocale'];
$arg['sourcelocale'] = $arg['locale'];
$arg['locale'] = $sourcelocale;
$query = http_build_query($arg);
} else {
$query = $_SERVER['QUERY_STRING'];
}
$address = (strstr($_SERVER['REQUEST_URI'], '?') ?
strstr($_SERVER['REQUEST_URI'], '?', true) :
$_SERVER['REQUEST_URI']) . '?' . $query;
} else {
$query = null;
$address = $_SERVER['REQUEST_URI'];
if (! $swap_locales) {
return $_SERVER['REQUEST_URI'] . (is_null($_SERVER['QUERY_STRING']) ? '?json=true' : '&json=true');
}

// We are going to split and then rebuild QUERY_STRING
parse_str($_SERVER['QUERY_STRING'], $args);

// We add a json parameter to the query, its value doesn't matter.
$args['json'] = 'true';

// Swap the values of locale and sourcelocale if they exist
if (isset($args['locale'], $args['sourcelocale'])) {
list($args['locale'], $args['sourcelocale']) = [$args['sourcelocale'], $args['locale']];
}

return $address . (is_null($query) ? '?json' : '&json');
// We don't want to encode slashes in searches for entity names
$query = urldecode(http_build_query($args));

return explode('?', $_SERVER['REQUEST_URI'])[0] . '?' . $query;
}
}
18 changes: 11 additions & 7 deletions tests/units/Transvision/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -458,14 +458,19 @@ public function redirectToAPIDP()
{
return [
[
'http://transvision.mozfr.org/string/?recherche=screen&repo=aurora&sourcelocale=en-US&locale=fr&search_type=strings',
'http://transvision.mozfr.org/string/?recherche=screen&repo=aurora&sourcelocale=en-US&locale=fr&search_type=strings&json',
'http://transvision.mozfr.org/string/?recherche=screen&repo=aurora&sourcelocale=fr&locale=en-US&search_type=strings&json',
'/string/?entity=browser/chrome/browser/downloads/downloads.properties:stateStarting&repo=central',
'/string/?entity=browser/chrome/browser/downloads/downloads.properties:stateStarting&repo=central&json=true',
'/string/?entity=browser/chrome/browser/downloads/downloads.properties:stateStarting&repo=central&json=true',
],
[
'http://transvision.mozfr.org/api/v1/versions/',
'http://transvision.mozfr.org/api/v1/versions/?json',
'http://transvision.mozfr.org/api/v1/versions/?json',
'/v1/versions/',
'/v1/versions/?json=true',
'/v1/versions/?json=true',
],
[
'/?recherche=home&repo=aurora&sourcelocale=en-US&locale=fr&search_type=strings',
'/?recherche=home&repo=aurora&sourcelocale=en-US&locale=fr&search_type=strings&json=true',
'/?recherche=home&repo=aurora&sourcelocale=fr&locale=en-US&search_type=strings&json=true',
],
];
}
Expand All @@ -480,7 +485,6 @@ public function testRedirectToAPI($a, $b, $c)
$_SERVER['QUERY_STRING'] = isset(parse_url($a)['query'])
? parse_url($a)['query']
: null;
$_SERVER['HTTP_HOST'] = 'http://' . parse_url($a)['host'];
$this
->string($obj->redirectToAPI())
->isEqualTo($b);
Expand Down

0 comments on commit 1d439b2

Please sign in to comment.