3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller
;
7 use PHPUnit\Framework\TestCase
;
8 use Shaarli\Bookmark\BookmarkServiceInterface
;
9 use Shaarli\Config\ConfigManager
;
10 use Shaarli\Container\ShaarliContainer
;
11 use Shaarli\Plugin\PluginManager
;
12 use Shaarli\Render\PageBuilder
;
13 use Shaarli\Security\LoginManager
;
14 use Slim\Http\Request
;
15 use Slim\Http\Response
;
17 class TagControllerTest
extends TestCase
19 /** @var ShaarliContainer */
22 /** @var TagController */
23 protected $controller;
25 public function setUp(): void
27 $this->container
= $this->createMock(ShaarliContainer
::class);
28 $this->controller
= new TagController($this->container
);
31 public function testAddTagWithReferer(): void
33 $this->createValidContainerMockSet();
34 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/controller/'];
36 $request = $this->createMock(Request
::class);
37 $response = new Response();
39 $tags = ['newTag' => 'abc'];
41 $result = $this->controller
->addTag($request, $response, $tags);
43 static::assertInstanceOf(Response
::class, $result);
44 static::assertSame(302, $result->getStatusCode());
45 static::assertSame(['/controller/?searchtags=abc'], $result->getHeader('location'));
48 public function testAddTagWithRefererAndExistingSearch(): void
50 $this->createValidContainerMockSet();
51 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def'];
53 $request = $this->createMock(Request
::class);
54 $response = new Response();
56 $tags = ['newTag' => 'abc'];
58 $result = $this->controller
->addTag($request, $response, $tags);
60 static::assertInstanceOf(Response
::class, $result);
61 static::assertSame(302, $result->getStatusCode());
62 static::assertSame(['/controller/?searchtags=def+abc'], $result->getHeader('location'));
65 public function testAddTagWithoutRefererAndExistingSearch(): void
67 $this->createValidContainerMockSet();
69 $request = $this->createMock(Request
::class);
70 $response = new Response();
72 $tags = ['newTag' => 'abc'];
74 $result = $this->controller
->addTag($request, $response, $tags);
76 static::assertInstanceOf(Response
::class, $result);
77 static::assertSame(302, $result->getStatusCode());
78 static::assertSame(['./?searchtags=abc'], $result->getHeader('location'));
81 public function testAddTagRemoveLegacyQueryParam(): void
83 $this->createValidContainerMockSet();
84 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def&addtag=abc'];
86 $request = $this->createMock(Request
::class);
87 $response = new Response();
89 $tags = ['newTag' => 'abc'];
91 $result = $this->controller
->addTag($request, $response, $tags);
93 static::assertInstanceOf(Response
::class, $result);
94 static::assertSame(302, $result->getStatusCode());
95 static::assertSame(['/controller/?searchtags=def+abc'], $result->getHeader('location'));
98 public function testAddTagResetPagination(): void
100 $this->createValidContainerMockSet();
101 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def&page=12'];
103 $request = $this->createMock(Request
::class);
104 $response = new Response();
106 $tags = ['newTag' => 'abc'];
108 $result = $this->controller
->addTag($request, $response, $tags);
110 static::assertInstanceOf(Response
::class, $result);
111 static::assertSame(302, $result->getStatusCode());
112 static::assertSame(['/controller/?searchtags=def+abc'], $result->getHeader('location'));
115 public function testAddTagWithRefererAndEmptySearch(): void
117 $this->createValidContainerMockSet();
118 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags='];
120 $request = $this->createMock(Request
::class);
121 $response = new Response();
123 $tags = ['newTag' => 'abc'];
125 $result = $this->controller
->addTag($request, $response, $tags);
127 static::assertInstanceOf(Response
::class, $result);
128 static::assertSame(302, $result->getStatusCode());
129 static::assertSame(['/controller/?searchtags=abc'], $result->getHeader('location'));
132 public function testAddTagWithoutNewTagWithReferer(): void
134 $this->createValidContainerMockSet();
135 $this->container
->environment
= ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def'];
137 $request = $this->createMock(Request
::class);
138 $response = new Response();
140 $result = $this->controller
->addTag($request, $response, []);
142 static::assertInstanceOf(Response
::class, $result);
143 static::assertSame(302, $result->getStatusCode());
144 static::assertSame(['/controller/?searchtags=def'], $result->getHeader('location'));
147 public function testAddTagWithoutNewTagWithoutReferer(): void
149 $this->createValidContainerMockSet();
151 $request = $this->createMock(Request
::class);
152 $response = new Response();
154 $result = $this->controller
->addTag($request, $response, []);
156 static::assertInstanceOf(Response
::class, $result);
157 static::assertSame(302, $result->getStatusCode());
158 static::assertSame(['./'], $result->getHeader('location'));
161 protected function createValidContainerMockSet(): void
164 $loginManager = $this->createMock(LoginManager
::class);
165 $loginManager->method('isLoggedIn')->willReturn(false);
166 $loginManager->method('canLogin')->willReturn(true);
167 $this->container
->loginManager
= $loginManager;
170 $conf = $this->createMock(ConfigManager
::class);
171 $conf->method('get')->willReturnCallback(function (string $parameter, $default) {
174 $this->container
->conf
= $conf;
177 $pageBuilder = $this->createMock(PageBuilder
::class);
180 ->willReturnCallback(function (string $template): string {
184 $this->container
->pageBuilder
= $pageBuilder;
186 $pluginManager = $this->createMock(PluginManager
::class);
187 $this->container
->pluginManager
= $pluginManager;
188 $bookmarkService = $this->createMock(BookmarkServiceInterface
::class);
189 $this->container
->bookmarkService
= $bookmarkService;