Skip to content

Commit

Permalink
Fix mozfr#604, adding two links for an API always working
Browse files Browse the repository at this point in the history
  • Loading branch information
Thegennok committed Feb 13, 2016
1 parent 9406f34 commit 35b5739
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
28 changes: 25 additions & 3 deletions app/classes/Transvision/Utils.php
Expand Up @@ -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');
}
}
17 changes: 16 additions & 1 deletion app/views/templates/api_promotion.php
@@ -1,4 +1,19 @@
<?php
if ($controller == "mainsearch") {
?>
<p class="api_link">
<span>API</span>These results are also available as an API request for the <a href="<?=\Transvision\Utils::redirectToAPI()?>"><?=$requested_sourcelocale?> locale</a> or the <a href="<?=\Transvision\Utils::redirectToAPI(true)?>"><?=$requested_locale?> locale</a>.<br>
<a href="https://github.com/mozfr/transvision/wiki/JSON-API">Learn more about the Transvision API</a>.
</p>
<?php

} else {
?>
<p class="api_link">
<span>API</span>These results are also available as an <a href="<?=\Transvision\Utils::redirectToAPI()?>">API request</a>.<br>
<a href="https://github.com/mozfr/transvision/wiki/JSON-API">Learn more about Transvision API</a>.
<a href="https://github.com/mozfr/transvision/wiki/JSON-API">Learn more about the Transvision API</a>.
</p>
<?php

}
?>
12 changes: 9 additions & 3 deletions tests/units/Transvision/Utils.php
Expand Up @@ -456,28 +456,34 @@ 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',
],
];
}

/**
* @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);
}
}

0 comments on commit 35b5739

Please sign in to comment.