diff options
Diffstat (limited to 'tests/unit')
-rw-r--r-- | tests/unit/articleenhancer/EnhancerTest.php | 5 | ||||
-rw-r--r-- | tests/unit/controller/ItemControllerTest.php | 48 | ||||
-rw-r--r-- | tests/unit/db/FolderMapperTest.php | 8 | ||||
-rw-r--r-- | tests/unit/db/ItemMapperTest.php | 55 | ||||
-rw-r--r-- | tests/unit/db/ItemTest.php | 12 | ||||
-rw-r--r-- | tests/unit/db/mappertestutility.php | 160 | ||||
-rw-r--r-- | tests/unit/fetcher/FeedFetcherTest.php | 1 | ||||
-rw-r--r-- | tests/unit/service/FolderServiceTest.php | 2 | ||||
-rw-r--r-- | tests/unit/service/ItemServiceTest.php | 40 |
9 files changed, 242 insertions, 89 deletions
diff --git a/tests/unit/articleenhancer/EnhancerTest.php b/tests/unit/articleenhancer/EnhancerTest.php index d25b20dc7..f7dc2246e 100644 --- a/tests/unit/articleenhancer/EnhancerTest.php +++ b/tests/unit/articleenhancer/EnhancerTest.php @@ -52,14 +52,15 @@ class EnhancerTest extends \PHPUnit_Framework_TestCase { 'http://test.com/', 'http://www.test.com' ]; - for ($i=0; $i < count($urls); $i++) { + $count = count($urls); + for ($i=0; $i < $count; $i++) { $this->articleEnhancer->expects($this->at($i)) ->method('enhance') ->with($this->equalTo($item)) ->will($this->returnValue($item)); } - for ($i=0; $i < count($urls); $i++) { + for ($i=0; $i < $count; $i++) { $url = $urls[$i]; $result = $this->enhancer->enhance($item, $url); $this->assertEquals($item, $result); diff --git a/tests/unit/controller/ItemControllerTest.php b/tests/unit/controller/ItemControllerTest.php index 88e49c690..72c6d38f0 100644 --- a/tests/unit/controller/ItemControllerTest.php +++ b/tests/unit/controller/ItemControllerTest.php @@ -226,7 +226,8 @@ class ItemControllerTest extends \PHPUnit_Framework_TestCase { $this->equalTo(0), $this->equalTo(true), $this->equalTo(false), - $this->equalTo($this->user)) + $this->equalTo($this->user), + $this->equalTo([])) ->will($this->returnValue($result['items'])); $response = $this->controller->index(FeedType::FEED, 2, 3); @@ -234,6 +235,51 @@ class ItemControllerTest extends \PHPUnit_Framework_TestCase { } + public function testIndexSearch(){ + $feeds = [new Feed()]; + $result = [ + 'items' => [new Item()], + 'feeds' => $feeds, + 'newestItemId' => $this->newestItemId, + 'starred' => 3111 + ]; + + $this->itemsApiExpects(2, FeedType::FEED, '0'); + + $this->feedService->expects($this->once()) + ->method('findAll') + ->with($this->equalTo($this->user)) + ->will($this->returnValue($feeds)); + + $this->itemService->expects($this->once()) + ->method('getNewestItemId') + ->with($this->equalTo($this->user)) + ->will($this->returnValue($this->newestItemId)); + + $this->itemService->expects($this->once()) + ->method('starredCount') + ->with($this->equalTo($this->user)) + ->will($this->returnValue(3111)); + + $this->itemService->expects($this->once()) + ->method('findAll') + ->with( + $this->equalTo(2), + $this->equalTo(FeedType::FEED), + $this->equalTo(3), + $this->equalTo(0), + $this->equalTo(true), + $this->equalTo(false), + $this->equalTo($this->user), + $this->equalTo(['test', 'search'])) + ->will($this->returnValue($result['items'])); + + $response = $this->controller->index(FeedType::FEED, 2, 3, + 0, null, null, 'test%20%20search%20'); + $this->assertEquals($result, $response); + } + + public function testItemsOffsetNotZero(){ $result = ['items' => [new Item()]]; diff --git a/tests/unit/db/FolderMapperTest.php b/tests/unit/db/FolderMapperTest.php index e9df37195..00ad2127d 100644 --- a/tests/unit/db/FolderMapperTest.php +++ b/tests/unit/db/FolderMapperTest.php @@ -125,14 +125,14 @@ class FolderMapperTest extends \OCA\News\Tests\Unit\Db\MapperTestUtility { $arguments = [$folder->getId()]; $sql2 = 'DELETE FROM `*PREFIX*news_feeds` WHERE `folder_id` = ?'; + $arguments2 = [$folder->getId()]; $sql3 = 'DELETE FROM `*PREFIX*news_items` WHERE `feed_id` NOT IN '. '(SELECT `feeds`.`id` FROM `*PREFIX*news_feeds` `feeds`)'; - $arguments2 = [$folder->getId()]; - $this->setMapperResult($sql, $arguments); - $this->setMapperResult($sql2, $arguments2); - $this->setMapperResult($sql3); + $this->setMapperResult($sql, $arguments, [], null, null, true); + $this->setMapperResult($sql2, $arguments2, [], null, null, true); + $this->setMapperResult($sql3, [], [], null, null, true); $this->folderMapper->delete($folder); } diff --git a/tests/unit/db/ItemMapperTest.php b/tests/unit/db/ItemMapperTest.php index adb1dfe7a..8b42aebb8 100644 --- a/tests/unit/db/ItemMapperTest.php +++ b/tests/unit/db/ItemMapperTest.php @@ -85,13 +85,19 @@ class ItemMapperTest extends \OCA\News\Tests\Unit\Db\MapperTestUtility { } private function makeSelectQueryStatus($prependTo, $status, - $oldestFirst=false) { + $oldestFirst=false, $search=[]) { $status = (int) $status; - return $this->makeSelectQuery( - 'AND ((`items`.`status` & ' . $status . ') = ' . $status . ') ' . - $prependTo, $oldestFirst - ); + // WARNING: Potential SQL injection if you change this carelessly + $sql = 'AND ((`items`.`status` & ' . $status . ') = ' . $status . ') '; + + foreach ($search as $_) { + $sql .= 'AND `items`.`search_index` LIKE ? '; + } + + $sql .= $prependTo; + + return $this->makeSelectQuery($sql, $oldestFirst); } @@ -244,6 +250,20 @@ class ItemMapperTest extends \OCA\News\Tests\Unit\Db\MapperTestUtility { } + public function testFindAllFeedSearch(){ + $sql = 'AND `items`.`feed_id` = ? ' . + 'AND `items`.`id` < ? '; + $search = ['%test_\\', 'a']; + $sql = $this->makeSelectQueryStatus($sql, $this->status, false, $search); + $params = [$this->user, '%\%test\\_\\\\%', '%a%', $this->id, $this->offset]; + $this->setMapperResult($sql, $params, $this->rows, $this->limit); + $result = $this->mapper->findAllFeed($this->id, $this->limit, + $this->offset, $this->status, false, $this->user, $search); + + $this->assertEquals($this->items, $result); + } + + public function testFindAllFeedNegativeLimit(){ $sql = 'AND `items`.`feed_id` = ? ' . 'AND `items`.`id` < ? '; @@ -294,6 +314,18 @@ class ItemMapperTest extends \OCA\News\Tests\Unit\Db\MapperTestUtility { $this->assertEquals($this->items, $result); } + public function testFindAllFolderSearch(){ + $sql = 'AND `feeds`.`folder_id` = ? ' . + 'AND `items`.`id` < ? '; + $search = ['%test_\\', 'a']; + $sql = $this->makeSelectQueryStatus($sql, $this->status, false, $search); + $params = [$this->user, '%\%test\\_\\\\%', '%a%', $this->id, $this->offset]; + $this->setMapperResult($sql, $params, $this->rows, $this->limit); + $result = $this->mapper->findAllFolder($this->id, $this->limit, + $this->offset, $this->status, false, $this->user, $search); + + $this->assertEquals($this->items, $result); + } public function testFindAllFolderNegativeLimit(){ $sql = 'AND `feeds`.`folder_id` = ? ' . @@ -346,6 +378,19 @@ class ItemMapperTest extends \OCA\News\Tests\Unit\Db\MapperTestUtility { } + public function testFindAllSearch(){ + $sql = 'AND `items`.`id` < ? '; + $search = ['%tEst_\\', 'a']; + $params = [$this->user, '%\%test\\_\\\\%', '%a%', $this->offset]; + $sql = $this->makeSelectQueryStatus($sql, $this->status, false, $search); + $this->setMapperResult($sql, $params, $this->rows, $this->limit); + $result = $this->mapper->findAll($this->limit, + $this->offset, $this->status, false, $this->user, $search); + + $this->assertEquals($this->items, $result); + } + + public function testFindAllNegativeLimit(){ $sql = 'AND `items`.`id` < ? '; $sql = $this->makeSelectQueryStatus($sql, $this->status); diff --git a/tests/unit/db/ItemTest.php b/tests/unit/db/ItemTest.php index c937053d1..ec92eb4d9 100644 --- a/tests/unit/db/ItemTest.php +++ b/tests/unit/db/ItemTest.php @@ -186,6 +186,18 @@ class ItemTest extends \PHPUnit_Framework_TestCase { } + public function testSearchIndex() { + $item = new Item(); + $item->setBody('<a>somEthäng</a>'); + $item->setUrl('http://link'); + $item->setAuthor('äuthor'); + $item->setTitle('<a>tätle</a>'); + $item->generateSearchIndex(); + $expected = 'somethängäuthortätlehttp://link'; + $this->assertEquals($expected, $item->getSearchIndex()); + } + + public function testFromImport() { $item = $this->createImportItem(false); diff --git a/tests/unit/db/mappertestutility.php b/tests/unit/db/mappertestutility.php index 278cbc475..de07f136c 100644 --- a/tests/unit/db/mappertestutility.php +++ b/tests/unit/db/mappertestutility.php @@ -31,7 +31,6 @@ namespace OCA\News\Tests\Unit\Db; abstract class MapperTestUtility extends \PHPUnit_Framework_TestCase { protected $db; private $query; - private $pdoResult; private $queryAt; private $prepareAt; private $fetchAt; @@ -46,18 +45,41 @@ abstract class MapperTestUtility extends \PHPUnit_Framework_TestCase { parent::setUp(); $this->db = $this->getMockBuilder( - '\OCP\IDb') + '\OCP\IDBConnection') ->disableOriginalConstructor() ->getMock(); - $this->query = $this->getMock('Query', array('execute', 'bindValue')); - $this->pdoResult = $this->getMock('Result', array('fetch', 'closeCursor')); + $this->query = $this->getMock('\PDOStatement'); $this->queryAt = 0; $this->prepareAt = 0; - $this->iterators = array(); + $this->iterators = []; $this->fetchAt = 0; } + /** + * Checks if an array is associative + * @param array $array + * @return bool true if associative + */ + private function isAssocArray(array $array) { + return array_values($array) !== $array; + } + + /** + * Returns the correct PDO constant based on the value type + * @param $value + * @return PDO constant + */ + private function getPDOType($value) { + switch (gettype($value)) { + case 'integer': + return \PDO::PARAM_INT; + case 'boolean': + return \PDO::PARAM_BOOL; + default: + return \PDO::PARAM_STR; + } + } /** * Create mocks and set expected results for database queries @@ -68,15 +90,40 @@ abstract class MapperTestUtility extends \PHPUnit_Framework_TestCase { * of the database query. If not provided, it wont be assumed that fetch * will be called on the result */ - protected function setMapperResult($sql, $arguments=[], $returnRows=[], + protected function setMapperResult($sql, $arguments=array(), $returnRows=array(), $limit=null, $offset=null, $expectClose=false){ + if($limit === null && $offset === null) { + $this->db->expects($this->at($this->prepareAt)) + ->method('prepare') + ->with($this->equalTo($sql)) + ->will(($this->returnValue($this->query))); + } elseif($limit !== null && $offset === null) { + $this->db->expects($this->at($this->prepareAt)) + ->method('prepare') + ->with($this->equalTo($sql), $this->equalTo($limit)) + ->will(($this->returnValue($this->query))); + } elseif($limit === null && $offset !== null) { + $this->db->expects($this->at($this->prepareAt)) + ->method('prepare') + ->with($this->equalTo($sql), + $this->equalTo(null), + $this->equalTo($offset)) + ->will(($this->returnValue($this->query))); + } else { + $this->db->expects($this->at($this->prepareAt)) + ->method('prepare') + ->with($this->equalTo($sql), + $this->equalTo($limit), + $this->equalTo($offset)) + ->will(($this->returnValue($this->query))); + } $this->iterators[] = new ArgumentIterator($returnRows); $iterators = $this->iterators; $fetchAt = $this->fetchAt; - $this->pdoResult->expects($this->any()) + $this->query->expects($this->any()) ->method('fetch') ->will($this->returnCallback( function() use ($iterators, $fetchAt){ @@ -87,83 +134,55 @@ abstract class MapperTestUtility extends \PHPUnit_Framework_TestCase { $fetchAt++; } + $this->queryAt++; + return $result; } )); - if ($expectClose) { - $closing = $this->once(); + + if ($this->isAssocArray($arguments)) { + foreach($arguments as $key => $argument) { + $pdoConstant = $this->getPDOType($argument); + $this->query->expects($this->at($this->queryAt)) + ->method('bindValue') + ->with($this->equalTo($key), + $this->equalTo($argument), + $this->equalTo($pdoConstant)); + $this->queryAt++; + } } else { - $closing = $this->any(); - } - $this->pdoResult->expects($closing) - ->method('closeCursor'); - - $index = 1; - foreach($arguments as $argument) { - switch (gettype($argument)) { - case 'integer': - $pdoConstant = \PDO::PARAM_INT; - break; - - case 'NULL': - $pdoConstant = \PDO::PARAM_NULL; - break; - - case 'boolean': - $pdoConstant = \PDO::PARAM_BOOL; - break; - - default: - $pdoConstant = \PDO::PARAM_STR; - break; + $index = 1; + foreach($arguments as $argument) { + $pdoConstant = $this->getPDOType($argument); + $this->query->expects($this->at($this->queryAt)) + ->method('bindValue') + ->with($this->equalTo($index), + $this->equalTo($argument), + $this->equalTo($pdoConstant)); + $index++; + $this->queryAt++; } - $this->query->expects($this->at($this->queryAt)) - ->method('bindValue') - ->with($this->equalTo($index), - $this->equalTo($argument), - $this->equalTo($pdoConstant)); - $index++; - $this->queryAt++; } $this->query->expects($this->at($this->queryAt)) ->method('execute') - ->with() - ->will($this->returnValue($this->pdoResult)); + ->will($this->returnCallback(function($sql, $p=null, $o=null, $s=null) { + + })); $this->queryAt++; - if($limit === null && $offset === null) { - $this->db->expects($this->at($this->prepareAt)) - ->method('prepareQuery') - ->with($this->equalTo($sql), - $this->equalTo(null), - $this->equalTo(null)) - ->will(($this->returnValue($this->query))); - } elseif($limit !== null && $offset === null) { - $this->db->expects($this->at($this->prepareAt)) - ->method('prepareQuery') - ->with($this->equalTo($sql), - $this->equalTo($limit), - $this->equalTo(null)) - ->will(($this->returnValue($this->query))); - } elseif($limit === null && $offset !== null) { - $this->db->expects($this->at($this->prepareAt)) - ->method('prepareQuery') - ->with($this->equalTo($sql), - $this->equalTo(null), - $this->equalTo($offset)) - ->will(($this->returnValue($this->query))); - } else { - $this->db->expects($this->at($this->prepareAt)) - ->method('prepareQuery') - ->with($this->equalTo($sql), - $this->equalTo($limit), - $this->equalTo($offset)) - ->will(($this->returnValue($this->query))); + + + if ($expectClose) { + $closing = $this->at($this->queryAt); + } else { + $closing = $this->any(); } + $this->query->expects($closing)->method('closeCursor'); + $this->queryAt++; + $this->prepareAt++; $this->fetchAt++; - } @@ -187,4 +206,3 @@ class ArgumentIterator { } } } - diff --git a/tests/unit/fetcher/FeedFetcherTest.php b/tests/unit/fetcher/FeedFetcherTest.php index b0320de4b..e1a3997ac 100644 --- a/tests/unit/fetcher/FeedFetcherTest.php +++ b/tests/unit/fetcher/FeedFetcherTest.php @@ -217,6 +217,7 @@ class FeedFetcherTest extends \PHPUnit_Framework_TestCase { $item->setGuidHash($this->guid); $item->setBody($this->body); $item->setLastModified($this->time); + $item->generateSearchIndex(); $this->expectItem('getAuthor', $this->author); $item->setAuthor(html_entity_decode($this->author)); diff --git a/tests/unit/service/FolderServiceTest.php b/tests/unit/service/FolderServiceTest.php index c6e17bdbd..275bc8992 100644 --- a/tests/unit/service/FolderServiceTest.php +++ b/tests/unit/service/FolderServiceTest.php @@ -54,7 +54,7 @@ class FolderServiceTest extends \PHPUnit_Framework_TestCase { } - function testFindAll(){ + public function testFindAll(){ $userId = 'jack'; $return = 'hi'; $this->folderMapper->expects($this->once()) diff --git a/tests/unit/service/ItemServiceTest.php b/tests/unit/service/ItemServiceTest.php index d7bf70dc9..0c062ff4c 100644 --- a/tests/unit/service/ItemServiceTest.php +++ b/tests/unit/service/ItemServiceTest.php @@ -30,7 +30,7 @@ class ItemServiceTest extends \PHPUnit_Framework_TestCase { private $time; private $newestItemId; private $config; - + private $systemConfig; protected function setUp(){ $this->time = 222; @@ -54,8 +54,13 @@ class ItemServiceTest extends \PHPUnit_Framework_TestCase { '\OCA\News\Config\Config') ->disableOriginalConstructor() ->getMock(); + $this->systemConfig = $this->getMockBuilder( + 'OCP\IConfig') + ->disableOriginalConstructor() + ->getMock(); $this->itemService = new ItemService($this->mapper, - $this->statusFlag, $this->timeFactory, $this->config); + $this->statusFlag, $this->timeFactory, $this->config, + $this->systemConfig); $this->user = 'jack'; $this->id = 3; $this->updatedSince = 20333; @@ -125,7 +130,8 @@ class ItemServiceTest extends \PHPUnit_Framework_TestCase { $this->equalTo($this->offset), $this->equalTo($this->status), $this->equalTo(false), - $this->equalTo($this->user)) + $this->equalTo($this->user), + $this->equalTo([])) ->will($this->returnValue($this->response)); $result = $this->itemService->findAll( @@ -145,7 +151,8 @@ class ItemServiceTest extends \PHPUnit_Framework_TestCase { $this->equalTo($this->offset), $this->equalTo($this->status), $this->equalTo(true), - $this->equalTo($this->user)) + $this->equalTo($this->user), + $this->equalTo([])) ->will($this->returnValue($this->response)); $result = $this->itemService->findAll( @@ -164,7 +171,8 @@ class ItemServiceTest extends \PHPUnit_Framework_TestCase { $this->equalTo($this->offset), $this->equalTo($this->status), $this->equalTo(true), - $this->equalTo($this->user)) + $this->equalTo($this->user), + $this->equalTo([])) ->will($this->returnValue($this->response)); $result = $this->itemService->findAll( @@ -175,6 +183,28 @@ class ItemServiceTest extends \PHPUnit_Framework_TestCase { } + public function testFindAllSearch(){ + $type = FeedType::STARRED; + $search = ['test']; + $this->mapper->expects($this->once()) + ->method('findAll') + ->with( $this->equalTo($this->limit), + $this->equalTo($this->offset), + $this->equalTo($this->status), + $this->equalTo(true), + $this->equalTo($this->user), + $this->equalTo($search)) + ->will($this->returnValue($this->response)); + + $result = $this->itemService->findAll( + $this->id, $type, $this->limit, $this->offset, + $this->showAll, true, $this->user, $search + ); + $this->assertEquals($this->response, $result); + } + + + public function testStar(){ $itemId = 3; $feedId = 5; |