diff --git a/.php_cs b/.php_cs index 3949491..2e08f9b 100644 --- a/.php_cs +++ b/.php_cs @@ -31,9 +31,7 @@ return Symfony\CS\Config\Config::create() 'strict_param', '-no_empty_lines_after_phpdocs', 'header_comment', - 'newline_after_open_tag', 'phpdoc_order', - 'phpdoc_var_to_type', '-phpdoc_to_comment', ] ) diff --git a/.travis.yml b/.travis.yml index a883581..f9fe0e6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,14 +7,12 @@ php: - 7.0 - hhvm - nightly - - hhvm-nightly matrix: allow_failures: - php: 7.0 - php: nightly - php: hhvm - - php: hhvm-nightly fast_finish: true sudo: false diff --git a/src/Gwa/AbstractResolver.php b/src/Gwa/AbstractResolver.php new file mode 100644 index 0000000..6d07694 --- /dev/null +++ b/src/Gwa/AbstractResolver.php @@ -0,0 +1,100 @@ + + * @copyright 2015 Great White Ark + * + * @link http://www.greatwhiteark.com + * + * @license MIT + */ + +/** + * AbstractResolver. + * + * @author Daniel Bannert + */ +abstract class AbstractResolver +{ + /** + * Folder path to wordpress, with trailing slash. + * + * @var string + */ + protected $wpDirectoryPath = ''; + + /** + * Wordpress folder name. + * + * @var string + */ + protected $wpFolderName = ''; + + /** + * MultisiteDirectoryResolver. + * + * @param string $wpdir + */ + public function __construct($wpdir) + { + $this->wpDirectoryPath = substr($wpdir, -1) === '/' ? $wpdir : $wpdir.'/'; + $this->setWpFolderName(); + } + + /** + * Set the right links in Adminbar. + * + * @param string $path + * @param string $scheme + * + * @return string + */ + public function fixNetworkAdminUrlFilter($path = '', $scheme = 'admin') + { + if (strpos($path, $this->wpDirectoryPath)) { + return $path; + } + + $wordpressUrl = [ + '/(wp-admin)/', + '/(wp-login\.php)/', + '/(wp-activate\.php)/', + '/(wp-signup\.php)/', + ]; + + $multiSiteUrl = [ + $this->wpFolderName.'/wp-admin', + $this->wpFolderName.'/wp-login.php', + $this->wpFolderName.'/wp-activate.php', + $this->wpFolderName.'/wp-signup.php', + ]; + + return preg_replace($wordpressUrl, $multiSiteUrl, $path, 1); + } + + /** + * Init all filter. + */ + public function init() + { + add_filter('network_admin_url', [$this, 'fixNetworkAdminUrlFilter'], 10, 2); + + add_filter('script_loader_src', [$this, 'fixStyleScriptPathFilter'], 10, 2); + add_filter('style_loader_src', [$this, 'fixStyleScriptPathFilter'], 10, 2); + } + + /** + * Set wordpress folder name. + * + * @param string + */ + protected function setWpFolderName() + { + $dirs = explode('/', $this->wpDirectoryPath); + + $this->wpFolderName = $dirs[count($dirs) - 2]; + } +} diff --git a/src/Gwa/Contracts/MultisiteDirectoryResolver.php b/src/Gwa/Contracts/MultisiteDirectoryResolver.php new file mode 100644 index 0000000..3d53e8a --- /dev/null +++ b/src/Gwa/Contracts/MultisiteDirectoryResolver.php @@ -0,0 +1,41 @@ + + * @copyright 2015 Great White Ark + * + * @link http://www.greatwhiteark.com + * + * @license MIT + */ + +/** + * MultisiteDirectoryResolver. + * + * @author Daniel Bannert + */ +Interface MultisiteDirectoryResolver +{ + /** + * Set the right links in Adminbar. + * + * @param string $path + * @param string $scheme + * + * @return string + */ + public function fixNetworkAdminUrlFilter($path = '', $scheme = 'admin'); + + /** + * Set the right path for script and style loader. + * + * @param string $src + * @param string $handle + * + * @return string + */ + public function fixStyleScriptPathFilter($src, $handle); +} diff --git a/src/Gwa/MultisiteDirectoryResolver.php b/src/Gwa/MultisiteDirectoryResolver.php index 8fe8141..fd36f7f 100644 --- a/src/Gwa/MultisiteDirectoryResolver.php +++ b/src/Gwa/MultisiteDirectoryResolver.php @@ -1,5 +1,4 @@ wpDirectoryPath = substr($wpdir, -1) === '/' ? $wpdir : $wpdir.'/'; + $this->wpDirectoryPath = substr($wpdir, -1) === '/' ? $wpdir : $wpdir.'/';; $this->setWpFolderName(); } - /** - * Set the right links in Adminbar. - * - * @param string $path - * @param string $scheme - * - * @return string - */ - public function fixNetworkAdminUrlFilter($path = '', $scheme = 'admin') - { - if (strpos($path, $this->wpDirectoryPath)) { - return $path; - } - - $wordpressUrl = [ - '/(wp-admin)/', - '/(wp-login\.php)/', - '/(wp-activate\.php)/', - '/(wp-signup\.php)/', - ]; - - $multiSiteUrl = [ - $this->wpFolderName.'/wp-admin', - $this->wpFolderName.'/wp-login.php', - $this->wpFolderName.'/wp-activate.php', - $this->wpFolderName.'/wp-signup.php', - ]; - - return preg_replace($wordpressUrl, $multiSiteUrl, $path, 1); - } - /** * Set the right path for wp-login and wp-admin. * @@ -97,7 +63,7 @@ public function fixSiteUrlFilter($url, $path, $scheme) } $wordpressUrl = ['/(wp-login\.php)/', '/(wp-admin)/']; - $multiSiteUrl = [$this->wpDirectoryPath.'wp-login.php', $this->wpDirectoryPath.'wp-admin']; + $multiSiteUrl = [trim($this->wpDirectoryPath, '/').'/wp-login.php', trim($this->wpDirectoryPath, '/').'/wp-admin']; return preg_replace($wordpressUrl, $multiSiteUrl, $url, 1); } @@ -120,10 +86,10 @@ public function fixStyleScriptPathFilter($src, $handle) strpos($src, $dir) === false ) { $styleUrl = explode(site_url(), $src); - $src = site_url().'/'.$dir.$styleUrl[1]; + $src = site_url().$dir.$styleUrl[1]; } - if (strpos($src, 'plugins') && strpos($src, '/app')) { + if (strpos($src, '/app')) { $src = str_replace('//app', '/app', $src); } @@ -145,34 +111,16 @@ public function fixWpIncludeFolder($url, $path) } $wordpressUrl = ['/(wp-includes)/']; - $multiSiteUrl = [$this->wpDirectoryPath.'wp-includes']; + $multiSiteUrl = [trim($this->wpDirectoryPath, '/').'/wp-includes']; return preg_replace($wordpressUrl, $multiSiteUrl, $url, 1); } - /** - * Init all filter. - */ public function init() { - add_filter('network_admin_url', [$this, 'fixNetworkAdminUrlFilter'], 10, 2); - add_filter('site_url', [$this, 'fixSiteUrlFilter'], 10, 3); - - add_filter('script_loader_src', [$this, 'fixStyleScriptPathFilter'], 10, 2); - add_filter('style_loader_src', [$this, 'fixStyleScriptPathFilter'], 10, 2); + parent::init(); + add_filter('site_url', [$this, 'fixSiteUrlFilter'], 10, 3); add_filter('includes_url', [$this, 'fixWpIncludeFolder'], 10, 2); } - - /** - * Set wordpress folder name. - * - * @param string - */ - protected function setWpFolderName() - { - $folders = explode('/', $this->wpDirectoryPath); - - $this->wpFolderName = $folders[count($folders) - 2]; - } } diff --git a/src/Gwa/MultisiteResolverManager.php b/src/Gwa/MultisiteResolverManager.php new file mode 100644 index 0000000..5f5dfa8 --- /dev/null +++ b/src/Gwa/MultisiteResolverManager.php @@ -0,0 +1,63 @@ + + * @copyright 2015 Great White Ark + * + * @link http://www.greatwhiteark.com + * + * @license MIT + */ + +/** + * MultisiteResolverManager. + * + * @author Daniel Bannert + */ +class MultisiteResolverManager +{ + const TYPE_SUBDOMAIN = '\Gwa\Wordpress\MultisiteSubDomainResolver'; + const TYPE_FOLDER = '\Gwa\Wordpress\MultisiteDirectoryResolver'; + + /** + * Resolver Handler + * + * @var \Gwa\Wordpress\Contracts\MultisiteDirectoryResolver + */ + protected $handler; + + /** + * MultisiteResolverManager. + * + * @param string $wpdir + * @param string $multisiteDomainType + */ + public function __construct($wpdir, $multisiteDomainType) { + if (!is_string($wpdir) || $wpdir === '' || $wpdir === '/') { + throw new \Exception('Please set the relative path to your Wordpress install folder.'); + } + + $this->handler = new $multisiteDomainType($wpdir); + } + + /** + * Get current Handler + * + * @return \Gwa\Wordpress\Contracts\MultisiteDirectoryResolver + */ + public function getHandler() + { + return $this->handler; + } + + /** + * Init all filter. + */ + public function init() + { + $this->handler->init(); + } +} diff --git a/src/Gwa/MultisiteSubDomainResolver.php b/src/Gwa/MultisiteSubDomainResolver.php new file mode 100644 index 0000000..5a56b34 --- /dev/null +++ b/src/Gwa/MultisiteSubDomainResolver.php @@ -0,0 +1,40 @@ + + * @copyright 2015 Great White Ark + * + * @link http://www.greatwhiteark.com + * + * @license MIT + */ + +use Gwa\Wordpress\Contracts\MultisiteDirectoryResolver as ResolverContract; + +/** + * MultisiteSubDomainResolver. + * + * @author Daniel Bannert + */ +class MultisiteSubDomainResolver extends AbstractResolver implements ResolverContract +{ + /** + * Set the right path for script and style loader. + * + * @param string $src + * @param string $handle + * + * @return string + */ + public function fixStyleScriptPathFilter($src, $handle) + { + if (strpos($src, '/app')) { + $src = str_replace('//app', '/app', $src); + } + + return esc_url($src); + } +} diff --git a/tests/MultisiteDirectoryResolverTest.php b/tests/MultisiteDirectoryResolverTest.php index 39698f7..166dc7d 100644 --- a/tests/MultisiteDirectoryResolverTest.php +++ b/tests/MultisiteDirectoryResolverTest.php @@ -11,18 +11,15 @@ * @link http://www.greatwhiteark.com * * @license MIT - * - * @version 0.0.5 */ -use Gwa\Wordpress\MultisiteDirectoryResolver as MDR; +use Gwa\Wordpress\MultisiteResolverManager as MRM; +use Gwa\Wordpress\MultisiteSubDomainResolver as MSDR; /** * MultisiteDirectoryResolverTest. * * @author Daniel Bannert - * - * @since 0.0.2-dev */ class MultisiteDirectoryResolverTest extends \PHPUnit_Framework_TestCase { @@ -31,15 +28,15 @@ class MultisiteDirectoryResolverTest extends \PHPUnit_Framework_TestCase */ public function testCheckForDefinitionException() { - new MDR(''); + new MRM('', MRM::TYPE_FOLDER); } public function testInitFilter() { $installsubfolder = 'foo/wp'; - $cwml = new MDR($installsubfolder); - $cwml->init(); + $cwml = new MRM($installsubfolder, MRM::TYPE_FOLDER); + $cwml->getHandler()->init(); $filters = \Gwa\Wordpress\getAddedFilters(); @@ -48,13 +45,13 @@ public function testInitFilter() $this->assertEquals('network_admin_url', $filters[0]->filtername); $this->assertInternalType('array', $filters[0]->callback); - $this->assertEquals('site_url', $filters[1]->filtername); + $this->assertEquals('script_loader_src', $filters[1]->filtername); $this->assertInternalType('array', $filters[1]->callback); - $this->assertEquals('script_loader_src', $filters[2]->filtername); + $this->assertEquals('style_loader_src', $filters[2]->filtername); $this->assertInternalType('array', $filters[2]->callback); - $this->assertEquals('style_loader_src', $filters[3]->filtername); + $this->assertEquals('site_url', $filters[3]->filtername); $this->assertInternalType('array', $filters[3]->callback); $this->assertEquals('includes_url', $filters[4]->filtername); @@ -70,8 +67,8 @@ public function testFixNetworkLogin() $defaultloginurl = $domain.$installpath.'/foo/wp-login.php'; $expectedloginurl = $domain.$installpath.'/'.$installsubfolder.'/wp-login.php'; - $cwml = new MDR($installsubfolder); - $this->assertEquals($expectedloginurl, $cwml->fixNetworkAdminUrlFilter($defaultloginurl, '', '')); + $cwml = new MRM($installsubfolder, MRM::TYPE_FOLDER); + $this->assertEquals($expectedloginurl, $cwml->getHandler()->fixNetworkAdminUrlFilter($defaultloginurl, '', '')); } public function testFixNetworkAdminUrl() @@ -83,8 +80,8 @@ public function testFixNetworkAdminUrl() $defaultadminurl = $domain.$installpath.'/foo/wp-admin/network'; $expectedadminurl = $domain.$installpath.'/'.$installsubfolder.'/wp-admin/network'; - $cwml = new MDR($installsubfolder); - $this->assertEquals($expectedadminurl, $cwml->fixNetworkAdminUrlFilter($defaultadminurl, '', '')); + $cwml = new MRM($installsubfolder, MRM::TYPE_FOLDER); + $this->assertEquals($expectedadminurl, $cwml->getHandler()->fixNetworkAdminUrlFilter($defaultadminurl, '', '')); } public function testLeavesNetworkAdminUrlWhenCorrect() @@ -95,8 +92,8 @@ public function testLeavesNetworkAdminUrlWhenCorrect() $fixedadminurl = $domain.$installpath.'/'.$installsubfolder.'/wp-admin/network'; - $cwml = new MDR($installsubfolder); - $this->assertEquals($fixedadminurl, $cwml->fixNetworkAdminUrlFilter($fixedadminurl, '', '')); + $cwml = new MRM($installsubfolder, MRM::TYPE_FOLDER); + $this->assertEquals($fixedadminurl, $cwml->getHandler()->fixNetworkAdminUrlFilter($fixedadminurl, '', '')); } public function testFixNetworkActive() @@ -108,8 +105,8 @@ public function testFixNetworkActive() $defaultloginurl = $domain.$installpath.'/foo/wp-activate.php'; $expectedloginurl = $domain.$installpath.'/'.$installsubfolder.'/wp-activate.php'; - $cwml = new MDR($installsubfolder); - $this->assertEquals($expectedloginurl, $cwml->fixNetworkAdminUrlFilter($defaultloginurl, '', '')); + $cwml = new MRM($installsubfolder, MRM::TYPE_FOLDER); + $this->assertEquals($expectedloginurl, $cwml->getHandler()->fixNetworkAdminUrlFilter($defaultloginurl, '', '')); } public function testFixNetworkSignup() @@ -121,8 +118,8 @@ public function testFixNetworkSignup() $defaultloginurl = $domain.$installpath.'/foo/wp-signup.php'; $expectedloginurl = $domain.$installpath.'/'.$installsubfolder.'/wp-signup.php'; - $cwml = new MDR($installsubfolder); - $this->assertEquals($expectedloginurl, $cwml->fixNetworkAdminUrlFilter($defaultloginurl, '', '')); + $cwml = new MRM($installsubfolder, MRM::TYPE_FOLDER); + $this->assertEquals($expectedloginurl, $cwml->getHandler()->fixNetworkAdminUrlFilter($defaultloginurl, '', '')); } public function testFixSiteAdminUrl() @@ -134,8 +131,8 @@ public function testFixSiteAdminUrl() $defaultadminurl = $domain.$installpath.'/wp-admin'; $expectedadminurl = $domain.$installpath.'/'.$installsubfolder.'/wp-admin'; - $cwml = new MDR($installsubfolder); - $this->assertEquals($expectedadminurl, $cwml->fixSiteUrlFilter($defaultadminurl, '', '')); + $cwml = new MRM($installsubfolder, MRM::TYPE_FOLDER); + $this->assertEquals($expectedadminurl, $cwml->getHandler()->fixSiteUrlFilter($defaultadminurl, '', '')); } public function testFixSiteLoginUrl() @@ -147,8 +144,8 @@ public function testFixSiteLoginUrl() $defaultloginurl = $domain.$installpath.'/wp-login.php'; $expectedloginurl = $domain.$installpath.'/'.$installsubfolder.'/wp-login.php'; - $cwml = new MDR($installsubfolder); - $this->assertEquals($expectedloginurl, $cwml->fixSiteUrlFilter($defaultloginurl, '', '')); + $cwml = new MRM($installsubfolder, MRM::TYPE_FOLDER); + $this->assertEquals($expectedloginurl, $cwml->getHandler()->fixSiteUrlFilter($defaultloginurl, '', '')); } public function testFixSiteUrlFilterWhenWpAdminPassed() @@ -160,14 +157,14 @@ public function testFixSiteUrlFilterWhenWpAdminPassed() $urlAdmin = $domain.$installpath.'/wp-admin/'; $correctUrlAdmin = $domain.$installpath.'/'.$installsubfolder.'/wp-admin/'; - $cwml = new MDR($installsubfolder); - $this->assertEquals($correctUrlAdmin, $cwml->fixSiteUrlFilter($urlAdmin, '', '')); + $cwml = new MRM($installsubfolder, MRM::TYPE_FOLDER); + $this->assertEquals($correctUrlAdmin, $cwml->getHandler()->fixSiteUrlFilter($urlAdmin, '', '')); - $cwml = new MDR($installsubfolder); - $this->assertNotEquals($urlAdmin, $cwml->fixSiteUrlFilter($urlAdmin, '', '')); + $cwml = new MRM($installsubfolder, MRM::TYPE_FOLDER); + $this->assertNotEquals($urlAdmin, $cwml->getHandler()->fixSiteUrlFilter($urlAdmin, '', '')); - $cwml = new MDR($installsubfolder); - $this->assertEquals($correctUrlAdmin, $cwml->fixSiteUrlFilter($correctUrlAdmin, '', '')); + $cwml = new MRM($installsubfolder, MRM::TYPE_FOLDER); + $this->assertEquals($correctUrlAdmin, $cwml->getHandler()->fixSiteUrlFilter($correctUrlAdmin, '', '')); } public function testFixSiteUrlFilterWhenWpLoginPassed() @@ -179,11 +176,11 @@ public function testFixSiteUrlFilterWhenWpLoginPassed() $urlLogin = $domain.$installpath.'/wp-login.php'; $correctUrlLogin = $domain.$installpath.'/'.$installsubfolder.'/wp-login.php'; - $cwml = new MDR($installsubfolder); - $this->assertEquals($correctUrlLogin, $cwml->fixSiteUrlFilter($urlLogin, '', '')); + $cwml = new MRM($installsubfolder, MRM::TYPE_FOLDER); + $this->assertEquals($correctUrlLogin, $cwml->getHandler()->fixSiteUrlFilter($urlLogin, '', '')); - $cwml = new MDR($installsubfolder); - $this->assertEquals($correctUrlLogin, $cwml->fixSiteUrlFilter($correctUrlLogin, '', '')); + $cwml = new MRM($installsubfolder, MRM::TYPE_FOLDER); + $this->assertEquals($correctUrlLogin, $cwml->getHandler()->fixSiteUrlFilter($correctUrlLogin, '', '')); } public function testFixesStyleScriptURLWhenExternUrlPassed($value = '') @@ -192,8 +189,8 @@ public function testFixesStyleScriptURLWhenExternUrlPassed($value = '') $externalUrl = '//fonts.googleapis.com/css?family=Open+Sans%3A300italic%2C400italic%2C600italic%2C300%2C400%2C600&subset=latin%2Clatin-ext&ver=4.2.1'; - $cwml = new MDR($installsubfolder); - $this->assertEquals($externalUrl, $cwml->fixStyleScriptPathFilter($externalUrl, '')); + $cwml = new MRM($installsubfolder, MRM::TYPE_FOLDER); + $this->assertEquals($externalUrl, $cwml->getHandler()->fixStyleScriptPathFilter($externalUrl, '')); } public function testFixesStyleScriptURLWhenPluginUrlPassed($value = '') @@ -205,14 +202,14 @@ public function testFixesStyleScriptURLWhenPluginUrlPassed($value = '') $urlPlugin = $domain.$installpath.'//app/plugins/'; $expectedUrl = $domain.$installpath.'/app/plugins/'; - $cwml = new MDR($installsubfolder); - $this->assertEquals($expectedUrl, $cwml->fixStyleScriptPathFilter($urlPlugin, '')); + $cwml = new MRM($installsubfolder, MRM::TYPE_FOLDER); + $this->assertEquals($expectedUrl, $cwml->getHandler()->fixStyleScriptPathFilter($urlPlugin, '')); $urlPlugin = 'http://example.org/projects/testWordpress/web/wp//app/plugins/'; $expectedUrl = 'http://example.org/projects/testWordpress/web/wp/app/plugins/'; - $cwml = new MDR($installsubfolder); - $this->assertEquals($expectedUrl, $cwml->fixStyleScriptPathFilter($urlPlugin, '')); + $cwml = new MRM($installsubfolder, MRM::TYPE_FOLDER); + $this->assertEquals($expectedUrl, $cwml->getHandler()->fixStyleScriptPathFilter($urlPlugin, '')); } public function testFixesStyleScriptURLWhenSiteUrlPassed() @@ -222,10 +219,10 @@ public function testFixesStyleScriptURLWhenSiteUrlPassed() $installsubfolder = 'foo/wp'; $siteurl = 'http://example.org/projects/testWordpress/'; - $urlexpected = $siteurl.'/'.$installsubfolder; + $urlexpected = $siteurl.$installsubfolder; - $cwml = new MDR($installsubfolder); - $this->assertEquals($urlexpected, $cwml->fixStyleScriptPathFilter($siteurl, '')); + $cwml = new MRM($installsubfolder, MRM::TYPE_FOLDER); + $this->assertEquals($urlexpected, $cwml->getHandler()->fixStyleScriptPathFilter($siteurl, '')); } public function testLeavesStyleScriptURLWhenCorrectUrlPassed() @@ -237,8 +234,8 @@ public function testLeavesStyleScriptURLWhenCorrectUrlPassed() $siteurl = $domain.$installpath.'/'; $urlpassed = $urlexpected = $siteurl.'/'.$installsubfolder.'/'; - $cwml = new MDR($installsubfolder); - $this->assertEquals($urlexpected, $cwml->fixStyleScriptPathFilter($urlpassed, '')); + $cwml = new MRM($installsubfolder, MRM::TYPE_FOLDER); + $this->assertEquals($urlexpected, $cwml->getHandler()->fixStyleScriptPathFilter($urlpassed, '')); } public function testFixWpIncludeFolder() @@ -250,14 +247,14 @@ public function testFixWpIncludeFolder() $url = $domain.$installpath.'/wp-includes/'; $correctUrl = $domain.$installpath.'/'.$installsubfolder.'/wp-includes/'; - $cwml = new MDR($installsubfolder); - $this->assertEquals($correctUrl, $cwml->fixWpIncludeFolder($url, '')); + $cwml = new MRM($installsubfolder, MRM::TYPE_FOLDER); + $this->assertEquals($correctUrl, $cwml->getHandler()->fixWpIncludeFolder($url, '')); - $cwml = new MDR($installsubfolder); - $this->assertNotEquals($url, $cwml->fixWpIncludeFolder($url, '')); + $cwml = new MRM($installsubfolder, MRM::TYPE_FOLDER); + $this->assertNotEquals($url, $cwml->getHandler()->fixWpIncludeFolder($url, '')); - $cwml = new MDR($installsubfolder); - $this->assertEquals($correctUrl, $cwml->fixWpIncludeFolder($correctUrl, '')); + $cwml = new MRM($installsubfolder, MRM::TYPE_FOLDER); + $this->assertEquals($correctUrl, $cwml->getHandler()->fixWpIncludeFolder($correctUrl, '')); } public function testSetWpFolderName() @@ -266,16 +263,16 @@ public function testSetWpFolderName() $installpath = '/path/to/my/project'; $installsubfolder = 'foo/wp'; - $cwml = new CwmlStub($installsubfolder); + $cwml = new CwmlStub($installsubfolder, MRM::TYPE_FOLDER); $this->assertEquals('wp', $cwml->getWordpressName()); $this->assertNotEquals('web', $cwml->getWordpressName()); } } -class CwmlStub extends MDR +class CwmlStub extends MSDR { public function getWordpressName() { return $this->wpFolderName; } -} +} \ No newline at end of file diff --git a/tests/MultisiteManagerTest.php b/tests/MultisiteManagerTest.php new file mode 100644 index 0000000..007e721 --- /dev/null +++ b/tests/MultisiteManagerTest.php @@ -0,0 +1,41 @@ + + * @copyright 2015 Great White Ark + * + * @link http://www.greatwhiteark.com + * + * @license MIT + */ + +use Gwa\Wordpress\MultisiteResolverManager as MRM; + +/** + * MultisiteResolverManager. + * + * @author Daniel Bannert + */ +class MultisiteResolverManager extends \PHPUnit_Framework_TestCase +{ + /** + * @expectedException \Exception + */ + public function testCheckForDefinitionException() + { + new MRM('', MRM::TYPE_SUBDOMAIN); + } + + public function testGetHandler() + { + $mrm = new MRM('/wp/', MRM::TYPE_SUBDOMAIN); + $this->assertInstanceOf('\Gwa\Wordpress\MultisiteSubDomainResolver', $mrm->getHandler()); + + $mrm = new MRM('/wp/', MRM::TYPE_FOLDER); + $this->assertInstanceOf('\Gwa\Wordpress\MultisiteDirectoryResolver', $mrm->getHandler()); + } +} diff --git a/tests/MultisiteSubDomainResolverTest.php b/tests/MultisiteSubDomainResolverTest.php new file mode 100644 index 0000000..1f14356 --- /dev/null +++ b/tests/MultisiteSubDomainResolverTest.php @@ -0,0 +1,162 @@ + + * @copyright 2015 Great White Ark + * + * @link http://www.greatwhiteark.com + * + * @license MIT + */ + +use Gwa\Wordpress\MultisiteResolverManager as MRM; +use Gwa\Wordpress\MultisiteDirectoryResolver as MDR; + +/** + * MultisiteSubDomainResolverTest. + * + * @author Daniel Bannert + */ +class MultisiteSubDomainResolverTest extends \PHPUnit_Framework_TestCase +{ + /** + * @expectedException \Exception + */ + public function testCheckForDefinitionException() + { + new MRM('', MRM::TYPE_SUBDOMAIN); + } + + public function testInitFilter() + { + $installsubfolder = 'foo/wp'; + + $cwml = new MRM($installsubfolder, MRM::TYPE_SUBDOMAIN); + $cwml->getHandler()->init(); + + $filters = \Gwa\Wordpress\getAddedFilters(); + + $this->assertEquals('network_admin_url', $filters[0]->filtername); + $this->assertInternalType('array', $filters[0]->callback); + + $this->assertEquals('script_loader_src', $filters[1]->filtername); + $this->assertInternalType('array', $filters[1]->callback); + + $this->assertEquals('style_loader_src', $filters[2]->filtername); + $this->assertInternalType('array', $filters[2]->callback); + } + + public function testFixNetworkLogin() + { + $domain = 'http://example.org'; + $installpath = '/path/to/my/project'; + $installsubfolder = 'foo/wp'; + + $defaultloginurl = $domain.$installpath.'/foo/wp-login.php'; + $expectedloginurl = $domain.$installpath.'/'.$installsubfolder.'/wp-login.php'; + + $cwml = new MRM($installsubfolder, MRM::TYPE_SUBDOMAIN); + $this->assertEquals($expectedloginurl, $cwml->getHandler()->fixNetworkAdminUrlFilter($defaultloginurl, '', '')); + } + + public function testFixNetworkAdminUrl() + { + $domain = 'http://example.org'; + $installpath = '/path/to/my/project'; + $installsubfolder = 'foo/wp'; + + $defaultadminurl = $domain.$installpath.'/foo/wp-admin/network'; + $expectedadminurl = $domain.$installpath.'/'.$installsubfolder.'/wp-admin/network'; + + $cwml = new MRM($installsubfolder, MRM::TYPE_SUBDOMAIN); + $this->assertEquals($expectedadminurl, $cwml->getHandler()->fixNetworkAdminUrlFilter($defaultadminurl, '', '')); + } + + public function testLeavesNetworkAdminUrlWhenCorrect() + { + $domain = 'http://example.org'; + $installpath = '/path/to/my/project'; + $installsubfolder = 'foo/wp'; + + $fixedadminurl = $domain.$installpath.'/'.$installsubfolder.'/wp-admin/network'; + + $cwml = new MRM($installsubfolder, MRM::TYPE_SUBDOMAIN); + $this->assertEquals($fixedadminurl, $cwml->getHandler()->fixNetworkAdminUrlFilter($fixedadminurl, '', '')); + } + + public function testFixNetworkActive() + { + $domain = 'http://example.org'; + $installpath = '/path/to/my/project'; + $installsubfolder = 'foo/wp'; + + $defaultloginurl = $domain.$installpath.'/foo/wp-activate.php'; + $expectedloginurl = $domain.$installpath.'/'.$installsubfolder.'/wp-activate.php'; + + $cwml = new MRM($installsubfolder, MRM::TYPE_SUBDOMAIN); + $this->assertEquals($expectedloginurl, $cwml->getHandler()->fixNetworkAdminUrlFilter($defaultloginurl, '', '')); + } + + public function testFixNetworkSignup() + { + $domain = 'http://example.org'; + $installpath = '/path/to/my/project'; + $installsubfolder = 'foo/wp'; + + $defaultloginurl = $domain.$installpath.'/foo/wp-signup.php'; + $expectedloginurl = $domain.$installpath.'/'.$installsubfolder.'/wp-signup.php'; + + $cwml = new MRM($installsubfolder, MRM::TYPE_SUBDOMAIN); + $this->assertEquals($expectedloginurl, $cwml->getHandler()->fixNetworkAdminUrlFilter($defaultloginurl, '', '')); + } + + public function testFixesAppURLWhenSiteUrlPassed() + { + $domain = 'http://example.org'; + $installpath = '/path/to/my/project'; + $installsubfolder = '/wp'; + $appFolder = 'app'; + + $siteurl = 'http://example.org/projects/testWordpress/'; + $urlexpected = $siteurl.$appFolder; + + $cwml = new MRM($installsubfolder, MRM::TYPE_SUBDOMAIN); + $this->assertEquals($urlexpected, $cwml->getHandler()->fixStyleScriptPathFilter($siteurl.'/app', '')); + } + + public function testLeavesStyleScriptURLWhenCorrectUrlPassed() + { + $domain = 'http://example.org'; + $installpath = '/path/to/my/project'; + $installsubfolder = 'foo/wp'; + + $siteurl = $domain.$installpath.'/'; + $urlpassed = $urlexpected = $siteurl.'/'.$installsubfolder.'/'; + + $cwml = new MRM($installsubfolder, MRM::TYPE_SUBDOMAIN); + $this->assertEquals($urlexpected, $cwml->getHandler()->fixStyleScriptPathFilter($urlpassed, '')); + } + + public function testSetWpFolderName() + { + $domain = 'http://example.org'; + $installpath = '/path/to/my/project'; + $installsubfolder = 'foo/wp'; + + $cwml = new SubDomainStub($installsubfolder, MRM::TYPE_SUBDOMAIN); + $this->assertEquals('wp', $cwml->getWordpressName()); + $this->assertNotEquals('web', $cwml->getWordpressName()); + } +} + +class SubDomainStub extends MDR +{ + public function getWordpressName() + { + return $this->wpFolderName; + } +} \ No newline at end of file