diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-11-12 13:11:07 +0100 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2020-11-12 13:11:07 +0100 |
commit | af50eba28a7bd286de4c8c9ee6dc5216b915d149 (patch) | |
tree | ffa30a9358e82d27be75d8fc5e57f3c8820dc6d3 /tests/front/controller/admin/ShaarePublishControllerTest/DisplayEditFormTest.php | |
parent | b6f678a5a1d15acf284ebcec16c905e976671ce1 (diff) | |
parent | 1409f1c89a7ca01456ae2dcd6357d296e2b99f5a (diff) | |
download | Shaarli-af50eba28a7bd286de4c8c9ee6dc5216b915d149.tar.gz Shaarli-af50eba28a7bd286de4c8c9ee6dc5216b915d149.tar.zst Shaarli-af50eba28a7bd286de4c8c9ee6dc5216b915d149.zip |
Merge tag 'v0.12.1' into latestlatest
v0.12.1
Diffstat (limited to 'tests/front/controller/admin/ShaarePublishControllerTest/DisplayEditFormTest.php')
-rw-r--r-- | tests/front/controller/admin/ShaarePublishControllerTest/DisplayEditFormTest.php | 155 |
1 files changed, 155 insertions, 0 deletions
diff --git a/tests/front/controller/admin/ShaarePublishControllerTest/DisplayEditFormTest.php b/tests/front/controller/admin/ShaarePublishControllerTest/DisplayEditFormTest.php new file mode 100644 index 00000000..738cea12 --- /dev/null +++ b/tests/front/controller/admin/ShaarePublishControllerTest/DisplayEditFormTest.php | |||
@@ -0,0 +1,155 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Shaarli\Front\Controller\Admin\ShaarePublishControllerTest; | ||
6 | |||
7 | use Shaarli\Bookmark\Bookmark; | ||
8 | use Shaarli\Bookmark\Exception\BookmarkNotFoundException; | ||
9 | use Shaarli\Front\Controller\Admin\FrontAdminControllerMockHelper; | ||
10 | use Shaarli\Front\Controller\Admin\ShaarePublishController; | ||
11 | use Shaarli\Http\HttpAccess; | ||
12 | use Shaarli\Security\SessionManager; | ||
13 | use Shaarli\TestCase; | ||
14 | use Slim\Http\Request; | ||
15 | use Slim\Http\Response; | ||
16 | |||
17 | class DisplayEditFormTest extends TestCase | ||
18 | { | ||
19 | use FrontAdminControllerMockHelper; | ||
20 | |||
21 | /** @var ShaarePublishController */ | ||
22 | protected $controller; | ||
23 | |||
24 | public function setUp(): void | ||
25 | { | ||
26 | $this->createContainer(); | ||
27 | |||
28 | $this->container->httpAccess = $this->createMock(HttpAccess::class); | ||
29 | $this->controller = new ShaarePublishController($this->container); | ||
30 | } | ||
31 | |||
32 | /** | ||
33 | * Test displaying bookmark edit form | ||
34 | * When an existing ID is provided, ensure that default workflow works properly. | ||
35 | */ | ||
36 | public function testDisplayEditFormDefault(): void | ||
37 | { | ||
38 | $assignedVariables = []; | ||
39 | $this->assignTemplateVars($assignedVariables); | ||
40 | |||
41 | $id = 11; | ||
42 | |||
43 | $request = $this->createMock(Request::class); | ||
44 | $response = new Response(); | ||
45 | |||
46 | $this->container->httpAccess->expects(static::never())->method('getHttpResponse'); | ||
47 | $this->container->httpAccess->expects(static::never())->method('getCurlDownloadCallback'); | ||
48 | |||
49 | $this->container->bookmarkService | ||
50 | ->expects(static::once()) | ||
51 | ->method('get') | ||
52 | ->with($id) | ||
53 | ->willReturn( | ||
54 | (new Bookmark()) | ||
55 | ->setId($id) | ||
56 | ->setUrl($url = 'http://domain.tld') | ||
57 | ->setTitle($title = 'Bookmark Title') | ||
58 | ->setDescription($description = 'Bookmark description.') | ||
59 | ->setTags($tags = ['abc', 'def']) | ||
60 | ->setPrivate(true) | ||
61 | ->setCreated($createdAt = new \DateTime('2020-06-10 18:45:44')) | ||
62 | ) | ||
63 | ; | ||
64 | |||
65 | $result = $this->controller->displayEditForm($request, $response, ['id' => (string) $id]); | ||
66 | |||
67 | static::assertSame(200, $result->getStatusCode()); | ||
68 | static::assertSame('editlink', (string) $result->getBody()); | ||
69 | |||
70 | static::assertSame('Edit Shaare - Shaarli', $assignedVariables['pagetitle']); | ||
71 | static::assertFalse($assignedVariables['link_is_new']); | ||
72 | |||
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']); | ||
80 | } | ||
81 | |||
82 | /** | ||
83 | * Test displaying bookmark edit form | ||
84 | * Invalid ID provided. | ||
85 | */ | ||
86 | public function testDisplayEditFormInvalidId(): void | ||
87 | { | ||
88 | $id = 'invalid'; | ||
89 | |||
90 | $request = $this->createMock(Request::class); | ||
91 | $response = new Response(); | ||
92 | |||
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.']) | ||
97 | ; | ||
98 | |||
99 | $result = $this->controller->displayEditForm($request, $response, ['id' => $id]); | ||
100 | |||
101 | static::assertSame(302, $result->getStatusCode()); | ||
102 | static::assertSame(['/subfolder/'], $result->getHeader('location')); | ||
103 | } | ||
104 | |||
105 | /** | ||
106 | * Test displaying bookmark edit form | ||
107 | * ID not provided. | ||
108 | */ | ||
109 | public function testDisplayEditFormIdNotProvided(): void | ||
110 | { | ||
111 | $request = $this->createMock(Request::class); | ||
112 | $response = new Response(); | ||
113 | |||
114 | $this->container->sessionManager | ||
115 | ->expects(static::once()) | ||
116 | ->method('setSessionParameter') | ||
117 | ->with(SessionManager::KEY_ERROR_MESSAGES, ['Bookmark with identifier could not be found.']) | ||
118 | ; | ||
119 | |||
120 | $result = $this->controller->displayEditForm($request, $response, []); | ||
121 | |||
122 | static::assertSame(302, $result->getStatusCode()); | ||
123 | static::assertSame(['/subfolder/'], $result->getHeader('location')); | ||
124 | } | ||
125 | |||
126 | /** | ||
127 | * Test displaying bookmark edit form | ||
128 | * Bookmark not found. | ||
129 | */ | ||
130 | public function testDisplayEditFormBookmarkNotFound(): void | ||
131 | { | ||
132 | $id = 123; | ||
133 | |||
134 | $request = $this->createMock(Request::class); | ||
135 | $response = new Response(); | ||
136 | |||
137 | $this->container->bookmarkService | ||
138 | ->expects(static::once()) | ||
139 | ->method('get') | ||
140 | ->with($id) | ||
141 | ->willThrowException(new BookmarkNotFoundException()) | ||
142 | ; | ||
143 | |||
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.']) | ||
148 | ; | ||
149 | |||
150 | $result = $this->controller->displayEditForm($request, $response, ['id' => (string) $id]); | ||
151 | |||
152 | static::assertSame(302, $result->getStatusCode()); | ||
153 | static::assertSame(['/subfolder/'], $result->getHeader('location')); | ||
154 | } | ||
155 | } | ||