wiki.techinc.nl/tests/phpunit/skins/SideBarTest.php
Timo Tijhof 181c7cdc8e Clean and repair many phpunit tests (+ fix implied configuration)
This commit depends on the introduction of
MediaWikiTestCase::setMwGlobals in change Iccf6ea81f4.

Various tests already set their globals, but forgot to restore
them afterwards, or forgot to call the parent setUp, tearDown...

Either way they won't have to anymore with setMwGlobals.

Consistent use of function characteristics:
* protected function setUp
* protected function tearDown
* public static function (provide..)

(Matching the function signature with PHPUnit/Framework/TestCase.php)

Replaces:
 * public function (setUp|tearDown)\(
 * protected function $1(

 * \tfunction (setUp|tearDown)\(
 * \tprotected function $1(

 * \tfunction (data|provide)\(
 * \tpublic static function $1\(

Also renamed a few "data#", "provider#" and "provides#" functions
to "provide#" for consistency. This also removes confusion where
the /media tests had a few private methods called dataFile(),
which were sometimes expected to be data providers.

Fixes:

TimestampTest often failed due to a previous test setting a
different language (it tests "1 hour ago" so need to make sure
it is set to English).

MWNamespaceTest became a lot cleaner now that it executes with
a known context. Though the now-redundant code that was removed
didn't work anyway because wgContentNamespaces isn't keyed by
namespace id, it had them was values...

FileBackendTest:
* Fixed: "PHP Fatal: Using $this when not in object context"

HttpTest
* Added comment about:
  "PHP Fatal: Call to protected MWHttpRequest::__construct()"
  (too much unrelated code to fix in this commit)

ExternalStoreTest
* Add an assertTrue as well, without it the test is useless
  because regardless of whether wgExternalStores is true or false
  it only uses it if it is an array.

Change-Id: I9d2b148e57bada64afeb7d5a99bec0e58f8e1561
2012-10-09 03:01:51 +02:00

209 lines
4.6 KiB
PHP

<?php
/**
* @group Skin
*/
class SideBarTest extends MediaWikiLangTestCase {
/** A skin template, reinitialized before each test */
private $skin;
/** Local cache for sidebar messages */
private $messages;
function __construct() {
parent::__construct();
}
/** Build $this->messages array */
private function initMessagesHref() {
# List of default messages for the sidebar:
$URL_messages = array(
'mainpage',
'portal-url',
'currentevents-url',
'recentchanges-url',
'randompage-url',
'helppage',
);
foreach( $URL_messages as $m ) {
$titleName = MessageCache::singleton()->get($m);
$title = Title::newFromText( $titleName );
$this->messages[$m]['href'] = $title->getLocalURL();
}
}
protected function setUp() {
parent::setUp();
$this->initMessagesHref();
$this->skin = new SkinTemplate();
$this->skin->getContext()->setLanguage( Language::factory( 'en' ) );
}
protected function tearDown() {
parent::tearDown();
$this->skin = null;
}
/**
* Internal helper to test the sidebar
* @param $expected
* @param $text
* @param $message (Default: '')
*/
private function assertSideBar( $expected, $text, $message = '' ) {
$bar = array();
$this->skin->addToSidebarPlain( $bar, $text );
$this->assertEquals( $expected, $bar, $message );
}
function testSidebarWithOnlyTwoTitles() {
$this->assertSideBar(
array(
'Title1' => array(),
'Title2' => array(),
),
'* Title1
* Title2
'
);
}
function testExpandMessages() {
$this->assertSidebar(
array( 'Title' => array(
array(
'text' => 'Help',
'href' => $this->messages['helppage']['href'],
'id' => 'n-help',
'active' => null
)
)),
'* Title
** helppage|help
'
);
}
function testExternalUrlsRequireADescription() {
$this->assertSidebar(
array( 'Title' => array(
# ** http://www.mediawiki.org/| Home
array(
'text' => 'Home',
'href' => 'http://www.mediawiki.org/',
'id' => 'n-Home',
'active' => null,
'rel' => 'nofollow',
),
# ** http://valid.no.desc.org/
# ... skipped since it is missing a pipe with a description
)),
'* Title
** http://www.mediawiki.org/| Home
** http://valid.no.desc.org/
'
);
}
/**
* bug 33321 - Make sure there's a | after transforming.
* @group Database
*/
function testTrickyPipe() {
$this->assertSidebar(
array( 'Title' => array(
# The first 2 are skipped
# Doesn't really test the url properly
# because it will vary with $wgArticlePath et al.
# ** Baz|Fred
array(
'text' => 'Fred',
'href' => Title::newFromText( 'Baz' )->getLocalUrl(),
'id' => 'n-Fred',
'active' => null,
),
array(
'text' => 'title-to-display',
'href' => Title::newFromText( 'page-to-go-to' )->getLocalUrl(),
'id' => 'n-title-to-display',
'active' => null,
),
)),
'* Title
** {{PAGENAME|Foo}}
** Bar
** Baz|Fred
** {{PLURAL:1|page-to-go-to{{int:pipe-separator/en}}title-to-display|branch not taken}}
'
);
}
#### Attributes for external links ##########################
private function getAttribs( ) {
# Sidebar text we will use everytime
$text = '* Title
** http://www.mediawiki.org/| Home';
$bar = array();
$this->skin->addToSideBarPlain( $bar, $text );
return $bar['Title'][0];
}
/**
* Simple test to verify our helper assertAttribs() is functional
* Please note this assume MediaWiki default settings:
* $wgNoFollowLinks = true
* $wgExternalLinkTarget = false
*/
function testTestAttributesAssertionHelper() {
$attribs = $this->getAttribs();
$this->assertArrayHasKey( 'rel', $attribs );
$this->assertEquals( 'nofollow', $attribs['rel'] );
$this->assertArrayNotHasKey( 'target', $attribs );
}
/**
* Test $wgNoFollowLinks in sidebar
*/
function testRespectWgnofollowlinks() {
global $wgNoFollowLinks;
$saved = $wgNoFollowLinks;
$wgNoFollowLinks = false;
$attribs = $this->getAttribs();
$this->assertArrayNotHasKey( 'rel', $attribs,
'External URL in sidebar do not have rel=nofollow when $wgNoFollowLinks = false'
);
// Restore global
$wgNoFollowLinks = $saved;
}
/**
* Test $wgExternaLinkTarget in sidebar
*/
function testRespectExternallinktarget() {
global $wgExternalLinkTarget;
$saved = $wgExternalLinkTarget;
$wgExternalLinkTarget = '_blank';
$attribs = $this->getAttribs();
$this->assertArrayHasKey( 'target', $attribs );
$this->assertEquals( $attribs['target'], '_blank' );
$wgExternalLinkTarget = '_self';
$attribs = $this->getAttribs();
$this->assertArrayHasKey( 'target', $attribs );
$this->assertEquals( $attribs['target'], '_self' );
// Restore global
$wgExternalLinkTarget = $saved;
}
}