Index: branches/RL2/extensions/Gadgets/tests/GadgetTest.php |
— | — | @@ -2,8 +2,9 @@ |
3 | 3 | |
4 | 4 | /** |
5 | 5 | * @group Gadgets |
| 6 | + * @group Database |
6 | 7 | */ |
7 | | -class GadgetTest extends PHPUnit_Framework_TestCase { |
| 8 | +class GadgetTest extends MediaWikiTestCase { |
8 | 9 | /** |
9 | 10 | * @dataProvider provideValidatePropertiesArray |
10 | 11 | */ |
— | — | @@ -48,9 +49,9 @@ |
49 | 50 | ); |
50 | 51 | } |
51 | 52 | |
52 | | - public function testGetters() { |
53 | | - $now = wfTimestampNow(); |
54 | | - $data = array( |
| 53 | + public static function getBoilerplateData() { |
| 54 | + // Should be static or const or something, but PHP won't let us do that cause PHP sucks |
| 55 | + return array( |
55 | 56 | 'settings' => array( |
56 | 57 | 'rights' => array( 'protect' ), |
57 | 58 | 'default' => true, |
— | — | @@ -65,7 +66,12 @@ |
66 | 67 | 'dependencies' => array( 'jquery.ui.button' ) |
67 | 68 | ) |
68 | 69 | ); |
69 | | - $g = new Gadget( 'GadgetTest', LocalGadgetRepo::singleton(), $data, wfTimestampNow() ); |
| 70 | + } |
| 71 | + |
| 72 | + public function testGetters() { |
| 73 | + $data = self::getBoilerplateData(); |
| 74 | + $now = wfTimestampNow(); |
| 75 | + $g = new Gadget( 'GadgetTest', LocalGadgetRepo::singleton(), $data, $now ); |
70 | 76 | $this->assertEquals( $data, $g->getMetadata(), 'getMetadata' ); |
71 | 77 | $this->assertEquals( FormatJson::encode( $data ), $g->getJSON(), 'getJSON' ); |
72 | 78 | $this->assertEquals( 'GadgetTest', $g->getId(), 'getId' ); |
— | — | @@ -80,4 +86,102 @@ |
81 | 87 | $this->assertEquals( $data['module']['styles'], $g->getStyles(), 'getStyles' ); |
82 | 88 | $this->assertEquals( $data['module']['dependencies'], $g->getDependencies(), 'getDependencies' ); |
83 | 89 | } |
| 90 | + |
| 91 | + public function testMessageFunctions() { |
| 92 | + global $wgLang; |
| 93 | + |
| 94 | + $g = new Gadget( 'gadgettest1', LocalGadgetRepo::singleton(), Gadget::getPropertiesBase(), wfTimestampNow() ); |
| 95 | + $this->assertEquals( 'gadget-gadgettest1-title', $g->getTitleMessageKey(), 'getTitleMessageKey' ); |
| 96 | + $this->assertEquals( 'gadget-gadgettest1-desc', $g->getDescriptionMessageKey(), 'getDescriptionMessageKey' ); |
| 97 | + |
| 98 | + // Make sure the gadget-gadgettest1-{title,desc} messages exist |
| 99 | + // In the test environment, the MessageCache is disabled because |
| 100 | + // $wgUseDatabaseMessages is set to false. Temporarily enable it |
| 101 | + // so we can write messages to the DB and use them. |
| 102 | + MessageCache::singleton()->enable(); |
| 103 | + |
| 104 | + $titleMsgTitle = Title::newFromText( 'MediaWiki:gadget-gadgettest1-title' ); |
| 105 | + $descMsgTitle = Title::newFromText( 'MediaWiki:gadget-gadgettest1-desc' ); |
| 106 | + if ( !$titleMsgTitle->exists() ) { |
| 107 | + ParserTest::addArticle( $titleMsgTitle->getPrefixedText(), 'Gadget test 1', __LINE__ ); |
| 108 | + } |
| 109 | + if ( !$descMsgTitle->exists() ) { |
| 110 | + ParserTest::addArticle( $descMsgTitle->getPrefixedText(), 'Description of gadget test 1', __LINE__ ); |
| 111 | + } |
| 112 | + $this->assertEquals( wfMessage( 'gadget-gadgettest1-title' )->plain(), $g->getTitleMessage(), 'getTitleMessage for existing message' ); |
| 113 | + $this->assertEquals( wfMessage( 'gadget-gadgettest1-desc' )->plain(), $g->getDescriptionMessage(), 'getDescriptionMessage for existing message' ); |
| 114 | + |
| 115 | + $g = new Gadget( 'gadgettest2', LocalGadgetRepo::singleton(), Gadget::getPropertiesBase(), wfTimestampNow() ); |
| 116 | + $titleMsgTitle = Title::newFromText( 'MediaWiki:gadget-gadgettest2-title' ); |
| 117 | + $descMsgTitle = Title::newFromText( 'MediaWiki:gadget-gadgettest2-desc' ); |
| 118 | + if ( !$titleMsgTitle->exists() ) { |
| 119 | + $page = WikiPage::factory( $titleMsgTitle ); |
| 120 | + $page->doDeleteArticle( 'Deleting to make way for test' ); |
| 121 | + } |
| 122 | + if ( !$descMsgTitle->exists() ) { |
| 123 | + $page = WikiPage::factory( $descMsgTitle ); |
| 124 | + $page->doDeleteArticle( 'Deleting to make way for test' ); |
| 125 | + } |
| 126 | + $this->assertEquals( $wgLang->ucfirst( 'gadgettest2' ), $g->getTitleMessage(), 'getTitleMessage for nonexistent message' ); |
| 127 | + $this->assertEquals( '', $g->getDescriptionMessage(), 'getDescriptionMessage for nonexistent message' ); |
| 128 | + |
| 129 | + MessageCache::singleton()->disable(); |
| 130 | + |
| 131 | + } |
| 132 | + |
| 133 | + public function testGetModule() { |
| 134 | + $data = self::getBoilerplateData(); |
| 135 | + $g = new Gadget( 'GadgetTest', LocalGadgetRepo::singleton(), $data, wfTimestampNow() ); |
| 136 | + $m = $g->getModule(); |
| 137 | + $pages = array( |
| 138 | + 'Gadget:Foo.js' => array( 'type' => 'script' ), |
| 139 | + 'Gadget:Bar.js' => array( 'type' => 'script' ), |
| 140 | + 'Gadget:Foo.css' => array( 'type' => 'style' ), |
| 141 | + ); |
| 142 | + |
| 143 | + $this->assertEquals( 'gadget.GadgetTest', $g->getModuleName(), 'getModuleName' ); |
| 144 | + $this->assertEquals( $g->getDependencies(), $m->getDependencies(), 'getDependencies' ); |
| 145 | + $this->assertEquals( $data['module']['messages'], $m->getMessages(), 'getMessages' ); |
| 146 | + $this->assertEquals( LocalGadgetRepo::singleton()->getSource(), $m->getSource(), 'getSource' ); |
| 147 | + $this->assertEquals( $pages, $m->getPages( ResourceLoaderContext::newDummyContext() ), 'getPages' ); |
| 148 | + } |
| 149 | + |
| 150 | + public function testIsEnabledForUser() { |
| 151 | + $defaultOff = self::buildPropertiesArray( array( 'settings' => array( 'default' => false ) ) ); |
| 152 | + $defaultOn = self::buildPropertiesArray( array( 'settings' => array( 'default' => true ) ) ); |
| 153 | + $gOff = new Gadget( 'GadgetTestOffByDefault', LocalGadgetRepo::singleton(), $defaultOff, wfTimestampNow() ); |
| 154 | + $gOn = new Gadget( 'GadgetTestOnByDefault', LocalGadgetRepo::singleton(), $defaultOn, wfTimestampNow() ); |
| 155 | + $user = new User; |
| 156 | + |
| 157 | + $this->assertFalse( $gOff->isEnabledByDefault(), 'isEnabledByDefault for gOff' ); |
| 158 | + $this->assertTrue( $gOn->isEnabledByDefault(), 'isEnabledByDefault for gOn' ); |
| 159 | + $this->assertFalse( $gOff->isEnabledForUser( $user ), 'isEnabledForUser for gOff with default pref' ); |
| 160 | + $this->assertTrue( $gOn->isEnabledForUser( $user ), 'isEnabledForUser for gOn with default pref' ); |
| 161 | + |
| 162 | + $user->setOption( 'gadget-GadgetTestOffByDefault', 0 ); |
| 163 | + $this->assertFalse( $gOff->isEnabledForUser( $user ), 'isEnabledForUser for gOff with pref off' ); |
| 164 | + $user->setOption( 'gadget-GadgetTestOffByDefault', 1 ); |
| 165 | + $this->assertTrue( $gOff->isEnabledForUser( $user ), 'isEnabledForUser for gOff with pref on' ); |
| 166 | + |
| 167 | + $user->setOption( 'gadget-GadgetTestOnByDefault', 0 ); |
| 168 | + $this->assertFalse( $gOn->isEnabledForUser( $user ), 'isEnabledForUser for gOn with pref off' ); |
| 169 | + $user->setOption( 'gadget-GadgetTestOnByDefault', 1 ); |
| 170 | + $this->assertTrue( $gOn->isEnabledForUser( $user ), 'isEnabledForUser for gOn with pref on' ); |
| 171 | + } |
| 172 | + |
| 173 | + public function testIsAllowed() { |
| 174 | + $data = self::buildPropertiesArray( array( 'settings' => array( 'rights' => array( 'foo', 'bar' ) ) ) ); |
| 175 | + $g = new Gadget( 'GadgetTest', LocalGadgetRepo::singleton(), $data, wfTimestampNow() ); |
| 176 | + $user = new User; |
| 177 | + |
| 178 | + // This is dirty, but I don't know how I would otherwise test this |
| 179 | + $user->mRights = array(); |
| 180 | + $this->assertFalse( $g->isAllowed( $user ), 'user has no rights' ); |
| 181 | + $user->mRights = array( 'foo' ); |
| 182 | + $this->assertFalse( $g->isAllowed( $user ), 'user has foo right only' ); |
| 183 | + $user->mRights = array( 'bar' ); |
| 184 | + $this->assertFalse( $g->isAllowed( $user ), 'user has bar right only' ); |
| 185 | + $user->mRights = array( 'foo', 'bar' ); |
| 186 | + $this->assertTrue( $g->isAllowed( $user ), 'user has both foo and bar rights' ); |
| 187 | + } |
84 | 188 | } |