]> gitweb.fluxo.info Git - semanticscuttle.git/commitdiff
tests for getadminlinkedtags
authorChristian Weiske <cweiske@cweiske.de>
Fri, 8 Oct 2010 15:43:05 +0000 (17:43 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Fri, 8 Oct 2010 15:43:05 +0000 (17:43 +0200)
src/SemanticScuttle/Service/User.php
tests/TestBase.php
tests/ajax/GetAdminLinkedTagsTest.php [new file with mode: 0644]

index d74d10473ced7692a01bab69ce03d25c4303dfc1..fd9d84fe480d5b3a410d15494c509d699c864c71 100644 (file)
@@ -203,18 +203,26 @@ class SemanticScuttle_Service_User extends SemanticScuttle_DbService
         }
     }
 
-    /* Takes an numerical "id" or a string "username"
-     and returns the numerical "id" if the user exists else returns NULL */
-    function getIdFromUser($user) {
+    /**
+     * Obtains the ID of the given user name.
+     * If a user ID is passed, it is returned.
+     * In case the user does not exist, NULL is returned.
+     *
+     * @param string|integer $user User name or user ID
+     *
+     * @return integer NULL if not found or the user ID
+     */
+    public function getIdFromUser($user)
+    {
         if (is_int($user)) {
             return intval($user);
         } else {
             $objectUser = $this->getObjectUserByUsername($user);
-            if($objectUser != NULL) {
+            if ($objectUser != null) {
                 return $objectUser->getId();
             }
         }
-        return NULL;
+        return null;
     }
 
     /**
index 402330b6cd28b5516bbc35d9ed270f366a9e921e..c0acd58b73d73e06a44287367436f451f6d54613 100644 (file)
@@ -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
diff --git a/tests/ajax/GetAdminLinkedTagsTest.php b/tests/ajax/GetAdminLinkedTagsTest.php
new file mode 100644 (file)
index 0000000..d8ec447
--- /dev/null
@@ -0,0 +1,160 @@
+<?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';
+
+if (!defined('PHPUnit_MAIN_METHOD')) {
+    define('PHPUnit_MAIN_METHOD', 'ajax_GetAdminLinkedTagsTest::main');
+}
+
+/**
+ * Unit tests for the ajax linked admin tags 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_GetAdminLinkedTagsTest extends TestBaseApi
+{
+    protected $urlPart = 'ajax/getadminlinkedtags.php';
+
+
+
+    /**
+     * Used to run this test class standalone
+     *
+     * @return void
+     */
+    public static function main()
+    {
+        require_once 'PHPUnit/TextUI/TestRunner.php';
+        PHPUnit_TextUI_TestRunner::run(
+            new PHPUnit_Framework_TestSuite(__CLASS__)
+        );
+    }
+
+
+
+    /**
+     * Verify that we get the configured root tags if
+     * we do not pass any parameters
+     */
+    public function testRootTags()
+    {
+        $req = $this->getRequest();
+        $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->assertType('array', $data);
+
+        //same number of elements as the menu2Tags array
+        $this->assertEquals(
+            count($GLOBALS['menu2Tags']),
+            count($data)
+        );
+
+        //and the same contents
+        foreach ($data as $tagObj) {
+            $tagName = $tagObj->data->title;
+            $this->assertContains($tagName, $GLOBALS['menu2Tags']);
+        }
+    }
+
+    /**
+     * Verify that we get subtags of a given tag
+     */
+    public function testSubTags()
+    {
+        $t2t = SemanticScuttle_Service_Factory::get('Tag2Tag');
+        $t2t->deleteAll();
+
+        $menu2Tag = reset($GLOBALS['menu2Tags']);
+        //we have a subtag now
+        $this->addBookmark(
+            $this->getAdminUser(),
+            null,
+            0,
+            $menu2Tag . '>adminsubtag'
+        );
+
+        $res = $this->getRequest('?tag=' . $menu2Tag)->send();
+        $this->assertEquals(200, $res->getStatus());
+        $this->assertEquals(
+            'application/json; charset=utf-8',
+            $res->getHeader('content-type')
+        );
+
+        $data = json_decode($res->getBody());
+        $this->assertType('array', $data);
+
+        //only one subtag
+        $this->assertEquals(1, count($data));
+        $this->assertEquals('adminsubtag', $data[0]->data->title);
+    }
+
+    /**
+     * Verify that we only get admin tags, not tags from
+     * non-admin people
+     */
+    public function testOnlyAdminTags()
+    {
+        $t2t = SemanticScuttle_Service_Factory::get('Tag2Tag');
+        $t2t->deleteAll();
+
+        $menu2Tag = reset($GLOBALS['menu2Tags']);
+        //we have a subtag now
+        $this->addBookmark(
+            $this->getAdminUser(),
+            null,
+            0,
+            $menu2Tag . '>adminsubtag'
+        );
+        //add another bookmark now, but for a normal user
+        $this->addBookmark(
+            null,
+            null,
+            0,
+            $menu2Tag . '>normalsubtag'
+        );
+
+        $res = $this->getRequest('?tag=' . $menu2Tag)->send();
+        $this->assertEquals(200, $res->getStatus());
+        $this->assertEquals(
+            'application/json; charset=utf-8',
+            $res->getHeader('content-type')
+        );
+
+        $data = json_decode($res->getBody());
+        $this->assertType('array', $data);
+
+        //we should have only one subtag now, the admin one
+        $this->assertEquals(1, count($data));
+        $this->assertEquals('adminsubtag', $data[0]->data->title);
+    }
+}
+
+if (PHPUnit_MAIN_METHOD == 'ajax_GetAdminLinkedTagsTest::main') {
+    ajax_GetAdminLinkedTagsTest::main();
+}
+?>
\ No newline at end of file