Merge pull request from ArthurHoaro/feature/history

History mechanism
This commit is contained in:
ArthurHoaro 2017-05-06 17:12:06 +02:00 committed by GitHub
commit f9ff7f1b69
11 changed files with 697 additions and 60 deletions

108
tests/FileUtilsTest.php Normal file
View file

@ -0,0 +1,108 @@
<?php
require_once 'application/FileUtils.php';
/**
* Class FileUtilsTest
*
* Test file utility class.
*/
class FileUtilsTest extends PHPUnit_Framework_TestCase
{
/**
* @var string Test file path.
*/
protected static $file = 'sandbox/flat.db';
/**
* Delete test file after every test.
*/
public function tearDown()
{
@unlink(self::$file);
}
/**
* Test writeDB, then readDB with different data.
*/
public function testSimpleWriteRead()
{
$data = ['blue', 'red'];
$this->assertTrue(FileUtils::writeFlatDB(self::$file, $data) > 0);
$this->assertTrue(startsWith(file_get_contents(self::$file), '<?php /*'));
$this->assertEquals($data, FileUtils::readFlatDB(self::$file));
$data = 0;
$this->assertTrue(FileUtils::writeFlatDB(self::$file, $data) > 0);
$this->assertEquals($data, FileUtils::readFlatDB(self::$file));
$data = null;
$this->assertTrue(FileUtils::writeFlatDB(self::$file, $data) > 0);
$this->assertEquals($data, FileUtils::readFlatDB(self::$file));
$data = false;
$this->assertTrue(FileUtils::writeFlatDB(self::$file, $data) > 0);
$this->assertEquals($data, FileUtils::readFlatDB(self::$file));
}
/**
* File not writable: raise an exception.
*
* @expectedException IOException
* @expectedExceptionMessage Error accessing "sandbox/flat.db"
*/
public function testWriteWithoutPermission()
{
touch(self::$file);
chmod(self::$file, 0440);
FileUtils::writeFlatDB(self::$file, null);
}
/**
* Folder non existent: raise an exception.
*
* @expectedException IOException
* @expectedExceptionMessage Error accessing "nopefolder"
*/
public function testWriteFolderDoesNotExist()
{
FileUtils::writeFlatDB('nopefolder/file', null);
}
/**
* Folder non writable: raise an exception.
*
* @expectedException IOException
* @expectedExceptionMessage Error accessing "sandbox"
*/
public function testWriteFolderPermission()
{
chmod(dirname(self::$file), 0555);
try {
FileUtils::writeFlatDB(self::$file, null);
} catch (Exception $e) {
chmod(dirname(self::$file), 0755);
throw $e;
}
}
/**
* Read non existent file, use default parameter.
*/
public function testReadNotExistentFile()
{
$this->assertEquals(null, FileUtils::readFlatDB(self::$file));
$this->assertEquals(['test'], FileUtils::readFlatDB(self::$file, ['test']));
}
/**
* Read non readable file, use default parameter.
*/
public function testReadNotReadable()
{
touch(self::$file);
chmod(self::$file, 0220);
$this->assertEquals(null, FileUtils::readFlatDB(self::$file));
$this->assertEquals(['test'], FileUtils::readFlatDB(self::$file, ['test']));
}
}

207
tests/HistoryTest.php Normal file
View file

@ -0,0 +1,207 @@
<?php
require_once 'application/History.php';
class HistoryTest extends PHPUnit_Framework_TestCase
{
/**
* @var string History file path
*/
protected static $historyFilePath = 'sandbox/history.php';
/**
* Delete history file.
*/
public function tearDown()
{
@unlink(self::$historyFilePath);
}
/**
* Test that the history file is created if it doesn't exist.
*/
public function testConstructLazyLoading()
{
new History(self::$historyFilePath);
$this->assertFileNotExists(self::$historyFilePath);
}
/**
* Test that the history file is created if it doesn't exist.
*/
public function testAddEventCreateFile()
{
$history = new History(self::$historyFilePath);
$history->updateSettings();
$this->assertFileExists(self::$historyFilePath);
}
/**
* Not writable history file: raise an exception.
*
* @expectedException Exception
* @expectedExceptionMessage History file isn't readable or writable
*/
public function testConstructNotWritable()
{
touch(self::$historyFilePath);
chmod(self::$historyFilePath, 0440);
$history = new History(self::$historyFilePath);
$history->updateSettings();
}
/**
* Not parsable history file: raise an exception.
*
* @expectedException Exception
* @expectedExceptionMessageRegExp /Could not parse history file/
*/
public function testConstructNotParsable()
{
file_put_contents(self::$historyFilePath, 'not parsable');
$history = new History(self::$historyFilePath);
// gzinflate generates a warning
@$history->updateSettings();
}
/**
* Test add link event
*/
public function testAddLink()
{
$history = new History(self::$historyFilePath);
$history->addLink(['id' => 0]);
$actual = $history->getHistory()[0];
$this->assertEquals(History::CREATED, $actual['event']);
$this->assertTrue(new DateTime('-2 seconds') < DateTime::createFromFormat(DateTime::ATOM, $actual['datetime']));
$this->assertEquals(0, $actual['id']);
$history = new History(self::$historyFilePath);
$history->addLink(['id' => 1]);
$actual = $history->getHistory()[0];
$this->assertEquals(History::CREATED, $actual['event']);
$this->assertTrue(new DateTime('-2 seconds') < DateTime::createFromFormat(DateTime::ATOM, $actual['datetime']));
$this->assertEquals(1, $actual['id']);
$history = new History(self::$historyFilePath);
$history->addLink(['id' => 'str']);
$actual = $history->getHistory()[0];
$this->assertEquals(History::CREATED, $actual['event']);
$this->assertTrue(new DateTime('-2 seconds') < DateTime::createFromFormat(DateTime::ATOM, $actual['datetime']));
$this->assertEquals('str', $actual['id']);
}
/**
* Test updated link event
*/
public function testUpdateLink()
{
$history = new History(self::$historyFilePath);
$history->updateLink(['id' => 1]);
$actual = $history->getHistory()[0];
$this->assertEquals(History::UPDATED, $actual['event']);
$this->assertTrue(new DateTime('-2 seconds') < DateTime::createFromFormat(DateTime::ATOM, $actual['datetime']));
$this->assertEquals(1, $actual['id']);
}
/**
* Test delete link event
*/
public function testDeleteLink()
{
$history = new History(self::$historyFilePath);
$history->deleteLink(['id' => 1]);
$actual = $history->getHistory()[0];
$this->assertEquals(History::DELETED, $actual['event']);
$this->assertTrue(new DateTime('-2 seconds') < DateTime::createFromFormat(DateTime::ATOM, $actual['datetime']));
$this->assertEquals(1, $actual['id']);
}
/**
* Test updated settings event
*/
public function testUpdateSettings()
{
$history = new History(self::$historyFilePath);
$history->updateSettings();
$actual = $history->getHistory()[0];
$this->assertEquals(History::SETTINGS, $actual['event']);
$this->assertTrue(new DateTime('-2 seconds') < DateTime::createFromFormat(DateTime::ATOM, $actual['datetime']));
$this->assertEmpty($actual['id']);
}
/**
* Make sure that new items are stored at the beginning
*/
public function testHistoryOrder()
{
$history = new History(self::$historyFilePath);
$history->updateLink(['id' => 1]);
$actual = $history->getHistory()[0];
$this->assertEquals(History::UPDATED, $actual['event']);
$this->assertTrue(new DateTime('-2 seconds') < DateTime::createFromFormat(DateTime::ATOM, $actual['datetime']));
$this->assertEquals(1, $actual['id']);
$history->addLink(['id' => 1]);
$actual = $history->getHistory()[0];
$this->assertEquals(History::CREATED, $actual['event']);
$this->assertTrue(new DateTime('-2 seconds') < DateTime::createFromFormat(DateTime::ATOM, $actual['datetime']));
$this->assertEquals(1, $actual['id']);
}
/**
* Re-read history from file after writing an event
*/
public function testHistoryRead()
{
$history = new History(self::$historyFilePath);
$history->updateLink(['id' => 1]);
$history = new History(self::$historyFilePath);
$actual = $history->getHistory()[0];
$this->assertEquals(History::UPDATED, $actual['event']);
$this->assertTrue(new DateTime('-2 seconds') < DateTime::createFromFormat(DateTime::ATOM, $actual['datetime']));
$this->assertEquals(1, $actual['id']);
}
/**
* Re-read history from file after writing an event and make sure that the order is correct
*/
public function testHistoryOrderRead()
{
$history = new History(self::$historyFilePath);
$history->updateLink(['id' => 1]);
$history->addLink(['id' => 1]);
$history = new History(self::$historyFilePath);
$actual = $history->getHistory()[0];
$this->assertEquals(History::CREATED, $actual['event']);
$this->assertTrue(new DateTime('-2 seconds') < DateTime::createFromFormat(DateTime::ATOM, $actual['datetime']));
$this->assertEquals(1, $actual['id']);
$actual = $history->getHistory()[1];
$this->assertEquals(History::UPDATED, $actual['event']);
$this->assertTrue(new DateTime('-2 seconds') < DateTime::createFromFormat(DateTime::ATOM, $actual['datetime']));
$this->assertEquals(1, $actual['id']);
}
/**
* Test retention time: delete old entries.
*/
public function testHistoryRententionTime()
{
$history = new History(self::$historyFilePath, 5);
$history->updateLink(['id' => 1]);
$this->assertEquals(1, count($history->getHistory()));
$arr = $history->getHistory();
$arr[0]['datetime'] = (new DateTime('-1 hour'))->format(DateTime::ATOM);
FileUtils::writeFlatDB(self::$historyFilePath, $arr);
$history = new History(self::$historyFilePath, 60);
$this->assertEquals(1, count($history->getHistory()));
$this->assertEquals(1, $history->getHistory()[0]['id']);
$history->updateLink(['id' => 2]);
$this->assertEquals(1, count($history->getHistory()));
$this->assertEquals(2, $history->getHistory()[0]['id']);
}
}

View file

@ -101,7 +101,7 @@ class LinkDBTest extends PHPUnit_Framework_TestCase
* Attempt to instantiate a LinkDB whereas the datastore is not writable
*
* @expectedException IOException
* @expectedExceptionMessageRegExp /Error accessing\nnull/
* @expectedExceptionMessageRegExp /Error accessing "null"/
*/
public function testConstructDatastoreNotWriteable()
{

View file

@ -33,6 +33,11 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase
*/
protected static $testDatastore = 'sandbox/datastore.php';
/**
* @var string History file path
*/
protected static $historyFilePath = 'sandbox/history.php';
/**
* @var LinkDB private LinkDB instance
*/
@ -48,6 +53,11 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase
*/
protected $conf;
/**
* @var History instance.
*/
protected $history;
/**
* @var string Save the current timezone.
*/
@ -73,6 +83,15 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase
$this->linkDb = new LinkDB(self::$testDatastore, true, false);
$this->conf = new ConfigManager('tests/utils/config/configJson');
$this->conf->set('resource.page_cache', $this->pagecache);
$this->history = new History(self::$historyFilePath);
}
/**
* Delete history file.
*/
public function tearDown()
{
@unlink(self::$historyFilePath);
}
public static function tearDownAfterClass()
@ -89,7 +108,7 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase
$this->assertEquals(
'File empty.htm (0 bytes) has an unknown file format.'
.' Nothing was imported.',
NetscapeBookmarkUtils::import(NULL, $files, NULL, $this->conf)
NetscapeBookmarkUtils::import(null, $files, null, $this->conf, $this->history)
);
$this->assertEquals(0, count($this->linkDb));
}
@ -102,7 +121,7 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase
$files = file2array('no_doctype.htm');
$this->assertEquals(
'File no_doctype.htm (350 bytes) has an unknown file format. Nothing was imported.',
NetscapeBookmarkUtils::import(NULL, $files, NULL, $this->conf)
NetscapeBookmarkUtils::import(null, $files, null, $this->conf, $this->history)
);
$this->assertEquals(0, count($this->linkDb));
}
@ -116,7 +135,7 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase
$this->assertEquals(
'File internet_explorer_encoding.htm (356 bytes) was successfully processed:'
.' 1 links imported, 0 links overwritten, 0 links skipped.',
NetscapeBookmarkUtils::import(array(), $files, $this->linkDb, $this->conf)
NetscapeBookmarkUtils::import([], $files, $this->linkDb, $this->conf, $this->history)
);
$this->assertEquals(1, count($this->linkDb));
$this->assertEquals(0, count_private($this->linkDb));
@ -145,7 +164,7 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase
$this->assertEquals(
'File netscape_nested.htm (1337 bytes) was successfully processed:'
.' 8 links imported, 0 links overwritten, 0 links skipped.',
NetscapeBookmarkUtils::import(array(), $files, $this->linkDb, $this->conf)
NetscapeBookmarkUtils::import([], $files, $this->linkDb, $this->conf, $this->history)
);
$this->assertEquals(8, count($this->linkDb));
$this->assertEquals(2, count_private($this->linkDb));
@ -267,7 +286,7 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase
$this->assertEquals(
'File netscape_basic.htm (482 bytes) was successfully processed:'
.' 2 links imported, 0 links overwritten, 0 links skipped.',
NetscapeBookmarkUtils::import(array(), $files, $this->linkDb, $this->conf)
NetscapeBookmarkUtils::import([], $files, $this->linkDb, $this->conf, $this->history)
);
$this->assertEquals(2, count($this->linkDb));
@ -312,7 +331,7 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase
$this->assertEquals(
'File netscape_basic.htm (482 bytes) was successfully processed:'
.' 2 links imported, 0 links overwritten, 0 links skipped.',
NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf)
NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
);
$this->assertEquals(2, count($this->linkDb));
$this->assertEquals(1, count_private($this->linkDb));
@ -356,7 +375,7 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase
$this->assertEquals(
'File netscape_basic.htm (482 bytes) was successfully processed:'
.' 2 links imported, 0 links overwritten, 0 links skipped.',
NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf)
NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
);
$this->assertEquals(2, count($this->linkDb));
$this->assertEquals(0, count_private($this->linkDb));
@ -380,7 +399,7 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase
$this->assertEquals(
'File netscape_basic.htm (482 bytes) was successfully processed:'
.' 2 links imported, 0 links overwritten, 0 links skipped.',
NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf)
NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
);
$this->assertEquals(2, count($this->linkDb));
$this->assertEquals(2, count_private($this->linkDb));
@ -406,7 +425,7 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase
$this->assertEquals(
'File netscape_basic.htm (482 bytes) was successfully processed:'
.' 2 links imported, 0 links overwritten, 0 links skipped.',
NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf)
NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
);
$this->assertEquals(2, count($this->linkDb));
$this->assertEquals(2, count_private($this->linkDb));
@ -426,7 +445,7 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase
$this->assertEquals(
'File netscape_basic.htm (482 bytes) was successfully processed:'
.' 2 links imported, 2 links overwritten, 0 links skipped.',
NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf)
NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
);
$this->assertEquals(2, count($this->linkDb));
$this->assertEquals(0, count_private($this->linkDb));
@ -452,7 +471,7 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase
$this->assertEquals(
'File netscape_basic.htm (482 bytes) was successfully processed:'
.' 2 links imported, 0 links overwritten, 0 links skipped.',
NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf)
NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
);
$this->assertEquals(2, count($this->linkDb));
$this->assertEquals(0, count_private($this->linkDb));
@ -473,7 +492,7 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase
$this->assertEquals(
'File netscape_basic.htm (482 bytes) was successfully processed:'
.' 2 links imported, 2 links overwritten, 0 links skipped.',
NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf)
NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
);
$this->assertEquals(2, count($this->linkDb));
$this->assertEquals(2, count_private($this->linkDb));
@ -497,7 +516,7 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase
$this->assertEquals(
'File netscape_basic.htm (482 bytes) was successfully processed:'
.' 2 links imported, 0 links overwritten, 0 links skipped.',
NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf)
NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
);
$this->assertEquals(2, count($this->linkDb));
$this->assertEquals(0, count_private($this->linkDb));
@ -507,7 +526,7 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase
$this->assertEquals(
'File netscape_basic.htm (482 bytes) was successfully processed:'
.' 0 links imported, 0 links overwritten, 2 links skipped.',
NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf)
NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
);
$this->assertEquals(2, count($this->linkDb));
$this->assertEquals(0, count_private($this->linkDb));
@ -526,7 +545,7 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase
$this->assertEquals(
'File netscape_basic.htm (482 bytes) was successfully processed:'
.' 2 links imported, 0 links overwritten, 0 links skipped.',
NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf)
NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
);
$this->assertEquals(2, count($this->linkDb));
$this->assertEquals(0, count_private($this->linkDb));
@ -553,7 +572,7 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase
$this->assertEquals(
'File netscape_basic.htm (482 bytes) was successfully processed:'
.' 2 links imported, 0 links overwritten, 0 links skipped.',
NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf)
NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
);
$this->assertEquals(2, count($this->linkDb));
$this->assertEquals(0, count_private($this->linkDb));
@ -578,7 +597,7 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase
$this->assertEquals(
'File same_date.htm (453 bytes) was successfully processed:'
.' 3 links imported, 0 links overwritten, 0 links skipped.',
NetscapeBookmarkUtils::import(array(), $files, $this->linkDb, $this->conf)
NetscapeBookmarkUtils::import(array(), $files, $this->linkDb, $this->conf, $this->history)
);
$this->assertEquals(3, count($this->linkDb));
$this->assertEquals(0, count_private($this->linkDb));
@ -595,4 +614,32 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase
$this->linkDb[2]['id']
);
}
public function testImportCreateUpdateHistory()
{
$post = [
'privacy' => 'public',
'overwrite' => 'true',
];
$files = file2array('netscape_basic.htm');
$nbLinks = 2;
NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history);
$history = $this->history->getHistory();
$this->assertEquals($nbLinks, count($history));
foreach ($history as $value) {
$this->assertEquals(History::CREATED, $value['event']);
$this->assertTrue(new DateTime('-5 seconds') < DateTime::createFromFormat(DateTime::ATOM, $value['datetime']));
$this->assertTrue(is_int($value['id']));
}
// re-import as private, enable overwriting
NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history);
$history = $this->history->getHistory();
$this->assertEquals($nbLinks * 2, count($history));
for ($i = 0 ; $i < $nbLinks ; $i++) {
$this->assertEquals(History::UPDATED, $history[$i]['event']);
$this->assertTrue(new DateTime('-5 seconds') < DateTime::createFromFormat(DateTime::ATOM, $history[$i]['datetime']));
$this->assertTrue(is_int($history[$i]['id']));
}
}
}