From be126cc958212ed74fe916eabcb1e6ac928df5e4 Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Fri, 8 Oct 2010 17:43:05 +0200 Subject: tests for getadminlinkedtags --- tests/TestBase.php | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'tests/TestBase.php') diff --git a/tests/TestBase.php b/tests/TestBase.php index 402330b..c0acd58 100644 --- a/tests/TestBase.php +++ b/tests/TestBase.php @@ -104,6 +104,34 @@ class TestBase extends PHPUnit_Framework_TestCase return $uid; } + + + /** + * Retrieves the UID of an admin user. + * If that user does not exist in the database, it is created. + * + * @return integer UID of admin user + */ + protected function getAdminUser() + { + if (count($GLOBALS['admin_users']) == 0) { + $this->fail('No admin users configured'); + } + $adminUserName = reset($GLOBALS['admin_users']); + + $us = SemanticScuttle_Service_Factory::get('User'); + $uid = $us->getIdFromUser($adminUserName); + if ($uid === null) { + //that user does not exist in the database; create it + $uid = $us->addUser( + $adminUserName, + rand(), + 'unittest-admin-' . $adminUserName . '@example.org' + ); + } + + return $uid; + } } ?> \ No newline at end of file -- cgit v1.2.3-54-g00ecf From d4ccb1d3d3abf433200b39e8a5566a71bb1e6c2b Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Wed, 23 Mar 2011 08:23:36 +0100 Subject: first unit tests for Bookmark2Tag::getPopularTags --- tests/Bookmark2TagTest.php | 82 ++++++++++++++++++++++++++++++++++++++++++++++ tests/TestBase.php | 5 +-- 2 files changed, 85 insertions(+), 2 deletions(-) (limited to 'tests/TestBase.php') diff --git a/tests/Bookmark2TagTest.php b/tests/Bookmark2TagTest.php index 1823c60..6932a10 100644 --- a/tests/Bookmark2TagTest.php +++ b/tests/Bookmark2TagTest.php @@ -204,6 +204,88 @@ class Bookmark2TagTest extends TestBase } } } + + + /** + * Create a bookmark + * + * @param integer $user User ID the bookmark shall belong + * @param array $tags Array of tags to attach. If "null" is given, + * it will automatically be "unittest" + * @param string $date strtotime-compatible string + * @param string $title Bookmark title + * + * @return integer ID of bookmark + */ + protected function addTagBookmark($user, $tags, $date = null, $title = null) + { + return $this->addBookmark( + $user, null, 0, $tags, $title, $date + ); + } + + + + /** + * Fetch the most popular tags in descending order + */ + public function testGetPopularTagsOrder() + { + $user = $this->addUser(); + $this->addTagBookmark($user, array('one', 'two')); + $this->addTagBookmark($user, array('one', 'three')); + $this->addTagBookmark($user, array('one', 'two')); + + $arTags = $this->b2ts->getPopularTags(); + $this->assertInternalType('array', $arTags); + $this->assertEquals(3, count($arTags)); + + $this->assertInternalType('array', $arTags[0]); + + $this->assertEquals( + array( + array('tag' => 'one', 'bCount' => '3'), + array('tag' => 'two', 'bCount' => '2'), + array('tag' => 'three', 'bCount' => '1') + ), + $arTags + ); + } + + + + public function testGetPopularTagsLimit() + { + $user = $this->addUser(); + $this->addTagBookmark($user, array('one', 'two')); + $this->addTagBookmark($user, array('one', 'three')); + $this->addTagBookmark($user, array('one', 'two')); + + $arTags = $this->b2ts->getPopularTags(); + $this->assertInternalType('array', $arTags); + $this->assertEquals(3, count($arTags)); + + $arTags = $this->b2ts->getPopularTags(null, 2); + $this->assertInternalType('array', $arTags); + $this->assertEquals(2, count($arTags)); + $this->assertEquals( + array( + array('tag' => 'one', 'bCount' => '3'), + array('tag' => 'two', 'bCount' => '2'), + ), + $arTags + ); + + $arTags = $this->b2ts->getPopularTags(null, 1); + $this->assertInternalType('array', $arTags); + $this->assertEquals(1, count($arTags)); + $this->assertEquals( + array( + array('tag' => 'one', 'bCount' => '3'), + ), + $arTags + ); + } } if (PHPUnit_MAIN_METHOD == 'Bookmark2TagTest::main') { diff --git a/tests/TestBase.php b/tests/TestBase.php index 6006f4e..3e2e213 100644 --- a/tests/TestBase.php +++ b/tests/TestBase.php @@ -31,6 +31,7 @@ class TestBase extends PHPUnit_Framework_TestCase * @param array $tags Array of tags to attach. If "null" is given, * it will automatically be "unittest" * @param string $title Bookmark title + * @param string $date strtotime-compatible string * * @return integer ID of bookmark * @@ -38,7 +39,7 @@ class TestBase extends PHPUnit_Framework_TestCase */ protected function addBookmark( $user = null, $address = null, $status = 0, - $tags = null, $title = null + $tags = null, $title = null, $date = null ) { if ($user === null) { $user = $this->addUser(); @@ -64,7 +65,7 @@ class TestBase extends PHPUnit_Framework_TestCase null, $status, $tags, - null, null, false, false, + null, $date, false, false, $user ); return $bid; -- cgit v1.2.3-54-g00ecf From 361c18469af5f6782e622420797c4acd5fa70b97 Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Sat, 26 Mar 2011 15:09:22 +0100 Subject: add new feature: allow unit test mode enabling via HTTP GET parameter --- data/config.default.php | 8 ++++++ src/SemanticScuttle/header.php | 16 ++++++++++- tests/TestBase.php | 63 +++++++++++++++++++++++++++++++++++++++++- 3 files changed, 85 insertions(+), 2 deletions(-) (limited to 'tests/TestBase.php') diff --git a/data/config.default.php b/data/config.default.php index 3d60b91..af79891 100644 --- a/data/config.default.php +++ b/data/config.default.php @@ -735,4 +735,12 @@ $authEmailSuffix = null; */ $unittestUrl = null; +/** + * Allow "unittestMode=1" in URLs. + * Should only be enabled on development systems + * + * @var boolean + */ +$allowUnittestMode = false; + ?> diff --git a/src/SemanticScuttle/header.php b/src/SemanticScuttle/header.php index 9c5f7da..75e5204 100644 --- a/src/SemanticScuttle/header.php +++ b/src/SemanticScuttle/header.php @@ -39,6 +39,20 @@ set_include_path( require_once $datadir . '/config.default.php'; require_once $datadir . '/config.php'; +if (isset($_GET['unittestMode']) && $_GET['unittestMode'] == 1 +) { + if ($allowUnittestMode !== true) { + header('HTTP/1.0 400 Bad Request'); + die("Unittestmode is not allowed\n"); + } + + $unittestConfigFile = $datadir . '/config.unittest.php'; + if (file_exists($unittestConfigFile)) { + require_once $unittestConfigFile; + } + define('HTTP_UNIT_TEST_MODE', true); + define('UNIT_TEST_MODE', true); +} if (defined('UNIT_TEST_MODE')) { //make local config vars global - needed for unit tests //run with phpunit @@ -118,7 +132,7 @@ $tplVars['currentUser'] = $currentUser; $tplVars['userservice'] = $userservice; // 6 // Force UTF-8 behaviour for server (cannot be moved into top.inc.php which is not included into every file) -if (!defined('UNIT_TEST_MODE')) { +if (!defined('UNIT_TEST_MODE') || defined('HTTP_UNIT_TEST_MODE')) { //API files define that, so we need a way to support both of them if (!isset($httpContentType)) { if (DEBUG_MODE) { diff --git a/tests/TestBase.php b/tests/TestBase.php index 3e2e213..b85c189 100644 --- a/tests/TestBase.php +++ b/tests/TestBase.php @@ -22,6 +22,18 @@ */ 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. * @@ -80,8 +92,25 @@ class TestBase extends PHPUnit_Framework_TestCase * @param string $password Password * * @return integer ID of user + * + * @uses addUserData() */ protected function addUser($username = null, $password = null) + { + return reset($this->addUserData($username, $password)); + } + + + + /** + * Creates a new user in the database and returns id, username and password. + * + * @param string $username Username + * @param string $password Password + * + * @return array ID of user, Name of user, password of user + */ + protected function addUserData($username = null, $password = null) { $us = SemanticScuttle_Service_Factory::get('User'); $rand = rand(); @@ -98,7 +127,7 @@ class TestBase extends PHPUnit_Framework_TestCase $password, 'unittest-' . $rand . '@example.org' ); - return $uid; + return array($uid, $username, $password); } @@ -129,6 +158,38 @@ 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 -- 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/TestBase.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