3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller\Visitor
;
7 use PHPUnit\Framework\TestCase
;
9 use Slim\Http\Response
;
11 class TagControllerTest
extends TestCase
13 use FrontControllerMockHelper
;
15 /** @var TagController */ protected $controller;
17 public function setUp(): void
19 $this->createContainer();
21 $this->controller
= new TagController($this->container
);
24 public function testAddTagWithReferer(): void
26 $this->createValidContainerMockSet();
28 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/controller/'];
30 $request = $this->createMock(Request
::class);
31 $response = new Response();
33 $tags = ['newTag' => 'abc'];
35 $result = $this->controller
->addTag($request, $response, $tags);
37 static::assertInstanceOf(Response
::class, $result);
38 static::assertSame(302, $result->getStatusCode());
39 static::assertSame(['/controller/?searchtags=abc'], $result->getHeader('location'));
42 public function testAddTagWithRefererAndExistingSearch(): void
44 $this->createValidContainerMockSet();
46 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def'];
48 $request = $this->createMock(Request
::class);
49 $response = new Response();
51 $tags = ['newTag' => 'abc'];
53 $result = $this->controller
->addTag($request, $response, $tags);
55 static::assertInstanceOf(Response
::class, $result);
56 static::assertSame(302, $result->getStatusCode());
57 static::assertSame(['/controller/?searchtags=def+abc'], $result->getHeader('location'));
60 public function testAddTagWithoutRefererAndExistingSearch(): void
62 $this->createValidContainerMockSet();
64 $request = $this->createMock(Request
::class);
65 $response = new Response();
67 $tags = ['newTag' => 'abc'];
69 $result = $this->controller
->addTag($request, $response, $tags);
71 static::assertInstanceOf(Response
::class, $result);
72 static::assertSame(302, $result->getStatusCode());
73 static::assertSame(['./?searchtags=abc'], $result->getHeader('location'));
76 public function testAddTagRemoveLegacyQueryParam(): void
78 $this->createValidContainerMockSet();
80 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def&addtag=abc'];
82 $request = $this->createMock(Request
::class);
83 $response = new Response();
85 $tags = ['newTag' => 'abc'];
87 $result = $this->controller
->addTag($request, $response, $tags);
89 static::assertInstanceOf(Response
::class, $result);
90 static::assertSame(302, $result->getStatusCode());
91 static::assertSame(['/controller/?searchtags=def+abc'], $result->getHeader('location'));
94 public function testAddTagResetPagination(): void
96 $this->createValidContainerMockSet();
98 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def&page=12'];
100 $request = $this->createMock(Request
::class);
101 $response = new Response();
103 $tags = ['newTag' => 'abc'];
105 $result = $this->controller
->addTag($request, $response, $tags);
107 static::assertInstanceOf(Response
::class, $result);
108 static::assertSame(302, $result->getStatusCode());
109 static::assertSame(['/controller/?searchtags=def+abc'], $result->getHeader('location'));
112 public function testAddTagWithRefererAndEmptySearch(): void
114 $this->createValidContainerMockSet();
116 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags='];
118 $request = $this->createMock(Request
::class);
119 $response = new Response();
121 $tags = ['newTag' => 'abc'];
123 $result = $this->controller
->addTag($request, $response, $tags);
125 static::assertInstanceOf(Response
::class, $result);
126 static::assertSame(302, $result->getStatusCode());
127 static::assertSame(['/controller/?searchtags=abc'], $result->getHeader('location'));
130 public function testAddTagWithoutNewTagWithReferer(): void
132 $this->createValidContainerMockSet();
134 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def'];
136 $request = $this->createMock(Request
::class);
137 $response = new Response();
139 $result = $this->controller
->addTag($request, $response, []);
141 static::assertInstanceOf(Response
::class, $result);
142 static::assertSame(302, $result->getStatusCode());
143 static::assertSame(['/controller/?searchtags=def'], $result->getHeader('location'));
146 public function testAddTagWithoutNewTagWithoutReferer(): void
148 $this->createValidContainerMockSet();
150 $request = $this->createMock(Request
::class);
151 $response = new Response();
153 $result = $this->controller
->addTag($request, $response, []);
155 static::assertInstanceOf(Response
::class, $result);
156 static::assertSame(302, $result->getStatusCode());
157 static::assertSame(['./'], $result->getHeader('location'));
160 public function testRemoveTagWithoutMatchingTag(): void
162 $this->createValidContainerMockSet();
164 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def'];
166 $request = $this->createMock(Request
::class);
167 $response = new Response();
169 $tags = ['tag' => 'abc'];
171 $result = $this->controller
->removeTag($request, $response, $tags);
173 static::assertInstanceOf(Response
::class, $result);
174 static::assertSame(302, $result->getStatusCode());
175 static::assertSame(['/controller/?searchtags=def'], $result->getHeader('location'));
178 public function testRemoveTagWithoutTagsearch(): void
180 $this->createValidContainerMockSet();
182 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/controller/'];
184 $request = $this->createMock(Request
::class);
185 $response = new Response();
187 $tags = ['tag' => 'abc'];
189 $result = $this->controller
->removeTag($request, $response, $tags);
191 static::assertInstanceOf(Response
::class, $result);
192 static::assertSame(302, $result->getStatusCode());
193 static::assertSame(['/controller/'], $result->getHeader('location'));
196 public function testRemoveTagWithoutReferer(): void
198 $this->createValidContainerMockSet();
200 $request = $this->createMock(Request
::class);
201 $response = new Response();
203 $tags = ['tag' => 'abc'];
205 $result = $this->controller
->removeTag($request, $response, $tags);
207 static::assertInstanceOf(Response
::class, $result);
208 static::assertSame(302, $result->getStatusCode());
209 static::assertSame(['./'], $result->getHeader('location'));
212 public function testRemoveTagWithoutTag(): void
214 $this->createValidContainerMockSet();
216 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/controller/?searchtag=abc'];
218 $request = $this->createMock(Request
::class);
219 $response = new Response();
221 $result = $this->controller
->removeTag($request, $response, []);
223 static::assertInstanceOf(Response
::class, $result);
224 static::assertSame(302, $result->getStatusCode());
225 static::assertSame(['/controller/?searchtag=abc'], $result->getHeader('location'));
228 public function testRemoveTagWithoutTagWithoutReferer(): void
230 $this->createValidContainerMockSet();
232 $request = $this->createMock(Request
::class);
233 $response = new Response();
235 $result = $this->controller
->removeTag($request, $response, []);
237 static::assertInstanceOf(Response
::class, $result);
238 static::assertSame(302, $result->getStatusCode());
239 static::assertSame(['./'], $result->getHeader('location'));