]> git.immae.eu Git - github/shaarli/Shaarli.git/blobdiff - tests/api/controllers/links/GetLinksTest.php
New plugin hook: ability to add custom filters to Shaarli search engine
[github/shaarli/Shaarli.git] / tests / api / controllers / links / GetLinksTest.php
index d22ed3bfe97fe8d98054f1eff9e9e3597573e5a9..3c96673282d4b657c7866fea8f7509334db2b1ba 100644 (file)
@@ -1,8 +1,13 @@
 <?php
 namespace Shaarli\Api\Controllers;
 
+use malkusch\lock\mutex\NoMutex;
+use Shaarli\Bookmark\Bookmark;
+use Shaarli\Bookmark\BookmarkFileService;
+use Shaarli\Bookmark\LinkDB;
 use Shaarli\Config\ConfigManager;
-
+use Shaarli\History;
+use Shaarli\Plugin\PluginManager;
 use Slim\Container;
 use Slim\Http\Environment;
 use Slim\Http\Request;
@@ -17,7 +22,7 @@ use Slim\Http\Response;
  *
  * @package Shaarli\Api\Controllers
  */
-class GetLinksTest extends \PHPUnit_Framework_TestCase
+class GetLinksTest extends \Shaarli\TestCase
 {
     /**
      * @var string datastore to test write operations
@@ -50,17 +55,27 @@ class GetLinksTest extends \PHPUnit_Framework_TestCase
     const NB_FIELDS_LINK = 9;
 
     /**
-     * Before every test, instantiate a new Api with its config, plugins and links.
+     * Before every test, instantiate a new Api with its config, plugins and bookmarks.
      */
-    public function setUp()
+    protected function setUp(): void
     {
+        $mutex = new NoMutex();
         $this->conf = new ConfigManager('tests/utils/config/configJson');
+        $this->conf->set('resource.datastore', self::$testDatastore);
         $this->refDB = new \ReferenceLinkDB();
         $this->refDB->write(self::$testDatastore);
+        $history = new History('sandbox/history.php');
 
         $this->container = new Container();
         $this->container['conf'] = $this->conf;
-        $this->container['db'] = new \LinkDB(self::$testDatastore, true, false);
+        $pluginManager = new PluginManager($this->conf);
+        $this->container['db'] = new BookmarkFileService(
+            $this->conf,
+            $pluginManager,
+            $history,
+            $mutex,
+            true
+        );
         $this->container['history'] = null;
 
         $this->controller = new Links($this->container);
@@ -69,13 +84,13 @@ class GetLinksTest extends \PHPUnit_Framework_TestCase
     /**
      * After every test, remove the test datastore.
      */
-    public function tearDown()
+    protected function tearDown(): void
     {
         @unlink(self::$testDatastore);
     }
 
     /**
-     * Test basic getLinks service: returns all links.
+     * Test basic getLinks service: returns all bookmarks.
      */
     public function testGetLinks()
     {
@@ -95,7 +110,7 @@ class GetLinksTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals($this->refDB->countLinks(), count($data));
 
         // Check order
-        $order = [41, 8, 6, 7, 0, 1, 9, 4, 42];
+        $order = [10, 11, 41, 8, 6, 7, 0, 1, 9, 4, 42];
         $cpt = 0;
         foreach ($data as $link) {
             $this->assertEquals(self::NB_FIELDS_LINK, count($link));
@@ -103,8 +118,8 @@ class GetLinksTest extends \PHPUnit_Framework_TestCase
         }
 
         // Check first element fields
-        $first = $data[0];
-        $this->assertEquals('http://domain.tld/?WDWyig', $first['url']);
+        $first = $data[2];
+        $this->assertEquals('http://domain.tld/shaare/WDWyig', $first['url']);
         $this->assertEquals('WDWyig', $first['shorturl']);
         $this->assertEquals('Link title: @website', $first['title']);
         $this->assertEquals(
@@ -114,18 +129,18 @@ class GetLinksTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals('sTuff', $first['tags'][0]);
         $this->assertEquals(false, $first['private']);
         $this->assertEquals(
-            \DateTime::createFromFormat(\LinkDB::LINK_DATE_FORMAT, '20150310_114651')->format(\DateTime::ATOM),
+            \DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651')->format(\DateTime::ATOM),
             $first['created']
         );
         $this->assertEmpty($first['updated']);
 
         // Multi tags
-        $link = $data[1];
+        $link = $data[3];
         $this->assertEquals(7, count($link['tags']));
 
         // Update date
         $this->assertEquals(
-            \DateTime::createFromFormat(\LinkDB::LINK_DATE_FORMAT, '20160803_093033')->format(\DateTime::ATOM),
+            \DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20160803_093033')->format(\DateTime::ATOM),
             $link['updated']
         );
     }
@@ -138,7 +153,7 @@ class GetLinksTest extends \PHPUnit_Framework_TestCase
     {
         $env = Environment::mock([
             'REQUEST_METHOD' => 'GET',
-            'QUERY_STRING' => 'offset=1&limit=1'
+            'QUERY_STRING' => 'offset=3&limit=1'
         ]);
         $request = Request::createFromEnvironment($env);
         $response = $this->controller->getLinks($request, new Response());
@@ -164,7 +179,7 @@ class GetLinksTest extends \PHPUnit_Framework_TestCase
         $data = json_decode((string) $response->getBody(), true);
         $this->assertEquals($this->refDB->countLinks(), count($data));
         // Check order
-        $order = [41, 8, 6, 7, 0, 1, 9, 4, 42];
+        $order = [10, 11, 41, 8, 6, 7, 0, 1, 9, 4, 42];
         $cpt = 0;
         foreach ($data as $link) {
             $this->assertEquals(self::NB_FIELDS_LINK, count($link));
@@ -205,7 +220,8 @@ class GetLinksTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals(200, $response->getStatusCode());
         $data = json_decode((string)$response->getBody(), true);
         $this->assertEquals($this->refDB->countLinks(), count($data));
-        $this->assertEquals(41, $data[0]['id']);
+        $this->assertEquals(10, $data[0]['id']);
+        $this->assertEquals(41, $data[2]['id']);
         $this->assertEquals(self::NB_FIELDS_LINK, count($data[0]));
     }
 
@@ -243,7 +259,8 @@ class GetLinksTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals(200, $response->getStatusCode());
         $data = json_decode((string)$response->getBody(), true);
         $this->assertEquals($this->refDB->countPublicLinks(), count($data));
-        $this->assertEquals(41, $data[0]['id']);
+        $this->assertEquals(10, $data[0]['id']);
+        $this->assertEquals(41, $data[2]['id']);
         $this->assertEquals(self::NB_FIELDS_LINK, count($data[0]));
     }
 
@@ -389,7 +406,7 @@ class GetLinksTest extends \PHPUnit_Framework_TestCase
         $response = $this->controller->getLinks($request, new Response());
         $this->assertEquals(200, $response->getStatusCode());
         $data = json_decode((string) $response->getBody(), true);
-        $this->assertEquals(4, count($data));
+        $this->assertEquals(5, count($data));
         $this->assertEquals(6, $data[0]['id']);
 
         // wildcard: placeholder at the middle
@@ -413,8 +430,9 @@ class GetLinksTest extends \PHPUnit_Framework_TestCase
         $response = $this->controller->getLinks($request, new Response());
         $this->assertEquals(200, $response->getStatusCode());
         $data = json_decode((string) $response->getBody(), true);
-        $this->assertEquals(9, count($data));
-        $this->assertEquals(41, $data[0]['id']);
+        $this->assertEquals(\ReferenceLinkDB::$NB_LINKS_TOTAL, count($data));
+        $this->assertEquals(10, $data[0]['id']);
+        $this->assertEquals(41, $data[2]['id']);
 
         // wildcard: optional ('*' does not need to expand)
         $env = Environment::mock([