diff --git a/_test/core/TestRequest.php b/_test/core/TestRequest.php index 66760b1e0cb38f848dff6b7392175ad700408675..aeda4b8920bb23dd9c1200bdd8fad5f6a6f12bf1 100644 --- a/_test/core/TestRequest.php +++ b/_test/core/TestRequest.php @@ -84,4 +84,69 @@ class TestRequest { return $response; } + + /** + * Set the virtual URI the request works against + * + * This parses the given URI and sets any contained GET variables + * but will not overwrite any previously set ones (eg. set via setGet()). + * + * It initializes the $_SERVER['REQUEST_URI'] and $_SERVER['QUERY_STRING'] + * with all set GET variables. + * + * @param string $url end URL to simulate, needs to start with /doku.php currently + */ + public function setUri($uri){ + if(substr($uri,0,9) != '/doku.php'){ + throw new Exception("only '/doku.php' is supported currently"); + } + + $params = array(); + list($uri, $query) = explode('?',$uri,2); + if($query) parse_str($query, $params); + + $this->get = array_merge($params, $this->get); + if(count($this->get)){ + $query = '?'.http_build_query($this->get, '', '&'); + $query = str_replace( + array('%3A', '%5B', '%5D'), + array(':', '[', ']'), + $query + ); + $uri = $uri.$query; + } + + $this->setServer('QUERY_STRING', $query); + $this->setServer('REQUEST_URI', $uri); + } + + /** + * Simulate a POST request with the given variables + * + * @param array $post all the POST parameters to use + * @param string $url end URL to simulate, needs to start with /doku.php currently + * @param return TestResponse + */ + public function post($post=array(), $uri='/doku.php') { + $this->setUri($uri); + $this->post = array_merge($this->post, $post); + $this->setServer('REQUEST_METHOD', 'POST'); + return $this->execute(); + } + + /** + * Simulate a GET request with the given variables + * + * @param array $GET all the POST parameters to use + * @param string $url end URL to simulate, needs to start with /doku.php currently + * @param return TestResponse + */ + public function get($get=array(), $uri='/doku.php') { + $this->get = array_merge($this->get, $get); + $this->setUri($uri); + $this->setServer('REQUEST_METHOD', 'GET'); + return $this->execute(); + } + + } diff --git a/_test/tests/test/basic.test.php b/_test/tests/test/basic.test.php index b4926d2ba522e80a67776c5b50708a5e0bc635e4..a0ea48a3a37cf69e3d21669ae1931ba4d797a16c 100644 --- a/_test/tests/test/basic.test.php +++ b/_test/tests/test/basic.test.php @@ -19,4 +19,87 @@ class InttestsBasicTest extends DokuWikiTest { 'DokuWiki was not a word in the output' ); } + + function testPost() { + $request = new TestRequest(); + + $input = array( + 'string' => 'A string', + 'array' => array(1, 2, 3), + 'id' => 'wiki:dokuwiki' + ); + + $response = $request->post($input); + + // server var check + $this->assertEquals('POST',$request->getServer('REQUEST_METHOD')); + $this->assertEquals('',$request->getServer('QUERY_STRING')); + $this->assertEquals('/doku.php',$request->getServer('REQUEST_URI')); + + // variable setup check + $this->assertEquals('A string', $request->getPost('string')); + $this->assertEquals(array(1, 2, 3), $request->getPost('array')); + $this->assertEquals('wiki:dokuwiki', $request->getPost('id')); + + // output check + $this->assertTrue(strpos($response->getContent(), 'Andreas Gohr') >= 0); + } + + function testPostGet() { + $request = new TestRequest(); + + $input = array( + 'string' => 'A string', + 'array' => array(1, 2, 3), + ); + + $response = $request->post($input,'/doku.php?id=wiki:dokuwiki'); + + // server var check + $this->assertEquals('POST',$request->getServer('REQUEST_METHOD')); + $this->assertEquals('?id=wiki:dokuwiki',$request->getServer('QUERY_STRING')); + $this->assertEquals('/doku.php?id=wiki:dokuwiki',$request->getServer('REQUEST_URI')); + + // variable setup check + $this->assertEquals('A string', $request->getPost('string')); + $this->assertEquals(array(1, 2, 3), $request->getPost('array')); + $this->assertEquals('wiki:dokuwiki', $request->getGet('id')); + + // output check + $this->assertTrue(strpos($response->getContent(), 'Andreas Gohr') >= 0); + } + + function testGet() { + $request = new TestRequest(); + + $input = array( + 'string' => 'A string', + 'array' => array(1, 2, 3), + 'test' => 'bar' + ); + + $response = $request->get($input,'/doku.php?id=wiki:dokuwiki&test=foo'); + + // server var check + $this->assertEquals('GET',$request->getServer('REQUEST_METHOD')); + $this->assertEquals( + '?id=wiki:dokuwiki&test=bar&string=A+string&array[0]=1&array[1]=2&array[2]=3', + $request->getServer('QUERY_STRING') + ); + $this->assertEquals( + '/doku.php?id=wiki:dokuwiki&test=bar&string=A+string&array[0]=1&array[1]=2&array[2]=3', + $request->getServer('REQUEST_URI') + ); + + // variable setup check + $this->assertEquals('A string', $request->getGet('string')); + $this->assertEquals(array(1, 2, 3), $request->getGet('array')); + $this->assertEquals('wiki:dokuwiki', $request->getGet('id')); + $this->assertEquals('bar', $request->getGet('test')); + + // output check + $this->assertTrue(strpos($response->getContent(), 'Andreas Gohr') >= 0); + } + + }