diff options
-rw-r--r-- | db/feedmapper.php | 1 | ||||
-rw-r--r-- | db/feedtype.php | 33 | ||||
-rw-r--r-- | db/feedtypes.php | 35 | ||||
-rw-r--r-- | db/foldermapper.php | 225 | ||||
-rw-r--r-- | tests/db/FeedMapperTest.php | 1 | ||||
-rw-r--r-- | tests/db/FolderMapperTest.php | 119 |
6 files changed, 186 insertions, 228 deletions
diff --git a/db/feedmapper.php b/db/feedmapper.php index a2d0f8fc7..3d1c4d773 100644 --- a/db/feedmapper.php +++ b/db/feedmapper.php @@ -90,4 +90,5 @@ class FeedMapper extends NewsMapper { return $result['size']; } + }
\ No newline at end of file diff --git a/db/feedtype.php b/db/feedtype.php index d330a5b2a..cf23de10b 100644 --- a/db/feedtype.php +++ b/db/feedtype.php @@ -1,22 +1,35 @@ <?php + /** -* ownCloud - News app +* ownCloud - News * +* @author Alessandro Cosentino * @author Bernhard Posselt -* Copyright (c) 2012 - Bernhard Posselt <nukeawhale@gmail.com> +* @copyright 2012 Alessandro Cosentino cosenal@gmail.com +* @copyright 2012 Bernhard Posselt nukeawhale@gmail.com +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE +* License as published by the Free Software Foundation; either +* version 3 of the License, or any later version. +* +* This library is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU AFFERO GENERAL PUBLIC LICENSE for more details. * -* This file is licensed under the Affero General Public License version 3 or later. -* See the COPYING-README file +* You should have received a copy of the GNU Affero General Public +* License along with this library. If not, see <http://www.gnu.org/licenses/>. * */ -namespace OCA\News; +namespace OCA\News\Db; class FeedType { - const FEED = 0; - const FOLDER = 1; - const STARRED = 2; - const SUBSCRIPTIONS = 3; - const SHARED = 4; + const FEED = 0; + const FOLDER = 1; + const STARRED = 2; + const SUBSCRIPTIONS = 3; + const SHARED = 4; };
\ No newline at end of file diff --git a/db/feedtypes.php b/db/feedtypes.php deleted file mode 100644 index 725d56eba..000000000 --- a/db/feedtypes.php +++ /dev/null @@ -1,35 +0,0 @@ -<?php - -/** -* ownCloud - News -* -* @author Alessandro Cosentino -* @author Bernhard Posselt -* @copyright 2012 Alessandro Cosentino cosenal@gmail.com -* @copyright 2012 Bernhard Posselt nukeawhale@gmail.com -* -* This library is free software; you can redistribute it and/or -* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE -* License as published by the Free Software Foundation; either -* version 3 of the License, or any later version. -* -* This library is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU AFFERO GENERAL PUBLIC LICENSE for more details. -* -* You should have received a copy of the GNU Affero General Public -* License along with this library. If not, see <http://www.gnu.org/licenses/>. -* -*/ - -namespace OCA\News\Db; - - -class FeedType { - const FEED = 0; - const FOLDER = 1; - const STARRED = 2; - const SUBSCRIPTIONS = 3; - const SHARED = 4; -};
\ No newline at end of file diff --git a/db/foldermapper.php b/db/foldermapper.php index d9040b50b..9cfa905cb 100644 --- a/db/foldermapper.php +++ b/db/foldermapper.php @@ -1,210 +1,71 @@ <?php + /** -* ownCloud - News app +* ownCloud - News * * @author Alessandro Cosentino -* Copyright (c) 2012 - Alessandro Cosentino <cosenal@gmail.com> +* @author Bernhard Posselt +* @copyright 2012 Alessandro Cosentino cosenal@gmail.com +* @copyright 2012 Bernhard Posselt nukeawhale@gmail.com +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE +* License as published by the Free Software Foundation; either +* version 3 of the License, or any later version. +* +* This library is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU AFFERO GENERAL PUBLIC LICENSE for more details. * -* This file is licensed under the Affero General Public License version 3 or later. -* See the COPYING-README file +* You should have received a copy of the GNU Affero General Public +* License along with this library. If not, see <http://www.gnu.org/licenses/>. * */ -namespace OCA\News; - -/** - * This class maps a feed to an entry in the feeds table of the database. - */ -class FolderMapper { - - const tableName = '*PREFIX*news_folders'; - - private $userid; - - public function __construct($userid = null) { - if ($userid !== null) { - $this->userid = $userid; - } - else { - $this->userid = \OCP\USER::getUser(); - } - } - - - /** - * @brief Returns the forest (list of trees) of folders children of $parentid - * @param - * @returns - */ - public function childrenOf($parentid) { - $folderlist = array(); - $stmt = \OCP\DB::prepare('SELECT * FROM ' . self::tableName . - ' WHERE user_id = ? AND parent_id = ?'); - $result = $stmt->execute(array($this->userid, $parentid)); - - while( $row = $result->fetchRow()) { - $folderid = $row['id']; - $folder = new Folder($row['name'], $folderid); - $folder->setOpened($row['opened']); - $children = self::childrenOf($folderid); - $folder->addChildren($children); - $folderlist[] = $folder; - } - - return $folderlist; - } - - /** - * @brief Returns the forest (list of trees) of folders children of $parentid - * @param - * @returns - */ - public function getAll() { - return $this->childrenOf(0); - } - - /** - * @brief Returns the forest (list of trees) of folders children of $parentid, - * including the feeds that they contain - * @param - * @returns - */ - public function childrenOfWithFeeds($parentid) { +namespace OCA\News\Db; - $feedmapper = new FeedMapper(); - $collectionlist = $feedmapper->findByFolderId($parentid); +use \OCA\AppFramework\Core\API; - $stmt = \OCP\DB::prepare('SELECT * FROM ' . self::tableName . - ' WHERE user_id = ? AND parent_id = ?'); - $result = $stmt->execute(array($this->userid, $parentid)); - while( $row = $result->fetchRow()) { - $folderid = $row['id']; - $folder = new Folder($row['name'], $folderid); - $folder->setOpened($row['opened']); - $children = self::childrenOfWithFeeds($folderid); - $folder->addChildren($children); - $collectionlist[] = $folder; - } +class FolderMapper extends NewsMapper { - return $collectionlist; + public function __construct(API $api) { + parent::__construct($api, 'news_folders'); } + public function find($id, $userId){ + $sql = 'SELECT * FROM `*dbprefix*news_folders` ' . + 'WHERE `id` = ? ' . + 'AND `user_id` = ?'; - /** - * This is being used for consistency - */ - public function findById($id){ - return $this->find($id); - } - - - /** - * @brief Retrieve a folder from the database - * @param id The id of the folder in the database table. - * @returns an instance of OC_News_Folder - */ - public function find($id) { - $stmt = \OCP\DB::prepare('SELECT * - FROM ' . self::tableName . - ' WHERE user_id = ? AND id = ?'); - $result = $stmt->execute(array($this->userid, $id)); - - $row = $result->fetchRow(); - if(!$row) - return null; - - $folder = new Folder($row['name'], $row['id']); - $folder->setOpened($row['opened']); + $row = $this->findRow($sql, $id, $userId); + $folder = new Folder(); + $folder->fromRow($row); return $folder; } - /** - * @brief Store the folder and all its feeds into the database - * @param folder the folder to be saved - * @returns The id of the folder in the database table. - */ - public function save(Folder $folder) { - $query = \OCP\DB::prepare(' - INSERT INTO ' . self::tableName . - '(name, parent_id, user_id, opened) - VALUES (?, ?, ?, ?) - '); - - $name = $folder->getName(); - if(empty($name)) { - $l = \OC_L10N::get('news'); - $name = $l->t('no name'); + private function findAllRows($sql, $params=array()){ + $result = $this->execute($sql, $params); + + $folders = array(); + while($row = $result->fetchRow()){ + $folder = new Folder(); + $folder->fromRow($row); + array_push($folders, $folder); } - $parentid = $folder->getParentId(); - - $params=array( - $name, - $parentid, - $this->userid, - $folder->getOpened() - ); - $query->execute($params); - $folderid = \OCP\DB::insertid(self::tableName); - - $folder->setId($folderid); - return $folderid; - } - - - /** - * @brief Updates the folder - * @param folder the folder to be updated - */ - public function update(Folder $folder) { - $query = \OCP\DB::prepare('UPDATE ' . self::tableName - . ' SET name = ?, opened = ?' . ' WHERE id = ?'); - - $params = array($folder->getName(), $folder->getOpened(), $folder->getId()); - $query->execute($params); - return true; - } - - /** - * @brief Delete the folder and all its feeds from the database - * @param folder the folder to be deleted (an instance of OCA\News\Folder) - * @returns true if the folder has been deleted, false if an error occurred - */ - public function delete(Folder $folder) { - $folderid = $folder->getId(); - return deleteById(folderid); + return $folders; } - /** - * @brief Delete the folder and all its feeds from the database - * @param folder the folder to be deleted (an instance of OCA\News\Folder) - * @returns true if the folder has been deleted, false if an error occurred - */ - public function deleteById($folderid) { - if ($folderid == null) { - return false; - } - - // delete child folders - $stmt = \OCP\DB::prepare('SELECT id FROM ' . self::tableName .' WHERE parent_id = ? AND user_id = ?'); - $result = $stmt->execute(array($folderid, $this->userid)); - while ($row = $result->fetchRow()) { - if (!self::deleteById($row['id'])) - return false; - } - $stmt = \OCP\DB::prepare('DELETE FROM ' . self::tableName .' WHERE id = ? AND user_id = ?'); - $result = $stmt->execute(array($folderid, $this->userid)); + public function findAllFromUser($userId){ + $sql = 'SELECT * FROM `*dbprefix*news_folders` ' . + 'AND `user_id` = ?'; + $params = array($userId); - $feedMapper = new FeedMapper($this->userid); - //TODO: handle the value that the execute returns - if(!$feedMapper->deleteAll($folderid)) - return false; - - return true; + return $this->findAllRows($sql, $params); } - }
\ No newline at end of file diff --git a/tests/db/FeedMapperTest.php b/tests/db/FeedMapperTest.php index 90ad07626..34747e0d9 100644 --- a/tests/db/FeedMapperTest.php +++ b/tests/db/FeedMapperTest.php @@ -130,7 +130,6 @@ class FeedMapperTest extends \OCA\AppFramework\Utility\MapperTestUtility { } - public function testGetStarredCount(){ $userId = 'john'; $row = array( diff --git a/tests/db/FolderMapperTest.php b/tests/db/FolderMapperTest.php new file mode 100644 index 000000000..469ea2701 --- /dev/null +++ b/tests/db/FolderMapperTest.php @@ -0,0 +1,119 @@ +<?php + +/** +* ownCloud - News +* +* @author Alessandro Cosentino +* @author Bernhard Posselt +* @copyright 2012 Alessandro Cosentino cosenal@gmail.com +* @copyright 2012 Bernhard Posselt nukeawhale@gmail.com +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE +* License as published by the Free Software Foundation; either +* version 3 of the License, or any later version. +* +* This library is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU AFFERO GENERAL PUBLIC LICENSE for more details. +* +* You should have received a copy of the GNU Affero General Public +* License along with this library. If not, see <http://www.gnu.org/licenses/>. +* +*/ + +namespace OCA\News\Db; + +require_once(__DIR__ . "/../classloader.php"); + + +class FolderMapperTest extends \OCA\AppFramework\Utility\MapperTestUtility { + + private $folderMapper; + private $folders; + + protected function setUp(){ + $this->beforeEach(); + + $this->folderMapper = new FolderMapper($this->api); + + // create mock folders + $folder1 = new Folder(); + $folder2 = new Folder(); + + $this->folders = array( + $folder1, + $folder2 + ); + } + + + public function testFind(){ + $userId = 'john'; + $id = 3; + $rows = array( + array('id' => $this->folders[0]->getId()), + ); + $sql = 'SELECT * FROM `*dbprefix*news_folders` ' . + 'WHERE `id` = ? ' . + 'AND `user_id` = ?'; + + $this->setMapperResult($sql, array($id, $userId), $rows); + + $result = $this->folderMapper->find($id, $userId); + $this->assertEquals($this->folders[0], $result); + + } + + + public function testFindNotFound(){ + $userId = 'john'; + $id = 3; + $sql = 'SELECT * FROM `*dbprefix*news_folders` ' . + 'WHERE `id` = ? ' . + 'AND `user_id` = ?'; + + $this->setMapperResult($sql, array($id, $userId)); + + $this->setExpectedException('\OCA\AppFramework\Db\DoesNotExistException'); + $result = $this->folderMapper->find($id, $userId); + } + + + public function testFindMoreThanOneResultFound(){ + $userId = 'john'; + $id = 3; + $rows = array( + array('id' => $this->folders[0]->getId()), + array('id' => $this->folders[1]->getId()) + ); + $sql = 'SELECT * FROM `*dbprefix*news_folders` ' . + 'WHERE `id` = ? ' . + 'AND `user_id` = ?'; + + $this->setMapperResult($sql, array($id, $userId), $rows); + + $this->setExpectedException('\OCA\AppFramework\Db\MultipleObjectsReturnedException'); + $result = $this->folderMapper->find($id, $userId); + } + + + + public function testFindAllFromUser(){ + $userId = 'john'; + $rows = array( + array('id' => $this->folders[0]->getId()), + array('id' => $this->folders[1]->getId()) + ); + $sql = 'SELECT * FROM `*dbprefix*news_folders` ' . + 'AND `user_id` = ?'; + + $this->setMapperResult($sql, array($userId), $rows); + + $result = $this->folderMapper->findAllFromUser($userId); + $this->assertEquals($this->folders, $result); + } + + +}
\ No newline at end of file |