From cba0776325ac82bc255feb9a44b3bf312b02f6dc Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Wed, 6 Apr 2011 08:43:42 +0200 Subject: cherry-pick: move setUnittestConfig to TestBaseApi since it makes only sense to use it there Conflicts: tests/TestBaseApi.php --- tests/TestBaseApi.php | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) (limited to 'tests/TestBaseApi.php') diff --git a/tests/TestBaseApi.php b/tests/TestBaseApi.php index dacdecd..73135fb 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. @@ -109,5 +121,83 @@ 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) + { + if (is_array($auth)) { + list($username, $password) = $auth; + } else { + $username = 'testuser'; + $password = 'testpassword'; + } + $uid = $this->addUser($username, $password); + + $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); + } + + + + /** + * 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 -- cgit v1.2.3 From 366987a97d41f1a276dfd43921a2fcf1f6e70f00 Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Wed, 6 Apr 2011 09:50:42 +0200 Subject: move unittest config deletion to setup since that makes it easier to debug, and does not break config when the test fatal errored --- tests/TestBaseApi.php | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) (limited to 'tests/TestBaseApi.php') diff --git a/tests/TestBaseApi.php b/tests/TestBaseApi.php index 73135fb..9081a4a 100644 --- a/tests/TestBaseApi.php +++ b/tests/TestBaseApi.php @@ -47,6 +47,11 @@ class TestBaseApi extends TestBase } $this->url = $GLOBALS['unittestUrl'] . $this->urlPart; + //clean up before test + if (file_exists($GLOBALS['datadir'] . '/config.unittest.php')) { + unlink($GLOBALS['datadir'] . '/config.unittest.php'); + } + $this->us = SemanticScuttle_Service_Factory::get('User'); $this->us->deleteAll(); $this->bs = SemanticScuttle_Service_Factory::get('Bookmark'); @@ -57,18 +62,6 @@ 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. -- cgit v1.2.3 From 5d5ca9efdd31fee53c52c601bdf7511bd67ff655 Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Mon, 18 Apr 2011 19:20:58 +0200 Subject: clarify documentation; we are returning get requests --- tests/TestBaseApi.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'tests/TestBaseApi.php') diff --git a/tests/TestBaseApi.php b/tests/TestBaseApi.php index 9081a4a..b381dad 100644 --- a/tests/TestBaseApi.php +++ b/tests/TestBaseApi.php @@ -63,7 +63,7 @@ class TestBaseApi extends TestBase /** - * Gets a HTTP request object. + * Creates and returns a HTTP GET request object. * Uses $this->url plus $urlSuffix as request URL. * * @param string $urlSuffix Suffix for the URL @@ -85,7 +85,7 @@ class TestBaseApi extends TestBase /** - * Creates a user and a HTTP request object and prepares + * Creates a user and a HTTP GET request object and prepares * the request object with authentication details, so that * the user is logged in. * @@ -118,7 +118,7 @@ class TestBaseApi extends TestBase /** * Creates a user and a HTTP_Request2 object, does a normal login - * and prepares the cookies for the HTTP request object so that + * and prepares the cookies for the HTTP GET request object so that * the user is seen as logged in when requesting any HTML page. * * Useful for testing HTML pages or ajax URLs. -- cgit v1.2.3 From f629d081ddf52e3cb83ffbfc973a97adc691790c Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Mon, 2 May 2011 09:03:35 +0200 Subject: make privacy tests clearer --- tests/Api/PostsAddTest.php | 75 +++++++++++++++++----------------------------- tests/TestBaseApi.php | 3 ++ 2 files changed, 31 insertions(+), 47 deletions(-) (limited to 'tests/TestBaseApi.php') diff --git a/tests/Api/PostsAddTest.php b/tests/Api/PostsAddTest.php index ddaa1fd..3c1177f 100644 --- a/tests/Api/PostsAddTest.php +++ b/tests/Api/PostsAddTest.php @@ -423,8 +423,8 @@ TXT; /** - * Test that a default privacy setting of 2 (Private) is used in adding - * a bookmark. + * Test that a default privacy setting of 2 (Private) is used in adding + * a bookmark. */ public function testDefaultPrivacyPrivate() { @@ -445,8 +445,8 @@ TXT; /** - * Test that a default privacy setting of 0 (Public) is used in adding - * a bookmark. + * Test that a default privacy setting of 0 (Public) is used in adding + * a bookmark. */ public function testDefaultPrivacyPublic() { @@ -490,7 +490,7 @@ TXT; $bm = reset($bms['bookmarks']); $bmId = $bm['bId']; - $reqUrl = $GLOBALS['unittestUrl'] . 'edit.php/' . $bmId . '?unittestMode=1'; + $reqUrl = $GLOBALS['unittestUrl'] . 'edit.php/' . $bmId . '?unittestMode=1'; $req2 = new HTTP_Request2($reqUrl, HTTP_Request2::METHOD_POST); $req2->setCookieJar($cookies); $req2->addPostParameter('address', 'http://www.example.org/testdefaultprivacyposts_edit'); @@ -502,7 +502,7 @@ TXT; $bm = $this->bs->getBookmark($bmId); $this->assertEquals('2', $bm['bStatus']); - }//end testDefaultPrivacyEdit + }//end testDefaultPrivacyEdit /** @@ -550,15 +550,15 @@ TXT; $this->assertEquals(3, count($bms['bookmarks'])); $bm = reset($bms['bookmarks']); $this->assertEquals('2', $bm['bStatus']); - }//end testDefaultPrivacyImport + }//end testDefaultPrivacyImport /** - * Test that the default privacy setting is selected in the Privacy - * drop-down list when an existing bookmark is accessed with bookmarks.php - * and the get action. + * Test that the default privacy setting is selected in the Privacy + * drop-down list when adding a new bookmark, sending the form and + * missing the title and the privacy setting. */ - public function testDefaultPrivacyBookmarksGet() + public function testDefaultPrivacyBookmarksAddMissingTitleMissingPrivacy() { $this->setUnittestConfig( array('defaults' => array('privacy' => 2)) @@ -574,7 +574,7 @@ TXT; $bms = $this->bs->getBookmarks(0, null, $uId); $this->assertEquals(1, count($bms['bookmarks'])); $user = $this->us->getUser($uId); - $reqUrl = $GLOBALS['unittestUrl'] . 'bookmarks.php/' . $user['username'] . '?action=get' . '&unittestMode=1'; + $reqUrl = $GLOBALS['unittestUrl'] . 'bookmarks.php/' . $user['username'] . '?action=get' . '&unittestMode=1'; list($req, $uId) = $this->getAuthRequest('?unittestMode=1'); $req->setMethod(HTTP_Request2::METHOD_POST); @@ -595,51 +595,32 @@ TXT; /** - * Test that the default privacy setting is selected in the Privacy - * drop-down list when an existing bookmark is accessed with bookmarks.php - * and the add action. + * Test that the default privacy setting is selected in the Privacy + * drop-down list when a new bookmark is being created. */ public function testDefaultPrivacyBookmarksAdd() { $this->setUnittestConfig( array('defaults' => array('privacy' => 1)) ); - list($req, $uId) = $this->getAuthRequest('?unittestMode=1'); - $req->setMethod(HTTP_Request2::METHOD_POST); - $req->addPostParameter('url', 'http://www.example.org/testdefaultprivacyposts_bookmarksadd'); - $req->addPostParameter('description', 'Test bookmark 2 for default privacy.'); - $req->addPostParameter('status', '0'); - $req->send(); - $bms = $this->bs->getBookmarks(0, null, $uId); - $this->assertEquals(1, count($bms['bookmarks'])); - $bm = reset($bms['bookmarks']); - $bmId = $bm['bId']; - $oldUid = $uId; + list($req, $uId) = $this->getLoggedInRequest('?unittestMode=1'); + $user = $this->us->getUser($uId); - $userId = $user['username']; - $reqUrl = $GLOBALS['unittestUrl'] . 'bookmarks.php/' . $userId . '?action=add' . '&unittestMode=1'; - list($req, $uId) = $this->getAuthRequest('?unittestMode=1'); - $req->setMethod(HTTP_Request2::METHOD_POST); + $reqUrl = $GLOBALS['unittestUrl'] . 'bookmarks.php/' + . $user['username'] . '?action=add' . '&unittestMode=1'; $req->setUrl($reqUrl); - $testcookiekey = md5($GLOBALS['dbname'].$GLOBALS['tableprefix']).'-login'; - $userinfo = $this->us->getUser($oldUid); - $testcookiepassword = $userinfo['password']; - $testusername = $userinfo['username']; - $testcookievalue = $oldUid . ':' . md5($testusername . $testcookiepassword); - $req->setCookieJar(true); - $req->addCookie($testcookiekey, $testcookievalue); - $req->addPostParameter('submitted', '1'); + $req->setMethod(HTTP_Request2::METHOD_GET); $response = $req->send(); $response_body = $response->getBody(); - $start = strpos($response_body, 'Privacy'); - $end = strpos($response_body, 'referrer'); - $length = $end - $start; - $response_body = substr($response_body, $start, $length); - $start = strpos($response_body, 'selected'); - $start = $start - 3; - $length = 1; - $selected_privacy = substr($response_body, $start, $length); - $this->assertEquals('1', $selected_privacy); + $this->assertNotEquals('', $response_body, 'Response is empty'); + + $x = simplexml_load_string($response_body); + $ns = $x->getDocNamespaces(); + $x->registerXPathNamespace('ns', reset($ns)); + + $elements = $x->xpath('//ns:select[@name="status"]/ns:option[@selected="selected"]'); + $this->assertEquals(1, count($elements), 'No selected status option found'); + $this->assertEquals(1, (string)$elements[0]['value']); }//end testDefaultPrivacyBookmarksAdd diff --git a/tests/TestBaseApi.php b/tests/TestBaseApi.php index b381dad..9759db5 100644 --- a/tests/TestBaseApi.php +++ b/tests/TestBaseApi.php @@ -89,6 +89,8 @@ class TestBaseApi extends TestBase * the request object with authentication details, so that * the user is logged in. * + * Only usable for API requests, not "normal" HTTP page requests + * * @param string $urlSuffix Suffix for the URL * @param mixed $auth If user authentication is needed (true/false) * or array with username and password @@ -96,6 +98,7 @@ class TestBaseApi extends TestBase * @return array(HTTP_Request2, integer) HTTP request object and user id * * @uses getRequest() + * @see getLoggedInRequest() */ protected function getAuthRequest($urlSuffix = null, $auth = true) { -- cgit v1.2.3