]> git.immae.eu Git - github/shaarli/Shaarli.git/blobdiff - tests/netscape/BookmarkImportTest.php
Feature: support any tag separator
[github/shaarli/Shaarli.git] / tests / netscape / BookmarkImportTest.php
index fef7f6d18450123cff395f0f5c8f510750721b59..6856ebcafebdecc18070638daa6430c6c71386da 100644 (file)
@@ -1,29 +1,32 @@
 <?php
+
 namespace Shaarli\Netscape;
 
 use DateTime;
+use malkusch\lock\mutex\NoMutex;
+use Psr\Http\Message\UploadedFileInterface;
 use Shaarli\Bookmark\Bookmark;
-use Shaarli\Bookmark\BookmarkFilter;
 use Shaarli\Bookmark\BookmarkFileService;
-use Shaarli\Bookmark\LinkDB;
+use Shaarli\Bookmark\BookmarkFilter;
 use Shaarli\Config\ConfigManager;
 use Shaarli\History;
+use Shaarli\TestCase;
+use Slim\Http\UploadedFile;
 
 /**
  * Utility function to load a file's metadata in a $_FILES-like array
  *
  * @param string $filename Basename of the file
  *
- * @return array A $_FILES-like array
+ * @return UploadedFileInterface Upload file in PSR-7 compatible object
  */
 function file2array($filename)
 {
-    return array(
-        'filetoupload' => array(
-            'name'     => $filename,
-            'tmp_name' => __DIR__ . '/input/' . $filename,
-            'size'     => filesize(__DIR__ . '/input/' . $filename)
-        )
+    return new UploadedFile(
+        __DIR__ . '/input/' . $filename,
+        $filename,
+        null,
+        filesize(__DIR__ . '/input/' . $filename)
     );
 }
 
@@ -31,7 +34,7 @@ function file2array($filename)
 /**
  * Netscape bookmark import
  */
-class BookmarkImportTest extends \PHPUnit\Framework\TestCase
+class BookmarkImportTest extends TestCase
 {
     /**
      * @var string datastore to test write operations
@@ -63,12 +66,17 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
      */
     protected $history;
 
+    /**
+     * @var NetscapeBookmarkUtils
+     */
+    protected $netscapeBookmarkUtils;
+
     /**
      * @var string Save the current timezone.
      */
     protected static $defaultTimeZone;
 
-    public static function setUpBeforeClass()
+    public static function setUpBeforeClass(): void
     {
         self::$defaultTimeZone = date_default_timezone_get();
         // Timezone without DST for test consistency
@@ -78,8 +86,9 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
     /**
      * Resets test data before each test
      */
-    protected function setUp()
+    protected function setUp(): void
     {
+        $mutex = new NoMutex();
         if (file_exists(self::$testDatastore)) {
             unlink(self::$testDatastore);
         }
@@ -90,18 +99,19 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
         $this->conf->set('resource.page_cache', $this->pagecache);
         $this->conf->set('resource.datastore', self::$testDatastore);
         $this->history = new History(self::$historyFilePath);
-        $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true);
+        $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, $mutex, true);
+        $this->netscapeBookmarkUtils = new NetscapeBookmarkUtils($this->bookmarkService, $this->conf, $this->history);
     }
 
     /**
      * Delete history file.
      */
-    public function tearDown()
+    protected function tearDown(): void
     {
         @unlink(self::$historyFilePath);
     }
 
-    public static function tearDownAfterClass()
+    public static function tearDownAfterClass(): void
     {
         date_default_timezone_set(self::$defaultTimeZone);
     }
@@ -115,7 +125,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, $this->history)
+            $this->netscapeBookmarkUtils->import(null, $files)
         );
         $this->assertEquals(0, $this->bookmarkService->count());
     }
@@ -128,7 +138,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, $this->history)
+            $this->netscapeBookmarkUtils->import(null, $files)
         );
         $this->assertEquals(0, $this->bookmarkService->count());
     }
@@ -142,7 +152,7 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
         $this->assertStringMatchesFormat(
             'File lowercase_doctype.htm (386 bytes) was successfully processed in %d seconds:'
             .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
-            NetscapeBookmarkUtils::import(null, $files, $this->bookmarkService, $this->conf, $this->history)
+            $this->netscapeBookmarkUtils->import(null, $files)
         );
         $this->assertEquals(2, $this->bookmarkService->count());
     }
@@ -157,7 +167,7 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
         $this->assertStringMatchesFormat(
             'File internet_explorer_encoding.htm (356 bytes) was successfully processed in %d seconds:'
             .' 1 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
-            NetscapeBookmarkUtils::import([], $files, $this->bookmarkService, $this->conf, $this->history)
+            $this->netscapeBookmarkUtils->import([], $files)
         );
         $this->assertEquals(1, $this->bookmarkService->count());
         $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
@@ -185,7 +195,7 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
         $this->assertStringMatchesFormat(
             'File netscape_nested.htm (1337 bytes) was successfully processed in %d seconds:'
             .' 8 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
-            NetscapeBookmarkUtils::import([], $files, $this->bookmarkService, $this->conf, $this->history)
+            $this->netscapeBookmarkUtils->import([], $files)
         );
         $this->assertEquals(8, $this->bookmarkService->count());
         $this->assertEquals(2, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
@@ -306,7 +316,7 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
         $this->assertStringMatchesFormat(
             'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
             .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
-            NetscapeBookmarkUtils::import([], $files, $this->bookmarkService, $this->conf, $this->history)
+            $this->netscapeBookmarkUtils->import([], $files)
         );
 
         $this->assertEquals(2, $this->bookmarkService->count());
@@ -349,7 +359,7 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
         $this->assertStringMatchesFormat(
             'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
             .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
-            NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history)
+            $this->netscapeBookmarkUtils->import($post, $files)
         );
 
         $this->assertEquals(2, $this->bookmarkService->count());
@@ -392,7 +402,7 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
         $this->assertStringMatchesFormat(
             'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
             .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
-            NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history)
+            $this->netscapeBookmarkUtils->import($post, $files)
         );
         $this->assertEquals(2, $this->bookmarkService->count());
         $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
@@ -410,7 +420,7 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
         $this->assertStringMatchesFormat(
             'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
             .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
-            NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history)
+            $this->netscapeBookmarkUtils->import($post, $files)
         );
         $this->assertEquals(2, $this->bookmarkService->count());
         $this->assertEquals(2, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
@@ -430,7 +440,7 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
         $this->assertStringMatchesFormat(
             'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
             .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
-            NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history)
+            $this->netscapeBookmarkUtils->import($post, $files)
         );
         $this->assertEquals(2, $this->bookmarkService->count());
         $this->assertEquals(2, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
@@ -445,7 +455,7 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
         $this->assertStringMatchesFormat(
             'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
             .' 2 bookmarks imported, 2 bookmarks overwritten, 0 bookmarks skipped.',
-            NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history)
+            $this->netscapeBookmarkUtils->import($post, $files)
         );
         $this->assertEquals(2, $this->bookmarkService->count());
         $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
@@ -465,7 +475,7 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
         $this->assertStringMatchesFormat(
             'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
             .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
-            NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history)
+            $this->netscapeBookmarkUtils->import($post, $files)
         );
         $this->assertEquals(2, $this->bookmarkService->count());
         $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
@@ -480,7 +490,7 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
         $this->assertStringMatchesFormat(
             'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
             .' 2 bookmarks imported, 2 bookmarks overwritten, 0 bookmarks skipped.',
-            NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history)
+            $this->netscapeBookmarkUtils->import($post, $files)
         );
         $this->assertEquals(2, $this->bookmarkService->count());
         $this->assertEquals(2, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
@@ -498,7 +508,7 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
         $this->assertStringMatchesFormat(
             'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
             .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
-            NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history)
+            $this->netscapeBookmarkUtils->import($post, $files)
         );
         $this->assertEquals(2, $this->bookmarkService->count());
         $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
@@ -508,7 +518,7 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
         $this->assertStringMatchesFormat(
             'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
             .' 0 bookmarks imported, 0 bookmarks overwritten, 2 bookmarks skipped.',
-            NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history)
+            $this->netscapeBookmarkUtils->import($post, $files)
         );
         $this->assertEquals(2, $this->bookmarkService->count());
         $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
@@ -521,13 +531,13 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
     {
         $post = array(
             'privacy' => 'public',
-            'default_tags' => 'tag1,tag2 tag3'
+            'default_tags' => 'tag1 tag2 tag3'
         );
         $files = file2array('netscape_basic.htm');
         $this->assertStringMatchesFormat(
             'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
             .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
-            NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history)
+            $this->netscapeBookmarkUtils->import($post, $files)
         );
         $this->assertEquals(2, $this->bookmarkService->count());
         $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
@@ -542,13 +552,13 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
     {
         $post = array(
             'privacy' => 'public',
-            'default_tags' => 'tag1&,tag2 "tag3"'
+            'default_tags' => 'tag1& tag2 "tag3"'
         );
         $files = file2array('netscape_basic.htm');
         $this->assertStringMatchesFormat(
             'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
             .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
-            NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history)
+            $this->netscapeBookmarkUtils->import($post, $files)
         );
         $this->assertEquals(2, $this->bookmarkService->count());
         $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
@@ -562,6 +572,43 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
         );
     }
 
+    /**
+     * Add user-specified tags to all imported bookmarks
+     */
+    public function testSetDefaultTagsWithCustomSeparator()
+    {
+        $separator = '@';
+        $this->conf->set('general.tags_separator', $separator);
+        $post = [
+            'privacy' => 'public',
+            'default_tags' => 'tag1@tag2@tag3@multiple words tag'
+        ];
+        $files = file2array('netscape_basic.htm');
+        $this->assertStringMatchesFormat(
+            'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
+            .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
+            $this->netscapeBookmarkUtils->import($post, $files)
+        );
+        $this->assertEquals(2, $this->bookmarkService->count());
+        $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
+        $this->assertEquals(
+            'tag1@tag2@tag3@multiple words tag@private@secret',
+            $this->bookmarkService->get(0)->getTagsString($separator)
+        );
+        $this->assertEquals(
+            ['tag1', 'tag2', 'tag3', 'multiple words tag', 'private', 'secret'],
+            $this->bookmarkService->get(0)->getTags()
+        );
+        $this->assertEquals(
+            'tag1@tag2@tag3@multiple words tag@public@hello@world',
+            $this->bookmarkService->get(1)->getTagsString($separator)
+        );
+        $this->assertEquals(
+            ['tag1', 'tag2', 'tag3', 'multiple words tag', 'public', 'hello', 'world'],
+            $this->bookmarkService->get(1)->getTags()
+        );
+    }
+
     /**
      * Ensure each imported bookmark has a unique id
      *
@@ -573,7 +620,7 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
         $this->assertStringMatchesFormat(
             'File same_date.htm (453 bytes) was successfully processed in %d seconds:'
             .' 3 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
-            NetscapeBookmarkUtils::import(array(), $files, $this->bookmarkService, $this->conf, $this->history)
+            $this->netscapeBookmarkUtils->import(array(), $files)
         );
         $this->assertEquals(3, $this->bookmarkService->count());
         $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
@@ -589,14 +636,14 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
             'overwrite' => 'true',
         ];
         $files = file2array('netscape_basic.htm');
-        NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history);
+        $this->netscapeBookmarkUtils->import($post, $files);
         $history = $this->history->getHistory();
         $this->assertEquals(1, count($history));
         $this->assertEquals(History::IMPORT, $history[0]['event']);
         $this->assertTrue(new DateTime('-5 seconds') < $history[0]['datetime']);
 
         // re-import as private, enable overwriting
-        NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history);
+        $this->netscapeBookmarkUtils->import($post, $files);
         $history = $this->history->getHistory();
         $this->assertEquals(2, count($history));
         $this->assertEquals(History::IMPORT, $history[0]['event']);