diff options
author | Yaco <franco@reevo.org> | 2020-06-04 11:01:00 -0300 |
---|---|---|
committer | Yaco <franco@reevo.org> | 2020-06-04 11:01:00 -0300 |
commit | fc7369835258467bf97eb64f184b93691f9a9fd5 (patch) | |
tree | daabd60089d2dd76d9f5fb416b005fbe159c799d /www/wiki/extensions/LocalisationUpdate/tests |
first commit
Diffstat (limited to 'www/wiki/extensions/LocalisationUpdate/tests')
6 files changed, 275 insertions, 0 deletions
diff --git a/www/wiki/extensions/LocalisationUpdate/tests/phan/config.php b/www/wiki/extensions/LocalisationUpdate/tests/phan/config.php new file mode 100644 index 00000000..f2660b8a --- /dev/null +++ b/www/wiki/extensions/LocalisationUpdate/tests/phan/config.php @@ -0,0 +1,3 @@ +<?php + +return require __DIR__ . '/../../vendor/mediawiki/mediawiki-phan-config/src/config.php'; diff --git a/www/wiki/extensions/LocalisationUpdate/tests/phpunit/Makefile b/www/wiki/extensions/LocalisationUpdate/tests/phpunit/Makefile new file mode 100644 index 00000000..e98c12ca --- /dev/null +++ b/www/wiki/extensions/LocalisationUpdate/tests/phpunit/Makefile @@ -0,0 +1,12 @@ +ifndef MW_INSTALL_PATH + MW_INSTALL_PATH=../../../.. +endif + +DIRS=reader + +default: + php ${MW_INSTALL_PATH}/tests/phpunit/phpunit.php . + +.PHONY: *Test.php $(DIRS) +*Test.php $(DIRS): + php ${MW_INSTALL_PATH}/tests/phpunit/phpunit.php $@ diff --git a/www/wiki/extensions/LocalisationUpdate/tests/phpunit/UpdaterTest.php b/www/wiki/extensions/LocalisationUpdate/tests/phpunit/UpdaterTest.php new file mode 100644 index 00000000..00c45e12 --- /dev/null +++ b/www/wiki/extensions/LocalisationUpdate/tests/phpunit/UpdaterTest.php @@ -0,0 +1,89 @@ +<?php +/** + * @file + * @author Niklas Laxström + * @license GPL-2.0-or-later + */ + +namespace LocalisationUpdate; + +use PHPUnit4And6Compat; + +/** + * @covers \LocalisationUpdate\Updater + */ +class UpdaterTest extends \PHPUnit\Framework\TestCase { + use PHPUnit4And6Compat; + + public function testIsDirectory() { + $updater = new Updater(); + + $this->assertTrue( + $updater->isDirectory( '/IP/extensions/Translate/i18n/*.json' ), + 'Extension json files are a file pattern' + ); + + $this->assertFalse( + $updater->isDirectory( '/IP/extensions/Translate/Translate.i18n.php' ), + 'Extension php file is not a pattern' + ); + } + + public function testExpandRemotePath() { + $updater = new Updater(); + $repos = [ 'main' => 'file:///repos/%NAME%/%SOME-VAR%' ]; + + $info = [ + 'repo' => 'main', + 'name' => 'product', + 'some-var' => 'file', + ]; + $this->assertEquals( + 'file:///repos/product/file', + $updater->expandRemotePath( $info, $repos ), + 'Variables are expanded correctly' + ); + } + + public function testReadMessages() { + $updater = $updater = new Updater(); + + $input = [ 'file' => 'Hello World!' ]; + $output = [ 'en' => [ 'key' => $input['file'] ] ]; + + $reader = $this->getMock( 'LocalisationUpdate\Reader' ); + $reader + ->expects( $this->once() ) + ->method( 'parse' ) + ->will( $this->returnValue( $output ) ); + + $factory = $this->getMock( 'LocalisationUpdate\ReaderFactory' ); + $factory + ->expects( $this->once() ) + ->method( 'getReader' ) + ->will( $this->returnValue( $reader ) ); + + $observed = $updater->readMessages( $factory, $input ); + $this->assertEquals( $output, $observed, 'Tries to parse given file' ); + } + + public function testFindChangedTranslations() { + $updater = $updater = new Updater(); + + $origin = [ + 'A' => '1', + 'C' => '3', + 'D' => '4', + ]; + $remote = [ + 'A' => '1', // No change key + 'B' => '2', // New key + 'C' => '33', // Changed key + 'D' => '44', // Blacklisted key + ]; + $blacklist = [ 'D' => 0 ]; + $expected = [ 'B' => '2', 'C' => '33' ]; + $observed = $updater->findChangedTranslations( $origin, $remote, $blacklist ); + $this->assertEquals( $expected, $observed, 'Changed and new keys returned' ); + } +} diff --git a/www/wiki/extensions/LocalisationUpdate/tests/phpunit/finder/FinderTest.php b/www/wiki/extensions/LocalisationUpdate/tests/phpunit/finder/FinderTest.php new file mode 100644 index 00000000..1b4db0f4 --- /dev/null +++ b/www/wiki/extensions/LocalisationUpdate/tests/phpunit/finder/FinderTest.php @@ -0,0 +1,86 @@ +<?php +/** + * @file + * @author Niklas Laxström + * @license GPL-2.0-or-later + */ + +namespace LocalisationUpdate; + +/** + * @covers \LocalisationUpdate\Finder + */ +class FinderTest extends \PHPUnit\Framework\TestCase { + public function testGetComponents() { + $finder = new Finder( + [ + 'TranslateSearch' => '/IP/extensions/Translate/TranslateSearch.i18n.php', + 'Babel' => '/IP/extensions/Babel/Babel.i18n.php', + ], + [ + 'Babel' => '/IP/extensions/Babel/i18n', + 'Door' => [ + 'core' => '/IP/extensions/Door/i18n/core', + 'extra' => '/IP/extensions/Door/i18n/extra', + ], + 'Vector' => '/IP/skins/Vector/i18n', + ], + '/IP' + ); + $observed = $finder->getComponents(); + + $expected = [ + 'repo' => 'mediawiki', + 'orig' => "file:///IP/languages/messages/Messages*.php", + 'path' => 'languages/messages/i18n/*.json', + ]; + + $this->assertArrayHasKey( 'core', $observed ); + $this->assertEquals( $expected, $observed['core'], 'Core php file' ); + + $expected = [ + 'repo' => 'extension', + 'name' => 'Translate', + 'orig' => 'file:///IP/extensions/Translate/TranslateSearch.i18n.php', + 'path' => 'TranslateSearch.i18n.php' + ]; + $this->assertArrayHasKey( 'TranslateSearch', $observed ); + $this->assertEquals( $expected, $observed['TranslateSearch'], 'PHP only extension' ); + + $expected = [ + 'repo' => 'extension', + 'name' => 'Babel', + 'orig' => 'file:///IP/extensions/Babel/i18n/*.json', + 'path' => 'i18n/*.json' + ]; + $this->assertArrayHasKey( 'Babel-0', $observed ); + $this->assertEquals( $expected, $observed['Babel-0'], 'PHP&JSON extension' ); + + $expected = [ + 'repo' => 'extension', + 'name' => 'Door', + 'orig' => 'file:///IP/extensions/Door/i18n/core/*.json', + 'path' => 'i18n/core/*.json' + ]; + $this->assertArrayHasKey( 'Door-core', $observed ); + $this->assertEquals( $expected, $observed['Door-core'], 'Multidir json extension' ); + + $expected = [ + 'repo' => 'extension', + 'name' => 'Door', + 'orig' => 'file:///IP/extensions/Door/i18n/extra/*.json', + 'path' => 'i18n/extra/*.json' + ]; + $this->assertArrayHasKey( 'Door-extra', $observed ); + $this->assertEquals( $expected, $observed['Door-extra'], 'Multidir json extension' ); + + $expected = [ + 'repo' => 'skin', + 'name' => 'Vector', + 'orig' => 'file:///IP/skins/Vector/i18n/*.json', + 'path' => 'i18n/*.json' + ]; + $this->assertArrayHasKey( 'Vector-0', $observed ); + $this->assertEquals( $expected, $observed['Vector-0'], 'Json skin' ); + } +} diff --git a/www/wiki/extensions/LocalisationUpdate/tests/phpunit/reader/JSONReaderTest.php b/www/wiki/extensions/LocalisationUpdate/tests/phpunit/reader/JSONReaderTest.php new file mode 100644 index 00000000..11f0a03b --- /dev/null +++ b/www/wiki/extensions/LocalisationUpdate/tests/phpunit/reader/JSONReaderTest.php @@ -0,0 +1,42 @@ +<?php +/** + * @file + * @author Niklas Laxström + * @license GPL-2.0-or-later + */ + +namespace LocalisationUpdate; + +/** + * @covers \LocalisationUpdate\JSONReader + */ +class JSONReaderTest extends \PHPUnit\Framework\TestCase { + /** + * @dataProvider parseProvider + */ + public function testParse( $input, $expected, $comment ) { + $reader = new JSONReader( 'xx' ); + $observed = $reader->parse( $input ); + $this->assertEquals( $expected, $observed['xx'], $comment ); + } + + public function parseProvider() { + return [ + [ + '{}', + [], + 'empty file', + ], + [ + '{"key":"value"}', + [ 'key' => 'value' ], + 'file with one string', + ], + [ + '{"@metadata":{"authors":["Nike"]},"key":"value2"}', + [ 'key' => 'value2' ], + '@metadata is ignored', + ] + ]; + } +} diff --git a/www/wiki/extensions/LocalisationUpdate/tests/phpunit/reader/ReaderFactoryTest.php b/www/wiki/extensions/LocalisationUpdate/tests/phpunit/reader/ReaderFactoryTest.php new file mode 100644 index 00000000..86776395 --- /dev/null +++ b/www/wiki/extensions/LocalisationUpdate/tests/phpunit/reader/ReaderFactoryTest.php @@ -0,0 +1,43 @@ +<?php +/** + * @file + * @author Niklas Laxström + * @license GPL-2.0-or-later + */ + +namespace LocalisationUpdate; + +/** + * @covers \LocalisationUpdate\ReaderFactory + */ +class ReaderFactoryTest extends \PHPUnit\Framework\TestCase { + /** + * @dataProvider getReaderProvider + */ + public function testGetReader( $input, $expected, $comment ) { + $factory = new ReaderFactory(); + $reader = $factory->getReader( $input ); + $observed = get_class( $reader ); + $this->assertEquals( $expected, $observed, $comment ); + } + + public function getReaderProvider() { + return [ + [ + 'languages/messages/MessagesFi.php', + 'LocalisationUpdate\PHPReader', + 'core php file', + ], + [ + 'extensions/Translate/Translate.i18n.php', + 'LocalisationUpdate\PHPReader', + 'extension php file', + ], + [ + 'extension/Translate/i18n/core/de.json', + 'LocalisationUpdate\JSONReader', + 'extension json file', + ], + ]; + } +} |