diff options
Diffstat (limited to 'www/wiki/extensions/Bootstrap/tests')
6 files changed, 601 insertions, 0 deletions
diff --git a/www/wiki/extensions/Bootstrap/tests/bootstrap.php b/www/wiki/extensions/Bootstrap/tests/bootstrap.php new file mode 100644 index 00000000..cff461f0 --- /dev/null +++ b/www/wiki/extensions/Bootstrap/tests/bootstrap.php @@ -0,0 +1,38 @@ +<?php + +if ( php_sapi_name() !== 'cli' ) { + die( 'Not an entry point' ); +} + +if ( !defined( 'MEDIAWIKI' ) ) { + die( 'MediaWiki is not available for the test environment' ); +} + +function registerAutoloaderPath( $identifier, $path ) { + print( "\nUsing the {$identifier} vendor autoloader ...\n\n" ); + return require $path; +} + +function runTestAutoLoader() { + + $mwVendorPath = __DIR__ . '/../../../vendor/autoload.php'; + $localVendorPath = __DIR__ . '/../vendor/autoload.php'; + + if ( is_readable( $localVendorPath ) ) { + $autoLoader = registerAutoloaderPath( 'local', $localVendorPath ); + } elseif ( is_readable( $mwVendorPath ) ) { + $autoLoader = registerAutoloaderPath( 'MediaWiki', $mwVendorPath ); + } + + if ( !$autoLoader instanceof \Composer\Autoload\ClassLoader ) { + return false; + } + + $autoLoader->addPsr4( 'Bootstrap\\Tests\\', __DIR__ . '/phpunit' ); + + return true; +} + +if ( !runTestAutoLoader() ) { + die( 'The required test autoloader was not accessible' ); +} diff --git a/www/wiki/extensions/Bootstrap/tests/mw-phpunit-runner.php b/www/wiki/extensions/Bootstrap/tests/mw-phpunit-runner.php new file mode 100644 index 00000000..c3928c64 --- /dev/null +++ b/www/wiki/extensions/Bootstrap/tests/mw-phpunit-runner.php @@ -0,0 +1,43 @@ +<?php + +/** + * Lazy script to invoke the MediaWiki phpunit runner + * + * php mw-phpunit-runner.php [options] + */ + +if ( php_sapi_name() !== 'cli' ) { + die( 'Not an entry point' ); +} + +print( "\nMediaWiki phpunit runnner ... \n" ); + +function isReadablePath( $path ) { + + if ( is_readable( $path ) ) { + return $path; + } + + throw new RuntimeException( "Expected an accessible {$path} path" ); +} + +function addArguments( $args ) { + + $arguments = array(); + + for ( $arg = reset( $args ); $arg !== false; $arg = next( $args ) ) { + + if ( $arg === basename( __FILE__ ) ) { + continue; + } + + $arguments[] = $arg; + } + + return $arguments; +} + +$mw = isReadablePath( __DIR__ . "/../../../tests/phpunit/phpunit.php" ); +$config = isReadablePath( __DIR__ . "/../phpunit.xml.dist" ); + +passthru( "php {$mw} -c {$config} " . implode( ' ', addArguments( $GLOBALS['argv'] ) ) ); diff --git a/www/wiki/extensions/Bootstrap/tests/phpunit/BootstrapManagerTest.php b/www/wiki/extensions/Bootstrap/tests/phpunit/BootstrapManagerTest.php new file mode 100644 index 00000000..5da60707 --- /dev/null +++ b/www/wiki/extensions/Bootstrap/tests/phpunit/BootstrapManagerTest.php @@ -0,0 +1,195 @@ +<?php + +namespace Bootstrap\Tests; + +use Bootstrap\BootstrapManager; + +/** + * @uses \Bootstrap\BootstrapManager + * + * @ingroup Test + * + * @group extension-bootstrap + * @group mediawiki-databaseless + * + * @license GNU GPL v3+ + * @since 1.0 + * + * @author mwjames + */ +class BootstrapManagerTest extends \PHPUnit_Framework_TestCase { + + protected $wgResourceModules = null; + + protected function setUp() { + parent::setUp(); + $this->wgResourceModules = $GLOBALS['wgResourceModules']; + + // Preset with empty default values to verify the initialization status + // during invocation + $GLOBALS['wgResourceModules'][ 'ext.bootstrap.styles' ] = array( + 'localBasePath' => '', + 'remoteBasePath' => '', + 'class' => '', + 'dependencies' => array(), + 'styles' => array(), + 'variables' => array(), + 'external styles' => array() + ); + + $GLOBALS['wgResourceModules'][ 'ext.bootstrap.scripts' ] = array( + 'dependencies' => array(), + 'scripts' => array() + ); + } + + protected function tearDown() { + $GLOBALS['wgResourceModules'] = $this->wgResourceModules; + BootstrapManager::clear(); + + parent::tearDown(); + } + + public function testCanConstruct() { + + $moduleDefinition = $this->getMockBuilder( '\Bootstrap\Definition\ModuleDefinition' ) + ->disableOriginalConstructor() + ->setMethods( array( 'get' ) ) + ->getMock(); + + $moduleDefinition->expects( $this->atLeastOnce() ) + ->method( 'get' ) + ->will( $this->returnValue( array() ) ); + + $this->assertInstanceOf( + '\Bootstrap\BootstrapManager', + new BootstrapManager( $moduleDefinition ) + ); + + BootstrapManager::clear(); + + $this->assertInstanceOf( + '\Bootstrap\BootstrapManager', + BootstrapManager::getInstance() + ); + } + + public function testLoadStylesFromModuleDefinition() { + + $this->assertEmpty( $this->getGlobalResourceModuleBootstrapStyles() ); + + $moduleDefinition = $this->getMockBuilder( '\Bootstrap\Definition\ModuleDefinition' ) + ->disableOriginalConstructor() + ->setMethods( array( 'get' ) ) + ->getMock(); + + $moduleDefinition->expects( $this->at( 0 ) ) + ->method( 'get' ) + ->with( $this->stringContains( 'descriptions' ) ) + ->will( $this->returnValue( array( 'variables' => array( 'styles' => 'variables' ) ) ) ); + + $moduleDefinition->expects( $this->at( 1 ) ) + ->method( 'get' ) + ->with( $this->stringContains( 'core' ) ) + ->will( $this->returnValue( array( 'variables' ) ) ); + + $moduleDefinition->expects( $this->at( 2 ) ) + ->method( 'get' ) + ->with( $this->stringContains( 'optional' ) ) + ->will( $this->returnValue( array( 'foo' ) ) ); + + $instance = new BootstrapManager( $moduleDefinition ); + $instance->addAllBootstrapModules(); + + $this->assertNotEmpty( $this->getGlobalResourceModuleBootstrapStyles() ); + } + + public function testSetLessVariables() { + + $moduleDefinition = $this->getMockBuilder( '\Bootstrap\Definition\ModuleDefinition' ) + ->disableOriginalConstructor() + ->setMethods( array( 'get' ) ) + ->getMock(); + + $moduleDefinition->expects( $this->atLeastOnce() ) + ->method( 'get' ) + ->will( $this->returnValue( array() ) ); + + $instance = new BootstrapManager( $moduleDefinition ); + $instance->setLessVariables( array( 'foo' => 'bar') ); + $instance->setLessVariable( 'ichi', 'ni' ); + + $this->assertArrayHasKey( + 'foo', + $this->getGlobalResourceModuleBootstrapVariables() + ); + + $this->assertArrayHasKey( + 'ichi', + $this->getGlobalResourceModuleBootstrapVariables() + ); + } + + public function testAddCacheTriggerFiles() { + + $moduleDefinition = $this->getMockBuilder( '\Bootstrap\Definition\ModuleDefinition' ) + ->disableOriginalConstructor() + ->setMethods( array( 'get' ) ) + ->getMock(); + + $moduleDefinition->expects( $this->atLeastOnce() ) + ->method( 'get' ) + ->will( $this->returnValue( array() ) ); + + $instance = new BootstrapManager( $moduleDefinition ); + $instance-> addCacheTriggerFile( array( 'foo' => 'bar') ); + $instance-> addCacheTriggerFile( 'ichi' ); + + $triggers = $this->getGlobalResourceModuleBootstrapCacheTriggers(); + + $this->assertTrue( + isset( $triggers[ 'foo' ] ) && $triggers[ 'foo' ] === 'bar' + ); + + $this->assertTrue( + array_search( 'ichi', $triggers ) !== false + ); + } + + public function testAddExternalModule() { + + $moduleDefinition = $this->getMockBuilder( '\Bootstrap\Definition\ModuleDefinition' ) + ->disableOriginalConstructor() + ->setMethods( array( 'get' ) ) + ->getMock(); + + $moduleDefinition->expects( $this->atLeastOnce() ) + ->method( 'get' ) + ->will( $this->returnValue( array() ) ); + + $instance = new BootstrapManager( $moduleDefinition ); + $instance->addExternalModule( 'ExternalFooModule', 'ExternalRemoteBarPath' ); + + $this->assertArrayHasKey( + 'ExternalFooModule', + $this->getGlobalResourceModuleBootstrapExternalStyles() + ); + } + + private function getGlobalResourceModuleBootstrapStyles() { + return $GLOBALS['wgResourceModules'][ 'ext.bootstrap.styles' ]['styles']; + } + + private function getGlobalResourceModuleBootstrapVariables() { + return $GLOBALS['wgResourceModules'][ 'ext.bootstrap.styles' ]['variables']; + } + + private function getGlobalResourceModuleBootstrapExternalStyles() { + return $GLOBALS['wgResourceModules'][ 'ext.bootstrap.styles' ]['external styles']; + } + + private function getGlobalResourceModuleBootstrapCacheTriggers() { + return $GLOBALS['wgResourceModules'][ 'ext.bootstrap.styles' ]['cachetriggers']; + } + +} diff --git a/www/wiki/extensions/Bootstrap/tests/phpunit/Definition/V3ModuleDefinitionTest.php b/www/wiki/extensions/Bootstrap/tests/phpunit/Definition/V3ModuleDefinitionTest.php new file mode 100644 index 00000000..cb52a6ae --- /dev/null +++ b/www/wiki/extensions/Bootstrap/tests/phpunit/Definition/V3ModuleDefinitionTest.php @@ -0,0 +1,71 @@ +<?php + +namespace Bootstrap\Tests\Definition; + +use Bootstrap\Definition\V3ModuleDefinition; +use Bootstrap\BootstrapManager; + +/** + * @uses \Bootstrap\Definition\V3ModuleDefinition + * + * @ingroup Test + * + * @group extension-bootstrap + * @group mediawiki-databaseless + * + * @license GNU GPL v3+ + * @since 1.0 + * + * @author mwjames + */ +class V3ModuleDefinitionTest extends \PHPUnit_Framework_TestCase { + + public function testCanConstruct() { + + $this->assertInstanceOf( + '\Bootstrap\Definition\ModuleDefinition', + new V3ModuleDefinition() + ); + } + + /** + * @dataProvider keyProvider + */ + public function testGet( $key ) { + + $instance = new V3ModuleDefinition(); + + $this->assertInternalType( + 'array', + $instance->get( $key ) + ); + } + + public function testBootstrapManagerIntegration() { + + $instance = new BootstrapManager( new V3ModuleDefinition() ); + $instance->addAllBootstrapModules(); + + $this->assertTrue( true ); + } + + public function testGetOnInvalidKeyThrowsException() { + + $instance = new V3ModuleDefinition(); + + $this->setExpectedException( 'InvalidArgumentException' ); + $instance->get( 'Foo' ); + } + + public function keyProvider() { + + $provider = array( + array( 'core' ), + array( 'optional' ), + array( 'descriptions' ) + ); + + return $provider; + } + +} diff --git a/www/wiki/extensions/Bootstrap/tests/phpunit/Hooks/SetupAfterCacheTest.php b/www/wiki/extensions/Bootstrap/tests/phpunit/Hooks/SetupAfterCacheTest.php new file mode 100644 index 00000000..7c9d6ea2 --- /dev/null +++ b/www/wiki/extensions/Bootstrap/tests/phpunit/Hooks/SetupAfterCacheTest.php @@ -0,0 +1,163 @@ +<?php + +namespace Bootstrap\Tests\Hooks; + +use Bootstrap\Hooks\SetupAfterCache; + +/** + * @uses \Bootstrap\Hooks\SetupAfterCache + * + * @ingroup Test + * + * @group extension-bootstrap + * @group mediawiki-databaseless + * + * @license GNU GPL v3+ + * @since 1.0 + * + * @author mwjames + */ +class SetupAfterCacheTest extends \PHPUnit_Framework_TestCase { + + protected $localBasePath = null; + protected $localBootstrapVendorPath = null; + + protected function setUp() { + parent::setUp(); + $this->localBootstrapVendorPath = __DIR__ . '/../../../resources/bootstrap'; + } + + public function testCanConstruct() { + + $configuration = array(); + + $this->assertInstanceOf( + '\Bootstrap\Hooks\SetupAfterCache', + new SetupAfterCache( $configuration ) + ); + } + + public function testProcessWithAccessibilityOnBootstrapVendorPath() { + + $configuration = array( + 'localBasePath' => $this->localBootstrapVendorPath, + 'remoteBasePath' => '', + 'IP' => 'someIP', + ); + + $instance = new SetupAfterCache( $configuration ); + + $this->assertTrue( $instance->process() ); + } + + public function testProcess_setsDefaultCacheTriggers() { + + $configuration = array( + 'localBasePath' => $this->localBootstrapVendorPath, + 'remoteBasePath' => '', + 'IP' => 'someIP', + ); + + $this->resetGlobals(); + + $instance = new SetupAfterCache( $configuration ); + + $this->assertTrue( $instance->process() ); + + $this->assertEquals( + 'someIP/LocalSettings.php', + $GLOBALS[ 'wgResourceModules' ][ 'ext.bootstrap.styles' ][ 'cachetriggers' ][ 'LocalSettings.php' ] + ); + + $this->assertEquals( + 'someIP/composer.lock', + $GLOBALS[ 'wgResourceModules' ][ 'ext.bootstrap.styles' ][ 'cachetriggers' ][ 'composer.lock' ] + ); + } + + public function testProcessWithAccessibilityOnAddedLocalResourcePaths() { + + $configuration = array( + 'localBasePath' => $this->localBootstrapVendorPath, + 'remoteBasePath' => '', + 'IP' => 'someIP', + ); + + $instance = new SetupAfterCache( $configuration ); + $instance->process(); + + $this->assertThatPathIsReadable( + $GLOBALS[ 'wgResourceModules' ][ 'ext.bootstrap.styles' ]['localBasePath'] + ); + + $this->assertThatPathIsReadable( + $GLOBALS[ 'wgResourceModules' ][ 'ext.bootstrap.scripts' ]['localBasePath'] + ); + } + + /** + * @dataProvider invalidConfigurationProvider + */ + public function testProcessOnInvalidConfigurationThrowsException( $configuration ) { + + $instance = new SetupAfterCache( $configuration ); + + $this->setExpectedException( 'InvalidArgumentException' ); + $instance->process(); + } + + public function testProcessOnInvalidLocalPathThrowsException() { + + $configuration = array( + 'localBasePath' => 'Foo', + 'remoteBasePath' => '', + 'IP' => 'someIP', + ); + + $instance = new SetupAfterCache( $configuration ); + + $this->setExpectedException( 'RuntimeException' ); + $instance->process(); + } + + public function invalidConfigurationProvider() { + + $provider = array(); + + $provider[] = array( + array() + ); + + $provider[] = array( + array( + 'localBasePath' => 'Foo' + ) + ); + + $provider[] = array( + array( + 'remoteBasePath' => 'Foo' + ) + ); + + return $provider; + } + + protected function assertThatPathIsReadable( $path ) { + $this->assertTrue( is_readable( $path ) ); + } + + private function resetGlobals() { + $GLOBALS[ 'wgResourceModules' ][ 'ext.bootstrap.styles' ] = array ( + 'class' => 'Bootstrap\ResourceLoaderBootstrapModule', + 'styles' => array (), + 'variables' => array (), + 'dependencies' => array (), + 'cachetriggers' => array ( + 'LocalSettings.php' => null, + 'composer.lock' => null, + ), + ); + } + +} diff --git a/www/wiki/extensions/Bootstrap/tests/phpunit/ResourceLoaderBootstrapModuleTest.php b/www/wiki/extensions/Bootstrap/tests/phpunit/ResourceLoaderBootstrapModuleTest.php new file mode 100644 index 00000000..542a9349 --- /dev/null +++ b/www/wiki/extensions/Bootstrap/tests/phpunit/ResourceLoaderBootstrapModuleTest.php @@ -0,0 +1,91 @@ +<?php + +namespace Bootstrap\Tests; + +use Bootstrap\ResourceLoaderBootstrapModule; + +use HashBagOStuff; + +/** + * @uses \Bootstrap\ResourceLoaderBootstrapModule + * + * @ingroup Test + * + * @group extension-bootstrap + * @group mediawiki-databaseless + * + * @license GNU GPL v3+ + * @since 1.0 + * + * @author mwjames + */ +class ResourceLoaderBootstrapModuleTest extends \PHPUnit_Framework_TestCase { + + public function testCanConstruct() { + + $this->assertInstanceOf( + '\Bootstrap\ResourceLoaderBootstrapModule', + new ResourceLoaderBootstrapModule() + ); + } + + public function testGetStyles() { + + $resourceLoaderContext = $this->getMockBuilder( '\ResourceLoaderContext' ) + ->disableOriginalConstructor() + ->getMock(); + + $instance = new ResourceLoaderBootstrapModule; + $instance->setCache( new HashBagOStuff ); + + $this->assertArrayHasKey( 'all', $instance->getStyles( $resourceLoaderContext ) ); + } + + public function testGetStylesFromPresetCache() { + + $resourceLoaderContext = $this->getMockBuilder( '\ResourceLoaderContext' ) + ->disableOriginalConstructor() + ->getMock(); + + $cache = new HashBagOStuff; + + $cache->set( + wfMemcKey( 'ext', 'bootstrap', $resourceLoaderContext->getHash() ), + array( + 'storetime' => time(), + 'styles' => 'foo' + ) + ); + + $instance = new ResourceLoaderBootstrapModule; + $instance->setCache( $cache ); + + $styles = $instance->getStyles( $resourceLoaderContext ); + + $this->assertArrayHasKey( 'all', $styles ); + $this->assertEquals( 'foo', $styles['all'] ); + } + + public function testGetStylesTryCatchExceptionIsThrownByLessParser() { + + $resourceLoaderContext = $this->getMockBuilder( '\ResourceLoaderContext' ) + ->disableOriginalConstructor() + ->getMock(); + + $options = array( + 'external styles' => array( 'Foo' => 'bar' ) + ); + + $instance = new ResourceLoaderBootstrapModule( $options ); + $instance->setCache( new HashBagOStuff ); + + $result = $instance->getStyles( $resourceLoaderContext ); + + $this->assertContains( 'LESS compile error', $result['all'] ); + } + + public function testSupportsURLLoading() { + $instance = new ResourceLoaderBootstrapModule(); + $this->assertFalse( $instance->supportsURLLoading() ); + } +} |