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