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->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/controller/'];
28 $request = $this->createMock(Request
::class);
29 $response = new Response();
31 $tags = ['newTag' => 'abc'];
33 $result = $this->controller
->addTag($request, $response, $tags);
35 static::assertInstanceOf(Response
::class, $result);
36 static::assertSame(302, $result->getStatusCode());
37 static::assertSame(['/controller/?searchtags=abc'], $result->getHeader('location'));
40 public function testAddTagWithRefererAndExistingSearch(): void
42 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def'];
44 $request = $this->createMock(Request
::class);
45 $response = new Response();
47 $tags = ['newTag' => 'abc'];
49 $result = $this->controller
->addTag($request, $response, $tags);
51 static::assertInstanceOf(Response
::class, $result);
52 static::assertSame(302, $result->getStatusCode());
53 static::assertSame(['/controller/?searchtags=def+abc'], $result->getHeader('location'));
56 public function testAddTagWithoutRefererAndExistingSearch(): void
58 $request = $this->createMock(Request
::class);
59 $response = new Response();
61 $tags = ['newTag' => 'abc'];
63 $result = $this->controller
->addTag($request, $response, $tags);
65 static::assertInstanceOf(Response
::class, $result);
66 static::assertSame(302, $result->getStatusCode());
67 static::assertSame(['/subfolder/?searchtags=abc'], $result->getHeader('location'));
70 public function testAddTagRemoveLegacyQueryParam(): void
72 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def&addtag=abc'];
74 $request = $this->createMock(Request
::class);
75 $response = new Response();
77 $tags = ['newTag' => 'abc'];
79 $result = $this->controller
->addTag($request, $response, $tags);
81 static::assertInstanceOf(Response
::class, $result);
82 static::assertSame(302, $result->getStatusCode());
83 static::assertSame(['/controller/?searchtags=def+abc'], $result->getHeader('location'));
86 public function testAddTagResetPagination(): void
88 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def&page=12'];
90 $request = $this->createMock(Request
::class);
91 $response = new Response();
93 $tags = ['newTag' => 'abc'];
95 $result = $this->controller
->addTag($request, $response, $tags);
97 static::assertInstanceOf(Response
::class, $result);
98 static::assertSame(302, $result->getStatusCode());
99 static::assertSame(['/controller/?searchtags=def+abc'], $result->getHeader('location'));
102 public function testAddTagWithRefererAndEmptySearch(): void
104 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags='];
106 $request = $this->createMock(Request
::class);
107 $response = new Response();
109 $tags = ['newTag' => 'abc'];
111 $result = $this->controller
->addTag($request, $response, $tags);
113 static::assertInstanceOf(Response
::class, $result);
114 static::assertSame(302, $result->getStatusCode());
115 static::assertSame(['/controller/?searchtags=abc'], $result->getHeader('location'));
118 public function testAddTagWithoutNewTagWithReferer(): void
120 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def'];
122 $request = $this->createMock(Request
::class);
123 $response = new Response();
125 $result = $this->controller
->addTag($request, $response, []);
127 static::assertInstanceOf(Response
::class, $result);
128 static::assertSame(302, $result->getStatusCode());
129 static::assertSame(['/controller/?searchtags=def'], $result->getHeader('location'));
132 public function testAddTagWithoutNewTagWithoutReferer(): void
134 $request = $this->createMock(Request
::class);
135 $response = new Response();
137 $result = $this->controller
->addTag($request, $response, []);
139 static::assertInstanceOf(Response
::class, $result);
140 static::assertSame(302, $result->getStatusCode());
141 static::assertSame(['/subfolder/'], $result->getHeader('location'));
144 public function testRemoveTagWithoutMatchingTag(): void
146 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def'];
148 $request = $this->createMock(Request
::class);
149 $response = new Response();
151 $tags = ['tag' => 'abc'];
153 $result = $this->controller
->removeTag($request, $response, $tags);
155 static::assertInstanceOf(Response
::class, $result);
156 static::assertSame(302, $result->getStatusCode());
157 static::assertSame(['/controller/?searchtags=def'], $result->getHeader('location'));
160 public function testRemoveTagWithoutTagsearch(): void
162 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/controller/'];
164 $request = $this->createMock(Request
::class);
165 $response = new Response();
167 $tags = ['tag' => 'abc'];
169 $result = $this->controller
->removeTag($request, $response, $tags);
171 static::assertInstanceOf(Response
::class, $result);
172 static::assertSame(302, $result->getStatusCode());
173 static::assertSame(['/controller/'], $result->getHeader('location'));
176 public function testRemoveTagWithoutReferer(): void
178 $request = $this->createMock(Request
::class);
179 $response = new Response();
181 $tags = ['tag' => 'abc'];
183 $result = $this->controller
->removeTag($request, $response, $tags);
185 static::assertInstanceOf(Response
::class, $result);
186 static::assertSame(302, $result->getStatusCode());
187 static::assertSame(['/subfolder/'], $result->getHeader('location'));
190 public function testRemoveTagWithoutTag(): void
192 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/controller/?searchtag=abc'];
194 $request = $this->createMock(Request
::class);
195 $response = new Response();
197 $result = $this->controller
->removeTag($request, $response, []);
199 static::assertInstanceOf(Response
::class, $result);
200 static::assertSame(302, $result->getStatusCode());
201 static::assertSame(['/controller/?searchtag=abc'], $result->getHeader('location'));
204 public function testRemoveTagWithoutTagWithoutReferer(): void
206 $request = $this->createMock(Request
::class);
207 $response = new Response();
209 $result = $this->controller
->removeTag($request, $response, []);
211 static::assertInstanceOf(Response
::class, $result);
212 static::assertSame(302, $result->getStatusCode());
213 static::assertSame(['/subfolder/'], $result->getHeader('location'));