2015-12-27 10:08:20 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once 'application/LinkFilter.php';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class LinkFilterTest.
|
|
|
|
*/
|
|
|
|
class LinkFilterTest extends PHPUnit_Framework_TestCase
|
|
|
|
{
|
2017-09-02 15:10:44 +02:00
|
|
|
/**
|
|
|
|
* @var string Test datastore path.
|
|
|
|
*/
|
|
|
|
protected static $testDatastore = 'sandbox/datastore.php';
|
2015-12-27 10:08:20 +01:00
|
|
|
/**
|
|
|
|
* @var LinkFilter instance.
|
|
|
|
*/
|
|
|
|
protected static $linkFilter;
|
|
|
|
|
2017-01-16 13:57:11 +01:00
|
|
|
/**
|
|
|
|
* @var ReferenceLinkDB instance
|
|
|
|
*/
|
|
|
|
protected static $refDB;
|
|
|
|
|
2017-09-02 15:10:44 +02:00
|
|
|
/**
|
|
|
|
* @var LinkDB instance
|
|
|
|
*/
|
|
|
|
protected static $linkDB;
|
|
|
|
|
2015-12-27 10:08:20 +01:00
|
|
|
/**
|
|
|
|
* Instanciate linkFilter with ReferenceLinkDB data.
|
|
|
|
*/
|
|
|
|
public static function setUpBeforeClass()
|
|
|
|
{
|
2017-01-16 13:57:11 +01:00
|
|
|
self::$refDB = new ReferenceLinkDB();
|
2017-09-02 15:10:44 +02:00
|
|
|
self::$refDB->write(self::$testDatastore);
|
|
|
|
self::$linkDB = new LinkDB(self::$testDatastore, true, false);
|
|
|
|
self::$linkFilter = new LinkFilter(self::$linkDB);
|
2015-12-27 10:08:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Blank filter.
|
|
|
|
*/
|
|
|
|
public function testFilter()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
2017-01-16 13:57:11 +01:00
|
|
|
self::$refDB->countLinks(),
|
2015-12-27 10:08:20 +01:00
|
|
|
count(self::$linkFilter->filter('', ''))
|
|
|
|
);
|
|
|
|
|
2017-01-16 13:57:11 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
self::$refDB->countLinks(),
|
|
|
|
count(self::$linkFilter->filter('', '', 'all'))
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
self::$refDB->countLinks(),
|
|
|
|
count(self::$linkFilter->filter('', '', 'randomstr'))
|
|
|
|
);
|
|
|
|
|
2015-12-27 10:08:20 +01:00
|
|
|
// Private only.
|
|
|
|
$this->assertEquals(
|
2017-01-16 13:57:11 +01:00
|
|
|
self::$refDB->countPrivateLinks(),
|
|
|
|
count(self::$linkFilter->filter('', '', false, 'private'))
|
|
|
|
);
|
|
|
|
|
|
|
|
// Public only.
|
|
|
|
$this->assertEquals(
|
|
|
|
self::$refDB->countPublicLinks(),
|
|
|
|
count(self::$linkFilter->filter('', '', false, 'public'))
|
2015-12-27 10:08:20 +01:00
|
|
|
);
|
2016-02-23 19:21:14 +01:00
|
|
|
|
|
|
|
$this->assertEquals(
|
2016-03-12 17:54:56 +01:00
|
|
|
ReferenceLinkDB::$NB_LINKS_TOTAL,
|
2016-02-23 19:21:14 +01:00
|
|
|
count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, ''))
|
|
|
|
);
|
|
|
|
|
2017-04-01 12:17:37 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
self::$refDB->countUntaggedLinks(),
|
2017-06-01 17:55:26 +02:00
|
|
|
count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, /*$request=*/'', /*$casesensitive=*/false, /*$visibility=*/'all', /*$untaggedonly=*/true))
|
2017-04-01 12:17:37 +02:00
|
|
|
);
|
|
|
|
|
2016-02-23 19:21:14 +01:00
|
|
|
$this->assertEquals(
|
2016-03-12 17:54:56 +01:00
|
|
|
ReferenceLinkDB::$NB_LINKS_TOTAL,
|
2016-02-23 19:21:14 +01:00
|
|
|
count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, ''))
|
|
|
|
);
|
2015-12-27 10:08:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter links using a tag
|
|
|
|
*/
|
|
|
|
public function testFilterOneTag()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
|
|
|
4,
|
|
|
|
count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, 'web', false))
|
|
|
|
);
|
|
|
|
|
2017-01-16 13:57:11 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
4,
|
|
|
|
count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, 'web', false, 'all'))
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
4,
|
|
|
|
count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, 'web', false, 'default-blabla'))
|
|
|
|
);
|
|
|
|
|
2015-12-27 10:08:20 +01:00
|
|
|
// Private only.
|
|
|
|
$this->assertEquals(
|
|
|
|
1,
|
2017-01-16 13:57:11 +01:00
|
|
|
count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, 'web', false, 'private'))
|
|
|
|
);
|
|
|
|
|
|
|
|
// Public only.
|
|
|
|
$this->assertEquals(
|
|
|
|
3,
|
|
|
|
count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, 'web', false, 'public'))
|
2015-12-27 10:08:20 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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(
|
2017-04-01 12:17:37 +02:00
|
|
|
4,
|
2015-12-27 10:08:20 +01:00
|
|
|
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',
|
2016-11-28 16:17:25 +01:00
|
|
|
$links[7]['title']
|
2015-12-27 10:08:20 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* No link for this hash
|
2016-03-21 21:40:49 +01:00
|
|
|
*
|
|
|
|
* @expectedException LinkNotFoundException
|
2015-12-27 10:08:20 +01:00
|
|
|
*/
|
|
|
|
public function testFilterUnknownSmallHash()
|
|
|
|
{
|
2016-03-21 21:40:49 +01:00
|
|
|
self::$linkFilter->filter(LinkFilter::$FILTER_HASH, 'Iblaah');
|
2015-12-27 10:08:20 +01:00
|
|
|
}
|
|
|
|
|
2016-02-02 19:42:48 +01:00
|
|
|
/**
|
|
|
|
* Full-text search - no result found.
|
|
|
|
*/
|
|
|
|
public function testFilterFullTextNoResult()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
|
|
|
0,
|
|
|
|
count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'azertyuiop'))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-12-27 10:08:20 +01:00
|
|
|
/**
|
|
|
|
* 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'))
|
|
|
|
);
|
2016-01-24 07:13:11 +01:00
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
2,
|
|
|
|
count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'ars org'))
|
|
|
|
);
|
2015-12-27 10:08:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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,
|
2016-01-24 07:13:11 +01:00
|
|
|
count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'publishing media'))
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
1,
|
|
|
|
count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'mercurial w3c'))
|
2015-12-27 10:08:20 +01:00
|
|
|
);
|
2016-01-24 07:13:11 +01:00
|
|
|
|
|
|
|
$this->assertEquals(
|
2016-02-01 20:33:58 +01:00
|
|
|
3,
|
2016-01-24 07:13:11 +01:00
|
|
|
count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, '"free software"'))
|
2018-10-13 00:35:47 +02:00
|
|
|
);
|
2015-12-27 10:08:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Full-text search - result from the link's tags only
|
|
|
|
*/
|
|
|
|
public function testFilterFullTextTags()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
2017-01-16 13:57:11 +01:00
|
|
|
6,
|
|
|
|
count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'web'))
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
6,
|
|
|
|
count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'web', 'all'))
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
6,
|
|
|
|
count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'web', 'bla'))
|
2015-12-27 10:08:20 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// Private only.
|
|
|
|
$this->assertEquals(
|
|
|
|
1,
|
2017-01-16 13:57:11 +01:00
|
|
|
count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'web', false, 'private'))
|
|
|
|
);
|
|
|
|
|
|
|
|
// Public only.
|
|
|
|
$this->assertEquals(
|
|
|
|
5,
|
|
|
|
count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'web', false, 'public'))
|
2015-12-27 10:08:20 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Full-text search - result set from mixed sources
|
|
|
|
*/
|
|
|
|
public function testFilterFullTextMixed()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
2016-02-01 20:33:58 +01:00
|
|
|
3,
|
2015-12-27 10:08:20 +01:00
|
|
|
count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'free software'))
|
|
|
|
);
|
|
|
|
}
|
2016-01-20 23:34:33 +01:00
|
|
|
|
2016-02-01 20:33:58 +01:00
|
|
|
/**
|
|
|
|
* Full-text search - test exclusion with '-'.
|
|
|
|
*/
|
|
|
|
public function testExcludeSearch()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
|
|
|
1,
|
2016-02-02 19:42:48 +01:00
|
|
|
count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'free -gnu'))
|
2016-02-01 20:33:58 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2017-04-01 12:17:37 +02:00
|
|
|
ReferenceLinkDB::$NB_LINKS_TOTAL - 1,
|
2016-02-02 19:42:48 +01:00
|
|
|
count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, '-revolution'))
|
2016-02-01 20:33:58 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-02-02 19:42:48 +01:00
|
|
|
* Full-text search - test AND, exact terms and exclusion combined, across fields.
|
2016-02-01 20:33:58 +01:00
|
|
|
*/
|
|
|
|
public function testMultiSearch()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
|
|
|
2,
|
2016-02-02 19:42:48 +01:00
|
|
|
count(self::$linkFilter->filter(
|
|
|
|
LinkFilter::$FILTER_TEXT,
|
|
|
|
'"Free Software " stallman "read this" @website stuff'
|
|
|
|
))
|
2016-02-01 20:33:58 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
1,
|
2016-02-02 19:42:48 +01:00
|
|
|
count(self::$linkFilter->filter(
|
|
|
|
LinkFilter::$FILTER_TEXT,
|
|
|
|
'"free software " stallman "read this" -beard @website stuff'
|
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Full-text search - make sure that exact search won't work across fields.
|
|
|
|
*/
|
|
|
|
public function testSearchExactTermMultiFieldsKo()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
|
|
|
0,
|
|
|
|
count(self::$linkFilter->filter(
|
|
|
|
LinkFilter::$FILTER_TEXT,
|
|
|
|
'"designer naming"'
|
|
|
|
))
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
0,
|
|
|
|
count(self::$linkFilter->filter(
|
|
|
|
LinkFilter::$FILTER_TEXT,
|
|
|
|
'"designernaming"'
|
|
|
|
))
|
2016-02-01 20:33:58 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-01-20 23:34:33 +01:00
|
|
|
/**
|
|
|
|
* Tag search with exclusion.
|
|
|
|
*/
|
|
|
|
public function testTagFilterWithExclusion()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
|
|
|
1,
|
|
|
|
count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, 'gnu -free'))
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2017-04-01 12:17:37 +02:00
|
|
|
ReferenceLinkDB::$NB_LINKS_TOTAL - 1,
|
2016-01-20 23:34:33 +01:00
|
|
|
count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, '-free'))
|
|
|
|
);
|
|
|
|
}
|
2016-02-23 19:21:14 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test crossed search (terms + tags).
|
|
|
|
*/
|
|
|
|
public function testFilterCrossedSearch()
|
|
|
|
{
|
|
|
|
$terms = '"Free Software " stallman "read this" @website stuff';
|
|
|
|
$tags = 'free';
|
|
|
|
$this->assertEquals(
|
|
|
|
1,
|
|
|
|
count(self::$linkFilter->filter(
|
|
|
|
LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT,
|
|
|
|
array($tags, $terms)
|
|
|
|
))
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
2,
|
|
|
|
count(self::$linkFilter->filter(
|
|
|
|
LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT,
|
|
|
|
array('', $terms)
|
|
|
|
))
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2017-04-01 12:17:37 +02:00
|
|
|
1,
|
|
|
|
count(self::$linkFilter->filter(
|
|
|
|
LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT,
|
|
|
|
array(false, 'PSR-2')
|
|
|
|
))
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2016-02-23 19:21:14 +01:00
|
|
|
1,
|
|
|
|
count(self::$linkFilter->filter(
|
|
|
|
LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT,
|
|
|
|
array($tags, '')
|
|
|
|
))
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2016-03-12 17:54:56 +01:00
|
|
|
ReferenceLinkDB::$NB_LINKS_TOTAL,
|
2016-02-23 19:21:14 +01:00
|
|
|
count(self::$linkFilter->filter(
|
|
|
|
LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT,
|
|
|
|
''
|
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
2016-05-10 23:18:04 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter links by #hashtag.
|
|
|
|
*/
|
|
|
|
public function testFilterByHashtag()
|
|
|
|
{
|
|
|
|
$hashtag = 'hashtag';
|
|
|
|
$this->assertEquals(
|
|
|
|
3,
|
|
|
|
count(self::$linkFilter->filter(
|
|
|
|
LinkFilter::$FILTER_TAG,
|
|
|
|
$hashtag
|
|
|
|
))
|
|
|
|
);
|
|
|
|
|
|
|
|
$hashtag = 'private';
|
|
|
|
$this->assertEquals(
|
|
|
|
1,
|
|
|
|
count(self::$linkFilter->filter(
|
|
|
|
LinkFilter::$FILTER_TAG,
|
|
|
|
$hashtag,
|
|
|
|
false,
|
2017-01-16 13:57:11 +01:00
|
|
|
'private'
|
2016-05-10 23:18:04 +02:00
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
2015-12-27 10:08:20 +01:00
|
|
|
}
|