Open Monograph Press  3.3.0
PKPRequestTest.php
1 <?php
2 
18 require_mock_env('env1');
19 
20 import('lib.pkp.tests.PKPTestCase');
21 import('lib.pkp.classes.core.PKPRequest');
22 import('lib.pkp.classes.plugins.HookRegistry'); // This imports our mock HookRegistry implementation.
23 
27 class PKPRequestTest extends PKPTestCase {
28  protected $request;
29  private $getRemoteAddrTestConfigData;
30 
31  public function setUp() : void {
32  parent::setUp();
34  $this->request = new PKPRequest();
35 
36  // Save the config data for testTrustXForwardedFor tests
37  $this->getRemoteAddrTestConfigData = Registry::get('configData');
38  }
39 
40  public function tearDown() : void {
42 
43  // Restore the config data after testTrustXForwardedFor tests
44  Registry::set('configData', $this->getRemoteAddrTestConfigData);
45  }
46 
50  public function testIsPathInfoEnabled1() {
51  $this->setTestConfiguration('request1', 'classes/core/config');
52  self::assertTrue($this->request->isPathInfoEnabled());
53  }
54 
58  public function testIsPathInfoEnabled2() {
59  $this->setTestConfiguration('request2', 'classes/core/config');
60  self::assertFalse($this->request->isPathInfoEnabled());
61  }
62 
66  public function testIsRestfulUrlsEnabled1() {
67  $this->setTestConfiguration('request1', 'classes/core/config');
68  self::assertFalse($this->request->isRestfulUrlsEnabled());
69  }
70 
74  public function testIsRestfulUrlsEnabled2() {
75  $this->setTestConfiguration('request2', 'classes/core/config');
76  self::assertTrue($this->request->isRestfulUrlsEnabled());
77  }
78 
82  public function testRedirectUrl() {
83  HookRegistry::register('Request::redirect', array($this, 'redirectUrlHook'));
84  $this->request->redirectUrl('http://some.url/');
85  self::assertEquals(
86  array(array('Request::redirect' , array('http://some.url/'))),
88  );
89  HookRegistry::clear('Request::redirect');
90  }
91 
97  public function redirectUrlHook($hookName, $args) {
98  // Returning true will avoid actual redirection.
99  return true;
100  }
101 
105  public function testGetBaseUrl() {
106  $this->setTestConfiguration('request1', 'classes/core/config'); // baseurl1
107  $_SERVER = array(
108  'SCRIPT_NAME' => '/index.php',
109  );
110  self::assertEquals('http://baseurl1/', $this->request->getBaseUrl());
111 
112  // Two hooks should have been triggered.
113  self::assertEquals(
114  array(
115  array('Request::getServerHost' , array(false, false, true)),
116  array('Request::getBaseUrl' , array('http://baseurl1/'))
117  ),
119  );
120 
121  // Calling getBaseUrl twice should return the same
122  // result without triggering the hooks again.
124  self::assertEquals('http://baseurl1/', $this->request->getBaseUrl());
125  self::assertEquals(
126  array(),
128  );
129  }
130 
135  $this->setTestConfiguration('request1', 'classes/core/config');
136  $_SERVER = array(
137  'SERVER_NAME' => 'hostname',
138  'SCRIPT_NAME' => '/some/base/path'
139  );
140  self::assertEquals('http://hostname/some/base/path', $this->request->getBaseUrl());
141  }
142 
146  public function testGetBasePath() {
147  $_SERVER = array(
148  'SCRIPT_NAME' => '/some/base/path'
149  );
150  self::assertEquals('/some/base/path', $this->request->getBasePath());
151 
152  // The hook should have been triggered once.
153  self::assertEquals(
154  array(array('Request::getBasePath' , array('/some/base/path'))),
156  );
157 
158  // Calling getBasePath twice should return the same
159  // result without triggering the hook again.
161  self::assertEquals('/some/base/path', $this->request->getBasePath());
162  self::assertEquals(
163  array(),
165  );
166  }
167 
171  public function testGetEmptyBasePath() {
172  $_SERVER = array(
173  'SCRIPT_NAME' => '/main'
174  );
175  self::assertEquals('/main', $this->request->getBasePath());
176  }
177 
181  public function testGetRequestPath() {
182  $_SERVER = array(
183  'SCRIPT_NAME' => 'some/script/name'
184  );
185  $this->setTestConfiguration('request1', 'classes/core/config'); // no restful URLs
186 
187  self::assertEquals('some/script/name', $this->request->getRequestPath());
188 
189  // The hook should have been triggered once.
190  self::assertEquals(
191  array(array('Request::getRequestPath' , array('some/script/name'))),
193  );
194 
195  // Calling getRequestPath() twice should return the same
196  // result without triggering the hook again.
198  self::assertEquals('some/script/name', $this->request->getRequestPath());
199  self::assertEquals(
200  array(),
202  );
203  }
204 
208  public function testGetRequestPathRestful() {
209  $_SERVER = array(
210  'SCRIPT_NAME' => 'some/script/name'
211  );
212  $this->setTestConfiguration('request2', 'classes/core/config'); // restful URLs
213 
214  self::assertEquals('some/script/name', $this->request->getRequestPath());
215  }
216 
217 
221  public function testGetRequestPathWithPathinfo() {
222  $_SERVER = array(
223  'SCRIPT_NAME' => 'some/script/name',
224  'PATH_INFO' => '/extra/path'
225  );
226  $this->setTestConfiguration('request1', 'classes/core/config'); // path info enabled
227 
228  self::assertEquals('some/script/name/extra/path', $this->request->getRequestPath());
229  }
230 
235  $_SERVER = array(
236  'SCRIPT_NAME' => 'some/script/name',
237  'PATH_INFO' => '/extra/path'
238  );
239  $this->setTestConfiguration('request2', 'classes/core/config'); // path info disabled
240 
241  self::assertEquals('some/script/name', $this->request->getRequestPath());
242  }
243 
247  public function testGetServerHostLocalhost() {
248  // if none of the server variables is set then return the default
249  $_SERVER = array(
250  'SCRIPT_NAME' => '/index.php',
251  );
252  self::assertEquals('localhost', $this->request->getServerHost());
253  }
254 
259  public function testGetServerHostWithHostname() {
260  // if SERVER_NAME is set then return it
261  $_SERVER = array(
262  'SERVER_NAME' => 'hostname',
263  'SCRIPT_NAME' => ''
264  );
265  self::assertEquals('hostname', $this->request->getServerHost());
266  }
267 
273  // if SERVER_NAME is set then return it
274  $_SERVER = array(
275  'SERVER_NAME' => 'hostname',
276  'SCRIPT_NAME' => '/index.php',
277  );
278  self::assertEquals('hostname', $this->request->getServerHost());
279  }
280 
285  public function testGetServerHostWithHttpHost() {
286  // if HTTP_HOST is set then return it
287  $_SERVER = array(
288  'SERVER_NAME' => 'hostname',
289  'HTTP_HOST' => 'http_host',
290  'SCRIPT_NAME' => '/index.php',
291  );
292  self::assertEquals('http_host', $this->request->getServerHost());
293  }
294 
300  // if HTTP_X_FORWARDED_HOST is set then return it
301  $_SERVER = array(
302  'SERVER_NAME' => 'hostname',
303  'HTTP_HOST' => 'http_host',
304  'HTTP_X_FORWARDED_HOST' => 'x_host',
305  'SCRIPT_NAME' => '/index.php',
306  );
307  self::assertEquals('x_host', $this->request->getServerHost());
308  }
309 
313  public function testGetProtocolNoHttpsVariable() {
314  $_SERVER = array();
315  self::assertEquals('http', $this->request->getProtocol());
316  // The hook should have been triggered once.
317  self::assertEquals(
318  array(array('Request::getProtocol' , array('http'))),
320  );
321 
322  // Calling getProtocol() twice should return the same
323  // result without triggering the hook again.
325  self::assertEquals('http', $this->request->getProtocol());
326  self::assertEquals(
327  array(),
329  );
330  }
331 
336  $_SERVER = array(
337  'HTTPS' => 'OFF',
338  'SCRIPT_NAME' => '/index.php',
339  );
340  self::assertEquals('http', $this->request->getProtocol());
341  }
342 
346  public function testGetProtocolHttpsVariableOn() {
347  $_SERVER = array(
348  'HTTPS' => 'ON',
349  'SCRIPT_NAME' => '/index.php',
350  );
351  self::assertEquals('https', $this->request->getProtocol());
352  }
353 
357  public function testTrustXForwardedForOn() {
358  list($forwardedIp, $remoteIp) = $this->getRemoteAddrTestPrepare(
359  array('trust_x_forwarded_for' => true)
360  );
361  self::assertEquals($forwardedIp, $this->request->getRemoteAddr());
362  }
363 
367  public function testTrustXForwardedForOff() {
368  list($forwardedIp, $remoteIp) = $this->getRemoteAddrTestPrepare(
369  array('trust_x_forwarded_for' => false)
370  );
371  self::assertEquals($remoteIp, $this->request->getRemoteAddr());
372  }
373 
377  public function testTrustXForwardedForNotSet() {
378  list($forwardedIp, $remoteIp) = $this->getRemoteAddrTestPrepare(array());
379  self::assertEquals($forwardedIp, $this->request->getRemoteAddr());
380  }
381 
382 
390  private function getRemoteAddrTestPrepare($generalConfigData = array()) {
391  // Remove cached IP address from registry
392  Registry::delete('remoteIpAddr');
393 
394  $_SERVER['HTTP_X_FORWARDED_FOR'] = '1.1.1.1';
395  $_SERVER['REMOTE_ADDR'] = '2.2.2.2';
396 
397  $configData =& Registry::get('configData', true, array());
398  $configData['general'] = $generalConfigData;
399 
400  return array($_SERVER['HTTP_X_FORWARDED_FOR'], $_SERVER['REMOTE_ADDR']);
401  }
402 
406  public function testGetUserVar() {
407  $_GET = array(
408  'par1' => '\'val1\'',
409  'par2' => ' val2'
410  );
411  $_POST = array(
412  'par3' => 'val3 ',
413  'par4' => 'val4'
414  );
415  self::assertEquals("'val1'", $this->request->getUserVar('par1'));
416  self::assertEquals('val2', $this->request->getUserVar('par2'));
417  self::assertEquals('val3', $this->request->getUserVar('par3'));
418  self::assertEquals('val4', $this->request->getUserVar('par4'));
419  }
420 
424  public function testGetUserVars() {
425  $_GET = array(
426  'par1' => '\'val1\'',
427  'par2' => ' val2'
428  );
429  $_POST = array(
430  'par3' => 'val3 ',
431  'par4' => 'val4'
432  );
433  $expectedResult = array(
434  'par1' => "'val1'",
435  'par2' => 'val2',
436  'par3' => 'val3',
437  'par4' => 'val4'
438  );
439  self::assertEquals($expectedResult, $this->request->getUserVars());
440  }
441 }
442 
PKPRequestTest\testGetServerHostWithHostname
testGetServerHostWithHostname()
Definition: PKPRequestTest.php:259
PKPRequestTest\testGetServerHostLocalhost
testGetServerHostLocalhost()
Definition: PKPRequestTest.php:247
PKPRequestTest\testGetProtocolNoHttpsVariable
testGetProtocolNoHttpsVariable()
Definition: PKPRequestTest.php:313
PKPRequestTest\testTrustXForwardedForOff
testTrustXForwardedForOff()
Definition: PKPRequestTest.php:367
HookRegistry\resetCalledHooks
static resetCalledHooks($leaveAlive=false)
Definition: HookRegistry.inc.php:143
Registry\delete
static delete($key)
Definition: Registry.inc.php:62
PKPRequestTest\testIsRestfulUrlsEnabled1
testIsRestfulUrlsEnabled1()
Definition: PKPRequestTest.php:66
PKPRequestTest\testTrustXForwardedForNotSet
testTrustXForwardedForNotSet()
Definition: PKPRequestTest.php:377
PKPRequestTest\testGetServerHostWithServerName
testGetServerHostWithServerName()
Definition: PKPRequestTest.php:272
PKPRequestTest\testGetUserVars
testGetUserVars()
Definition: PKPRequestTest.php:424
HookRegistry\rememberCalledHooks
static rememberCalledHooks($askOnly=false, $updateTo=true)
Definition: HookRegistry.inc.php:128
PKPRequestTest\testGetRequestPathWithoutPathinfo
testGetRequestPathWithoutPathinfo()
Definition: PKPRequestTest.php:234
HookRegistry\clear
static clear($hookName)
Definition: HookRegistry.inc.php:58
PKPTestCase
Class that implements functionality common to all PKP unit test cases.
Definition: PKPTestCase.inc.php:27
PKPRequestTest\testGetProtocolHttpsVariableOn
testGetProtocolHttpsVariableOn()
Definition: PKPRequestTest.php:346
PKPRequestTest\testIsRestfulUrlsEnabled2
testIsRestfulUrlsEnabled2()
Definition: PKPRequestTest.php:74
Registry\set
static set($key, &$value)
Definition: Registry.inc.php:53
PKPRequestTest
Tests for the PKPRequest class.
Definition: PKPRequestTest.php:27
PKPRequestTest\testIsPathInfoEnabled1
testIsPathInfoEnabled1()
Definition: PKPRequestTest.php:50
PKPRequestTest\testGetBaseUrlWithHostDetection
testGetBaseUrlWithHostDetection()
Definition: PKPRequestTest.php:134
HookRegistry\getCalledHooks
static & getCalledHooks()
Definition: HookRegistry.inc.php:153
PKPRequestTest\testTrustXForwardedForOn
testTrustXForwardedForOn()
Definition: PKPRequestTest.php:357
PKPRequest
Class providing operations associated with HTTP requests.
Definition: PKPRequest.inc.php:17
PKPRequestTest\testGetBasePath
testGetBasePath()
Definition: PKPRequestTest.php:146
PKPRequestTest\testGetRequestPathRestful
testGetRequestPathRestful()
Definition: PKPRequestTest.php:208
PKPRequestTest\testGetRequestPathWithPathinfo
testGetRequestPathWithPathinfo()
Definition: PKPRequestTest.php:221
Registry\get
static & get($key, $createIfEmpty=false, $createWithDefault=null)
Definition: Registry.inc.php:35
PKPRequestTest\setUp
setUp()
Definition: PKPRequestTest.php:31
PKPTestCase\setTestConfiguration
setTestConfiguration($config, $configPath='config')
Definition: PKPTestCase.inc.php:113
PKPRequestTest\testGetBaseUrl
testGetBaseUrl()
Definition: PKPRequestTest.php:105
PKPRequestTest\testGetEmptyBasePath
testGetEmptyBasePath()
Definition: PKPRequestTest.php:171
PKPRequestTest\testRedirectUrl
testRedirectUrl()
Definition: PKPRequestTest.php:82
PKPRequestTest\testGetServerHostWithHttpHost
testGetServerHostWithHttpHost()
Definition: PKPRequestTest.php:285
PKPRequestTest\testGetServerHostWithHttpXForwardedHost
testGetServerHostWithHttpXForwardedHost()
Definition: PKPRequestTest.php:299
PKPRequestTest\$request
$request
Definition: PKPRequestTest.php:28
PKPRequestTest\testGetRequestPath
testGetRequestPath()
Definition: PKPRequestTest.php:181
HookRegistry\register
static register($hookName, $callback, $hookSequence=HOOK_SEQUENCE_NORMAL)
Definition: HookRegistry.inc.php:70
PKPRequestTest\testIsPathInfoEnabled2
testIsPathInfoEnabled2()
Definition: PKPRequestTest.php:58
PKPRequestTest\tearDown
tearDown()
Definition: PKPRequestTest.php:40
PKPRequestTest\redirectUrlHook
redirectUrlHook($hookName, $args)
Definition: PKPRequestTest.php:97
PKPRequestTest\testGetProtocolHttpsVariableOff
testGetProtocolHttpsVariableOff()
Definition: PKPRequestTest.php:335
PKPRequestTest\testGetUserVar
testGetUserVar()
Definition: PKPRequestTest.php:406