FauxRequestTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. use MediaWiki\Session\SessionManager;
  3. class FauxRequestTest extends PHPUnit\Framework\TestCase {
  4. use MediaWikiCoversValidator;
  5. use PHPUnit4And6Compat;
  6. /**
  7. * @covers FauxRequest::__construct
  8. */
  9. public function testConstructInvalidData() {
  10. $this->setExpectedException( MWException::class, 'bogus data' );
  11. $req = new FauxRequest( 'x' );
  12. }
  13. /**
  14. * @covers FauxRequest::__construct
  15. */
  16. public function testConstructInvalidSession() {
  17. $this->setExpectedException( MWException::class, 'bogus session' );
  18. $req = new FauxRequest( [], false, 'x' );
  19. }
  20. /**
  21. * @covers FauxRequest::__construct
  22. */
  23. public function testConstructWithSession() {
  24. $session = SessionManager::singleton()->getEmptySession( new FauxRequest( [] ) );
  25. $this->assertInstanceOf(
  26. FauxRequest::class,
  27. new FauxRequest( [], false, $session )
  28. );
  29. }
  30. /**
  31. * @covers FauxRequest::getText
  32. */
  33. public function testGetText() {
  34. $req = new FauxRequest( [ 'x' => 'Value' ] );
  35. $this->assertEquals( 'Value', $req->getText( 'x' ) );
  36. $this->assertEquals( '', $req->getText( 'z' ) );
  37. }
  38. /**
  39. * Integration test for parent method
  40. * @covers FauxRequest::getVal
  41. */
  42. public function testGetVal() {
  43. $req = new FauxRequest( [ 'crlf' => "A\r\nb" ] );
  44. $this->assertSame( "A\r\nb", $req->getVal( 'crlf' ), 'CRLF' );
  45. }
  46. /**
  47. * Integration test for parent method
  48. * @covers FauxRequest::getRawVal
  49. */
  50. public function testGetRawVal() {
  51. $req = new FauxRequest( [
  52. 'x' => 'Value',
  53. 'y' => [ 'a' ],
  54. 'crlf' => "A\r\nb"
  55. ] );
  56. $this->assertSame( 'Value', $req->getRawVal( 'x' ) );
  57. $this->assertSame( null, $req->getRawVal( 'z' ), 'Not found' );
  58. $this->assertSame( null, $req->getRawVal( 'y' ), 'Array is ignored' );
  59. $this->assertSame( "A\r\nb", $req->getRawVal( 'crlf' ), 'CRLF' );
  60. }
  61. /**
  62. * @covers FauxRequest::getValues
  63. */
  64. public function testGetValues() {
  65. $values = [ 'x' => 'Value', 'y' => '' ];
  66. $req = new FauxRequest( $values );
  67. $this->assertEquals( $values, $req->getValues() );
  68. }
  69. /**
  70. * @covers FauxRequest::getQueryValues
  71. */
  72. public function testGetQueryValues() {
  73. $values = [ 'x' => 'Value', 'y' => '' ];
  74. $req = new FauxRequest( $values );
  75. $this->assertEquals( $values, $req->getQueryValues() );
  76. $req = new FauxRequest( $values, /*wasPosted*/ true );
  77. $this->assertEquals( [], $req->getQueryValues() );
  78. }
  79. /**
  80. * @covers FauxRequest::getMethod
  81. */
  82. public function testGetMethod() {
  83. $req = new FauxRequest( [] );
  84. $this->assertEquals( 'GET', $req->getMethod() );
  85. $req = new FauxRequest( [], /*wasPosted*/ true );
  86. $this->assertEquals( 'POST', $req->getMethod() );
  87. }
  88. /**
  89. * @covers FauxRequest::wasPosted
  90. */
  91. public function testWasPosted() {
  92. $req = new FauxRequest( [] );
  93. $this->assertFalse( $req->wasPosted() );
  94. $req = new FauxRequest( [], /*wasPosted*/ true );
  95. $this->assertTrue( $req->wasPosted() );
  96. }
  97. /**
  98. * @covers FauxRequest::getCookie
  99. * @covers FauxRequest::setCookie
  100. * @covers FauxRequest::setCookies
  101. */
  102. public function testCookies() {
  103. $req = new FauxRequest();
  104. $this->assertSame( null, $req->getCookie( 'z', '' ) );
  105. $req->setCookie( 'x', 'Value', '' );
  106. $this->assertEquals( 'Value', $req->getCookie( 'x', '' ) );
  107. $req->setCookies( [ 'x' => 'One', 'y' => 'Two' ], '' );
  108. $this->assertEquals( 'One', $req->getCookie( 'x', '' ) );
  109. $this->assertEquals( 'Two', $req->getCookie( 'y', '' ) );
  110. }
  111. /**
  112. * @covers FauxRequest::getCookie
  113. * @covers FauxRequest::setCookie
  114. * @covers FauxRequest::setCookies
  115. */
  116. public function testCookiesDefaultPrefix() {
  117. global $wgCookiePrefix;
  118. $oldPrefix = $wgCookiePrefix;
  119. $wgCookiePrefix = '_';
  120. $req = new FauxRequest();
  121. $this->assertSame( null, $req->getCookie( 'z' ) );
  122. $req->setCookie( 'x', 'Value' );
  123. $this->assertEquals( 'Value', $req->getCookie( 'x' ) );
  124. $wgCookiePrefix = $oldPrefix;
  125. }
  126. /**
  127. * @covers FauxRequest::getRequestURL
  128. */
  129. public function testGetRequestURL() {
  130. $req = new FauxRequest();
  131. $this->setExpectedException( MWException::class );
  132. $req->getRequestURL();
  133. }
  134. /**
  135. * @covers FauxRequest::setRequestURL
  136. * @covers FauxRequest::getRequestURL
  137. */
  138. public function testSetRequestURL() {
  139. $req = new FauxRequest();
  140. $req->setRequestURL( 'https://example.org' );
  141. $this->assertEquals( 'https://example.org', $req->getRequestURL() );
  142. }
  143. /**
  144. * @covers FauxRequest::__construct
  145. * @covers FauxRequest::getProtocol
  146. */
  147. public function testProtocol() {
  148. $req = new FauxRequest();
  149. $this->assertEquals( 'http', $req->getProtocol() );
  150. $req = new FauxRequest( [], false, null, 'http' );
  151. $this->assertEquals( 'http', $req->getProtocol() );
  152. $req = new FauxRequest( [], false, null, 'https' );
  153. $this->assertEquals( 'https', $req->getProtocol() );
  154. }
  155. /**
  156. * @covers FauxRequest::setHeader
  157. * @covers FauxRequest::setHeaders
  158. * @covers FauxRequest::getHeader
  159. */
  160. public function testGetSetHeader() {
  161. $value = 'text/plain, text/html';
  162. $request = new FauxRequest();
  163. $request->setHeader( 'Accept', $value );
  164. $this->assertEquals( $request->getHeader( 'Nonexistent' ), false );
  165. $this->assertEquals( $request->getHeader( 'Accept' ), $value );
  166. $this->assertEquals( $request->getHeader( 'ACCEPT' ), $value );
  167. $this->assertEquals( $request->getHeader( 'accept' ), $value );
  168. $this->assertEquals(
  169. $request->getHeader( 'Accept', WebRequest::GETHEADER_LIST ),
  170. [ 'text/plain', 'text/html' ]
  171. );
  172. }
  173. /**
  174. * @covers FauxRequest::initHeaders
  175. */
  176. public function testGetAllHeaders() {
  177. $_SERVER['HTTP_TEST'] = 'Example';
  178. $request = new FauxRequest();
  179. $this->assertEquals(
  180. [],
  181. $request->getAllHeaders()
  182. );
  183. $this->assertEquals(
  184. false,
  185. $request->getHeader( 'test' )
  186. );
  187. }
  188. /**
  189. * @covers FauxRequest::__construct
  190. * @covers FauxRequest::getSessionArray
  191. */
  192. public function testSessionData() {
  193. $values = [ 'x' => 'Value', 'y' => '' ];
  194. $req = new FauxRequest( [], false, /*session*/ $values );
  195. $this->assertEquals( $values, $req->getSessionArray() );
  196. $req = new FauxRequest();
  197. $this->assertSame( null, $req->getSessionArray() );
  198. }
  199. /**
  200. * @covers FauxRequest::getRawQueryString
  201. * @covers FauxRequest::getRawPostString
  202. * @covers FauxRequest::getRawInput
  203. */
  204. public function testDummies() {
  205. $req = new FauxRequest();
  206. $this->assertEquals( '', $req->getRawQueryString() );
  207. $this->assertEquals( '', $req->getRawPostString() );
  208. $this->assertEquals( '', $req->getRawInput() );
  209. }
  210. }