X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=tests%2FLinkDBTest.php;h=3b1a20572a75d428724c8dcf37f50a0f1edb4ff7;hb=9f57d1916cc4d21fdeeb5506681eaae177b61660;hp=d34ea4f568bf4dcef51456615bfd9e00cb8dd602;hpb=bba021defce15184c986d5e3db138bdbdd4df5d5;p=github%2Fshaarli%2FShaarli.git diff --git a/tests/LinkDBTest.php b/tests/LinkDBTest.php index d34ea4f5..3b1a2057 100644 --- a/tests/LinkDBTest.php +++ b/tests/LinkDBTest.php @@ -3,6 +3,8 @@ * Link datastore tests */ +require_once 'application/Cache.php'; +require_once 'application/FileUtils.php'; require_once 'application/LinkDB.php'; require_once 'application/Utils.php'; require_once 'tests/utils/ReferenceLinkDB.php'; @@ -14,7 +16,7 @@ require_once 'tests/utils/ReferenceLinkDB.php'; class LinkDBTest extends PHPUnit_Framework_TestCase { // datastore to test write operations - protected static $testDatastore = 'tests/datastore.php'; + protected static $testDatastore = 'sandbox/datastore.php'; protected static $refDB = null; protected static $publicLinkDB = null; protected static $privateLinkDB = null; @@ -86,8 +88,8 @@ class LinkDBTest extends PHPUnit_Framework_TestCase /** * Attempt to instantiate a LinkDB whereas the datastore is not writable * - * @expectedException PHPUnit_Framework_Error_Warning - * @expectedExceptionMessageRegExp /failed to open stream: No such file or directory/ + * @expectedException IOException + * @expectedExceptionMessageRegExp /Error accessing null/ */ public function testConstructDatastoreNotWriteable() { @@ -180,11 +182,7 @@ class LinkDBTest extends PHPUnit_Framework_TestCase 'tags'=>'unit test' ); $testDB[$link['linkdate']] = $link; - - // TODO: move PageCache to a proper class/file - function invalidateCaches() {} - - $testDB->savedb(); + $testDB->savedb('tests'); $testDB = new LinkDB(self::$testDatastore, true, false); $this->assertEquals($dbSize + 1, sizeof($testDB)); @@ -228,12 +226,12 @@ class LinkDBTest extends PHPUnit_Framework_TestCase public function testDays() { $this->assertEquals( - ['20121206', '20130614', '20150310'], + array('20121206', '20130614', '20150310'), self::$publicLinkDB->days() ); $this->assertEquals( - ['20121206', '20130614', '20141125', '20150310'], + array('20121206', '20130614', '20141125', '20150310'), self::$privateLinkDB->days() ); } @@ -269,7 +267,7 @@ class LinkDBTest extends PHPUnit_Framework_TestCase public function testAllTags() { $this->assertEquals( - [ + array( 'web' => 3, 'cartoon' => 2, 'gnu' => 2, @@ -279,12 +277,12 @@ class LinkDBTest extends PHPUnit_Framework_TestCase 'software' => 1, 'stallman' => 1, 'free' => 1 - ], + ), self::$publicLinkDB->allTags() ); $this->assertEquals( - [ + array( 'web' => 4, 'cartoon' => 3, 'gnu' => 2, @@ -298,220 +296,55 @@ class LinkDBTest extends PHPUnit_Framework_TestCase 'w3c' => 1, 'css' => 1, 'Mercurial' => 1 - ], + ), self::$privateLinkDB->allTags() ); } /** - * Filter links using a tag - */ - public function testFilterOneTag() - { - $this->assertEquals( - 3, - sizeof(self::$publicLinkDB->filterTags('web', false)) - ); - - $this->assertEquals( - 4, - sizeof(self::$privateLinkDB->filterTags('web', false)) - ); - } - - /** - * Filter links using a tag - case-sensitive - */ - public function testFilterCaseSensitiveTag() - { - $this->assertEquals( - 0, - sizeof(self::$privateLinkDB->filterTags('mercurial', true)) - ); - - $this->assertEquals( - 1, - sizeof(self::$privateLinkDB->filterTags('Mercurial', true)) - ); - } - - /** - * Filter links using a tag combination - */ - public function testFilterMultipleTags() - { - $this->assertEquals( - 1, - sizeof(self::$publicLinkDB->filterTags('dev cartoon', false)) - ); - - $this->assertEquals( - 2, - sizeof(self::$privateLinkDB->filterTags('dev cartoon', false)) - ); - } - - /** - * Filter links using a non-existent tag - */ - public function testFilterUnknownTag() - { - $this->assertEquals( - 0, - sizeof(self::$publicLinkDB->filterTags('null', false)) - ); - } - - /** - * Return links for a given day - */ - public function testFilterDay() - { - $this->assertEquals( - 2, - sizeof(self::$publicLinkDB->filterDay('20121206')) - ); - - $this->assertEquals( - 3, - sizeof(self::$privateLinkDB->filterDay('20121206')) - ); - } - - /** - * 404 - day not found + * Test real_url without redirector. */ - public function testFilterUnknownDay() + public function testLinkRealUrlWithoutRedirector() { - $this->assertEquals( - 0, - sizeof(self::$publicLinkDB->filterDay('19700101')) - ); - - $this->assertEquals( - 0, - sizeof(self::$privateLinkDB->filterDay('19700101')) - ); - } - - /** - * Use an invalid date format - * @expectedException Exception - * @expectedExceptionMessageRegExp /Invalid date format/ - */ - public function testFilterInvalidDayWithChars() - { - self::$privateLinkDB->filterDay('Rainy day, dream away'); - } - - /** - * Use an invalid date format - * @expectedException Exception - * @expectedExceptionMessageRegExp /Invalid date format/ - */ - public function testFilterInvalidDayDigits() - { - self::$privateLinkDB->filterDay('20'); - } - - /** - * Retrieve a link entry with its hash - */ - public function testFilterSmallHash() - { - $links = self::$privateLinkDB->filterSmallHash('IuWvgA'); - - $this->assertEquals( - 1, - sizeof($links) - ); - - $this->assertEquals( - 'MediaGoblin', - $links['20130614_184135']['title'] - ); - - } - - /** - * No link for this hash - */ - public function testFilterUnknownSmallHash() - { - $this->assertEquals( - 0, - sizeof(self::$privateLinkDB->filterSmallHash('Iblaah')) - ); - } - - /** - * Full-text search - result from a link's URL - */ - public function testFilterFullTextURL() - { - $this->assertEquals( - 2, - sizeof(self::$publicLinkDB->filterFullText('ars.userfriendly.org')) - ); - } - - /** - * Full-text search - result from a link's title only - */ - public function testFilterFullTextTitle() - { - // use miscellaneous cases - $this->assertEquals( - 2, - sizeof(self::$publicLinkDB->filterFullText('userfriendly -')) - ); - $this->assertEquals( - 2, - sizeof(self::$publicLinkDB->filterFullText('UserFriendly -')) - ); - $this->assertEquals( - 2, - sizeof(self::$publicLinkDB->filterFullText('uSeRFrIendlY -')) - ); - - // use miscellaneous case and offset - $this->assertEquals( - 2, - sizeof(self::$publicLinkDB->filterFullText('RFrIendL')) - ); + $db = new LinkDB(self::$testDatastore, false, false); + foreach($db as $link) { + $this->assertEquals($link['url'], $link['real_url']); + } } /** - * Full-text search - result from the link's description only + * Test real_url with redirector. */ - public function testFilterFullTextDescription() + public function testLinkRealUrlWithRedirector() { - $this->assertEquals( - 1, - sizeof(self::$publicLinkDB->filterFullText('media publishing')) - ); + $redirector = 'http://redirector.to?'; + $db = new LinkDB(self::$testDatastore, false, false, $redirector); + foreach($db as $link) { + $this->assertStringStartsWith($redirector, $link['real_url']); + } } /** - * Full-text search - result from the link's tags only + * Test filter with string. */ - public function testFilterFullTextTags() + public function testFilterString() { + $tags = 'dev cartoon'; $this->assertEquals( 2, - sizeof(self::$publicLinkDB->filterFullText('gnu')) + count(self::$privateLinkDB->filter(LinkFilter::$FILTER_TAG, $tags, true, false)) ); } /** - * Full-text search - result set from mixed sources + * Test filter with string. */ - public function testFilterFullTextMixed() + public function testFilterArray() { + $tags = array('dev', 'cartoon'); $this->assertEquals( 2, - sizeof(self::$publicLinkDB->filterFullText('free software')) + count(self::$privateLinkDB->filter(LinkFilter::$FILTER_TAG, $tags, true, false)) ); } } -?>