Skip to content

Commit

Permalink
Fix API calls for all_projects (#1023)
Browse files Browse the repository at this point in the history
  • Loading branch information
flodolo authored Nov 16, 2022
1 parent c549e71 commit f154bdb
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 7 deletions.
16 changes: 16 additions & 0 deletions app/classes/Transvision/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ private function isValidServiceCall($service)
switch ($service) {
case 'entity':
// ex: /api/v1/entity/mozilla_org/?id=mozilla_org/mozorg/home.lang:d9d4307d
// Use 'global' in API calls instead of the meta project ID
if (Project::isMetaRepository($this->parameters[2])) {
$this->parameters[2] = 'global';
}
if (! $this->verifyRepositoryExists($this->parameters[2])) {
return false;
}
Expand All @@ -217,6 +221,10 @@ private function isValidServiceCall($service)
return false;
}

// Use 'global' in API calls instead of the meta project ID
if (Project::isMetaRepository($this->parameters[2])) {
$this->parameters[2] = 'global';
}
if (! $this->verifyRepositoryExists($this->parameters[2])) {
return false;
}
Expand Down Expand Up @@ -259,6 +267,10 @@ private function isValidServiceCall($service)
return false;
}

// Use 'global' in API calls instead of the meta project ID
if (Project::isMetaRepository($this->parameters[3])) {
$this->parameters[3] = 'global';
}
if (! $this->verifyRepositoryExists($this->parameters[3], true)) {
return false;
}
Expand All @@ -283,6 +295,10 @@ private function isValidServiceCall($service)
return false;
}

// Use 'global' in API calls instead of the meta project ID
if (Project::isMetaRepository($this->parameters[2])) {
$this->parameters[2] = 'global';
}
if (! $this->verifyRepositoryExists($this->parameters[2], true)) {
return false;
}
Expand Down
18 changes: 18 additions & 0 deletions app/classes/Transvision/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ class Project
*/
public static $repos_info = [
'all_projects' => [
/*
* Only one project should be set as meta, as it's used to track
* the repository ID that includes all other projects.
*/
'meta' => true,
],
'firefox_ios' => [
Expand Down Expand Up @@ -287,6 +291,20 @@ public static function isMetaRepository($repository)
: false;
}

/**
* Return meta repository
*
* @return string ID of the meta repository
*/
public static function getMetaRepository()
{
foreach (self::getRepositories() as $repository) {
if (self::isMetaRepository($repository)) {
return $repository;
}
}
}

/**
* Return true if the locale is the reference locale for a repository
*
Expand Down
4 changes: 0 additions & 4 deletions app/controllers/mainsearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@
};

$repo = $get_value('repo', 'gecko_strings');
// Redirect "all_projects" to 'global'
if ($repo == 'all_projects') {
$repo = 'global';
}
$type = $get_value('search_type', 'strings');
$source = $get_value('sourcelocale', Project::getReferenceLocale($repo));
$target = $get_value('locale', 'fr');
Expand Down
4 changes: 2 additions & 2 deletions app/inc/search_options.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@

/*
Check for default_repository cookie if we don't have a GET value,
otherwise fall back to all_projects instead of gecko_strings,
otherwise fall back to meta repository instead of gecko_strings,
which is the default for the search() class.
*/
if (! isset($_GET['repo'])) {
if (isset($_COOKIE['default_repository']) && Project::isValidRepository($_COOKIE['default_repository'])) {
$repo = $_COOKIE['default_repository'];
} else {
$repo = 'all_projects';
$repo = Project::getMetaRepository();
}
}

Expand Down
2 changes: 2 additions & 0 deletions tests/functional/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
['v1/locales/iDontExist/', 400, '{"error":"The repo queried (iDontExist) doesn\'t exist."}'],
['v1/repositories/', 200, '["gecko_strings","mozilla_org"]'],
['v1/repositories/fr/', 200, '["gecko_strings","mozilla_org"]'],
['v1/suggestions/all_projects/en-US/fr/ar/?max_results=2', 200, '["Bookmark","Marque-page"]'],
['v1/suggestions/global/en-US/fr/ar/?max_results=2', 200, '["Bookmark","Marque-page"]'],
['v1/suggestions/gecko_strings/en-US/fr/ar/?max_results=2', 200, '["Bookmark","Marque-page"]'],
['v1/suggestions/gecko_strings/en-US/fr/ar/?max_results=10', 200, '["Bookmark","Bookmarks","New Bookmarks","Bookmark This Page","Marque-page","Marque-pages","Marquer cette page","Nouveaux marque-pages"]'],
['v1/suggestions/gecko_strings/en-US/fr/ar/?max_results=0', 200, '["Bookmark","Bookmarks","New Bookmarks","Bookmark This Page","Marque-page","Marque-pages","Marquer cette page","Nouveaux marque-pages"]'],
Expand Down
5 changes: 4 additions & 1 deletion tests/units/Transvision/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function testGetRepositories()
->isEqualTo($repos);
}

public function testIsMetaRepository()
public function testMetaRepository()
{
$obj = new _Project();
$this
Expand All @@ -31,6 +31,9 @@ public function testIsMetaRepository()
$this
->boolean($obj->isMetaRepository('gecko_strings'))
->isEqualTo(false);
$this
->string($obj->getMetaRepository())
->isEqualTo('all_projects');
}

public function testIsReferenceLocale()
Expand Down

0 comments on commit f154bdb

Please sign in to comment.