123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- <?php
- use MediaWiki\Session\SessionManager;
- class FauxRequestTest extends PHPUnit\Framework\TestCase {
- use MediaWikiCoversValidator;
- use PHPUnit4And6Compat;
- /**
- * @covers FauxRequest::__construct
- */
- public function testConstructInvalidData() {
- $this->setExpectedException( MWException::class, 'bogus data' );
- $req = new FauxRequest( 'x' );
- }
- /**
- * @covers FauxRequest::__construct
- */
- public function testConstructInvalidSession() {
- $this->setExpectedException( MWException::class, 'bogus session' );
- $req = new FauxRequest( [], false, 'x' );
- }
- /**
- * @covers FauxRequest::__construct
- */
- public function testConstructWithSession() {
- $session = SessionManager::singleton()->getEmptySession( new FauxRequest( [] ) );
- $this->assertInstanceOf(
- FauxRequest::class,
- new FauxRequest( [], false, $session )
- );
- }
- /**
- * @covers FauxRequest::getText
- */
- public function testGetText() {
- $req = new FauxRequest( [ 'x' => 'Value' ] );
- $this->assertEquals( 'Value', $req->getText( 'x' ) );
- $this->assertEquals( '', $req->getText( 'z' ) );
- }
- /**
- * Integration test for parent method
- * @covers FauxRequest::getVal
- */
- public function testGetVal() {
- $req = new FauxRequest( [ 'crlf' => "A\r\nb" ] );
- $this->assertSame( "A\r\nb", $req->getVal( 'crlf' ), 'CRLF' );
- }
- /**
- * Integration test for parent method
- * @covers FauxRequest::getRawVal
- */
- public function testGetRawVal() {
- $req = new FauxRequest( [
- 'x' => 'Value',
- 'y' => [ 'a' ],
- 'crlf' => "A\r\nb"
- ] );
- $this->assertSame( 'Value', $req->getRawVal( 'x' ) );
- $this->assertSame( null, $req->getRawVal( 'z' ), 'Not found' );
- $this->assertSame( null, $req->getRawVal( 'y' ), 'Array is ignored' );
- $this->assertSame( "A\r\nb", $req->getRawVal( 'crlf' ), 'CRLF' );
- }
- /**
- * @covers FauxRequest::getValues
- */
- public function testGetValues() {
- $values = [ 'x' => 'Value', 'y' => '' ];
- $req = new FauxRequest( $values );
- $this->assertEquals( $values, $req->getValues() );
- }
- /**
- * @covers FauxRequest::getQueryValues
- */
- public function testGetQueryValues() {
- $values = [ 'x' => 'Value', 'y' => '' ];
- $req = new FauxRequest( $values );
- $this->assertEquals( $values, $req->getQueryValues() );
- $req = new FauxRequest( $values, /*wasPosted*/ true );
- $this->assertEquals( [], $req->getQueryValues() );
- }
- /**
- * @covers FauxRequest::getMethod
- */
- public function testGetMethod() {
- $req = new FauxRequest( [] );
- $this->assertEquals( 'GET', $req->getMethod() );
- $req = new FauxRequest( [], /*wasPosted*/ true );
- $this->assertEquals( 'POST', $req->getMethod() );
- }
- /**
- * @covers FauxRequest::wasPosted
- */
- public function testWasPosted() {
- $req = new FauxRequest( [] );
- $this->assertFalse( $req->wasPosted() );
- $req = new FauxRequest( [], /*wasPosted*/ true );
- $this->assertTrue( $req->wasPosted() );
- }
- /**
- * @covers FauxRequest::getCookie
- * @covers FauxRequest::setCookie
- * @covers FauxRequest::setCookies
- */
- public function testCookies() {
- $req = new FauxRequest();
- $this->assertSame( null, $req->getCookie( 'z', '' ) );
- $req->setCookie( 'x', 'Value', '' );
- $this->assertEquals( 'Value', $req->getCookie( 'x', '' ) );
- $req->setCookies( [ 'x' => 'One', 'y' => 'Two' ], '' );
- $this->assertEquals( 'One', $req->getCookie( 'x', '' ) );
- $this->assertEquals( 'Two', $req->getCookie( 'y', '' ) );
- }
- /**
- * @covers FauxRequest::getCookie
- * @covers FauxRequest::setCookie
- * @covers FauxRequest::setCookies
- */
- public function testCookiesDefaultPrefix() {
- global $wgCookiePrefix;
- $oldPrefix = $wgCookiePrefix;
- $wgCookiePrefix = '_';
- $req = new FauxRequest();
- $this->assertSame( null, $req->getCookie( 'z' ) );
- $req->setCookie( 'x', 'Value' );
- $this->assertEquals( 'Value', $req->getCookie( 'x' ) );
- $wgCookiePrefix = $oldPrefix;
- }
- /**
- * @covers FauxRequest::getRequestURL
- */
- public function testGetRequestURL() {
- $req = new FauxRequest();
- $this->setExpectedException( MWException::class );
- $req->getRequestURL();
- }
- /**
- * @covers FauxRequest::setRequestURL
- * @covers FauxRequest::getRequestURL
- */
- public function testSetRequestURL() {
- $req = new FauxRequest();
- $req->setRequestURL( 'https://example.org' );
- $this->assertEquals( 'https://example.org', $req->getRequestURL() );
- }
- /**
- * @covers FauxRequest::__construct
- * @covers FauxRequest::getProtocol
- */
- public function testProtocol() {
- $req = new FauxRequest();
- $this->assertEquals( 'http', $req->getProtocol() );
- $req = new FauxRequest( [], false, null, 'http' );
- $this->assertEquals( 'http', $req->getProtocol() );
- $req = new FauxRequest( [], false, null, 'https' );
- $this->assertEquals( 'https', $req->getProtocol() );
- }
- /**
- * @covers FauxRequest::setHeader
- * @covers FauxRequest::setHeaders
- * @covers FauxRequest::getHeader
- */
- public function testGetSetHeader() {
- $value = 'text/plain, text/html';
- $request = new FauxRequest();
- $request->setHeader( 'Accept', $value );
- $this->assertEquals( $request->getHeader( 'Nonexistent' ), false );
- $this->assertEquals( $request->getHeader( 'Accept' ), $value );
- $this->assertEquals( $request->getHeader( 'ACCEPT' ), $value );
- $this->assertEquals( $request->getHeader( 'accept' ), $value );
- $this->assertEquals(
- $request->getHeader( 'Accept', WebRequest::GETHEADER_LIST ),
- [ 'text/plain', 'text/html' ]
- );
- }
- /**
- * @covers FauxRequest::initHeaders
- */
- public function testGetAllHeaders() {
- $_SERVER['HTTP_TEST'] = 'Example';
- $request = new FauxRequest();
- $this->assertEquals(
- [],
- $request->getAllHeaders()
- );
- $this->assertEquals(
- false,
- $request->getHeader( 'test' )
- );
- }
- /**
- * @covers FauxRequest::__construct
- * @covers FauxRequest::getSessionArray
- */
- public function testSessionData() {
- $values = [ 'x' => 'Value', 'y' => '' ];
- $req = new FauxRequest( [], false, /*session*/ $values );
- $this->assertEquals( $values, $req->getSessionArray() );
- $req = new FauxRequest();
- $this->assertSame( null, $req->getSessionArray() );
- }
- /**
- * @covers FauxRequest::getRawQueryString
- * @covers FauxRequest::getRawPostString
- * @covers FauxRequest::getRawInput
- */
- public function testDummies() {
- $req = new FauxRequest();
- $this->assertEquals( '', $req->getRawQueryString() );
- $this->assertEquals( '', $req->getRawPostString() );
- $this->assertEquals( '', $req->getRawInput() );
- }
- }
|