Add test for Special:page::buildPrevNextNavigation
authordaniel <dkinzler@wikimedia.org>
Mon, 11 Mar 2019 13:33:18 +0000 (14:33 +0100)
committerdaniel <dkinzler@wikimedia.org>
Tue, 2 Apr 2019 11:59:46 +0000 (13:59 +0200)
Change-Id: Ie17221305ec979acebebb62798b83f5d91df1f60

tests/phpunit/includes/specialpage/SpecialPageTest.php

index 2eddb01..ec4bf0f 100644 (file)
@@ -1,5 +1,7 @@
 <?php
 
+use Wikimedia\TestingAccessWrapper;
+
 /**
  * @covers SpecialPage
  *
@@ -101,4 +103,82 @@ class SpecialPageTest extends MediaWikiTestCase {
                $this->assertTrue( true );
        }
 
+       public function provideBuildPrevNextNavigation() {
+               yield [ 0, 20, false, false ];
+               yield [ 17, 20, false, false ];
+               yield [ 0, 17, false, false ];
+               yield [ 0, 20, true, 'Foo' ];
+               yield [ 17, 20, true, 'Föö_Bär' ];
+       }
+
+       /**
+        * @dataProvider provideBuildPrevNextNavigation
+        */
+       public function testBuildPrevNextNavigation( $offset, $limit, $atEnd, $subPage ) {
+               $this->setUserLang( Language::factory( 'qqx' ) ); // disable i18n
+
+               $specialPage = new SpecialPage( 'Watchlist' );
+               $specialPage = TestingAccessWrapper::newFromObject( $specialPage );
+
+               $html = $specialPage->buildPrevNextNavigation(
+                       $offset,
+                       $limit,
+                       [ 'x' => 25 ],
+                       $atEnd,
+                       $subPage
+               );
+
+               $this->assertStringStartsWith( '(viewprevnext:', $html );
+
+               preg_match_all( '!<a.*?</a>!', $html, $m, PREG_PATTERN_ORDER );
+               $links = $m[0];
+
+               foreach ( $links as $a ) {
+                       if ( $subPage ) {
+                               $this->assertContains( 'Special:Watchlist/' . wfUrlencode( $subPage ), $a );
+                       } else {
+                               $this->assertContains( 'Special:Watchlist', $a );
+                               $this->assertNotContains( 'Special:Watchlist/', $a );
+                       }
+                       $this->assertContains( 'x=25', $a );
+               }
+
+               $i = 0;
+
+               if ( $offset > 0 ) {
+                       $this->assertContains(
+                               'limit=' . $limit . '&amp;offset=' . max( 0, $offset - $limit ) . '&amp;',
+                               $links[ $i ]
+                       );
+                       $this->assertContains( 'title="(prevn-title: ' . $limit . ')"', $links[$i] );
+                       $this->assertContains( 'class="mw-prevlink"', $links[$i] );
+                       $this->assertContains( '>(prevn: ' . $limit . ')<', $links[$i] );
+                       $i += 1;
+               }
+
+               if ( !$atEnd ) {
+                       $this->assertContains(
+                               'limit=' . $limit . '&amp;offset=' . ( $offset + $limit ) . '&amp;',
+                               $links[ $i ]
+                       );
+                       $this->assertContains( 'title="(nextn-title: ' . $limit . ')"', $links[$i] );
+                       $this->assertContains( 'class="mw-nextlink"', $links[$i] );
+                       $this->assertContains( '>(nextn: ' . $limit . ')<', $links[$i] );
+                       $i += 1;
+               }
+
+               $this->assertCount( 5 + $i, $links );
+
+               $this->assertContains( 'limit=20&amp;offset=' . $offset, $links[$i] );
+               $this->assertContains( 'title="(shown-title: 20)"', $links[$i] );
+               $this->assertContains( 'class="mw-numlink"', $links[$i] );
+               $this->assertContains( '>20<', $links[$i] );
+               $i += 4;
+
+               $this->assertContains( 'limit=500&amp;offset=' . $offset, $links[$i] );
+               $this->assertContains( 'title="(shown-title: 500)"', $links[$i] );
+               $this->assertContains( 'class="mw-numlink"', $links[$i] );
+               $this->assertContains( '>500<', $links[$i] );
+       }
+
 }