From db71e63467a26afb8265979f8c8071935ec349b4 Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Fri, 25 Mar 2011 19:26:21 +0100 Subject: begin with ajax unittests - but they do not work yet --- tests/TestBaseApi.php | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'tests/TestBaseApi.php') diff --git a/tests/TestBaseApi.php b/tests/TestBaseApi.php index dacdecd..b7c1921 100644 --- a/tests/TestBaseApi.php +++ b/tests/TestBaseApi.php @@ -84,6 +84,9 @@ class TestBaseApi extends TestBase * the request object with authentication details, so that * the user is logged in. * + * Useful for HTTP API methods only, cannot be used with + * "normal" HTML pages since they do not support HTTP auth. + * * @param string $urlSuffix Suffix for the URL * @param mixed $auth If user authentication is needed (true/false) * or array with username and password @@ -109,5 +112,38 @@ class TestBaseApi extends TestBase return array($req, $uid); } + + + /** + * Creates a user and a HTTP_Request2 object, does a normal login + * and prepares the cookies for the HTTP request object so that + * the user is seen as logged in when requesting any HTML page. + * + * Useful for testing HTML pages or ajax URLs. + * + * @param string $urlSuffix Suffix for the URL + * @param mixed $auth If user authentication is needed (true/false) + * or array with username and password + * + * @return array(HTTP_Request2, integer) HTTP request object and user id + * + * @uses getRequest() + */ + protected function getLoggedInRequest($urlSuffix = null, $auth = true) + { + $req = $this->getRequest($urlSuffix); + if (is_array($auth)) { + list($username, $password) = $auth; + } else { + $username = 'testuser'; + $password = 'testpassword'; + } + $uid = $this->addUser($username, $password); + + //FIXME: login via the login form, check if it worked + //FIXME: prepare new request with cookie + return array($req, $uid); + } + } ?> \ No newline at end of file -- cgit v1.2.3-54-g00ecf From 0f9d06c6644f634b436549f0d5e56fc96ebd361c Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Sat, 26 Mar 2011 10:00:18 +0100 Subject: first working unittest for ajax/getcontacttags with login! --- tests/TestBaseApi.php | 19 ++++++++++++++++--- tests/ajax/GetContactTagsTest.php | 20 ++++++++++++-------- 2 files changed, 28 insertions(+), 11 deletions(-) (limited to 'tests/TestBaseApi.php') diff --git a/tests/TestBaseApi.php b/tests/TestBaseApi.php index b7c1921..2af228a 100644 --- a/tests/TestBaseApi.php +++ b/tests/TestBaseApi.php @@ -131,7 +131,6 @@ class TestBaseApi extends TestBase */ protected function getLoggedInRequest($urlSuffix = null, $auth = true) { - $req = $this->getRequest($urlSuffix); if (is_array($auth)) { list($username, $password) = $auth; } else { @@ -140,8 +139,22 @@ class TestBaseApi extends TestBase } $uid = $this->addUser($username, $password); - //FIXME: login via the login form, check if it worked - //FIXME: prepare new request with cookie + $req = new HTTP_Request2( + $GLOBALS['unittestUrl'] . '/login.php', + HTTP_Request2::METHOD_POST + ); + $cookies = $req->setCookieJar()->getCookieJar(); + $req->addPostParameter('username', $username); + $req->addPostParameter('password', $password); + $req->addPostParameter('submitted', 'Log In'); + $res = $req->send(); + + //after login, we normally get redirected + $this->assertEquals(302, $res->getStatus(), 'Login failure'); + + $req = $this->getRequest($urlSuffix); + $req->setCookieJar($cookies); + return array($req, $uid); } diff --git a/tests/ajax/GetContactTagsTest.php b/tests/ajax/GetContactTagsTest.php index 7f46888..757dce9 100644 --- a/tests/ajax/GetContactTagsTest.php +++ b/tests/ajax/GetContactTagsTest.php @@ -47,13 +47,17 @@ class ajax_GetContactTagsTest extends TestBaseApi } - public function testUserLoggedIn() + public function testUserLoggedInWatchlist() { - list($req, $uId) = $this->getAuthRequest(); - $this->addBookmark($uId, null, 0, array('public')); - $this->addBookmark($uId, null, 1, array('shared')); - $this->addBookmark($uId, null, 2, array('private')); - + list($req, $uId) = $this->getLoggedInRequest(); + $this->addBookmark($uId, null, 0, array('public', 'public2')); + + $user2 = $this->addUser(); + $this->us->setCurrentUserId($uId); + $this->us->setWatchStatus($user2); + //uId watches user2 now + $this->addBookmark($user2, null, 0, array('user2tag')); + $res = $req->send(); $this->assertEquals(200, $res->getStatus()); $this->assertEquals( @@ -64,8 +68,8 @@ class ajax_GetContactTagsTest extends TestBaseApi $this->assertInternalType('array', $data); $this->assertEquals(3, count($data)); $this->assertContains('public', $data); - $this->assertContains('shared', $data); - $this->assertContains('private', $data); + $this->assertContains('public2', $data); + $this->assertContains('user2tag', $data); } } -- cgit v1.2.3-54-g00ecf From a756799ef4df0449244d41c2ecc7133c3fb8ce70 Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Sat, 26 Mar 2011 17:04:00 +0100 Subject: move setUnittestConfig to TestBaseApi since it makes only sense to use it there --- tests/TestBase.php | 44 ---------------------------- tests/TestBaseApi.php | 44 ++++++++++++++++++++++++++++ tests/ajax/GetAdminTagsTest.php | 63 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+), 44 deletions(-) create mode 100644 tests/ajax/GetAdminTagsTest.php (limited to 'tests/TestBaseApi.php') diff --git a/tests/TestBase.php b/tests/TestBase.php index b85c189..095f32d 100644 --- a/tests/TestBase.php +++ b/tests/TestBase.php @@ -22,18 +22,6 @@ */ class TestBase extends PHPUnit_Framework_TestCase { - /** - * Clean up after test - */ - public function tearDown() - { - if (file_exists($GLOBALS['datadir'] . '/config.unittest.php')) { - unlink($GLOBALS['datadir'] . '/config.unittest.php'); - } - } - - - /** * Create a new bookmark. * @@ -158,38 +146,6 @@ class TestBase extends PHPUnit_Framework_TestCase return $uid; } - - - /** - * Writes a special unittest configuration file. - * The unittest config file is read when a GET request with unittestMode=1 - * is sent, and the user allowed unittestmode in config.php. - * - * @param array $arConfig Array with config names as key and their value as - * value - * - * @return void - */ - protected function setUnittestConfig($arConfig) - { - $str = '<' . "?php\r\n"; - foreach ($arConfig as $name => $value) { - $str .= '$' . $name . ' = ' - . var_export($value, true) . ";\n"; - } - - if (!is_dir($GLOBALS['datadir'])) { - $this->fail( - 'datadir not set or not a directory: ' . $GLOBALS['datadir'] - ); - } - - $this->assertInternalType( - 'integer', - file_put_contents($GLOBALS['datadir'] . '/config.unittest.php', $str), - 'Writing config.unittest.php failed' - ); - } } ?> \ No newline at end of file diff --git a/tests/TestBaseApi.php b/tests/TestBaseApi.php index 2af228a..2341ea1 100644 --- a/tests/TestBaseApi.php +++ b/tests/TestBaseApi.php @@ -57,6 +57,18 @@ class TestBaseApi extends TestBase + /** + * Clean up after test + */ + public function tearDown() + { + if (file_exists($GLOBALS['datadir'] . '/config.unittest.php')) { + unlink($GLOBALS['datadir'] . '/config.unittest.php'); + } + } + + + /** * Gets a HTTP request object. * Uses $this->url plus $urlSuffix as request URL. @@ -158,5 +170,37 @@ class TestBaseApi extends TestBase return array($req, $uid); } + + + /** + * Writes a special unittest configuration file. + * The unittest config file is read when a GET request with unittestMode=1 + * is sent, and the user allowed unittestmode in config.php. + * + * @param array $arConfig Array with config names as key and their value as + * value + * + * @return void + */ + protected function setUnittestConfig($arConfig) + { + $str = '<' . "?php\r\n"; + foreach ($arConfig as $name => $value) { + $str .= '$' . $name . ' = ' + . var_export($value, true) . ";\n"; + } + + if (!is_dir($GLOBALS['datadir'])) { + $this->fail( + 'datadir not set or not a directory: ' . $GLOBALS['datadir'] + ); + } + + $this->assertInternalType( + 'integer', + file_put_contents($GLOBALS['datadir'] . '/config.unittest.php', $str), + 'Writing config.unittest.php failed' + ); + } } ?> \ No newline at end of file diff --git a/tests/ajax/GetAdminTagsTest.php b/tests/ajax/GetAdminTagsTest.php new file mode 100644 index 0000000..5c941e8 --- /dev/null +++ b/tests/ajax/GetAdminTagsTest.php @@ -0,0 +1,63 @@ + + * @author Christian Weiske + * @author Eric Dane + * @license GPL http://www.gnu.org/licenses/gpl.html + * @link http://sourceforge.net/projects/semanticscuttle + */ + +require_once dirname(__FILE__) . '/../prepare.php'; +require_once 'HTTP/Request2.php'; + +/** + * Unit tests for the ajax getadmintags.php script + * + * @category Bookmarking + * @package SemanticScuttle + * @author Christian Weiske + * @license GPL http://www.gnu.org/licenses/gpl.html + * @link http://sourceforge.net/projects/semanticscuttle + */ +class ajax_GetAdminTagsTest extends TestBaseApi +{ + protected $urlPart = 'ajax/getadmintags.php'; + + + public function testTags() + { + list($user1, $uname1) = $this->addUserData(); + $user2 = $this->addUser(); + $this->addBookmark($user1, null, 0, array('admintag', 'admintag2')); + $this->addBookmark($user2, null, 0, array('lusertag', 'lusertag2')); + + $this->setUnittestConfig( + array( + 'admin_users' => array($uname1) + ) + ); + + $req = $this->getRequest('?unittestMode=1'); + $res = $req->send(); + $this->assertEquals(200, $res->getStatus()); + $this->assertEquals( + 'application/json; charset=utf-8', + $res->getHeader('content-type') + ); + $data = json_decode($res->getBody()); + $this->assertInternalType('array', $data); + $this->assertEquals(2, count($data)); + $this->assertContains('admintag', $data); + $this->assertContains('admintag2', $data); + } + +} + + +?> \ No newline at end of file -- cgit v1.2.3-54-g00ecf From a5bd7789297378be3fb8dc3814c6649ea82a88d8 Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Mon, 28 Mar 2011 08:34:12 +0200 Subject: introduce assertResponseJson200 for API/ajax tests and use it --- tests/TestBaseApi.php | 19 +++++++++++++++++++ tests/ajax/GetAdminLinkedTagsTest.php | 20 +++----------------- tests/ajax/GetAdminTagsTest.php | 30 +++++------------------------- tests/ajax/GetContactTagsTest.php | 26 +++++--------------------- 4 files changed, 32 insertions(+), 63 deletions(-) (limited to 'tests/TestBaseApi.php') diff --git a/tests/TestBaseApi.php b/tests/TestBaseApi.php index 2341ea1..f054973 100644 --- a/tests/TestBaseApi.php +++ b/tests/TestBaseApi.php @@ -172,6 +172,25 @@ class TestBaseApi extends TestBase + /** + * Verifies that the HTTP response has status code 200 and + * content-type application/json; charset=utf-8 + * + * @param HTTP_Request2_Response $res HTTP Response object + * + * @return void + */ + protected function assertResponseJson200(HTTP_Request2_Response $res) + { + $this->assertEquals(200, $res->getStatus()); + $this->assertEquals( + 'application/json; charset=utf-8', + $res->getHeader('content-type') + ); + } + + + /** * Writes a special unittest configuration file. * The unittest config file is read when a GET request with unittestMode=1 diff --git a/tests/ajax/GetAdminLinkedTagsTest.php b/tests/ajax/GetAdminLinkedTagsTest.php index 2552ea0..aded834 100644 --- a/tests/ajax/GetAdminLinkedTagsTest.php +++ b/tests/ajax/GetAdminLinkedTagsTest.php @@ -58,13 +58,7 @@ class ajax_GetAdminLinkedTagsTest extends TestBaseApi { $req = $this->getRequest(); $res = $req->send(); - - $this->assertEquals(200, $res->getStatus()); - $this->assertEquals( - 'application/json; charset=utf-8', - $res->getHeader('content-type') - ); - + $this->assertResponseJson200($res); $data = json_decode($res->getBody()); $this->assertInternalType('array', $data); @@ -99,11 +93,7 @@ class ajax_GetAdminLinkedTagsTest extends TestBaseApi ); $res = $this->getRequest('?tag=' . $menu2Tag)->send(); - $this->assertEquals(200, $res->getStatus()); - $this->assertEquals( - 'application/json; charset=utf-8', - $res->getHeader('content-type') - ); + $this->assertResponseJson200($res); $data = json_decode($res->getBody()); $this->assertInternalType('array', $data); @@ -139,11 +129,7 @@ class ajax_GetAdminLinkedTagsTest extends TestBaseApi ); $res = $this->getRequest('?tag=' . $menu2Tag)->send(); - $this->assertEquals(200, $res->getStatus()); - $this->assertEquals( - 'application/json; charset=utf-8', - $res->getHeader('content-type') - ); + $this->assertResponseJson200($res); $data = json_decode($res->getBody()); $this->assertInternalType('array', $data); diff --git a/tests/ajax/GetAdminTagsTest.php b/tests/ajax/GetAdminTagsTest.php index 6afe45c..80d702f 100644 --- a/tests/ajax/GetAdminTagsTest.php +++ b/tests/ajax/GetAdminTagsTest.php @@ -45,11 +45,7 @@ class ajax_GetAdminTagsTest extends TestBaseApi $req = $this->getRequest('?unittestMode=1'); $res = $req->send(); - $this->assertEquals(200, $res->getStatus()); - $this->assertEquals( - 'application/json; charset=utf-8', - $res->getHeader('content-type') - ); + $this->assertResponseJson200($res); $data = json_decode($res->getBody()); $this->assertInternalType('array', $data); $this->assertEquals(2, count($data)); @@ -70,12 +66,8 @@ class ajax_GetAdminTagsTest extends TestBaseApi $req = $this->getRequest('?unittestMode=1&beginsWith=foo'); $res = $req->send(); - $this->assertEquals(200, $res->getStatus()); - $this->assertEquals( - 'application/json; charset=utf-8', - $res->getHeader('content-type') - ); $data = json_decode($res->getBody()); + $this->assertResponseJson200($res); $this->assertInternalType('array', $data); $this->assertEquals(2, count($data)); $this->assertContains('foo', $data); @@ -99,11 +91,7 @@ class ajax_GetAdminTagsTest extends TestBaseApi $req = $this->getRequest('?unittestMode=1&limit=1'); $res = $req->send(); - $this->assertEquals(200, $res->getStatus()); - $this->assertEquals( - 'application/json; charset=utf-8', - $res->getHeader('content-type') - ); + $this->assertResponseJson200($res); $data = json_decode($res->getBody()); $this->assertInternalType('array', $data); $this->assertEquals(1, count($data)); @@ -111,11 +99,7 @@ class ajax_GetAdminTagsTest extends TestBaseApi $req = $this->getRequest('?unittestMode=1&limit=2'); $res = $req->send(); - $this->assertEquals(200, $res->getStatus()); - $this->assertEquals( - 'application/json; charset=utf-8', - $res->getHeader('content-type') - ); + $this->assertResponseJson200($res); $data = json_decode($res->getBody()); $this->assertInternalType('array', $data); $this->assertEquals(2, count($data)); @@ -123,11 +107,7 @@ class ajax_GetAdminTagsTest extends TestBaseApi $req = $this->getRequest('?unittestMode=1&limit=3'); $res = $req->send(); - $this->assertEquals(200, $res->getStatus()); - $this->assertEquals( - 'application/json; charset=utf-8', - $res->getHeader('content-type') - ); + $this->assertResponseJson200($res); $data = json_decode($res->getBody()); $this->assertInternalType('array', $data); $this->assertEquals(3, count($data)); diff --git a/tests/ajax/GetContactTagsTest.php b/tests/ajax/GetContactTagsTest.php index 682173b..559040f 100644 --- a/tests/ajax/GetContactTagsTest.php +++ b/tests/ajax/GetContactTagsTest.php @@ -36,11 +36,7 @@ class ajax_GetContactTagsTest extends TestBaseApi public function testNoUserLoggedIn() { $res = $this->getRequest()->send(); - $this->assertEquals(200, $res->getStatus()); - $this->assertEquals( - 'application/json; charset=utf-8', - $res->getHeader('content-type') - ); + $this->assertResponseJson200($res); $data = json_decode($res->getBody()); $this->assertInternalType('array', $data); $this->assertEquals(0, count($data)); @@ -59,11 +55,7 @@ class ajax_GetContactTagsTest extends TestBaseApi $this->addBookmark($user2, null, 0, array('user2tag')); $res = $req->send(); - $this->assertEquals(200, $res->getStatus()); - $this->assertEquals( - 'application/json; charset=utf-8', - $res->getHeader('content-type') - ); + $this->assertResponseJson200($res); $data = json_decode($res->getBody()); $this->assertInternalType('array', $data); $this->assertEquals(3, count($data)); @@ -78,11 +70,7 @@ class ajax_GetContactTagsTest extends TestBaseApi $this->addBookmark($uId, null, 0, array('foobar', 'barmann')); $res = $req->send(); - $this->assertEquals(200, $res->getStatus()); - $this->assertEquals( - 'application/json; charset=utf-8', - $res->getHeader('content-type') - ); + $this->assertResponseJson200($res); $data = json_decode($res->getBody()); $this->assertInternalType('array', $data); $this->assertEquals(1, count($data)); @@ -95,11 +83,7 @@ class ajax_GetContactTagsTest extends TestBaseApi $this->addBookmark($uId, null, 0, array('foo', 'bar', 'baz', 'omg')); $res = $req->send(); - $this->assertEquals(200, $res->getStatus()); - $this->assertEquals( - 'application/json; charset=utf-8', - $res->getHeader('content-type') - ); + $this->assertResponseJson200($res); $data = json_decode($res->getBody()); $this->assertInternalType('array', $data); $this->assertEquals(2, count($data)); @@ -107,7 +91,7 @@ class ajax_GetContactTagsTest extends TestBaseApi $req2 = $this->getRequest('?limit=3'); $req2->setCookieJar($req->getCookieJar()); $res = $req2->send(); - $this->assertEquals(200, $res->getStatus()); + $this->assertResponseJson200($res); $data = json_decode($res->getBody()); $this->assertInternalType('array', $data); $this->assertEquals(3, count($data)); -- cgit v1.2.3-54-g00ecf From 542824a9ba6da477c0e1969b1c8815d0561dfcdf Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Thu, 14 Apr 2011 16:53:09 +0200 Subject: include HTTP_Request2 in api base file --- tests/TestBaseApi.php | 2 ++ 1 file changed, 2 insertions(+) (limited to 'tests/TestBaseApi.php') diff --git a/tests/TestBaseApi.php b/tests/TestBaseApi.php index f054973..8ed41cd 100644 --- a/tests/TestBaseApi.php +++ b/tests/TestBaseApi.php @@ -11,6 +11,8 @@ * @link http://sourceforge.net/projects/semanticscuttle */ +require_once 'HTTP/Request2.php'; + /** * Base unittest class for web API tests. * -- cgit v1.2.3-54-g00ecf