3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller\Admin\ShaareManageControllerTest
;
7 use Shaarli\Bookmark\Bookmark
;
8 use Shaarli\Front\Controller\Admin\FrontAdminControllerMockHelper
;
9 use Shaarli\Front\Controller\Admin\ShaareManageController
;
10 use Shaarli\Http\HttpAccess
;
12 use Slim\Http\Request
;
13 use Slim\Http\Response
;
16 * Test GET /admin/shaare/private/{hash}
18 class SharePrivateTest
extends TestCase
20 use FrontAdminControllerMockHelper
;
22 /** @var ShaareManageController */
23 protected $controller;
25 public function setUp(): void
27 $this->createContainer();
29 $this->container
->httpAccess
= $this->createMock(HttpAccess
::class);
30 $this->controller
= new ShaareManageController($this->container
);
34 * Test shaare private with a private bookmark which does not have a key yet.
36 public function testSharePrivateWithNewPrivateBookmark(): void
39 $request = $this->createMock(Request
::class);
40 $response = new Response();
42 $bookmark = (new Bookmark())
44 ->setUrl('http://domain.tld')
45 ->setTitle('Title 123')
49 $this->container
->bookmarkService
50 ->expects(static::once())
51 ->method('findByHash')
53 ->willReturn($bookmark)
55 $this->container
->bookmarkService
56 ->expects(static::once())
58 ->with($bookmark, true)
59 ->willReturnCallback(function (Bookmark
$bookmark): Bookmark
{
60 static::assertSame(32, strlen($bookmark->getAdditionalContentEntry('private_key')));
66 $result = $this->controller
->sharePrivate($request, $response, ['hash' => $hash]);
68 static::assertSame(302, $result->getStatusCode());
69 static::assertRegExp('#/subfolder/shaare/' . $hash . '\?key=\w{32}#', $result->getHeaderLine('Location'));
73 * Test shaare private with a private bookmark which does already have a key.
75 public function testSharePrivateWithExistingPrivateBookmark(): void
78 $existingKey = 'this is a private key';
79 $request = $this->createMock(Request
::class);
80 $response = new Response();
82 $bookmark = (new Bookmark())
84 ->setUrl('http://domain.tld')
85 ->setTitle('Title 123')
87 ->addAdditionalContentEntry('private_key', $existingKey)
90 $this->container
->bookmarkService
91 ->expects(static::once())
92 ->method('findByHash')
94 ->willReturn($bookmark)
96 $this->container
->bookmarkService
97 ->expects(static::never())
101 $result = $this->controller
->sharePrivate($request, $response, ['hash' => $hash]);
103 static::assertSame(302, $result->getStatusCode());
104 static::assertSame('/subfolder/shaare/' . $hash . '?key=' . $existingKey, $result->getHeaderLine('Location'));
108 * Test shaare private with a public bookmark.
110 public function testSharePrivateWithPublicBookmark(): void
113 $request = $this->createMock(Request
::class);
114 $response = new Response();
116 $bookmark = (new Bookmark())
118 ->setUrl('http://domain.tld')
119 ->setTitle('Title 123')
123 $this->container
->bookmarkService
124 ->expects(static::once())
125 ->method('findByHash')
127 ->willReturn($bookmark)
129 $this->container
->bookmarkService
130 ->expects(static::never())
134 $result = $this->controller
->sharePrivate($request, $response, ['hash' => $hash]);
136 static::assertSame(302, $result->getStatusCode());
137 static::assertSame('/subfolder/shaare/' . $hash, $result->getHeaderLine('Location'));