2017-01-16 12:31:08 +01:00
|
|
|
<?php
|
|
|
|
|
2018-12-02 23:24:58 +01:00
|
|
|
namespace Shaarli;
|
2017-01-16 12:31:08 +01:00
|
|
|
|
2018-12-02 23:24:58 +01:00
|
|
|
use DateTime;
|
2020-01-17 21:34:12 +01:00
|
|
|
use Shaarli\Bookmark\Bookmark;
|
2017-01-16 12:31:08 +01:00
|
|
|
|
2020-09-29 14:41:40 +02:00
|
|
|
class HistoryTest extends \Shaarli\TestCase
|
2017-01-16 12:31:08 +01:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string History file path
|
|
|
|
*/
|
|
|
|
protected static $historyFilePath = 'sandbox/history.php';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete history file.
|
|
|
|
*/
|
2020-09-26 15:08:39 +02:00
|
|
|
protected function setUp(): void
|
2017-01-16 12:31:08 +01:00
|
|
|
{
|
2020-01-17 21:34:12 +01:00
|
|
|
if (file_exists(self::$historyFilePath)) {
|
|
|
|
unlink(self::$historyFilePath);
|
|
|
|
}
|
2017-01-16 12:31:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that the history file is created if it doesn't exist.
|
|
|
|
*/
|
2017-01-16 12:50:36 +01:00
|
|
|
public function testConstructLazyLoading()
|
2017-01-16 12:31:08 +01:00
|
|
|
{
|
|
|
|
new History(self::$historyFilePath);
|
2017-01-16 12:50:36 +01:00
|
|
|
$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();
|
2017-01-16 12:31:08 +01:00
|
|
|
$this->assertFileExists(self::$historyFilePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Not writable history file: raise an exception.
|
|
|
|
*/
|
|
|
|
public function testConstructNotWritable()
|
|
|
|
{
|
2020-09-27 14:07:08 +02:00
|
|
|
$this->expectException(\Exception::class);
|
|
|
|
$this->expectExceptionMessage('History file isn\'t readable or writable');
|
|
|
|
|
2017-01-16 12:31:08 +01:00
|
|
|
touch(self::$historyFilePath);
|
|
|
|
chmod(self::$historyFilePath, 0440);
|
2017-01-16 12:50:36 +01:00
|
|
|
$history = new History(self::$historyFilePath);
|
|
|
|
$history->updateSettings();
|
2017-01-16 12:31:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Not parsable history file: raise an exception.
|
|
|
|
*/
|
|
|
|
public function testConstructNotParsable()
|
|
|
|
{
|
2020-09-27 14:07:08 +02:00
|
|
|
$this->expectException(\Exception::class);
|
|
|
|
$this->expectExceptionMessageRegExp('/Could not parse history file/');
|
|
|
|
|
2017-01-16 12:31:08 +01:00
|
|
|
file_put_contents(self::$historyFilePath, 'not parsable');
|
2017-01-16 12:50:36 +01:00
|
|
|
$history = new History(self::$historyFilePath);
|
2017-01-16 12:31:08 +01:00
|
|
|
// gzinflate generates a warning
|
2017-01-16 12:50:36 +01:00
|
|
|
@$history->updateSettings();
|
2017-01-16 12:31:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test add link event
|
|
|
|
*/
|
|
|
|
public function testAddLink()
|
|
|
|
{
|
|
|
|
$history = new History(self::$historyFilePath);
|
2020-01-17 21:34:12 +01:00
|
|
|
$bookmark = (new Bookmark())->setId(0);
|
|
|
|
$history->addLink($bookmark);
|
2017-01-16 12:31:08 +01:00
|
|
|
$actual = $history->getHistory()[0];
|
|
|
|
$this->assertEquals(History::CREATED, $actual['event']);
|
2017-05-07 16:50:20 +02:00
|
|
|
$this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
|
2017-01-16 12:31:08 +01:00
|
|
|
$this->assertEquals(0, $actual['id']);
|
|
|
|
|
|
|
|
$history = new History(self::$historyFilePath);
|
2020-01-17 21:34:12 +01:00
|
|
|
$bookmark = (new Bookmark())->setId(1);
|
|
|
|
$history->addLink($bookmark);
|
2017-01-16 12:31:08 +01:00
|
|
|
$actual = $history->getHistory()[0];
|
|
|
|
$this->assertEquals(History::CREATED, $actual['event']);
|
2017-05-07 16:50:20 +02:00
|
|
|
$this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
|
2017-01-16 12:31:08 +01:00
|
|
|
$this->assertEquals(1, $actual['id']);
|
|
|
|
|
|
|
|
$history = new History(self::$historyFilePath);
|
2020-01-17 21:34:12 +01:00
|
|
|
$bookmark = (new Bookmark())->setId('str');
|
|
|
|
$history->addLink($bookmark);
|
2017-01-16 12:31:08 +01:00
|
|
|
$actual = $history->getHistory()[0];
|
|
|
|
$this->assertEquals(History::CREATED, $actual['event']);
|
2017-05-07 16:50:20 +02:00
|
|
|
$this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
|
2017-01-16 12:31:08 +01:00
|
|
|
$this->assertEquals('str', $actual['id']);
|
|
|
|
}
|
|
|
|
|
2020-01-17 21:34:12 +01:00
|
|
|
// /**
|
|
|
|
// * 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') < $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') < $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') < $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') < $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') < $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') < $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') < $actual['datetime']);
|
|
|
|
// $this->assertEquals(1, $actual['id']);
|
|
|
|
//
|
|
|
|
// $actual = $history->getHistory()[1];
|
|
|
|
// $this->assertEquals(History::UPDATED, $actual['event']);
|
|
|
|
// $this->assertTrue(new DateTime('-2 seconds') < $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');
|
|
|
|
// 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']);
|
|
|
|
// }
|
2017-01-16 12:31:08 +01:00
|
|
|
}
|