3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller\Admin\ManageShaareControllerTest
;
7 use PHPUnit\Framework\TestCase
;
8 use Shaarli\Bookmark\Bookmark
;
9 use Shaarli\Bookmark\Exception\BookmarkNotFoundException
;
10 use Shaarli\Front\Controller\Admin\FrontAdminControllerMockHelper
;
11 use Shaarli\Front\Controller\Admin\ManageShaareController
;
12 use Shaarli\Http\HttpAccess
;
13 use Shaarli\Security\SessionManager
;
14 use Slim\Http\Request
;
15 use Slim\Http\Response
;
17 class DisplayEditFormTest
extends TestCase
19 use FrontAdminControllerMockHelper
;
21 /** @var ManageShaareController */
22 protected $controller;
24 public function setUp(): void
26 $this->createContainer();
28 $this->container
->httpAccess
= $this->createMock(HttpAccess
::class);
29 $this->controller
= new ManageShaareController($this->container
);
33 * Test displaying bookmark edit form
34 * When an existing ID is provided, ensure that default workflow works properly.
36 public function testDisplayEditFormDefault(): void
38 $assignedVariables = [];
39 $this->assignTemplateVars($assignedVariables);
43 $request = $this->createMock(Request
::class);
44 $response = new Response();
46 $this->container
->httpAccess
->expects(static::never())->method('getHttpResponse');
47 $this->container
->httpAccess
->expects(static::never())->method('getCurlDownloadCallback');
49 $this->container
->bookmarkService
50 ->expects(static::once())
56 ->setUrl($url = 'http://domain.tld')
57 ->setTitle($title = 'Bookmark Title')
58 ->setDescription($description = 'Bookmark description.')
59 ->setTags($tags = ['abc', 'def'])
61 ->setCreated($createdAt = new \
DateTime('2020-06-10 18:45:44'))
65 $result = $this->controller
->displayEditForm($request, $response, ['id' => (string) $id]);
67 static::assertSame(200, $result->getStatusCode());
68 static::assertSame('editlink', (string) $result->getBody());
70 static::assertSame('Edit Shaare - Shaarli', $assignedVariables['pagetitle']);
71 static::assertFalse($assignedVariables['link_is_new']);
73 static::assertSame($id, $assignedVariables['link']['id']);
74 static::assertSame($url, $assignedVariables['link']['url']);
75 static::assertSame($title, $assignedVariables['link']['title']);
76 static::assertSame($description, $assignedVariables['link']['description']);
77 static::assertSame(implode(' ', $tags), $assignedVariables['link']['tags']);
78 static::assertTrue($assignedVariables['link']['private']);
79 static::assertSame($createdAt, $assignedVariables['link']['created']);
83 * Test displaying bookmark edit form
84 * Invalid ID provided.
86 public function testDisplayEditFormInvalidId(): void
90 $request = $this->createMock(Request
::class);
91 $response = new Response();
93 $this->container
->sessionManager
94 ->expects(static::once())
95 ->method('setSessionParameter')
96 ->with(SessionManager
::KEY_ERROR_MESSAGES
, ['Bookmark with identifier invalid could not be found.'])
99 $result = $this->controller
->displayEditForm($request, $response, ['id' => $id]);
101 static::assertSame(302, $result->getStatusCode());
102 static::assertSame(['/subfolder/'], $result->getHeader('location'));
106 * Test displaying bookmark edit form
109 public function testDisplayEditFormIdNotProvided(): void
111 $request = $this->createMock(Request
::class);
112 $response = new Response();
114 $this->container
->sessionManager
115 ->expects(static::once())
116 ->method('setSessionParameter')
117 ->with(SessionManager
::KEY_ERROR_MESSAGES
, ['Bookmark with identifier could not be found.'])
120 $result = $this->controller
->displayEditForm($request, $response, []);
122 static::assertSame(302, $result->getStatusCode());
123 static::assertSame(['/subfolder/'], $result->getHeader('location'));
127 * Test displaying bookmark edit form
128 * Bookmark not found.
130 public function testDisplayEditFormBookmarkNotFound(): void
134 $request = $this->createMock(Request
::class);
135 $response = new Response();
137 $this->container
->bookmarkService
138 ->expects(static::once())
141 ->willThrowException(new BookmarkNotFoundException())
144 $this->container
->sessionManager
145 ->expects(static::once())
146 ->method('setSessionParameter')
147 ->with(SessionManager
::KEY_ERROR_MESSAGES
, ['Bookmark with identifier 123 could not be found.'])
150 $result = $this->controller
->displayEditForm($request, $response, ['id' => (string) $id]);
152 static::assertSame(302, $result->getStatusCode());
153 static::assertSame(['/subfolder/'], $result->getHeader('location'));