From 004fcbbcc7609ca83807f2e38967ef54f469bf72 Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Sat, 23 Jul 2016 21:24:54 +0200 Subject: Move to new directory structure --- tests/Unit/Db/ItemTest.php | 316 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 316 insertions(+) create mode 100644 tests/Unit/Db/ItemTest.php (limited to 'tests/Unit/Db/ItemTest.php') diff --git a/tests/Unit/Db/ItemTest.php b/tests/Unit/Db/ItemTest.php new file mode 100644 index 000000000..b7bf76180 --- /dev/null +++ b/tests/Unit/Db/ItemTest.php @@ -0,0 +1,316 @@ + + * @author Bernhard Posselt + * @copyright Alessandro Cosentino 2012 + * @copyright Bernhard Posselt 2012, 2014 + */ + +namespace OCA\News\Db; + + +class ItemTest extends \PHPUnit_Framework_TestCase { + + private $item; + + protected function setUp(){ + $this->item = new Item(); + $this->item->setStatus(0); + } + + + public function testSetRead(){ + $this->item->setRead(); + + $this->assertTrue($this->item->isRead()); + } + + + public function testSetUnread(){ + $this->item->setUnread(); + + $this->assertTrue($this->item->isUnread()); + } + + + public function testSetStarred(){ + $this->item->setStarred(); + + $this->assertTrue($this->item->isStarred()); + } + + + public function testSetUnstarred(){ + $this->item->setUnstarred(); + + $this->assertTrue($this->item->isUnstarred()); + } + + + public function testToAPI() { + $item = new Item(); + $item->setId(3); + $item->setGuid('guid'); + $item->setGuidHash('hash'); + $item->setUrl('https://google'); + $item->setTitle('title'); + $item->setAuthor('author'); + $item->setPubDate(123); + $item->setBody('body'); + $item->setEnclosureMime('audio/ogg'); + $item->setEnclosureLink('enclink'); + $item->setRtl(true); + $item->setFeedId(1); + $item->setStatus(0); + $item->setUnread(); + $item->setStarred(); + $item->setLastModified(321); + $item->setFingerprint('fingerprint'); + $item->setContentHash('contentHash'); + + $this->assertEquals([ + 'id' => 3, + 'guid' => 'guid', + 'guidHash' => 'hash', + 'url' => 'https://google', + 'title' => 'title', + 'author' => 'author', + 'pubDate' => 123, + 'body' => 'body', + 'enclosureMime' => 'audio/ogg', + 'enclosureLink' => 'enclink', + 'feedId' => 1, + 'unread' => true, + 'starred' => true, + 'lastModified' => 321, + 'rtl' => true, + 'fingerprint' => 'fingerprint', + 'contentHash' => 'contentHash' + ], $item->toAPI()); + } + + + public function testJSONSerialize() { + $item = new Item(); + $item->setId(3); + $item->setGuid('guid'); + $item->setGuidHash('hash'); + $item->setUrl('https://google'); + $item->setTitle('title'); + $item->setAuthor('author'); + $item->setPubDate(123); + $item->setBody('
this is a test'); + $item->setEnclosureMime('audio/ogg'); + $item->setEnclosureLink('enclink'); + $item->setFeedId(1); + $item->setStatus(0); + $item->setRtl(true); + $item->setUnread(); + $item->setFingerprint('fingerprint'); + $item->setStarred(); + $item->setLastModified(321); + + $this->assertEquals([ + 'id' => 3, + 'guid' => 'guid', + 'guidHash' => 'hash', + 'url' => 'https://google', + 'title' => 'title', + 'author' => 'author', + 'pubDate' => 123, + 'body' => '
this is a test', + 'enclosureMime' => 'audio/ogg', + 'enclosureLink' => 'enclink', + 'feedId' => 1, + 'unread' => true, + 'starred' => true, + 'lastModified' => 321, + 'rtl' => true, + 'intro' => 'this is a test', + 'fingerprint' => 'fingerprint' + ], $item->jsonSerialize()); + } + + public function testToExport() { + $item = new Item(); + $item->setId(3); + $item->setGuid('guid'); + $item->setGuidHash('hash'); + $item->setUrl('https://google'); + $item->setTitle('title'); + $item->setAuthor('author'); + $item->setPubDate(123); + $item->setBody('body'); + $item->setEnclosureMime('audio/ogg'); + $item->setEnclosureLink('enclink'); + $item->setFeedId(1); + $item->setRtl(true); + $item->setStatus(0); + $item->setRead(); + $item->setStarred(); + $item->setLastModified(321); + + $feed = new Feed(); + $feed->setLink('http://test'); + $feeds = ["feed1" => $feed]; + + $this->assertEquals([ + 'guid' => 'guid', + 'url' => 'https://google', + 'title' => 'title', + 'author' => 'author', + 'pubDate' => 123, + 'body' => 'body', + 'enclosureMime' => 'audio/ogg', + 'enclosureLink' => 'enclink', + 'unread' => false, + 'starred' => true, + 'feedLink' => 'http://test', + 'rtl' => true + ], $item->toExport($feeds)); + } + + + private function createImportItem($isRead) { + $item = new Item(); + $item->setGuid('guid'); + $item->setGuidHash('guid'); + $item->setUrl('https://google'); + $item->setTitle('title'); + $item->setAuthor('author'); + $item->setPubDate(123); + $item->setBody('body'); + $item->setEnclosureMime('audio/ogg'); + $item->setEnclosureLink('enclink'); + $item->setStarred(); + $item->setRtl(true); + + if ($isRead) { + $item->setUnread(); + } else { + $item->setRead(); + } + + return $item; + } + + + public function testSearchIndex() { + $item = new Item(); + $item->setBody('somEthäng'); + $item->setUrl('http://link'); + $item->setAuthor('äuthor'); + $item->setTitle('tätle'); + $item->generateSearchIndex(); + $expected = 'somethängäuthortätlehttp://link'; + $this->assertEquals($expected, $item->getSearchIndex()); + } + + + public function testFromImport() { + $item = $this->createImportItem(false); + + $import = [ + 'guid' => $item->getGuid(), + 'url' => $item->getUrl(), + 'title' => $item->getTitle(), + 'author' => $item->getAuthor(), + 'pubDate' => $item->getPubDate(), + 'body' => $item->getBody(), + 'enclosureMime' => $item->getEnclosureMime(), + 'enclosureLink' => $item->getEnclosureLink(), + 'unread' => $item->isUnread(), + 'starred' => $item->isStarred(), + 'rtl' => $item->getRtl() + ]; + + $compareWith = Item::fromImport($import); + + $this->assertEquals($item, $compareWith); + } + + + public function testFromImportRead() { + $item = $this->createImportItem(true); + + $import = [ + 'guid' => $item->getGuid(), + 'url' => $item->getUrl(), + 'title' => $item->getTitle(), + 'author' => $item->getAuthor(), + 'pubDate' => $item->getPubDate(), + 'body' => $item->getBody(), + 'enclosureMime' => $item->getEnclosureMime(), + 'enclosureLink' => $item->getEnclosureLink(), + 'unread' => $item->isUnread(), + 'starred' => $item->isStarred(), + 'rtl' => $item->getRtl() + ]; + + $compareWith = Item::fromImport($import); + + $this->assertEquals($item, $compareWith); + } + + + + public function testSetAuthor(){ + $item = new Item(); + $item->setAuthor('my link'); + $this->assertEquals('my link', $item->getAuthor()); + $this->assertContains('author', $item->getUpdatedFields()); + } + + + public function testSetTitle(){ + $item = new Item(); + $item->setTitle('my link'); + $this->assertEquals('my link', $item->getTitle()); + $this->assertContains('title', $item->getUpdatedFields()); + } + + + public function testSetXSSUrl() { + $item = new Item(); + $item->setUrl('javascript:alert()'); + $this->assertEquals('', $item->getUrl()); + } + + + public function testSetMagnetUrl() { + $item = new Item(); + $item->setUrl('magnet://link.com'); + $this->assertEquals('magnet://link.com', $item->getUrl()); + } + + + public function testMakeLinksInBodyOpenNewTab() { + $item = new Item(); + $item->setBody("ha"); + $this->assertEquals("ha", + $item->getBody()); + } + + public function testComputeFingerPrint() { + $title = 'a'; + $body = 'b'; + $url = 'http://google.com'; + $link = 'ho'; + + $item = new Item(); + $item->setBody($body); + $item->setTitle($title); + $item->setUrl($url); + $item->setEnclosureLink($link); + $item->generateSearchIndex(); + + $this->assertEquals(md5($title . $url . $body . $link), + $item->getFingerprint()); + } + +} -- cgit v1.2.3