From 822bffced8212e7f34bcb2ad063b31a78bd57bdb Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sun, 27 Dec 2015 10:08:20 +0100 Subject: Link filter refactoring * introduce class LinkFilter to handle link filter operation (and lighten LinkDB). * handle 'private only' in filtering. * update template to prefill search fields with current search terms. * coding style. * unit test (mostly move from LinkDB to LinkFilter). PS: preparation for #358 #315 and 'AND' search. --- tests/LinkDBTest.php | 229 ++++--------------------------------- tests/LinkFilterTest.php | 242 ++++++++++++++++++++++++++++++++++++++++ tests/utils/ReferenceLinkDB.php | 5 + 3 files changed, 268 insertions(+), 208 deletions(-) create mode 100644 tests/LinkFilterTest.php (limited to 'tests') diff --git a/tests/LinkDBTest.php b/tests/LinkDBTest.php index 7b22b270..3b1a2057 100644 --- a/tests/LinkDBTest.php +++ b/tests/LinkDBTest.php @@ -302,236 +302,49 @@ class LinkDBTest extends PHPUnit_Framework_TestCase } /** - * 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 - */ - public function testFilterUnknownDay() - { - $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 + * Test real_url without redirector. */ - public function testFilterFullTextTitle() + public function testLinkRealUrlWithoutRedirector() { - // 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)) ); } - - /** - * Test real_url without redirector. - */ - public function testLinkRealUrlWithoutRedirector() - { - $db = new LinkDB(self::$testDatastore, false, false); - foreach($db as $link) { - $this->assertEquals($link['url'], $link['real_url']); - } - } - - /** - * Test real_url with redirector. - */ - public function testLinkRealUrlWithRedirector() - { - $redirector = 'http://redirector.to?'; - $db = new LinkDB(self::$testDatastore, false, false, $redirector); - foreach($db as $link) { - $this->assertStringStartsWith($redirector, $link['real_url']); - } - } } diff --git a/tests/LinkFilterTest.php b/tests/LinkFilterTest.php new file mode 100644 index 00000000..5107ab72 --- /dev/null +++ b/tests/LinkFilterTest.php @@ -0,0 +1,242 @@ +getLinks()); + } + + /** + * Blank filter. + */ + public function testFilter() + { + $this->assertEquals( + 6, + count(self::$linkFilter->filter('', '')) + ); + + // Private only. + $this->assertEquals( + 2, + count(self::$linkFilter->filter('', '', false, true)) + ); + } + + /** + * Filter links using a tag + */ + public function testFilterOneTag() + { + $this->assertEquals( + 4, + count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, 'web', false)) + ); + + // Private only. + $this->assertEquals( + 1, + count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, 'web', false, true)) + ); + } + + /** + * Filter links using a tag - case-sensitive + */ + public function testFilterCaseSensitiveTag() + { + $this->assertEquals( + 0, + count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, 'mercurial', true)) + ); + + $this->assertEquals( + 1, + count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, 'Mercurial', true)) + ); + } + + /** + * Filter links using a tag combination + */ + public function testFilterMultipleTags() + { + $this->assertEquals( + 2, + count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, 'dev cartoon', false)) + ); + } + + /** + * Filter links using a non-existent tag + */ + public function testFilterUnknownTag() + { + $this->assertEquals( + 0, + count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, 'null', false)) + ); + } + + /** + * Return links for a given day + */ + public function testFilterDay() + { + $this->assertEquals( + 3, + count(self::$linkFilter->filter(LinkFilter::$FILTER_DAY, '20121206')) + ); + } + + /** + * 404 - day not found + */ + public function testFilterUnknownDay() + { + $this->assertEquals( + 0, + count(self::$linkFilter->filter(LinkFilter::$FILTER_DAY, '19700101')) + ); + } + + /** + * Use an invalid date format + * @expectedException Exception + * @expectedExceptionMessageRegExp /Invalid date format/ + */ + public function testFilterInvalidDayWithChars() + { + self::$linkFilter->filter(LinkFilter::$FILTER_DAY, 'Rainy day, dream away'); + } + + /** + * Use an invalid date format + * @expectedException Exception + * @expectedExceptionMessageRegExp /Invalid date format/ + */ + public function testFilterInvalidDayDigits() + { + self::$linkFilter->filter(LinkFilter::$FILTER_DAY, '20'); + } + + /** + * Retrieve a link entry with its hash + */ + public function testFilterSmallHash() + { + $links = self::$linkFilter->filter(LinkFilter::$FILTER_HASH, 'IuWvgA'); + + $this->assertEquals( + 1, + count($links) + ); + + $this->assertEquals( + 'MediaGoblin', + $links['20130614_184135']['title'] + ); + } + + /** + * No link for this hash + */ + public function testFilterUnknownSmallHash() + { + $this->assertEquals( + 0, + count(self::$linkFilter->filter(LinkFilter::$FILTER_HASH, 'Iblaah')) + ); + } + + /** + * Full-text search - result from a link's URL + */ + public function testFilterFullTextURL() + { + $this->assertEquals( + 2, + count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'ars.userfriendly.org')) + ); + } + + /** + * Full-text search - result from a link's title only + */ + public function testFilterFullTextTitle() + { + // use miscellaneous cases + $this->assertEquals( + 2, + count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'userfriendly -')) + ); + $this->assertEquals( + 2, + count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'UserFriendly -')) + ); + $this->assertEquals( + 2, + count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'uSeRFrIendlY -')) + ); + + // use miscellaneous case and offset + $this->assertEquals( + 2, + count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'RFrIendL')) + ); + } + + /** + * Full-text search - result from the link's description only + */ + public function testFilterFullTextDescription() + { + $this->assertEquals( + 1, + count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'media publishing')) + ); + } + + /** + * Full-text search - result from the link's tags only + */ + public function testFilterFullTextTags() + { + $this->assertEquals( + 2, + count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'gnu')) + ); + + // Private only. + $this->assertEquals( + 1, + count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'web', false, true)) + ); + } + + /** + * Full-text search - result set from mixed sources + */ + public function testFilterFullTextMixed() + { + $this->assertEquals( + 2, + count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'free software')) + ); + } +} diff --git a/tests/utils/ReferenceLinkDB.php b/tests/utils/ReferenceLinkDB.php index 47b51829..011317ef 100644 --- a/tests/utils/ReferenceLinkDB.php +++ b/tests/utils/ReferenceLinkDB.php @@ -124,4 +124,9 @@ class ReferenceLinkDB { return $this->_privateCount; } + + public function getLinks() + { + return $this->_links; + } } -- cgit v1.2.3