]> gitweb.fluxo.info Git - semanticscuttle.git/commitdiff
cherry-pick:
authorChristian Weiske <cweiske@cweiske.de>
Wed, 6 Apr 2011 06:43:42 +0000 (08:43 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Wed, 6 Apr 2011 06:43:42 +0000 (08:43 +0200)
 move setUnittestConfig to TestBaseApi since it makes only sense to use it there

Conflicts:

tests/TestBaseApi.php

tests/TestBase.php
tests/TestBaseApi.php
tests/ajax/GetAdminTagsTest.php [new file with mode: 0644]

index edafd3d7f0f61101f5217f10d7d9840b5c3b951e..5a61b7ba20302b5d8d155b038c30fed2bad68762 100644 (file)
  */
 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.
      *
@@ -157,38 +145,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
index dacdecdf26c05f0bd63b66183ff1ba34c301c33d..73135fb79e0da54774c2d5bea2839dc1354aebce 100644 (file)
@@ -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
diff --git a/tests/ajax/GetAdminTagsTest.php b/tests/ajax/GetAdminTagsTest.php
new file mode 100644 (file)
index 0000000..5c941e8
--- /dev/null
@@ -0,0 +1,63 @@
+<?php
+/**
+ * SemanticScuttle - your social bookmark manager.
+ *
+ * PHP version 5.
+ *
+ * @category Bookmarking
+ * @package  SemanticScuttle
+ * @author   Benjamin Huynh-Kim-Bang <mensonge@users.sourceforge.net>
+ * @author   Christian Weiske <cweiske@cweiske.de>
+ * @author   Eric Dane <ericdane@users.sourceforge.net>
+ * @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 <cweiske@cweiske.de>
+ * @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