]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/front/controller/visitor/TagControllerTest.php
Initialize admin Slim controllers
[github/shaarli/Shaarli.git] / tests / front / controller / visitor / TagControllerTest.php
CommitLineData
03340c18
A
1<?php
2
3declare(strict_types=1);
4
2899ebb5 5namespace Shaarli\Front\Controller\Visitor;
03340c18
A
6
7use PHPUnit\Framework\TestCase;
03340c18
A
8use Slim\Http\Request;
9use Slim\Http\Response;
10
11class TagControllerTest extends TestCase
12{
dd09ec52 13 use FrontControllerMockHelper;
03340c18 14
2899ebb5 15 /** @var TagController */ protected $controller;
03340c18
A
16
17 public function setUp(): void
18 {
dd09ec52
A
19 $this->createContainer();
20
03340c18
A
21 $this->controller = new TagController($this->container);
22 }
23
24 public function testAddTagWithReferer(): void
25 {
26 $this->createValidContainerMockSet();
dd09ec52 27
03340c18
A
28 $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/'];
29
30 $request = $this->createMock(Request::class);
31 $response = new Response();
32
33 $tags = ['newTag' => 'abc'];
34
35 $result = $this->controller->addTag($request, $response, $tags);
36
37 static::assertInstanceOf(Response::class, $result);
38 static::assertSame(302, $result->getStatusCode());
39 static::assertSame(['/controller/?searchtags=abc'], $result->getHeader('location'));
40 }
41
42 public function testAddTagWithRefererAndExistingSearch(): void
43 {
44 $this->createValidContainerMockSet();
dd09ec52 45
03340c18
A
46 $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def'];
47
48 $request = $this->createMock(Request::class);
49 $response = new Response();
50
51 $tags = ['newTag' => 'abc'];
52
53 $result = $this->controller->addTag($request, $response, $tags);
54
55 static::assertInstanceOf(Response::class, $result);
56 static::assertSame(302, $result->getStatusCode());
57 static::assertSame(['/controller/?searchtags=def+abc'], $result->getHeader('location'));
58 }
59
60 public function testAddTagWithoutRefererAndExistingSearch(): void
61 {
62 $this->createValidContainerMockSet();
63
64 $request = $this->createMock(Request::class);
65 $response = new Response();
66
67 $tags = ['newTag' => 'abc'];
68
69 $result = $this->controller->addTag($request, $response, $tags);
70
71 static::assertInstanceOf(Response::class, $result);
72 static::assertSame(302, $result->getStatusCode());
73 static::assertSame(['./?searchtags=abc'], $result->getHeader('location'));
74 }
75
76 public function testAddTagRemoveLegacyQueryParam(): void
77 {
78 $this->createValidContainerMockSet();
dd09ec52 79
03340c18
A
80 $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def&addtag=abc'];
81
82 $request = $this->createMock(Request::class);
83 $response = new Response();
84
85 $tags = ['newTag' => 'abc'];
86
87 $result = $this->controller->addTag($request, $response, $tags);
88
89 static::assertInstanceOf(Response::class, $result);
90 static::assertSame(302, $result->getStatusCode());
91 static::assertSame(['/controller/?searchtags=def+abc'], $result->getHeader('location'));
92 }
93
94 public function testAddTagResetPagination(): void
95 {
96 $this->createValidContainerMockSet();
dd09ec52 97
03340c18
A
98 $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def&page=12'];
99
100 $request = $this->createMock(Request::class);
101 $response = new Response();
102
103 $tags = ['newTag' => 'abc'];
104
105 $result = $this->controller->addTag($request, $response, $tags);
106
107 static::assertInstanceOf(Response::class, $result);
108 static::assertSame(302, $result->getStatusCode());
109 static::assertSame(['/controller/?searchtags=def+abc'], $result->getHeader('location'));
110 }
111
112 public function testAddTagWithRefererAndEmptySearch(): void
113 {
114 $this->createValidContainerMockSet();
dd09ec52 115
03340c18
A
116 $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags='];
117
118 $request = $this->createMock(Request::class);
119 $response = new Response();
120
121 $tags = ['newTag' => 'abc'];
122
123 $result = $this->controller->addTag($request, $response, $tags);
124
125 static::assertInstanceOf(Response::class, $result);
126 static::assertSame(302, $result->getStatusCode());
127 static::assertSame(['/controller/?searchtags=abc'], $result->getHeader('location'));
128 }
129
130 public function testAddTagWithoutNewTagWithReferer(): void
131 {
132 $this->createValidContainerMockSet();
dd09ec52 133
03340c18
A
134 $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def'];
135
136 $request = $this->createMock(Request::class);
137 $response = new Response();
138
139 $result = $this->controller->addTag($request, $response, []);
140
141 static::assertInstanceOf(Response::class, $result);
142 static::assertSame(302, $result->getStatusCode());
143 static::assertSame(['/controller/?searchtags=def'], $result->getHeader('location'));
144 }
145
146 public function testAddTagWithoutNewTagWithoutReferer(): void
147 {
148 $this->createValidContainerMockSet();
149
150 $request = $this->createMock(Request::class);
151 $response = new Response();
152
153 $result = $this->controller->addTag($request, $response, []);
154
155 static::assertInstanceOf(Response::class, $result);
156 static::assertSame(302, $result->getStatusCode());
157 static::assertSame(['./'], $result->getHeader('location'));
158 }
893f5159
A
159
160 public function testRemoveTagWithoutMatchingTag(): void
161 {
162 $this->createValidContainerMockSet();
163
164 $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def'];
165
166 $request = $this->createMock(Request::class);
167 $response = new Response();
168
169 $tags = ['tag' => 'abc'];
170
171 $result = $this->controller->removeTag($request, $response, $tags);
172
173 static::assertInstanceOf(Response::class, $result);
174 static::assertSame(302, $result->getStatusCode());
175 static::assertSame(['/controller/?searchtags=def'], $result->getHeader('location'));
176 }
177
178 public function testRemoveTagWithoutTagsearch(): void
179 {
180 $this->createValidContainerMockSet();
181
182 $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/'];
183
184 $request = $this->createMock(Request::class);
185 $response = new Response();
186
187 $tags = ['tag' => 'abc'];
188
189 $result = $this->controller->removeTag($request, $response, $tags);
190
191 static::assertInstanceOf(Response::class, $result);
192 static::assertSame(302, $result->getStatusCode());
193 static::assertSame(['/controller/'], $result->getHeader('location'));
194 }
195
196 public function testRemoveTagWithoutReferer(): void
197 {
198 $this->createValidContainerMockSet();
199
200 $request = $this->createMock(Request::class);
201 $response = new Response();
202
203 $tags = ['tag' => 'abc'];
204
205 $result = $this->controller->removeTag($request, $response, $tags);
206
207 static::assertInstanceOf(Response::class, $result);
208 static::assertSame(302, $result->getStatusCode());
209 static::assertSame(['./'], $result->getHeader('location'));
210 }
211
212 public function testRemoveTagWithoutTag(): void
213 {
214 $this->createValidContainerMockSet();
215
216 $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/?searchtag=abc'];
217
218 $request = $this->createMock(Request::class);
219 $response = new Response();
220
221 $result = $this->controller->removeTag($request, $response, []);
222
223 static::assertInstanceOf(Response::class, $result);
224 static::assertSame(302, $result->getStatusCode());
225 static::assertSame(['/controller/?searchtag=abc'], $result->getHeader('location'));
226 }
227
228 public function testRemoveTagWithoutTagWithoutReferer(): void
229 {
230 $this->createValidContainerMockSet();
231
232 $request = $this->createMock(Request::class);
233 $response = new Response();
234
235 $result = $this->controller->removeTag($request, $response, []);
236
237 static::assertInstanceOf(Response::class, $result);
238 static::assertSame(302, $result->getStatusCode());
239 static::assertSame(['./'], $result->getHeader('location'));
240 }
03340c18 241}