X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=tests%2FLinkDBTest.php;h=765f771ec5381c1cbf8f3fe21843fc892488e566;hb=195acf9f0931aa3b1a6db8832ab52a260d11090d;hp=8b0bd23b6c36ad845aff9596585778d3de1d6122;hpb=e92f1ba59edb9fd60b185dc633e64a62dffe3b04;p=github%2Fshaarli%2FShaarli.git diff --git a/tests/LinkDBTest.php b/tests/LinkDBTest.php index 8b0bd23b..765f771e 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() { @@ -103,7 +105,7 @@ class LinkDBTest extends PHPUnit_Framework_TestCase unlink(self::$testDatastore); $this->assertFileNotExists(self::$testDatastore); - $checkDB = self::getMethod('checkDB'); + $checkDB = self::getMethod('_checkDB'); $checkDB->invokeArgs($linkDB, array()); $this->assertFileExists(self::$testDatastore); @@ -120,7 +122,7 @@ class LinkDBTest extends PHPUnit_Framework_TestCase $datastoreSize = filesize(self::$testDatastore); $this->assertGreaterThan(0, $datastoreSize); - $checkDB = self::getMethod('checkDB'); + $checkDB = self::getMethod('_checkDB'); $checkDB->invokeArgs($linkDB, array()); // ensure the datastore is left unmodified @@ -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, @@ -278,13 +276,14 @@ class LinkDBTest extends PHPUnit_Framework_TestCase 'media' => 1, 'software' => 1, 'stallman' => 1, - 'free' => 1 - ], + 'free' => 1, + '-exclude' => 1, + ), self::$publicLinkDB->allTags() ); $this->assertEquals( - [ + array( 'web' => 4, 'cartoon' => 3, 'gnu' => 2, @@ -297,221 +296,76 @@ class LinkDBTest extends PHPUnit_Framework_TestCase 'html' => 1, 'w3c' => 1, 'css' => 1, - 'Mercurial' => 1 - ], + 'Mercurial' => 1, + '-exclude' => 1, + '.hidden' => 1, + ), self::$privateLinkDB->allTags() ); } /** - * Filter links using a tag + * Test real_url without redirector. */ - public function testFilterOneTag() + public function testLinkRealUrlWithoutRedirector() { - $this->assertEquals( - 3, - sizeof(self::$publicLinkDB->filterTags('web', false)) - ); - - $this->assertEquals( - 4, - sizeof(self::$privateLinkDB->filterTags('web', false)) - ); + $db = new LinkDB(self::$testDatastore, false, false); + foreach($db as $link) { + $this->assertEquals($link['url'], $link['real_url']); + } } /** - * Filter links using a tag - case-sensitive + * Test real_url with redirector. */ - public function testFilterCaseSensitiveTag() + public function testLinkRealUrlWithRedirector() { - $this->assertEquals( - 0, - sizeof(self::$privateLinkDB->filterTags('mercurial', true)) - ); - - $this->assertEquals( - 1, - sizeof(self::$privateLinkDB->filterTags('Mercurial', true)) - ); + $redirector = 'http://redirector.to?'; + $db = new LinkDB(self::$testDatastore, false, false, $redirector); + foreach($db as $link) { + $this->assertStringStartsWith($redirector, $link['real_url']); + } } /** - * Filter links using a tag combination + * Test filter with string. */ - public function testFilterMultipleTags() + public function testFilterString() { - $this->assertEquals( - 1, - sizeof(self::$publicLinkDB->filterTags('dev cartoon', false)) - ); - + $tags = 'dev cartoon'; $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)) + count(self::$privateLinkDB->filter(LinkFilter::$FILTER_TAG, $tags, true, false)) ); } /** - * Return links for a given day + * Test filter with string. */ - public function testFilterDay() + public function testFilterArray() { + $tags = array('dev', 'cartoon'); $this->assertEquals( 2, - sizeof(self::$publicLinkDB->filterDay('20121206')) - ); - - $this->assertEquals( - 3, - sizeof(self::$privateLinkDB->filterDay('20121206')) - ); - } - - /** - * 404 - day not found - */ - public function testFilterUnknownDay() - { - $this->assertEquals( - 0, - sizeof(self::$publicLinkDB->filterDay('19700101')) - ); - - $this->assertEquals( - 0, - sizeof(self::$privateLinkDB->filterDay('19700101')) + count(self::$privateLinkDB->filter(LinkFilter::$FILTER_TAG, $tags, true, false)) ); } /** - * 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/ + * Test hidden tags feature: + * tags starting with a dot '.' are only visible when logged in. */ - public function testFilterInvalidDayDigits() + public function testHiddenTags() { - self::$privateLinkDB->filterDay('20'); - } - - /** - * Retrieve a link entry with its hash - */ - public function testFilterSmallHash() - { - $links = self::$privateLinkDB->filterSmallHash('IuWvgA'); - + $tags = '.hidden'; $this->assertEquals( 1, - sizeof($links) - ); - - $this->assertEquals( - 'MediaGoblin', - $links['20130614_184135']['title'] + count(self::$privateLinkDB->filter(LinkFilter::$FILTER_TAG, $tags, true, false)) ); - - } - /** - * 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')) - ); - } - - /** - * Full-text search - result from the link's description only - */ - public function testFilterFullTextDescription() - { - $this->assertEquals( - 1, - sizeof(self::$publicLinkDB->filterFullText('media publishing')) - ); - } - - /** - * Full-text search - result from the link's tags only - */ - public function testFilterFullTextTags() - { - $this->assertEquals( - 2, - sizeof(self::$publicLinkDB->filterFullText('gnu')) - ); - } - - /** - * Full-text search - result set from mixed sources - */ - public function testFilterFullTextMixed() - { - $this->assertEquals( - 2, - sizeof(self::$publicLinkDB->filterFullText('free software')) + count(self::$publicLinkDB->filter(LinkFilter::$FILTER_TAG, $tags, true, false)) ); } } -?>