X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=tests%2FNetscapeBookmarkUtils%2FBookmarkImportTest.php;h=f838f2594793a40be4e0110a20c4160d41c6b363;hb=eb6e729808e1c3d9787ad1183aa61ab2e375a537;hp=36425d8525f02d7243b5d3abc8e01958bb5a951f;hpb=48417aed1d83f1566c039529faf0354ec3b42e4b;p=github%2Fshaarli%2FShaarli.git diff --git a/tests/NetscapeBookmarkUtils/BookmarkImportTest.php b/tests/NetscapeBookmarkUtils/BookmarkImportTest.php index 36425d85..f838f259 100644 --- a/tests/NetscapeBookmarkUtils/BookmarkImportTest.php +++ b/tests/NetscapeBookmarkUtils/BookmarkImportTest.php @@ -2,6 +2,7 @@ require_once 'application/NetscapeBookmarkUtils.php'; +use Shaarli\Config\ConfigManager; /** * Utility function to load a file's metadata in a $_FILES-like array @@ -32,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 */ @@ -47,6 +53,11 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase */ protected $conf; + /** + * @var History instance. + */ + protected $history; + /** * @var string Save the current timezone. */ @@ -72,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() @@ -88,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)); } @@ -101,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)); } @@ -115,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)); @@ -144,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)); @@ -266,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)); @@ -311,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)); @@ -355,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)); @@ -379,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)); @@ -405,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)); @@ -425,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)); @@ -451,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)); @@ -472,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)); @@ -496,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)); @@ -506,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)); @@ -525,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)); @@ -552,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)); @@ -577,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)); @@ -594,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'])); + } + } }