]> git.immae.eu Git - github/shaarli/Shaarli.git/blobdiff - tests/bookmark/LinkDBTest.php
Fix UT: LinkDBTest - make each tests independant
[github/shaarli/Shaarli.git] / tests / bookmark / LinkDBTest.php
index f18a3155bd060efdf0477dd7150cc6126176a614..ffe03cc5bde08322b0016275693a827bac0c37a6 100644 (file)
@@ -6,7 +6,6 @@
 namespace Shaarli\Bookmark;
 
 use DateTime;
-use LinkNotFoundException;
 use ReferenceLinkDB;
 use ReflectionClass;
 use Shaarli;
@@ -50,17 +49,7 @@ class LinkDBTest extends \PHPUnit\Framework\TestCase
      *    - by tag,
      *    - by text,
      *  - etc.
-     */
-    public static function setUpBeforeClass()
-    {
-        self::$refDB = new ReferenceLinkDB();
-        self::$refDB->write(self::$testDatastore);
-
-        self::$publicLinkDB = new LinkDB(self::$testDatastore, false, false);
-        self::$privateLinkDB = new LinkDB(self::$testDatastore, true, false);
-    }
-
-    /**
+     *
      * Resets test data for each test
      */
     protected function setUp()
@@ -68,6 +57,12 @@ class LinkDBTest extends \PHPUnit\Framework\TestCase
         if (file_exists(self::$testDatastore)) {
             unlink(self::$testDatastore);
         }
+
+        self::$refDB = new ReferenceLinkDB();
+        self::$refDB->write(self::$testDatastore);
+
+        self::$publicLinkDB = new LinkDB(self::$testDatastore, false, false);
+        self::$privateLinkDB = new LinkDB(self::$testDatastore, true, false);
     }
 
     /**
@@ -192,7 +187,7 @@ class LinkDBTest extends \PHPUnit\Framework\TestCase
         $dbSize = sizeof($testDB);
 
         $link = array(
-            'id' => 42,
+            'id' => 43,
             'title' => 'an additional link',
             'url' => 'http://dum.my',
             'description' => 'One more',
@@ -362,36 +357,6 @@ class LinkDBTest extends \PHPUnit\Framework\TestCase
         );
     }
 
-    /**
-     * Test real_url without redirector.
-     */
-    public function testLinkRealUrlWithoutRedirector()
-    {
-        $db = new LinkDB(self::$testDatastore, false, false);
-        foreach ($db as $link) {
-            $this->assertEquals($link['url'], $link['real_url']);
-        }
-    }
-
-    /**
-     * Test real_url with redirector.
-     */
-    public function testLinkRealUrlWithRedirector()
-    {
-        $redirector = 'http://redirector.to?';
-        $db = new LinkDB(self::$testDatastore, false, false, $redirector);
-        foreach ($db as $link) {
-            $this->assertStringStartsWith($redirector, $link['real_url']);
-            $this->assertNotFalse(strpos($link['real_url'], urlencode('://')));
-        }
-
-        $db = new LinkDB(self::$testDatastore, false, false, $redirector, false);
-        foreach ($db as $link) {
-            $this->assertStringStartsWith($redirector, $link['real_url']);
-            $this->assertFalse(strpos($link['real_url'], urlencode('://')));
-        }
-    }
-
     /**
      * Test filter with string.
      */
@@ -457,7 +422,7 @@ class LinkDBTest extends \PHPUnit\Framework\TestCase
     /**
      * Test filterHash() with an invalid smallhash.
      *
-     * @expectedException LinkNotFoundException
+     * @expectedException \Shaarli\Bookmark\Exception\LinkNotFoundException
      */
     public function testFilterHashInValid1()
     {
@@ -468,7 +433,7 @@ class LinkDBTest extends \PHPUnit\Framework\TestCase
     /**
      * Test filterHash() with an empty smallhash.
      *
-     * @expectedException LinkNotFoundException
+     * @expectedException \Shaarli\Bookmark\Exception\LinkNotFoundException
      */
     public function testFilterHashInValid()
     {
@@ -517,7 +482,7 @@ class LinkDBTest extends \PHPUnit\Framework\TestCase
     public function testRenameTagCaseSensitive()
     {
         self::$refDB->write(self::$testDatastore);
-        $linkDB = new LinkDB(self::$testDatastore, true, false, '');
+        $linkDB = new LinkDB(self::$testDatastore, true, false);
 
         $res = $linkDB->renameTag('sTuff', 'Taz');
         $this->assertEquals(1, count($res));
@@ -650,4 +615,42 @@ class LinkDBTest extends \PHPUnit\Framework\TestCase
 
         $this->assertEquals($expected, $tags, var_export($tags, true));
     }
+
+    /**
+     * Make sure that bookmarks with the same timestamp have a consistent order:
+     * if their creation date is equal, bookmarks are sorted by ID DESC.
+     */
+    public function testConsistentOrder()
+    {
+        $nextId = 43;
+        $creation = DateTime::createFromFormat('Ymd_His', '20190807_130444');
+        $linkDB = new LinkDB(self::$testDatastore, true, false);
+        for ($i = 0; $i < 4; ++$i) {
+            $linkDB[$nextId + $i] = [
+                'id' => $nextId + $i,
+                'url' => 'http://'. $i,
+                'created' => $creation,
+                'title' => true,
+                'description' => true,
+                'tags' => true,
+            ];
+        }
+
+        // Check 4 new links 4 times
+        for ($i = 0; $i < 4; ++$i) {
+            $linkDB->save('tests');
+            $linkDB = new LinkDB(self::$testDatastore, true, false);
+            $count = 3;
+            foreach ($linkDB as $link) {
+                if ($link['sticky'] === true) {
+                    continue;
+                }
+                $this->assertEquals($nextId + $count, $link['id']);
+                $this->assertEquals('http://'. $count, $link['url']);
+                if (--$count < 0) {
+                    break;
+                }
+            }
+        }
+    }
 }