]> gitweb.fluxo.info Git - semanticscuttle.git/commitdiff
test if deleting bookmarks works. to do this, we need a new testbase method to create...
authorcweiske <cweiske@b3834d28-1941-0410-a4f8-b48e95affb8f>
Sun, 25 Oct 2009 19:32:48 +0000 (19:32 +0000)
committercweiske <cweiske@b3834d28-1941-0410-a4f8-b48e95affb8f>
Sun, 25 Oct 2009 19:32:48 +0000 (19:32 +0000)
git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@417 b3834d28-1941-0410-a4f8-b48e95affb8f

src/SemanticScuttle/Service/Bookmark.php
src/SemanticScuttle/Service/User.php
tests/BookmarkTest.php
tests/TestBase.php
www/register.php

index 29a2291e5e0059354283510451ba1e1826d0a25e..82e23a3f71f4f9256bc0067e54f34d3a701a222d 100644 (file)
@@ -212,9 +212,9 @@ class SemanticScuttle_Service_Bookmark extends SemanticScuttle_DbService
      */
     public function addBookmark(
         $address, $title, $description, $privateNote, $status, $categories,
-        $date = null, $fromApi = false, $fromImport = false, $sId = -1
+        $date = null, $fromApi = false, $fromImport = false, $sId = null
     ) {
-        if ($sId == -1) {
+        if ($sId === null) {
             $userservice = SemanticScuttle_Service_Factory::get('User');
             $sId = $userservice->getCurrentUserId();
         }
@@ -436,16 +436,17 @@ class SemanticScuttle_Service_Bookmark extends SemanticScuttle_DbService
         $terms = null, $sortOrder = null, $watched = null,
         $startdate = null, $enddate = null, $hash = null
     ) {
-        $userservice =SemanticScuttle_Service_Factory::get('User');
-        $b2tservice =SemanticScuttle_Service_Factory::get('Bookmark2Tag');
-        $tag2tagservice =SemanticScuttle_Service_Factory::get('Tag2Tag');
-        $sId = $userservice->getCurrentUserId();
+        $userservice    = SemanticScuttle_Service_Factory::get('User');
+        $b2tservice     = SemanticScuttle_Service_Factory::get('Bookmark2Tag');
+        $tag2tagservice = SemanticScuttle_Service_Factory::get('Tag2Tag');
+        $sId            = $userservice->getCurrentUserId();
 
         if ($userservice->isLoggedOn()) {
-            // All public bookmarks, user's own bookmarks and any shared with user
+            // All public bookmarks, user's own bookmarks
+            // and any shared with user
             $privacy = ' AND ((B.bStatus = 0) OR (B.uId = '. $sId .')';
             $watchnames = $userservice->getWatchNames($sId, true);
-            foreach($watchnames as $watchuser) {
+            foreach ($watchnames as $watchuser) {
                 $privacy .= ' OR (U.username = "'. $watchuser .'" AND B.bStatus = 1)';
             }
             $privacy .= ')';
index bf44c4bb00d7a5dbaa2791fcb7b2a8925e6c2d8c..035c9e2c7fe8afcf9257da8079e9e1d506112c63 100644 (file)
@@ -400,24 +400,47 @@ class SemanticScuttle_Service_User extends SemanticScuttle_DbService
         return true;
     }
 
-    function addUser($username, $password, $email) {
+    /**
+     * Create a new user in database.
+     * No checks are done in here - you ought to have checked
+     * everything before calling this method!
+     *
+     * @param string $username Username to use
+     * @param string $password Password to use
+     * @param string $email    Email to use
+     *
+     * @return mixed Integer user ID if all is well,
+     *               boolean false if an error occured
+     */
+    public function addUser($username, $password, $email)
+    {
         // Set up the SQL UPDATE statement.
         $datetime = gmdate('Y-m-d H:i:s', time());
         $password = $this->sanitisePassword($password);
-        $values = array('username' => $username, 'password' => $password, 'email' => $email, 'uDatetime' => $datetime, 'uModified' => $datetime);
-        $sql = 'INSERT INTO '. $this->getTableName() .' '. $this->db->sql_build_array('INSERT', $values);
+        $values   = array(
+            'username'  => $username,
+            'password'  => $password,
+            'email'     => $email,
+            'uDatetime' => $datetime,
+            'uModified' => $datetime
+        );
+        $sql = 'INSERT INTO '. $this->getTableName()
+            . ' '. $this->db->sql_build_array('INSERT', $values);
 
         // Execute the statement.
         $this->db->sql_transaction('begin');
         if (!($dbresult = & $this->db->sql_query($sql))) {
             $this->db->sql_transaction('rollback');
-            message_die(GENERAL_ERROR, 'Could not insert user', '', __LINE__, __FILE__, $sql, $this->db);
+            message_die(
+                GENERAL_ERROR, 'Could not insert user',
+                '', __LINE__, __FILE__, $sql, $this->db
+            );
             return false;
         }
+        $uId = $this->db->sql_nextid($dbresult);
         $this->db->sql_transaction('commit');
 
-        // Everything worked out, so return true.
-        return true;
+        return $uId;
     }
 
     function updateUser($uId, $password, $name, $email, $homepage, $uContent) {
@@ -564,7 +587,7 @@ class SemanticScuttle_Service_User extends SemanticScuttle_DbService
      *
      * @see updateSessionStability()
      */
-    publi function isSessionStable()
+    public function isSessionStable()
     {
         return $_SESSION['sessionStable'] == 1;
     }
index bbc377446c910dc2c87bc79cf7f8f36047ae53a0..d7f4240be0af37063578a6060f048ba6aa820fb2 100644 (file)
@@ -28,10 +28,10 @@ if (!defined('PHPUnit_MAIN_METHOD')) {
  */
 class BookmarkTest extends TestBase
 {
-       protected $us;
-       protected $bs;
-       protected $ts;
-       protected $tts;
+    protected $us;
+    protected $bs;
+    protected $ts;
+    protected $tts;
 
 
 
@@ -50,83 +50,132 @@ class BookmarkTest extends TestBase
 
 
 
-       protected function setUp()
-       {
-               $this->us =SemanticScuttle_Service_Factory::get('User');
-               $this->bs =SemanticScuttle_Service_Factory::get('Bookmark');
-               $this->bs->deleteAll();
-               $this->b2ts=SemanticScuttle_Service_Factory::get('Bookmark2Tag');
-               $this->b2ts->deleteAll();
-               $this->tts =SemanticScuttle_Service_Factory::get('Tag2Tag');
-               $this->tts->deleteAll();
-               $this->tsts =SemanticScuttle_Service_Factory::get('TagStat');
-               $this->tsts->deleteAll();
-       }
-
-       public function testHardCharactersInBookmarks()
-       {               
-               $bs = $this->bs;
-               $title = "title&é\"'(-è_çà)=";
-               $desc = "description#{[|`\^@]}³<> ¹¡÷׿&é\"'(-è\\_çà)=";
-               $tag1 = "#{|`^@]³¹¡¿<&é\"'(-è\\_çà)";   
-               $tag2 = "&é\"'(-è.[?./§!_çà)";
-
-               $bs->addBookmark(
+    protected function setUp()
+    {
+        $this->us = SemanticScuttle_Service_Factory::get('User');
+        $this->bs = SemanticScuttle_Service_Factory::get('Bookmark');
+        $this->bs->deleteAll();
+        $this->b2ts= SemanticScuttle_Service_Factory::get('Bookmark2Tag');
+        $this->b2ts->deleteAll();
+        $this->tts = SemanticScuttle_Service_Factory::get('Tag2Tag');
+        $this->tts->deleteAll();
+        $this->tsts = SemanticScuttle_Service_Factory::get('TagStat');
+        $this->tsts->deleteAll();
+        $this->vs = SemanticScuttle_Service_Factory::get('Vote');
+        $this->vs->deleteAll();
+    }
+
+    public function testHardCharactersInBookmarks()
+    {
+        $bs = $this->bs;
+        $title = "title&é\"'(-è_çà)=";
+        $desc = "description#{[|`\^@]}³<> ¹¡÷׿&é\"'(-è\\_çà)=";
+        $tag1 = "#{|`^@]³¹¡¿<&é\"'(-è\\_çà)";
+        $tag2 = "&é\"'(-è.[?./§!_çà)";
+
+        $bs->addBookmark(
             'http://site1.com', $title, $desc, 'note',
             0, array($tag1, $tag2),
             null, false, false, 1
         );
 
-               $bookmarks = $bs->getBookmarks(0, 1);
+        $bookmarks = $bs->getBookmarks(0, 1);
 
-               $b0 = $bookmarks['bookmarks'][0];
-               $this->assertEquals($title, $b0['bTitle']);
-               $this->assertEquals($desc, $b0['bDescription']);
-               $this->assertEquals(
-            str_replace(array('"', '\'', '/'), "_", $tag1), 
+        $b0 = $bookmarks['bookmarks'][0];
+        $this->assertEquals($title, $b0['bTitle']);
+        $this->assertEquals($desc, $b0['bDescription']);
+        $this->assertEquals(
+            str_replace(array('"', '\'', '/'), "_", $tag1),
             $b0['tags'][0]
         );
-               $this->assertEquals(
+        $this->assertEquals(
             str_replace(array('"', '\'', '/'), "_", $tag2),
             $b0['tags'][1]
         );
-       }
+    }
 
-       public function testUnificationOfBookmarks()
-       {               
-               $bs = $this->bs;
+    public function testUnificationOfBookmarks()
+    {
+        $bs = $this->bs;
 
-               $bs->addBookmark(
+        $bs->addBookmark(
             'http://site1.com', "title", "description", 'note',
             0, array('tag1'), null, false, false,
             1
         );
-               $bs->addBookmark(
+        $bs->addBookmark(
             "http://site1.com", "title2", "description2", 'note',
             0, array('tag2'), null, false, false,
             2
         );
 
-               $bookmarks = $bs->getBookmarks();
-               $this->assertEquals(1, $bookmarks['total']);
-       }
+        $bookmarks = $bs->getBookmarks();
+        $this->assertEquals(1, $bookmarks['total']);
+    }
 
-       /*public function testSearchingBookmarksAccentsInsensible()
-        {
-        $bs = $this->bs;
+    /*public function testSearchingBookmarksAccentsInsensible()
+     {
+     $bs = $this->bs;
 
-        $bs->addBookmark("http://site1.com", "title", "éèüaàê", "status", array('tag1'), null, false, false, 1);
-        $bookmarks =& $bs->getBookmarks(0, NULL, NULL, NULL, $terms = "eeaae"); //void
-        $this->assertEquals(0, $bookmarks['total']);
-        $bookmarks =& $bs->getBookmarks(0, NULL, NULL, NULL, $terms = "eeuaae");
-        $this->assertEquals(1, $bookmarks['total']);
-        }*/
+     $bs->addBookmark("http://site1.com", "title", "éèüaàê", "status", array('tag1'), null, false, false, 1);
+     $bookmarks =& $bs->getBookmarks(0, NULL, NULL, NULL, $terms = "eeaae"); //void
+     $this->assertEquals(0, $bookmarks['total']);
+     $bookmarks =& $bs->getBookmarks(0, NULL, NULL, NULL, $terms = "eeuaae");
+     $this->assertEquals(1, $bookmarks['total']);
+     }*/
 
 
 
+    /**
+     * Test if deleting a bookmark works.
+     *
+     * @return void
+     */
     public function testDeleteBookmark()
     {
-        //FIXME
+        $bookmarks = $this->bs->getBookmarks();
+        $this->assertEquals(0, $bookmarks['total']);
+
+        $bid = $this->addBookmark();
+        $bookmarks = $this->bs->getBookmarks();
+        $this->assertEquals(1, $bookmarks['total']);
+
+        $bid2 = $this->addBookmark();
+        $bookmarks = $this->bs->getBookmarks();
+        $this->assertEquals(2, $bookmarks['total']);
+
+        $this->assertTrue($this->bs->deleteBookmark($bid));
+        $bookmarks = $this->bs->getBookmarks();
+        $this->assertEquals(1, $bookmarks['total']);
+
+        $this->assertTrue($this->bs->deleteBookmark($bid2));
+        $bookmarks = $this->bs->getBookmarks();
+        $this->assertEquals(0, $bookmarks['total']);
+    }
+
+
+
+    /**
+     * Test if deleting a bookmark with a vote works.
+     *
+     * @return void
+     */
+    public function testDeleteBookmarkWithVote()
+    {
+        $uid = $this->addUser();
+        $bid = $this->addBookmark();
+
+        $bid = $this->addBookmark();
+        $this->vs->vote($bid, $uid, 1);
+        $this->assertTrue($this->vs->hasVoted($bid, $uid));
+
+        $bid2 = $this->addBookmark();
+        $this->vs->vote($bid2, $uid, 1);
+        $this->assertTrue($this->vs->hasVoted($bid2, $uid));
+
+        $this->assertTrue($this->bs->deleteBookmark($bid));
+        $this->assertFalse($this->vs->hasVoted($bid, $uid));
+        $this->assertTrue($this->vs->hasVoted($bid2, $uid));
     }
 
 }
index dc5643f27d8fd8f52c4ee117fa53cda98973ea25..f9946e2da42541c073232da653c3a0075ba8bef8 100644 (file)
@@ -27,23 +27,50 @@ class TestBase extends PHPUnit_Framework_TestCase
     /**
      * Create a new bookmark.
      *
+     * @param integer $user User ID the bookmark shall belong
+     *
      * @return integer ID of bookmark
      */
-    protected function addBookmark()
+    protected function addBookmark($user = null)
     {
-        $bs = SemanticScuttle_Service_Factory::get('Bookmark');
+        if ($user === null) {
+            $user = $this->addUser();
+        }
+
+        $bs   = SemanticScuttle_Service_Factory::get('Bookmark');
         $rand = rand();
-        $bid = $bs->addBookmark(
+        $bid  = $bs->addBookmark(
             'http://example.org/' . $rand,
             'unittest bookmark #' . $rand,
             'description',
             null,
             0,
-            array('unittest')
+            array('unittest'),
+            null, false, false,
+            $user
         );
         return $bid;
     }
 
+
+
+    /**
+     * Creates a new user in the database.
+     *
+     * @return integer ID of user
+     */
+    protected function addUser()
+    {
+        $us   = SemanticScuttle_Service_Factory::get('User');
+        $rand = rand();
+        $uid  = $us->addUser(
+            'unittestuser-' . $rand,
+            $rand,
+            'unittest-' . $rand . '@example.org'
+        );
+        return $uid;
+    }
+
 }
 
 ?>
\ No newline at end of file
index 6a0e1beab385674aa31d0cb92076343eb4081374..379dec48a4ea31b6a7b46f0695fecf947631be9b 100644 (file)
@@ -60,7 +60,7 @@ if (POST_SUBMITTED != '') {
         $tplVars['error'] = T_('Antispam answer is not valid. Please try again.');
 
     // Register details
-    } elseif ($userservice->addUser($posteduser, POST_PASS, POST_MAIL)) {
+    } elseif ($userservice->addUser($posteduser, POST_PASS, POST_MAIL) !== false) {
         // Log in with new username
         $login = $userservice->login($posteduser, POST_PASS);
         if ($login) {