diff --git a/app/classes/Transvision/Utils.php b/app/classes/Transvision/Utils.php index 4ed12ff4..0ddd907a 100644 --- a/app/classes/Transvision/Utils.php +++ b/app/classes/Transvision/Utils.php @@ -438,10 +438,32 @@ 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 * - * @return string URL with 'json' appended as part of the query string + * @param boolean $revert if true, invert the locale and the source_locale in the API generated link + * @return string URL with 'json' appended as part of the query string */ - public static function redirectToAPI() + public static function redirectToAPI($revert = false) { - return $_SERVER["REQUEST_URI"] . (is_null($_SERVER['QUERY_STRING']) ? '?json' : '&json'); + if (! is_null($_SERVER['QUERY_STRING'])) { + if ($revert == true) { + $arg = []; + parse_str($_SERVER['QUERY_STRING'], $arg); + $source_locale = $arg["sourcelocale"]; + $locale = $arg["locale"]; + $arg["sourcelocale"] = $locale; + $arg["locale"] = $source_locale; + $query = http_build_query($arg); + } else { + $query = $_SERVER['QUERY_STRING']; + } +// dump($_SERVER["REQUEST_URI"]); + $address = (strstr($_SERVER["REQUEST_URI"], '?') ? + strstr($_SERVER["REQUEST_URI"], '?', true) : + $_SERVER["REQUEST_URI"]) . '?' . $query; + } else { + $query = null; + $address = $_SERVER["REQUEST_URI"]; + } + + return $address . (is_null($query) ? '?json' : '&json'); } } diff --git a/app/views/templates/api_promotion.php b/app/views/templates/api_promotion.php index 89ecae32..53d35821 100644 --- a/app/views/templates/api_promotion.php +++ b/app/views/templates/api_promotion.php @@ -1,4 +1,19 @@ + + + + diff --git a/tests/units/Transvision/Utils.php b/tests/units/Transvision/Utils.php index 6e344ea8..b9364c42 100644 --- a/tests/units/Transvision/Utils.php +++ b/tests/units/Transvision/Utils.php @@ -456,12 +456,14 @@ public function redirectToAPIDP() { return [ [ - 'http://transvision.mozfr.org/string/?entity=browser/chrome/browser/downloads/downloads.properties:stateStarting&repo=central', - 'http://transvision.mozfr.org/string/?entity=browser/chrome/browser/downloads/downloads.properties:stateStarting&repo=central&json', + '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', ], [ 'http://transvision.mozfr.org/api/v1/versions/', 'http://transvision.mozfr.org/api/v1/versions/?json', + 'http://transvision.mozfr.org/api/v1/versions/?json', ], ]; } @@ -469,15 +471,19 @@ public function redirectToAPIDP() /** * @dataProvider redirectToAPIDP */ - public function testRedirectToAPI($a, $b) + public function testRedirectToAPI($a, $b, $c) { $obj = new _Utils(); $_SERVER["REQUEST_URI"] = $a; $_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); + $this + ->string($obj->redirectToAPI(true)) + ->isEqualTo($c); } }