]> git.immae.eu Git - github/shaarli/Shaarli.git/blobdiff - tests/api/ApiUtilsTest.php
Merge pull request #727 from ArthurHoaro/api/getlinks
[github/shaarli/Shaarli.git] / tests / api / ApiUtilsTest.php
index 10da1459a1fbfee77f469ba251ea6036884d3c2d..b4431d1be66d8ed34e75ffdaed18965625717140 100644 (file)
@@ -2,6 +2,9 @@
 
 namespace Shaarli\Api;
 
+use Shaarli\Base64Url;
+
+
 /**
  * Class ApiUtilsTest
  */
@@ -24,14 +27,14 @@ class ApiUtilsTest extends \PHPUnit_Framework_TestCase
      */
     public static function generateValidJwtToken($secret)
     {
-        $header = base64_encode('{
+        $header = Base64Url::encode('{
             "typ": "JWT",
             "alg": "HS512"
         }');
-        $payload = base64_encode('{
+        $payload = Base64Url::encode('{
             "iat": '. time() .'
         }');
-        $signature = hash_hmac('sha512', $header .'.'. $payload , $secret);
+        $signature = Base64Url::encode(hash_hmac('sha512', $header .'.'. $payload , $secret, true));
         return $header .'.'. $payload .'.'. $signature;
     }
 
@@ -46,9 +49,9 @@ class ApiUtilsTest extends \PHPUnit_Framework_TestCase
      */
     public static function generateCustomJwtToken($header, $payload, $secret)
     {
-        $header = base64_encode($header);
-        $payload = base64_encode($payload);
-        $signature = hash_hmac('sha512', $header . '.' . $payload, $secret);
+        $header = Base64Url::encode($header);
+        $payload = Base64Url::encode($payload);
+        $signature = Base64Url::encode(hash_hmac('sha512', $header . '.' . $payload, $secret, true));
         return $header . '.' . $payload . '.' . $signature;
     }
 
@@ -203,4 +206,69 @@ class ApiUtilsTest extends \PHPUnit_Framework_TestCase
         $token = $this->generateCustomJwtToken('{"JSON":1}', '{"iat":' . (time() + 60) . '}', 'secret');
         ApiUtils::validateJwtToken($token, 'secret');
     }
+
+    /**
+     * Test formatLink() with a link using all useful fields.
+     */
+    public function testFormatLinkComplete()
+    {
+        $indexUrl = 'https://domain.tld/sub/';
+        $link = [
+            'id' => 12,
+            'url' => 'http://lol.lol',
+            'shorturl' => 'abc',
+            'title' => 'Important Title',
+            'description' => 'It is very lol<tag>' . PHP_EOL . 'new line',
+            'tags' => 'blip   .blop ',
+            'private' => '1',
+            'created' => \DateTime::createFromFormat('Ymd_His', '20170107_160102'),
+            'updated' => \DateTime::createFromFormat('Ymd_His', '20170107_160612'),
+        ];
+
+        $expected = [
+            'id' => 12,
+            'url' => 'http://lol.lol',
+            'shorturl' => 'abc',
+            'title' => 'Important Title',
+            'description' => 'It is very lol<tag>' . PHP_EOL . 'new line',
+            'tags' => ['blip', '.blop'],
+            'private' => true,
+            'created' => '2017-01-07T16:01:02+00:00',
+            'updated' => '2017-01-07T16:06:12+00:00',
+        ];
+
+        $this->assertEquals($expected, ApiUtils::formatLink($link, $indexUrl));
+    }
+
+    /**
+     * Test formatLink() with only minimal fields filled, and internal link.
+     */
+    public function testFormatLinkMinimalNote()
+    {
+        $indexUrl = 'https://domain.tld/sub/';
+        $link = [
+            'id' => 12,
+            'url' => '?abc',
+            'shorturl' => 'abc',
+            'title' => 'Note',
+            'description' => '',
+            'tags' => '',
+            'private' => '',
+            'created' => \DateTime::createFromFormat('Ymd_His', '20170107_160102'),
+        ];
+
+        $expected = [
+            'id' => 12,
+            'url' => 'https://domain.tld/sub/?abc',
+            'shorturl' => 'abc',
+            'title' => 'Note',
+            'description' => '',
+            'tags' => [],
+            'private' => false,
+            'created' => '2017-01-07T16:01:02+00:00',
+            'updated' => '',
+        ];
+
+        $this->assertEquals($expected, ApiUtils::formatLink($link, $indexUrl));
+    }
 }