jenkins-bot has submitted this change and it was merged.
Change subject: Add basic tests for all public methods in all classes
......................................................................
Add basic tests for all public methods in all classes
This is supposed to be a basic set of very basic tests. They should
cover most of the code. Feel free to expand in later patches. I will
also try to add more test cases to cover more edge cases.
Change-Id: I860c78450a2f31e07ce4058665a1664a952f62a3
---
M Babel.class.php
A tests/phpunit/BabelAutoCreateTest.php
A tests/phpunit/BabelLanguageCodesTest.php
A tests/phpunit/BabelStaticTest.php
A tests/phpunit/BabelTest.php
5 files changed, 224 insertions(+), 1 deletion(-)
Approvals:
Hoo man: Looks good to me, approved
jenkins-bot: Verified
diff --git a/Babel.class.php b/Babel.class.php
index cf8e897..0e4e117 100644
--- a/Babel.class.php
+++ b/Babel.class.php
@@ -40,7 +40,7 @@
$content = '';
$templateParameters = array(); // collects name=value
parameters to be passed to wiki templates.
- $createCategories = !$parser->mOptions->getIsPreview();
+ $createCategories = !$parser->getOptions()->getIsPreview();
foreach ( $parameters as $name ) {
if ( strpos( $name, '=' ) !== false ) {
$templateParameters[] = $name;
diff --git a/tests/phpunit/BabelAutoCreateTest.php
b/tests/phpunit/BabelAutoCreateTest.php
new file mode 100644
index 0000000..9eb6ac6
--- /dev/null
+++ b/tests/phpunit/BabelAutoCreateTest.php
@@ -0,0 +1,66 @@
+<?php
+
+namespace Babel\Tests;
+
+use BabelAutoCreate;
+use Language;
+use MediaWikiTestCase;
+use Title;
+use WikiPage;
+
+/**
+ * @covers BabelAutoCreate
+ *
+ * @group Babel
+ * @group Database
+ *
+ * @licence GNU GPL v2+
+ * @author Thiemo Mättig
+ */
+class BabelAutoCreateTest extends MediaWikiTestCase {
+
+ protected function setUp() {
+ parent::setUp();
+
+ $this->setMwGlobals( array(
+ 'wgContLang' => Language::factory( 'qqx' ),
+ ) );
+ }
+
+ public function testOnUserGetReservedNames() {
+ $names = array();
+ $this->assertSame( array(), $names, 'Precondition' );
+
+ $this->assertTrue( BabelAutoCreate::onUserGetReservedNames(
$names ) );
+ $this->assertSame( array( 'msg:babel-autocreate-user' ), $names
);
+ }
+
+ /**
+ * @dataProvider createProvider
+ */
+ public function testCreate( $category, $code, $level, $expected ) {
+ BabelAutoCreate::create( $category, $code, $level );
+ $page = WikiPage::factory( Title::newFromText( 'Category:' .
$category ) );
+ $this->assertTrue( $page->exists() );
+ $this->assertSame( $expected,
$page->getContent()->getNativeData() );
+ }
+
+ public function createProvider() {
+ return array(
+ array(
+ 'category-1', 'en', null,
+ '(babel-autocreate-text-main: English, en)'
+ ),
+ array(
+ 'category-2', 'en', 'level-2',
+ '(babel-autocreate-text-levels: level-2,
English, en)'
+ ),
+ );
+ }
+
+ public function testUser() {
+ $user = BabelAutoCreate::user();
+ $this->assertInstanceOf( 'User', $user );
+ }
+
+}
diff --git a/tests/phpunit/BabelLanguageCodesTest.php
b/tests/phpunit/BabelLanguageCodesTest.php
new file mode 100644
index 0000000..fe4da08
--- /dev/null
+++ b/tests/phpunit/BabelLanguageCodesTest.php
@@ -0,0 +1,47 @@
+<?php
+
+namespace Babel\Tests;
+
+use BabelLanguageCodes;
+use PHPUnit_Framework_TestCase;
+
+/**
+ * @covers BabelLanguageCodes
+ *
+ * @group Babel
+ *
+ * @licence GNU GPL v2+
+ * @author Thiemo Mättig
+ */
+class BabelLanguageCodesTest extends PHPUnit_Framework_TestCase {
+
+ /**
+ * @dataProvider getCodeProvider
+ */
+ public function testGetCode( $code, $expected ) {
+ $this->assertSame( $expected, BabelLanguageCodes::getCode(
$code ) );
+ }
+
+ public function getCodeProvider() {
+ return array(
+ array( 'invalidLanguageCode', false ),
+ array( 'en', 'en' ),
+ );
+ }
+
+ /**
+ * @dataProvider getNameProvider
+ */
+ public function testGetName( $code, $language, $expected ) {
+ $this->assertSame( $expected, BabelLanguageCodes::getName(
$code, $language ) );
+ }
+
+ public function getNameProvider() {
+ return array(
+ array( 'invalidLanguageCode', null, false ),
+ array( 'en', null, 'English' ),
+ array( 'en', 'en', 'English' ),
+ );
+ }
+
+}
diff --git a/tests/phpunit/BabelStaticTest.php
b/tests/phpunit/BabelStaticTest.php
new file mode 100644
index 0000000..de0f983
--- /dev/null
+++ b/tests/phpunit/BabelStaticTest.php
@@ -0,0 +1,30 @@
+<?php
+
+namespace Babel\Tests;
+
+use BabelStatic;
+use PHPUnit_Framework_TestCase;
+
+/**
+ * @covers BabelStatic
+ *
+ * @group Babel
+ *
+ * @licence GNU GPL v2+
+ * @author Thiemo Mättig
+ */
+class BabelStaticTest extends PHPUnit_Framework_TestCase {
+
+ public function testOnParserFirstCallInit() {
+ $parser = $this->getMockBuilder( 'Parser' )
+ ->disableOriginalConstructor()
+ ->getMock();
+ $parser->expects( $this->once() )
+ ->method( 'setFunctionHook' )
+ ->with( 'babel', array( 'Babel', 'Render' ) )
+ ->will( $this->returnValue( true ) );
+
+ BabelStatic::onParserFirstCallInit( $parser );
+ }
+
+}
diff --git a/tests/phpunit/BabelTest.php b/tests/phpunit/BabelTest.php
new file mode 100644
index 0000000..0c7bf6d
--- /dev/null
+++ b/tests/phpunit/BabelTest.php
@@ -0,0 +1,80 @@
+<?php
+
+namespace Babel\Tests;
+
+use Babel;
+use Language;
+use MediaWikiTestCase;
+use ParserOptions;
+use ParserOutput;
+use Title;
+use User;
+
+/**
+ * @covers Babel
+ *
+ * @group Babel
+ * @group Database
+ *
+ * @licence GNU GPL v2+
+ * @author Thiemo Mättig
+ */
+class BabelTest extends MediaWikiTestCase {
+
+ protected function setUp() {
+ parent::setUp();
+
+ $this->setMwGlobals( array(
+ 'wgContLang' => Language::factory( 'qqx' ),
+ ) );
+ $this->insertPage( 'User:User-1', '[[Category:en]]' );
+ }
+
+ public function testRender() {
+ $options = new ParserOptions();
+ $options->setIsPreview( true );
+
+ $parser = $this->getMockBuilder( 'Parser' )
+ ->disableOriginalConstructor()
+ ->getMock();
+ $parser->expects( $this->any() )
+ ->method( 'getOptions' )
+ ->will( $this->returnValue( $options ) );
+ $parser->expects( $this->any() )
+ ->method( 'getTitle' )
+ ->will( $this->returnValue( Title::newFromText(
'User:User-1' ) ) );
+ $parser->expects( $this->any() )
+ ->method( 'getOutput' )
+ ->will( $this->returnValue( new ParserOutput() ) );
+
+ $wikiText = Babel::Render( $parser, 'en' );
+ $this->assertSame(
+ '{|style=" padding: (babel-box-cellpadding);
border-spacing: (babel-box-cellspacing);" class="mw-babel-wrapper"'
+ . "\n"
+ . '! class="mw-babel-header" | [[(babel-url)|(babel:
User-1)]]'
+ . "\n|-\n"
+ . '| <div class="mw-babel-box mw-babel-box-N"
dir="ltr">'
+ . "\n"
+ . '{|style=" padding: (babel-cellpadding);
border-spacing: (babel-cellspacing);"'
+ . "\n"
+ . '! dir="ltr" | [[(babel-portal: en)|en]]<span
class="mw-babel-box-level-N">-N</span>'
+ . "\n"
+ . '| dir="ltr" lang="en" | This user has a
[[:Category:en-N|native]] understanding of [[:Category:en|English]].'
+ . "\n|}\n"
+ . '</div>[[Category:en|N]][[Category:en-N]]'
+ . "\n|-\n"
+ . '! class="mw-babel-footer" |
[[(babel-footer-url)|(babel-footer: User-1)]]'
+ . "\n|}",
+ $wikiText
+ );
+ }
+
+ public function testGetUserLanguages() {
+ $user = User::newFromName( 'User-1' );
+ $languages = Babel::getUserLanguages( $user );
+ $this->assertSame( array(
+ 'en',
+ ), $languages );
+ }
+
+}
--
To view, visit https://gerrit.wikimedia.org/r/246704
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I860c78450a2f31e07ce4058665a1664a952f62a3
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/Babel
Gerrit-Branch: master
Gerrit-Owner: Thiemo Mättig (WMDE) <[email protected]>
Gerrit-Reviewer: Addshore <[email protected]>
Gerrit-Reviewer: Hoo man <[email protected]>
Gerrit-Reviewer: Siebrand <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits