Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor subdomain processing and add test to it (#204)
* refactor subdomain processing and add test to it * add other envs to deployment
- Loading branch information
1 parent
0676f38
commit 1bc591c
Showing
4 changed files
with
240 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,8 @@ on: | |
- live | ||
- staging | ||
- beta | ||
- alpha | ||
- edge | ||
|
||
jobs: | ||
deployer: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
/* | ||
* This file is part of the Goteo Package. | ||
* | ||
* (c) Platoniq y Fundación Goteo <fundacion@goteo.org> | ||
* | ||
* For the full copyright and license information, please view the README.md | ||
* and LICENSE files that was distributed with this source code. | ||
*/ | ||
|
||
namespace Goteo\Util\Parsers; | ||
|
||
use Goteo\Application\Config; | ||
use Goteo\Application\Lang; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
class UrlLang { | ||
public $request; | ||
protected $path; | ||
protected $host; | ||
|
||
public function __construct(Request $request) { | ||
$this->request = $request; | ||
$this->path = $request->getPathInfo(); | ||
$this->host = $request->getHttpHost(); | ||
} | ||
|
||
/** | ||
* Returns false if the requests is configured to skip any cookie setting | ||
*/ | ||
public function skipSessionManagement() { | ||
$skip = Config::get('session.skip'); | ||
if ($this->matchPrefix($this->path, $skip)) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* Returns the proper URL after processing url_lang configurations (adds subdomain lang if needed) | ||
*/ | ||
public function getHost($lang) { | ||
$host = $this->host; | ||
if(empty($lang)) { | ||
return $host; | ||
} | ||
|
||
// Routes to leave as they are | ||
$skip = Config::get('url.redirect.skip'); | ||
// Redirect to proper URL if url_lang is defined | ||
if (!Config::get('url.url_lang') || $this->matchPrefix($this->path, $skip)) { | ||
return $host; | ||
} | ||
|
||
// Routes to alway reditect to the main url | ||
$fixed = Config::get('url.redirect.fixed'); | ||
$parts = explode('.', $host); | ||
$sub_lang = $parts[0]; | ||
if($sub_lang == 'www') $sub_lang = Config::get('lang'); | ||
if (Lang::exists($sub_lang)) { | ||
// reduce url: ca.goteo.org => goteo.org | ||
array_shift($parts); | ||
$host = implode('.', $parts); | ||
} | ||
// if reduced URL is the main domain, redirect to sub-level lang | ||
if($host === Config::get("url.url_lang")) { | ||
if($this->request->query->has('lang')) { | ||
$this->request->query->remove('lang'); | ||
} | ||
// Login controller should mantaing always the same URL to help browser password management | ||
if($this->matchPrefix($this->path, $fixed)) { | ||
// $host = "$host"; | ||
$this->request->query->set('lang', $lang); | ||
} | ||
else { | ||
$host = preg_replace('!https?://|/$!i', '', Lang::getUrl($lang)); | ||
} | ||
|
||
} | ||
return $host; | ||
} | ||
|
||
/** | ||
* Chechsk if any of the elements in array $prefixes starts with the same chars as $full_str | ||
* @param string $full_str full string | ||
* @param array $prefixes array of prexixes | ||
* @return boolean found or not | ||
*/ | ||
protected function matchPrefix($full_str, $prefixes) { | ||
if(!is_array($prefixes)) { | ||
$prefixes = [$prefixes]; | ||
} | ||
foreach($prefixes as $str) { | ||
if(strpos($full_str, $str) === 0) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
<?php | ||
|
||
namespace Goteo\Util\Tests; | ||
|
||
use Goteo\Util\Parsers\UrlLang; | ||
use Goteo\Application\Config; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
class UrlLangTest extends \PHPUnit\Framework\TestCase { | ||
|
||
protected function getParser($url, $host="http://example.com") { | ||
$request = Request::create("$host$url"); | ||
return new UrlLang($request); | ||
} | ||
|
||
public function testInstance() { | ||
$parser = $this->getParser("/"); | ||
|
||
$this->assertInstanceOf('\Goteo\Util\Parsers\UrlLang', $parser); | ||
|
||
return $parser; | ||
} | ||
|
||
/** | ||
* @depends testInstance | ||
* */ | ||
public function testSkipSessionManagement($parser) { | ||
$this->assertFalse($parser->skipSessionManagement()); | ||
|
||
$parser = $this->getParser("/test/something"); | ||
$this->assertFalse($parser->skipSessionManagement()); | ||
|
||
Config::set('session.skip', "/test"); | ||
$this->assertTrue($parser->skipSessionManagement()); | ||
|
||
Config::set('session.skip', ["/test"]); | ||
$this->assertTrue($parser->skipSessionManagement()); | ||
|
||
} | ||
|
||
/** | ||
* @depends testInstance | ||
* */ | ||
public function testDomainLang($parser) { | ||
$this->assertEquals("example.com", $parser->getHost("")); | ||
|
||
$parser = $this->getParser("/test/something"); | ||
$this->assertEquals("example.com", $parser->getHost("")); | ||
|
||
Config::set('url.url_lang', "example.com"); | ||
$this->assertEquals("example.com", $parser->getHost("")); | ||
$this->assertEquals("en.example.com", $parser->getHost("en")); | ||
|
||
Config::set("lang", "es"); | ||
$this->assertEquals("www.example.com", $parser->getHost("es")); | ||
$this->assertEquals("en.example.com", $parser->getHost("en")); | ||
|
||
$parser = $this->getParser("/", "http://www.example.com"); | ||
$this->assertEquals("en.example.com", $parser->getHost("en")); | ||
|
||
return $parser; | ||
} | ||
|
||
/** | ||
* @depends testDomainLang | ||
* */ | ||
public function testRedirectSkip($parser) { | ||
$parser = $this->getParser("/api"); | ||
$this->assertEquals("example.com", $parser->getHost("en")); | ||
|
||
$parser = $this->getParser("/api", "http://www.example.com"); | ||
$this->assertEquals("www.example.com", $parser->getHost("en")); | ||
|
||
$parser = $this->getParser("/password-recovery", "http://ca.example.com"); | ||
$this->assertEquals("ca.example.com", $parser->getHost("en")); | ||
} | ||
|
||
/** | ||
* @depends testDomainLang | ||
* */ | ||
public function testFixedRedirection($parser) { | ||
$parser = $this->getParser("/login"); | ||
$this->assertEquals("example.com", $parser->getHost("en")); | ||
|
||
$parser = $this->getParser("/login", "http://www.example.com"); | ||
$this->assertEquals("example.com", $parser->getHost("en")); | ||
|
||
$parser = $this->getParser("/password-reset", "http://ca.example.com"); | ||
$this->assertEquals("example.com", $parser->getHost("en")); | ||
} | ||
|
||
/** | ||
* @depends testDomainLang | ||
* */ | ||
public function testSubdomains($parser) { | ||
$parser = $this->getParser("/api", "http://www.sub.example.com"); | ||
$this->assertEquals("www.sub.example.com", $parser->getHost("en")); | ||
$parser = $this->getParser("/login", "http://www.sub.example.com"); | ||
$this->assertEquals("sub.example.com", $parser->getHost("en")); | ||
|
||
$parser = $this->getParser("/", "http://www.sub.example.com"); | ||
$this->assertEquals("sub.example.com", $parser->getHost("en")); | ||
|
||
Config::set('url.url_lang', "sub.example.com"); | ||
|
||
$parser = $this->getParser("/api", "http://www.sub.example.com"); | ||
$this->assertEquals("www.sub.example.com", $parser->getHost("en")); | ||
$parser = $this->getParser("/login", "http://www.sub.example.com"); | ||
$this->assertEquals("sub.example.com", $parser->getHost("en")); | ||
|
||
$parser = $this->getParser("/", "http://www.sub.example.com"); | ||
$this->assertEquals("en.sub.example.com", $parser->getHost("en")); | ||
|
||
$parser = $this->getParser("/test/something", "http://en.sub.example.com"); | ||
$this->assertEquals("www.sub.example.com", $parser->getHost("es")); | ||
$this->assertEquals("ca.sub.example.com", $parser->getHost("ca")); | ||
} | ||
} |