summaryrefslogtreecommitdiff
path: root/tests/TestBase.php
diff options
context:
space:
mode:
authorChristian Weiske <cweiske@cweiske.de>2011-04-06 08:42:26 +0200
committerChristian Weiske <cweiske@cweiske.de>2011-04-06 08:42:26 +0200
commitc81566f5d8c6149e87432b158331fd724e7e35e5 (patch)
treebf3556fd268f419410aeaef1c61af7a5b13834fb /tests/TestBase.php
parent1a2b37cc3421ba24931c1ef5edb0f06e61db6d5c (diff)
downloadsemanticscuttle-c81566f5d8c6149e87432b158331fd724e7e35e5.tar.gz
semanticscuttle-c81566f5d8c6149e87432b158331fd724e7e35e5.tar.bz2
cherry-pick:
add new feature: allow unit test mode enabling via HTTP GET parameter
Diffstat (limited to 'tests/TestBase.php')
-rw-r--r--tests/TestBase.php89
1 files changed, 89 insertions, 0 deletions
diff --git a/tests/TestBase.php b/tests/TestBase.php
index 8c1a934..edafd3d 100644
--- a/tests/TestBase.php
+++ b/tests/TestBase.php
@@ -23,6 +23,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.
*
* @param integer $user User ID the bookmark shall belong
@@ -79,9 +91,26 @@ 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();
@@ -97,9 +126,69 @@ class TestBase extends PHPUnit_Framework_TestCase
$password,
'unittest-' . $rand . '@example.org'
);
+ return array($uid, $username, $password);
+ }
+
+
+
+ /**
+ * 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;
}
+
+ /**
+ * 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