r93050 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r93049‎ | r93050 | r93051 >
Date:15:42, 25 July 2011
Author:hashar
Status:resolved
Tags:
Comment:
Test for external links in the sidebar

follow up r90949
Note: this does NOT test the parser external links only externals
links inside the sidebar. This might help rewrite r90949.
Modified paths:
  • /trunk/phase3/tests/phpunit/skins/SideBarTest.php (added) (history)

Diff [purge]

Index: trunk/phase3/tests/phpunit/skins/SideBarTest.php
@@ -0,0 +1,170 @@
 2+<?php
 3+
 4+class SideBarTest extends MediaWikiTestCase {
 5+
 6+ /** A skin template, reinitialized before each test */
 7+ private $skin;
 8+ /** Local cache for sidebar messages */
 9+ private $messages;
 10+
 11+ function __construct() {
 12+ $this->initMessagesHref();
 13+ parent::__construct();
 14+ }
 15+
 16+ /** Build $this->messages array */
 17+ private function initMessagesHref() {
 18+ # List of default messages for the sidebar:
 19+ $URL_messages = array(
 20+ 'mainpage',
 21+ 'portal-url',
 22+ 'currentevents-url',
 23+ 'recentchanges-url',
 24+ 'randompage-url',
 25+ 'helppage',
 26+ );
 27+
 28+ foreach( $URL_messages as $m ) {
 29+ $titleName = MessageCache::singleton()->get($m);
 30+ $title = Title::newFromText( $titleName );
 31+ $this->messages[$m]['href'] = $title->getLocalURL();
 32+ }
 33+ }
 34+
 35+ function setUp() {
 36+ $this->skin = new SkinTemplate();
 37+ }
 38+ function tearDown() {
 39+ $this->skin = null;
 40+ }
 41+
 42+ /**
 43+ * Internal helper to test the sidebar
 44+ * @param $expected
 45+ * @param $text
 46+ * @param $message (Default: '')
 47+ */
 48+ private function assertSideBar( $expected, $text, $message = '' ) {
 49+ $bar = array();
 50+ $this->skin->addToSidebarPlain( $bar, $text );
 51+ $this->assertEquals( $expected, $bar, $message );
 52+ }
 53+
 54+ function testSidebarWithOnlyTwoTitles() {
 55+ $this->assertSideBar(
 56+ array(
 57+ 'Title1' => array(),
 58+ 'Title2' => array(),
 59+ ),
 60+'* Title1
 61+* Title2
 62+'
 63+ );
 64+ }
 65+
 66+ function testExpandMessages() {
 67+ $this->assertSidebar(
 68+ array( 'Title' => array(
 69+ array(
 70+ 'text' => 'Help',
 71+ 'href' => $this->messages['helppage']['href'],
 72+ 'id' => 'n-help',
 73+ 'active' => null
 74+ )
 75+ )),
 76+'* Title
 77+** helppage|help
 78+'
 79+ );
 80+ }
 81+
 82+ function testExternalUrlsRequireADescription() {
 83+ $this->assertSidebar(
 84+ array( 'Title' => array(
 85+ # ** http://www.mediawiki.org/| Home
 86+ array(
 87+ 'text' => 'Home',
 88+ 'href' => 'http://www.mediawiki.org/',
 89+ 'id' => 'n-Home',
 90+ 'active' => null,
 91+ 'rel' => 'nofollow',
 92+ ),
 93+ # ** http://valid.no.desc.org/
 94+ # ... skipped since it is missing a pipe with a description
 95+ )),
 96+'* Title
 97+** http://www.mediawiki.org/| Home
 98+** http://valid.no.desc.org/
 99+'
 100+
 101+ );
 102+
 103+ }
 104+
 105+
 106+ #### Attributes for external links ##########################
 107+ private function getAttribs( ) {
 108+ # Sidebar text we will use everytime
 109+ $text = '* Title
 110+** http://www.mediawiki.org/| Home';
 111+
 112+ $bar = array();
 113+ $this->skin->addToSideBarPlain( $bar, $text );
 114+
 115+ return $bar['Title'][0];
 116+ }
 117+
 118+ /**
 119+ * Simple test to verify our helper assertAttribs() is functional
 120+ * Please note this assume MediaWiki default settings:
 121+ * $wgNoFollowLinks = true
 122+ * $wgExternalLinkTarget = false
 123+ */
 124+ function testTestAttributesAssertionHelper() {
 125+ $attribs = $this->getAttribs();
 126+
 127+ $this->assertArrayHasKey( 'rel', $attribs );
 128+ $this->assertEquals( 'nofollow', $attribs['rel'] );
 129+
 130+ $this->assertArrayNotHasKey( 'target', $attribs );
 131+ }
 132+
 133+ /**
 134+ * Test wgNoFollowLinks in sidebar
 135+ */
 136+ function testRespectWgnofollowlinks() {
 137+ global $wgNoFollowLinks;
 138+ $saved = $wgNoFollowLinks;
 139+ $wgNoFollowLinks = false;
 140+
 141+ $attribs = $this->getAttribs();
 142+ $this->assertArrayNotHasKey( 'rel', $attribs,
 143+ 'External URL in sidebar do not have rel=nofollow when wgNoFollowLinks = false'
 144+ );
 145+
 146+ // Restore global
 147+ $wgNoFollowLinks = $saved;
 148+ }
 149+
 150+ /**
 151+ * Test wgExternaLinkTarget in sidebar
 152+ */
 153+ function testRespectExternallinktarget() {
 154+ global $wgExternalLinkTarget;
 155+ $saved = $wgExternalLinkTarget;
 156+
 157+ $wgExternalLinkTarget = '_blank';
 158+ $attribs = $this->getAttribs();
 159+ $this->assertArrayHasKey( 'target', $attribs );
 160+ $this->assertEquals( $attribs['target'], '_blank' );
 161+
 162+ $wgExternalLinkTarget = '_self';
 163+ $attribs = $this->getAttribs();
 164+ $this->assertArrayHasKey( 'target', $attribs );
 165+ $this->assertEquals( $attribs['target'], '_self' );
 166+
 167+ // Restore global
 168+ $wgExternalLinkTarget = $saved;
 169+ }
 170+
 171+}

Follow-up revisions

RevisionCommit summaryAuthorDate
r93070Make SideBarTest work with a Language other than English set....platonides17:17, 25 July 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r90949Added spinner when checking for image existencejanpaul12310:27, 28 June 2011

Status & tagging log