summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Gadgets/tests
diff options
context:
space:
mode:
authorYaco <franco@reevo.org>2020-06-04 11:01:00 -0300
committerYaco <franco@reevo.org>2020-06-04 11:01:00 -0300
commitfc7369835258467bf97eb64f184b93691f9a9fd5 (patch)
treedaabd60089d2dd76d9f5fb416b005fbe159c799d /www/wiki/extensions/Gadgets/tests
first commit
Diffstat (limited to 'www/wiki/extensions/Gadgets/tests')
-rw-r--r--www/wiki/extensions/Gadgets/tests/GadgetTest.php80
-rw-r--r--www/wiki/extensions/Gadgets/tests/phan/config.php7
-rw-r--r--www/wiki/extensions/Gadgets/tests/phpunit/GadgetTest.php206
3 files changed, 293 insertions, 0 deletions
diff --git a/www/wiki/extensions/Gadgets/tests/GadgetTest.php b/www/wiki/extensions/Gadgets/tests/GadgetTest.php
new file mode 100644
index 00000000..86ac0fd3
--- /dev/null
+++ b/www/wiki/extensions/Gadgets/tests/GadgetTest.php
@@ -0,0 +1,80 @@
+<?php
+/**
+ * @group Gadgets
+ */
+
+class GadgetsTest extends MediaWikiTestCase {
+ /**
+ * @param string $line
+ * @return Gadget
+ */
+ private function create( $line ) {
+ $repo = new MediaWikiGadgetsDefinitionRepo();
+ $g = $repo->newFromDefinition( $line, 'misc' );
+ $this->assertInstanceOf( 'Gadget', $g );
+
+ return $g;
+ }
+
+ public function testInvalidLines() {
+ $repo = new MediaWikiGadgetsDefinitionRepo();
+ $this->assertFalse( $repo->newFromDefinition( '', 'misc' ) );
+ $this->assertFalse( $repo->newFromDefinition( '<foo|bar>', 'misc' ) );
+ }
+
+ public function testSimpleCases() {
+ $g = $this->create( '* foo bar| foo.css|foo.js|foo.bar' );
+ $this->assertEquals( 'foo_bar', $g->getName() );
+ $this->assertEquals( 'ext.gadget.foo_bar', Gadget::getModuleName( $g->getName() ) );
+ $this->assertEquals( array( 'MediaWiki:Gadget-foo.js' ), $g->getScripts() );
+ $this->assertEquals( array( 'MediaWiki:Gadget-foo.css' ), $g->getStyles() );
+ $this->assertEquals( array( 'MediaWiki:Gadget-foo.js', 'MediaWiki:Gadget-foo.css' ),
+ $g->getScriptsAndStyles() );
+ $this->assertEquals( array( 'MediaWiki:Gadget-foo.js' ), $g->getLegacyScripts() );
+ $this->assertFalse( $g->supportsResourceLoader() );
+ $this->assertTrue( $g->hasModule() );
+ }
+
+ public function testRLtag() {
+ $g = $this->create( '*foo [ResourceLoader]|foo.js|foo.css' );
+ $this->assertEquals( 'foo', $g->getName() );
+ $this->assertTrue( $g->supportsResourceLoader() );
+ $this->assertEquals( 0, count( $g->getLegacyScripts() ) );
+ }
+
+ public function testDependencies() {
+ $g = $this->create( '* foo[ResourceLoader|dependencies=jquery.ui]|bar.js' );
+ $this->assertEquals( array( 'MediaWiki:Gadget-bar.js' ), $g->getScripts() );
+ $this->assertTrue( $g->supportsResourceLoader() );
+ $this->assertEquals( array( 'jquery.ui' ), $g->getDependencies() );
+ }
+
+ public function testPreferences() {
+ $prefs = array();
+ $repo = TestingAccessWrapper::newFromObject( new MediaWikiGadgetsDefinitionRepo() );
+ // Force usage of a MediaWikiGadgetsDefinitionRepo
+ GadgetRepo::setSingleton( $repo );
+
+ $gadgets = $repo->fetchStructuredList( '* foo | foo.js
+==keep-section1==
+* bar| bar.js
+==remove-section==
+* baz [rights=embezzle] |baz.js
+==keep-section2==
+* quux [rights=read] | quux.js' );
+ $this->assertGreaterThanOrEqual( 2, count( $gadgets ), "Gadget list parsed" );
+
+ $repo->definitionCache = $gadgets;
+ $this->assertTrue( GadgetHooks::getPreferences( new User, $prefs ), 'GetPrefences hook should return true' );
+
+ $options = $prefs['gadgets']['options'];
+ $this->assertFalse( isset( $options['&lt;gadget-section-remove-section&gt;'] ), 'Must not show empty sections' );
+ $this->assertTrue( isset( $options['&lt;gadget-section-keep-section1&gt;'] ) );
+ $this->assertTrue( isset( $options['&lt;gadget-section-keep-section2&gt;'] ) );
+ }
+
+ public function tearDown() {
+ GadgetRepo::setSingleton();
+ parent::tearDown();
+ }
+}
diff --git a/www/wiki/extensions/Gadgets/tests/phan/config.php b/www/wiki/extensions/Gadgets/tests/phan/config.php
new file mode 100644
index 00000000..6bc24d22
--- /dev/null
+++ b/www/wiki/extensions/Gadgets/tests/phan/config.php
@@ -0,0 +1,7 @@
+<?php
+
+$cfg = require __DIR__ . '/../../vendor/mediawiki/mediawiki-phan-config/src/config.php';
+// Namespace constants
+$cfg['file_list'][] = 'Gadgets.namespaces.php';
+
+return $cfg;
diff --git a/www/wiki/extensions/Gadgets/tests/phpunit/GadgetTest.php b/www/wiki/extensions/Gadgets/tests/phpunit/GadgetTest.php
new file mode 100644
index 00000000..39a742ec
--- /dev/null
+++ b/www/wiki/extensions/Gadgets/tests/phpunit/GadgetTest.php
@@ -0,0 +1,206 @@
+<?php
+
+use Wikimedia\TestingAccessWrapper;
+
+/**
+ * @group Gadgets
+ */
+class GadgetTest extends MediaWikiTestCase {
+
+ public function tearDown() {
+ GadgetRepo::setSingleton();
+ parent::tearDown();
+ }
+
+ /**
+ * @param string $line
+ * @return Gadget
+ */
+ private function create( $line ) {
+ $repo = new MediaWikiGadgetsDefinitionRepo();
+ $g = $repo->newFromDefinition( $line, 'misc' );
+ $this->assertInstanceOf( Gadget::class, $g );
+ return $g;
+ }
+
+ private function getModule( Gadget $g ) {
+ $module = TestingAccessWrapper::newFromObject(
+ new GadgetResourceLoaderModule( [ 'id' => null ] )
+ );
+ $module->gadget = $g;
+ return $module;
+ }
+
+ /**
+ * @covers MediaWikiGadgetsDefinitionRepo::newFromDefinition
+ */
+ public function testInvalidLines() {
+ $repo = new MediaWikiGadgetsDefinitionRepo();
+ $this->assertFalse( $repo->newFromDefinition( '', 'misc' ) );
+ $this->assertFalse( $repo->newFromDefinition( '<foo|bar>', 'misc' ) );
+ }
+
+ /**
+ * @covers MediaWikiGadgetsDefinitionRepo::newFromDefinition
+ * @covers Gadget::__construct
+ * @covers Gadget::getName
+ * @covers Gadget::getModuleName
+ */
+ public function testSimpleCases() {
+ $g = $this->create( '* foo bar| foo.css|foo.js|foo.bar' );
+ $this->assertEquals( 'foo_bar', $g->getName() );
+ $this->assertEquals( 'ext.gadget.foo_bar', Gadget::getModuleName( $g->getName() ) );
+ $this->assertEquals( [ 'MediaWiki:Gadget-foo.js' ], $g->getScripts() );
+ $this->assertEquals( [ 'MediaWiki:Gadget-foo.css' ], $g->getStyles() );
+ $this->assertEquals( [ 'MediaWiki:Gadget-foo.js', 'MediaWiki:Gadget-foo.css' ],
+ $g->getScriptsAndStyles() );
+ $this->assertEquals( [ 'MediaWiki:Gadget-foo.js' ], $g->getLegacyScripts() );
+ $this->assertFalse( $g->supportsResourceLoader() );
+ $this->assertTrue( $g->hasModule() );
+ }
+
+ /**
+ * @covers MediaWikiGadgetsDefinitionRepo::newFromDefinition
+ * @covers Gadget::supportsResourceLoader
+ * @covers Gadget::getLegacyScripts
+ */
+ public function testRLtag() {
+ $g = $this->create( '*foo [ResourceLoader]|foo.js|foo.css' );
+ $this->assertEquals( 'foo', $g->getName() );
+ $this->assertTrue( $g->supportsResourceLoader() );
+ $this->assertEquals( 0, count( $g->getLegacyScripts() ) );
+ }
+
+ /**
+ * @covers MediaWikiGadgetsDefinitionRepo::newFromDefinition
+ * @covers Gadget::getDependencies
+ */
+ public function testDependencies() {
+ $g = $this->create( '* foo[ResourceLoader|dependencies=jquery.ui]|bar.js' );
+ $this->assertEquals( [ 'MediaWiki:Gadget-bar.js' ], $g->getScripts() );
+ $this->assertTrue( $g->supportsResourceLoader() );
+ $this->assertEquals( [ 'jquery.ui' ], $g->getDependencies() );
+ }
+
+ public static function provideGetType() {
+ return [
+ [
+ 'Default (mixed)',
+ '* foo[ResourceLoader]|bar.css|bar.js',
+ 'general',
+ ResourceLoaderModule::LOAD_GENERAL,
+ ],
+ [
+ 'Default (styles only)',
+ '* foo[ResourceLoader]|bar.css',
+ 'styles',
+ ResourceLoaderModule::LOAD_STYLES,
+ ],
+ [
+ 'Default (scripts only)',
+ '* foo[ResourceLoader]|bar.js',
+ 'general',
+ ResourceLoaderModule::LOAD_GENERAL,
+ ],
+ [
+ 'Default (styles only with dependencies)',
+ '* foo[ResourceLoader|dependencies=jquery.ui]|bar.css',
+ 'general',
+ ResourceLoaderModule::LOAD_GENERAL,
+ ],
+ [
+ 'Styles type (mixed)',
+ '* foo[ResourceLoader|type=styles]|bar.css|bar.js',
+ 'styles',
+ ResourceLoaderModule::LOAD_STYLES,
+ ],
+ [
+ 'Styles type (styles only)',
+ '* foo[ResourceLoader|type=styles]|bar.css',
+ 'styles',
+ ResourceLoaderModule::LOAD_STYLES,
+ ],
+ [
+ 'Styles type (scripts only)',
+ '* foo[ResourceLoader|type=styles]|bar.js',
+ 'styles',
+ ResourceLoaderModule::LOAD_STYLES,
+ ],
+ [
+ 'General type (mixed)',
+ '* foo[ResourceLoader|type=general]|bar.css|bar.js',
+ 'general',
+ ResourceLoaderModule::LOAD_GENERAL,
+ ],
+ [
+ 'General type (styles only)',
+ '* foo[ResourceLoader|type=general]|bar.css',
+ 'general',
+ ResourceLoaderModule::LOAD_GENERAL,
+ ],
+ [
+ 'General type (scripts only)',
+ '* foo[ResourceLoader|type=general]|bar.js',
+ 'general',
+ ResourceLoaderModule::LOAD_GENERAL,
+ ],
+ ];
+ }
+
+ /**
+ * @dataProvider provideGetType
+ * @covers MediaWikiGadgetsDefinitionRepo::newFromDefinition
+ * @covers Gadget::getType
+ * @covers GadgetResourceLoaderModule::getType
+ */
+ public function testType( $message, $definition, $gType, $mType ) {
+ $g = $this->create( $definition );
+ $this->assertEquals( $gType, $g->getType(), "Gadget: $message" );
+ $this->assertEquals( $mType, $this->getModule( $g )->getType(), "Module: $message" );
+ }
+
+ /**
+ * @covers MediaWikiGadgetsDefinitionRepo::newFromDefinition
+ * @covers Gadget::isHidden
+ */
+ public function testIsHidden() {
+ $g = $this->create( '* foo[hidden]|bar.js' );
+ $this->assertTrue( $g->isHidden() );
+
+ $g = $this->create( '* foo[ResourceLoader|hidden]|bar.js' );
+ $this->assertTrue( $g->isHidden() );
+
+ $g = $this->create( '* foo[ResourceLoader]|bar.js' );
+ $this->assertFalse( $g->isHidden() );
+ }
+
+ /**
+ * @covers MediaWikiGadgetsDefinitionRepo::fetchStructuredList
+ * @covers GadgetHooks::getPreferences
+ */
+ public function testPreferences() {
+ $prefs = [];
+ $repo = TestingAccessWrapper::newFromObject( new MediaWikiGadgetsDefinitionRepo() );
+ // Force usage of a MediaWikiGadgetsDefinitionRepo
+ GadgetRepo::setSingleton( $repo );
+
+ $gadgets = $repo->fetchStructuredList( '* foo | foo.js
+==keep-section1==
+* bar| bar.js
+==remove-section==
+* baz [rights=embezzle] |baz.js
+==keep-section2==
+* quux [rights=read] | quux.js' );
+ $this->assertGreaterThanOrEqual( 2, count( $gadgets ), "Gadget list parsed" );
+
+ $repo->definitionCache = $gadgets;
+ $this->assertTrue( GadgetHooks::getPreferences( new User, $prefs ),
+ 'GetPrefences hook should return true' );
+
+ $options = $prefs['gadgets']['options'];
+ $this->assertArrayNotHasKey( '⧼gadget-section-remove-section⧽', $options,
+ 'Must not show empty sections' );
+ $this->assertArrayHasKey( '⧼gadget-section-keep-section1⧽', $options );
+ $this->assertArrayHasKey( '⧼gadget-section-keep-section2⧽', $options );
+ }
+}