diff options
Diffstat (limited to 'www/wiki/extensions/Mermaid/tests')
8 files changed, 475 insertions, 0 deletions
diff --git a/www/wiki/extensions/Mermaid/tests/bootstrap.php b/www/wiki/extensions/Mermaid/tests/bootstrap.php new file mode 100644 index 00000000..7bbb37e3 --- /dev/null +++ b/www/wiki/extensions/Mermaid/tests/bootstrap.php @@ -0,0 +1,31 @@ +<?php + +if ( PHP_SAPI !== 'cli' && PHP_SAPI !== 'phpdbg' ) { + die( 'Not an entry point' ); +} + +error_reporting( E_ALL | E_STRICT ); +date_default_timezone_set( 'UTC' ); +ini_set( 'display_errors', 1 ); + +if ( !class_exists( 'Mermaid' ) || ( $version = Mermaid::getVersion() ) === null ) { + die( "\nMermaid is not available, please check your Composer or LocalSettings (wfLoadExtension).\n" ); +} + +print sprintf( "\n%-20s%s\n", "Mermaid: ", $version ); + +if ( is_readable( $path = __DIR__ . '/../vendor/autoload.php' ) ) { + print sprintf( "%-20s%s\n", "MediaWiki:", $GLOBALS['wgVersion'] . " (Extension vendor autoloader)" ); +} elseif ( is_readable( $path = __DIR__ . '/../../../vendor/autoload.php' ) ) { + print sprintf( "%-20s%s\n", "MediaWiki:", $GLOBALS['wgVersion'] . " (MediaWiki vendor autoloader)" ); +} else { + die( 'To run tests it is required that packages are installed using Composer.' ); +} + +$dateTimeUtc = new \DateTime( 'now', new \DateTimeZone( 'UTC' ) ); +print sprintf( "\n%-20s%s\n\n", "Execution time:", $dateTimeUtc->format( 'Y-m-d h:i' ) ); + +$autoloader = require $path; +$autoloader->addPsr4( 'Mermaid\\Tests\\', __DIR__ . '/phpunit/Unit' ); +$autoloader->addPsr4( 'Mermaid\\Tests\\Integration\\', __DIR__ . '/phpunit/Integration' ); +unset( $autoloader ); diff --git a/www/wiki/extensions/Mermaid/tests/phpunit/Integration/I18nJsonFileIntegrityTest.php b/www/wiki/extensions/Mermaid/tests/phpunit/Integration/I18nJsonFileIntegrityTest.php new file mode 100644 index 00000000..c5a1c4ae --- /dev/null +++ b/www/wiki/extensions/Mermaid/tests/phpunit/Integration/I18nJsonFileIntegrityTest.php @@ -0,0 +1,77 @@ +<?php + +namespace Mermaid\Tests\Integration; + +/** + * @group mermaid + * @group medium + * + * @license GNU GPL v2+ + * @since 1.0 + * + * @author mwjames + */ +class I18nJsonFileIntegrityTest extends \PHPUnit_Framework_TestCase { + + /** + * @dataProvider i18nFileProvider + */ + public function testI18NJsonDecodeEncode( $file ) { + + $contents = file_get_contents( $file ); + + $this->assertInternalType( + 'array', + json_decode( $contents, true ), + 'Failed with ' . $this->getDescriptiveJsonError( json_last_error() ) . ' in ' . $file + ); + } + + public function i18nFileProvider() { + + $provider = array(); + + $files = $this->findFilesForExtension( + $GLOBALS['wgMessagesDirs']['Mermaid'], + 'json' + ); + + foreach ( $files as $file ) { + $provider[] = array( $file ); + } + + return $provider; + } + + private function findFilesForExtension( $path, $extension ) { + + $files = array(); + + $directoryIterator = new \RecursiveDirectoryIterator( + str_replace( array( '\\', '/' ), DIRECTORY_SEPARATOR, $path ) + ); + + foreach ( new \RecursiveIteratorIterator( $directoryIterator ) as $fileInfo ) { + if ( strtolower( substr( $fileInfo->getFilename(), -( strlen( $extension ) + 1 ) ) ) === ( '.' . $extension ) ) { + $files[$fileInfo->getFilename()] = $fileInfo->getPathname(); + } + } + + return $files; + } + + private function getDescriptiveJsonError( $errorCode ) { + + $errorMessages = array( + JSON_ERROR_NONE => '', + JSON_ERROR_STATE_MISMATCH => 'Underflow or the modes mismatch, malformed JSON', + JSON_ERROR_CTRL_CHAR => 'Unexpected control character found, possibly incorrectly encoded', + JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON', + JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded', + JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded' + ); + + return $errorMessages[$errorCode]; + } + +} diff --git a/www/wiki/extensions/Mermaid/tests/phpunit/Unit/HookRegistryTest.php b/www/wiki/extensions/Mermaid/tests/phpunit/Unit/HookRegistryTest.php new file mode 100644 index 00000000..2c98737a --- /dev/null +++ b/www/wiki/extensions/Mermaid/tests/phpunit/Unit/HookRegistryTest.php @@ -0,0 +1,119 @@ +<?php + +namespace Mermaid\Tests; + +use Mermaid\HookRegistry; +use Title; + +/** + * @covers \Mermaid\HookRegistry + * @group mermaid + * + * @license GNU GPL v2+ + * @since 1.0 + * + * @author mwjames + */ +class HookRegistryTest extends \PHPUnit_Framework_TestCase { + + public function testCanConstruct() { + + $this->assertInstanceOf( + HookRegistry::class, + new HookRegistry() + ); + } + + public function testRegister() { + + $title = Title::newFromText( __METHOD__ ); + + $parserOutput = $this->getMockBuilder( '\ParserOutput' ) + ->disableOriginalConstructor() + ->getMock(); + + $parser = $this->getMockBuilder( '\Parser' ) + ->disableOriginalConstructor() + ->getMock(); + + $parser->expects( $this->any() ) + ->method( 'getTitle' ) + ->will( $this->returnValue( $title ) ); + + $parser->expects( $this->any() ) + ->method( 'getOutput' ) + ->will( $this->returnValue( $parserOutput ) ); + + $instance = new HookRegistry(); + $instance->deregister(); + $instance->register(); + + $this->doTestParserFirstCallInit( $instance, $parser ); + $this->doTestResourceLoaderGetConfigVars( $instance ); + $this->doTestOutputPageParserOutput( $instance ); + } + + public function doTestParserFirstCallInit( $instance, $parser ) { + + $handler = 'ParserFirstCallInit'; + + $this->assertTrue( + $instance->isRegistered( $handler ) + ); + + $this->assertThatHookIsExcutable( + $instance->getHandlerFor( $handler ), + array( &$parser ) + ); + } + + public function doTestResourceLoaderGetConfigVars( $instance ) { + + $handler = 'ResourceLoaderGetConfigVars'; + + $this->assertTrue( + $instance->isRegistered( $handler ) + ); + + $vars = array(); + + $this->assertThatHookIsExcutable( + $instance->getHandlerFor( $handler ), + array( &$vars ) + ); + } + + public function doTestOutputPageParserOutput( $instance ) { + + $parserOutput = $this->getMockBuilder( '\ParserOutput' ) + ->disableOriginalConstructor() + ->getMock(); + + $parserOutput->expects( $this->any() ) + ->method( 'getExtensionData' ) + ->will( $this->returnValue( true ) ); + + $outputPage = $this->getMockBuilder( '\OutputPage' ) + ->disableOriginalConstructor() + ->getMock(); + + $handler = 'OutputPageParserOutput'; + + $this->assertTrue( + $instance->isRegistered( $handler ) + ); + + $this->assertThatHookIsExcutable( + $instance->getHandlerFor( $handler ), + array( $outputPage, $parserOutput ) + ); + } + + private function assertThatHookIsExcutable( \Closure $handler, $arguments ) { + $this->assertInternalType( + 'boolean', + call_user_func_array( $handler, $arguments ) + ); + } + +} diff --git a/www/wiki/extensions/Mermaid/tests/phpunit/Unit/MermaidParserFunctionTest.php b/www/wiki/extensions/Mermaid/tests/phpunit/Unit/MermaidParserFunctionTest.php new file mode 100644 index 00000000..2a819c17 --- /dev/null +++ b/www/wiki/extensions/Mermaid/tests/phpunit/Unit/MermaidParserFunctionTest.php @@ -0,0 +1,109 @@ +<?php + +namespace Mermaid\Tests; + +use Mermaid\MermaidParserFunction; + +/** + * @covers \Mermaid\MermaidParserFunction + * @group mermaid + * + * @license GNU GPL v2+ + * @since 1.0 + * + * @author mwjames + */ +class MermaidParserFunctionTest extends \PHPUnit_Framework_TestCase { + + public function testCanConstruct() { + + $parser = $this->getMockBuilder( '\Parser' ) + ->disableOriginalConstructor() + ->getMock(); + + $this->assertInstanceOf( + MermaidParserFunction::class, + new MermaidParserFunction( $parser ) + ); + } + + public function testInitCallback() { + + $callback = MermaidParserFunction::newCallback( 'foo' ); + + $this->assertInstanceOf( + '\Closure', + $callback + ); + + $parserOutput = $this->getMockBuilder( '\ParserOutput' ) + ->disableOriginalConstructor() + ->getMock(); + + $parser = $this->getMockBuilder( '\Parser' ) + ->disableOriginalConstructor() + ->getMock(); + + $parser->expects( $this->any() ) + ->method( 'getOutput' ) + ->will( $this->returnValue( $parserOutput ) ); + + $this->assertNotEmpty( + call_user_func_array( $callback, [ $parser ] ) + ); + } + + /** + * @dataProvider textProvider + */ + public function testParse( $text, $expected ) { + + $parserOutput = $this->getMockBuilder( '\ParserOutput' ) + ->disableOriginalConstructor() + ->getMock(); + + $parserOutput->expects( $this->once() ) + ->method( 'setExtensionData' ) + ->with( + $this->equalTo( 'ext-mermaid' ), + $this->equalTo( true ) ); + + $parser = $this->getMockBuilder( '\Parser' ) + ->disableOriginalConstructor() + ->getMock(); + + $parser->expects( $this->any() ) + ->method( 'getOutput' ) + ->will( $this->returnValue( $parserOutput ) ); + + $instance = new MermaidParserFunction( + $parser + ); + + $this->assertContains( + $expected, + $instance->parse( $text ) + ); + } + + public function textProvider() { + + yield [ + [ 'sequenceDiagram...', 'config.theme=foo' ], + 'class="ext-mermaid" data-mermaid="{"content":"sequenceDiagram...","config":{"theme":"foo"}}"><div class="mermaid-dots"></div></div>' + ]; + + // [ ... ] + yield [ + [ 'sequenceDiagram id1["This is the (text) in the box"]', 'config.theme=foo' ], + 'data-mermaid="{"content":"sequenceDiagram id1[\"This is the (text) in the box\"]' + ]; + + // | + yield [ + [ 'A[Hard edge] -->|Link text| B(Round edge)' ], + 'data-mermaid="{"content":"A[Hard edge] -->|Link text| B(Round edge)"' + ]; + } + +} diff --git a/www/wiki/extensions/Mermaid/tests/travis/install-mediawiki.sh b/www/wiki/extensions/Mermaid/tests/travis/install-mediawiki.sh new file mode 100644 index 00000000..0fe25fb1 --- /dev/null +++ b/www/wiki/extensions/Mermaid/tests/travis/install-mediawiki.sh @@ -0,0 +1,43 @@ +#!/bin/bash +set -ex + +cd .. + +## Use sha (master@5cc1f1d) to download a particular commit to avoid breakages +## introduced by MediaWiki core +if [[ "$MW" == *@* ]] +then + arrMw=(${MW//@/ }) + MW=${arrMw[0]} + SOURCE=${arrMw[1]} +else + MW=$MW + SOURCE=$MW +fi + +wget https://github.com/wikimedia/mediawiki/archive/$SOURCE.tar.gz -O $MW.tar.gz + +tar -zxf $MW.tar.gz +mv mediawiki-* mw + +cd mw + +## MW 1.25+ requires Psr\Logger +if [ -f composer.json ] +then + composer self-update + composer install --prefer-source +fi + +if [ "$DB" == "postgres" ] +then + # See #458 + sudo /etc/init.d/postgresql stop + sudo /etc/init.d/postgresql start + + psql -c 'create database its_a_mw;' -U postgres + php maintenance/install.php --dbtype $DB --dbuser postgres --dbname its_a_mw --pass nyan TravisWiki admin --scriptpath /TravisWiki +else + mysql -e 'create database its_a_mw;' + php maintenance/install.php --dbtype $DB --dbuser root --dbname its_a_mw --dbpath $(pwd) --pass nyan TravisWiki admin --scriptpath /TravisWiki +fi diff --git a/www/wiki/extensions/Mermaid/tests/travis/install-mermaid.sh b/www/wiki/extensions/Mermaid/tests/travis/install-mermaid.sh new file mode 100644 index 00000000..d87aa25a --- /dev/null +++ b/www/wiki/extensions/Mermaid/tests/travis/install-mermaid.sh @@ -0,0 +1,72 @@ +#!/bin/bash +set -ex + +BASE_PATH=$(pwd) +MW_INSTALL_PATH=$BASE_PATH/../mw + +# Run Composer installation from the MW root directory +function installToMediaWikiRoot { + echo -e "Running MW root composer install build on $TRAVIS_BRANCH \n" + + cd $MW_INSTALL_PATH + + if [ "$PHPUNIT" != "" ] + then + composer require 'phpunit/phpunit='$PHPUNIT --update-with-dependencies + else + composer require 'phpunit/phpunit=4.8.*' --update-with-dependencies + fi + + if [ "$MERMAID" != "" ] + then + composer require 'mediawiki/mermaid='$MERMAID --update-with-dependencies + else + composer init --stability dev + composer require "mediawiki/mermaid:dev-master" --dev --update-with-dependencies + + cd extensions + cd Mermaid + + # Pull request number, "false" if it's not a pull request + # After the install via composer an additional get fetch is carried out to + # update th repository to make sure that the latests code changes are + # deployed for testing + if [ "$TRAVIS_PULL_REQUEST" != "false" ] + then + git fetch origin +refs/pull/"$TRAVIS_PULL_REQUEST"/merge: + git checkout -qf FETCH_HEAD + else + git fetch origin "$TRAVIS_BRANCH" + git checkout -qf FETCH_HEAD + fi + + cd ../.. + fi + + # Rebuild the class map for added classes during git fetch + composer dump-autoload +} + +function updateConfiguration { + + cd $MW_INSTALL_PATH + + # Site language + if [ "$SITELANG" != "" ] + then + echo '$wgLanguageCode = "'$SITELANG'";' >> LocalSettings.php + fi + + echo 'error_reporting(E_ALL| E_STRICT);' >> LocalSettings.php + echo 'ini_set("display_errors", 1);' >> LocalSettings.php + echo '$wgShowExceptionDetails = true;' >> LocalSettings.php + echo '$wgDevelopmentWarnings = true;' >> LocalSettings.php + echo "putenv( 'MW_INSTALL_PATH=$(pwd)' );" >> LocalSettings.php + + echo "wfLoadExtension( 'Mermaid' );" >> LocalSettings.php + + php maintenance/update.php --quick +} + +installToMediaWikiRoot +updateConfiguration diff --git a/www/wiki/extensions/Mermaid/tests/travis/run-tests.sh b/www/wiki/extensions/Mermaid/tests/travis/run-tests.sh new file mode 100644 index 00000000..7f37b652 --- /dev/null +++ b/www/wiki/extensions/Mermaid/tests/travis/run-tests.sh @@ -0,0 +1,14 @@ +#! /bin/bash +set -ex + +BASE_PATH=$(pwd) +MW_INSTALL_PATH=$BASE_PATH/../mw + +cd $MW_INSTALL_PATH/extensions/Mermaid + +if [ "$TYPE" == "coverage" ] +then + composer phpunit -- --coverage-clover $BASE_PATH/build/coverage.clover +else + composer phpunit +fi diff --git a/www/wiki/extensions/Mermaid/tests/travis/upload-coverage-report.sh b/www/wiki/extensions/Mermaid/tests/travis/upload-coverage-report.sh new file mode 100644 index 00000000..aff95cf7 --- /dev/null +++ b/www/wiki/extensions/Mermaid/tests/travis/upload-coverage-report.sh @@ -0,0 +1,10 @@ +#! /bin/bash +set -ex + +BASE_PATH=$(pwd) + +if [ "$TYPE" == "coverage" ] +then + wget https://scrutinizer-ci.com/ocular.phar + php ocular.phar code-coverage:upload --format=php-clover $BASE_PATH/build/coverage.clover +fi
\ No newline at end of file |