]>
Commit | Line | Data |
---|---|---|
03340c18 A |
1 | <?php |
2 | ||
3 | declare(strict_types=1); | |
4 | ||
2899ebb5 | 5 | namespace Shaarli\Front\Controller\Visitor; |
03340c18 A |
6 | |
7 | use PHPUnit\Framework\TestCase; | |
03340c18 A |
8 | use Slim\Http\Request; |
9 | use Slim\Http\Response; | |
10 | ||
11 | class 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 | { | |
03340c18 A |
26 | $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/']; |
27 | ||
28 | $request = $this->createMock(Request::class); | |
29 | $response = new Response(); | |
30 | ||
31 | $tags = ['newTag' => 'abc']; | |
32 | ||
33 | $result = $this->controller->addTag($request, $response, $tags); | |
34 | ||
35 | static::assertInstanceOf(Response::class, $result); | |
36 | static::assertSame(302, $result->getStatusCode()); | |
37 | static::assertSame(['/controller/?searchtags=abc'], $result->getHeader('location')); | |
38 | } | |
39 | ||
40 | public function testAddTagWithRefererAndExistingSearch(): void | |
41 | { | |
03340c18 A |
42 | $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def']; |
43 | ||
44 | $request = $this->createMock(Request::class); | |
45 | $response = new Response(); | |
46 | ||
47 | $tags = ['newTag' => 'abc']; | |
48 | ||
49 | $result = $this->controller->addTag($request, $response, $tags); | |
50 | ||
51 | static::assertInstanceOf(Response::class, $result); | |
52 | static::assertSame(302, $result->getStatusCode()); | |
53 | static::assertSame(['/controller/?searchtags=def+abc'], $result->getHeader('location')); | |
54 | } | |
55 | ||
56 | public function testAddTagWithoutRefererAndExistingSearch(): void | |
57 | { | |
03340c18 A |
58 | $request = $this->createMock(Request::class); |
59 | $response = new Response(); | |
60 | ||
61 | $tags = ['newTag' => 'abc']; | |
62 | ||
63 | $result = $this->controller->addTag($request, $response, $tags); | |
64 | ||
65 | static::assertInstanceOf(Response::class, $result); | |
66 | static::assertSame(302, $result->getStatusCode()); | |
9c75f877 | 67 | static::assertSame(['/subfolder/?searchtags=abc'], $result->getHeader('location')); |
03340c18 A |
68 | } |
69 | ||
70 | public function testAddTagRemoveLegacyQueryParam(): void | |
71 | { | |
03340c18 A |
72 | $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def&addtag=abc']; |
73 | ||
74 | $request = $this->createMock(Request::class); | |
75 | $response = new Response(); | |
76 | ||
77 | $tags = ['newTag' => 'abc']; | |
78 | ||
79 | $result = $this->controller->addTag($request, $response, $tags); | |
80 | ||
81 | static::assertInstanceOf(Response::class, $result); | |
82 | static::assertSame(302, $result->getStatusCode()); | |
83 | static::assertSame(['/controller/?searchtags=def+abc'], $result->getHeader('location')); | |
84 | } | |
85 | ||
86 | public function testAddTagResetPagination(): void | |
87 | { | |
03340c18 A |
88 | $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def&page=12']; |
89 | ||
90 | $request = $this->createMock(Request::class); | |
91 | $response = new Response(); | |
92 | ||
93 | $tags = ['newTag' => 'abc']; | |
94 | ||
95 | $result = $this->controller->addTag($request, $response, $tags); | |
96 | ||
97 | static::assertInstanceOf(Response::class, $result); | |
98 | static::assertSame(302, $result->getStatusCode()); | |
99 | static::assertSame(['/controller/?searchtags=def+abc'], $result->getHeader('location')); | |
100 | } | |
101 | ||
102 | public function testAddTagWithRefererAndEmptySearch(): void | |
103 | { | |
03340c18 A |
104 | $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=']; |
105 | ||
106 | $request = $this->createMock(Request::class); | |
107 | $response = new Response(); | |
108 | ||
109 | $tags = ['newTag' => 'abc']; | |
110 | ||
111 | $result = $this->controller->addTag($request, $response, $tags); | |
112 | ||
113 | static::assertInstanceOf(Response::class, $result); | |
114 | static::assertSame(302, $result->getStatusCode()); | |
115 | static::assertSame(['/controller/?searchtags=abc'], $result->getHeader('location')); | |
116 | } | |
117 | ||
118 | public function testAddTagWithoutNewTagWithReferer(): void | |
119 | { | |
03340c18 A |
120 | $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def']; |
121 | ||
122 | $request = $this->createMock(Request::class); | |
123 | $response = new Response(); | |
124 | ||
125 | $result = $this->controller->addTag($request, $response, []); | |
126 | ||
127 | static::assertInstanceOf(Response::class, $result); | |
128 | static::assertSame(302, $result->getStatusCode()); | |
129 | static::assertSame(['/controller/?searchtags=def'], $result->getHeader('location')); | |
130 | } | |
131 | ||
132 | public function testAddTagWithoutNewTagWithoutReferer(): void | |
133 | { | |
03340c18 A |
134 | $request = $this->createMock(Request::class); |
135 | $response = new Response(); | |
136 | ||
137 | $result = $this->controller->addTag($request, $response, []); | |
138 | ||
139 | static::assertInstanceOf(Response::class, $result); | |
140 | static::assertSame(302, $result->getStatusCode()); | |
9c75f877 | 141 | static::assertSame(['/subfolder/'], $result->getHeader('location')); |
03340c18 | 142 | } |
893f5159 A |
143 | |
144 | public function testRemoveTagWithoutMatchingTag(): void | |
145 | { | |
893f5159 A |
146 | $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def']; |
147 | ||
148 | $request = $this->createMock(Request::class); | |
149 | $response = new Response(); | |
150 | ||
151 | $tags = ['tag' => 'abc']; | |
152 | ||
153 | $result = $this->controller->removeTag($request, $response, $tags); | |
154 | ||
155 | static::assertInstanceOf(Response::class, $result); | |
156 | static::assertSame(302, $result->getStatusCode()); | |
157 | static::assertSame(['/controller/?searchtags=def'], $result->getHeader('location')); | |
158 | } | |
159 | ||
160 | public function testRemoveTagWithoutTagsearch(): void | |
161 | { | |
893f5159 A |
162 | $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/']; |
163 | ||
164 | $request = $this->createMock(Request::class); | |
165 | $response = new Response(); | |
166 | ||
167 | $tags = ['tag' => 'abc']; | |
168 | ||
169 | $result = $this->controller->removeTag($request, $response, $tags); | |
170 | ||
171 | static::assertInstanceOf(Response::class, $result); | |
172 | static::assertSame(302, $result->getStatusCode()); | |
173 | static::assertSame(['/controller/'], $result->getHeader('location')); | |
174 | } | |
175 | ||
176 | public function testRemoveTagWithoutReferer(): void | |
177 | { | |
893f5159 A |
178 | $request = $this->createMock(Request::class); |
179 | $response = new Response(); | |
180 | ||
181 | $tags = ['tag' => 'abc']; | |
182 | ||
183 | $result = $this->controller->removeTag($request, $response, $tags); | |
184 | ||
185 | static::assertInstanceOf(Response::class, $result); | |
186 | static::assertSame(302, $result->getStatusCode()); | |
9c75f877 | 187 | static::assertSame(['/subfolder/'], $result->getHeader('location')); |
893f5159 A |
188 | } |
189 | ||
190 | public function testRemoveTagWithoutTag(): void | |
191 | { | |
893f5159 A |
192 | $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/?searchtag=abc']; |
193 | ||
194 | $request = $this->createMock(Request::class); | |
195 | $response = new Response(); | |
196 | ||
197 | $result = $this->controller->removeTag($request, $response, []); | |
198 | ||
199 | static::assertInstanceOf(Response::class, $result); | |
200 | static::assertSame(302, $result->getStatusCode()); | |
201 | static::assertSame(['/controller/?searchtag=abc'], $result->getHeader('location')); | |
202 | } | |
203 | ||
204 | public function testRemoveTagWithoutTagWithoutReferer(): void | |
205 | { | |
893f5159 A |
206 | $request = $this->createMock(Request::class); |
207 | $response = new Response(); | |
208 | ||
209 | $result = $this->controller->removeTag($request, $response, []); | |
210 | ||
211 | static::assertInstanceOf(Response::class, $result); | |
212 | static::assertSame(302, $result->getStatusCode()); | |
9c75f877 | 213 | static::assertSame(['/subfolder/'], $result->getHeader('location')); |
893f5159 | 214 | } |
03340c18 | 215 | } |