Refactor filter in LinkDB

* search type now carried by LinkDB in order to factorize code between different search sources.
  * LinkDB->filter split in 3 method: filterSearch, filterHash, filterDay (we know what type of filter is needed).
  * filterHash now throw a LinkNotFoundException if it doesn't exist: internal implementation choice, still displays a 404.
  * Smallhash regex has been rewritten.
  * Unit tests update
This commit is contained in:
ArthurHoaro 2016-03-21 21:40:49 +01:00
parent ee88a4bcc2
commit 528a6f8a23
8 changed files with 158 additions and 106 deletions

View file

@ -17,8 +17,20 @@ class LinkDBTest extends PHPUnit_Framework_TestCase
{
// datastore to test write operations
protected static $testDatastore = 'sandbox/datastore.php';
/**
* @var ReferenceLinkDB instance.
*/
protected static $refDB = null;
/**
* @var LinkDB public LinkDB instance.
*/
protected static $publicLinkDB = null;
/**
* @var LinkDB private LinkDB instance.
*/
protected static $privateLinkDB = null;
/**
@ -335,9 +347,10 @@ class LinkDBTest extends PHPUnit_Framework_TestCase
public function testFilterString()
{
$tags = 'dev cartoon';
$request = array('searchtags' => $tags);
$this->assertEquals(
2,
count(self::$privateLinkDB->filter(LinkFilter::$FILTER_TAG, $tags, true, false))
count(self::$privateLinkDB->filterSearch($request, true, false))
);
}
@ -347,9 +360,10 @@ class LinkDBTest extends PHPUnit_Framework_TestCase
public function testFilterArray()
{
$tags = array('dev', 'cartoon');
$request = array('searchtags' => $tags);
$this->assertEquals(
2,
count(self::$privateLinkDB->filter(LinkFilter::$FILTER_TAG, $tags, true, false))
count(self::$privateLinkDB->filterSearch($request, true, false))
);
}
@ -360,14 +374,48 @@ class LinkDBTest extends PHPUnit_Framework_TestCase
public function testHiddenTags()
{
$tags = '.hidden';
$request = array('searchtags' => $tags);
$this->assertEquals(
1,
count(self::$privateLinkDB->filter(LinkFilter::$FILTER_TAG, $tags, true, false))
count(self::$privateLinkDB->filterSearch($request, true, false))
);
$this->assertEquals(
0,
count(self::$publicLinkDB->filter(LinkFilter::$FILTER_TAG, $tags, true, false))
count(self::$publicLinkDB->filterSearch($request, true, false))
);
}
/**
* Test filterHash() with a valid smallhash.
*/
public function testFilterHashValid()
{
$request = smallHash('20150310_114651');
$this->assertEquals(
1,
count(self::$publicLinkDB->filterHash($request))
);
}
/**
* Test filterHash() with an invalid smallhash.
*
* @expectedException LinkNotFoundException
*/
public function testFilterHashInValid1()
{
$request = 'blabla';
self::$publicLinkDB->filterHash($request);
}
/**
* Test filterHash() with an empty smallhash.
*
* @expectedException LinkNotFoundException
*/
public function testFilterHashInValid()
{
self::$publicLinkDB->filterHash('');
}
}

View file

@ -165,13 +165,12 @@ class LinkFilterTest extends PHPUnit_Framework_TestCase
/**
* No link for this hash
*
* @expectedException LinkNotFoundException
*/
public function testFilterUnknownSmallHash()
{
$this->assertEquals(
0,
count(self::$linkFilter->filter(LinkFilter::$FILTER_HASH, 'Iblaah'))
);
self::$linkFilter->filter(LinkFilter::$FILTER_HASH, 'Iblaah');
}
/**

View file

@ -236,9 +236,9 @@ class UpdaterTest extends PHPUnit_Framework_TestCase
$refDB = new ReferenceLinkDB();
$refDB->write(self::$testDatastore);
$linkDB = new LinkDB(self::$testDatastore, true, false);
$this->assertEmpty($linkDB->filter(LinkFilter::$FILTER_TAG, 'exclude'));
$this->assertEmpty($linkDB->filterSearch(array('searchtags' => 'exclude')));
$updater = new Updater(array(), self::$configFields, $linkDB, true);
$updater->updateMethodRenameDashTags();
$this->assertNotEmpty($linkDB->filter(LinkFilter::$FILTER_TAG, 'exclude'));
$this->assertNotEmpty($linkDB->filterSearch(array('searchtags' => 'exclude')));
}
}