diff options
Diffstat (limited to 'tests/front')
-rw-r--r-- | tests/front/controller/TagControllerTest.php | 191 |
1 files changed, 191 insertions, 0 deletions
diff --git a/tests/front/controller/TagControllerTest.php b/tests/front/controller/TagControllerTest.php new file mode 100644 index 00000000..bbac5652 --- /dev/null +++ b/tests/front/controller/TagControllerTest.php | |||
@@ -0,0 +1,191 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Shaarli\Front\Controller; | ||
6 | |||
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; | ||
16 | |||
17 | class TagControllerTest extends TestCase | ||
18 | { | ||
19 | /** @var ShaarliContainer */ | ||
20 | protected $container; | ||
21 | |||
22 | /** @var TagController */ | ||
23 | protected $controller; | ||
24 | |||
25 | public function setUp(): void | ||
26 | { | ||
27 | $this->container = $this->createMock(ShaarliContainer::class); | ||
28 | $this->controller = new TagController($this->container); | ||
29 | } | ||
30 | |||
31 | public function testAddTagWithReferer(): void | ||
32 | { | ||
33 | $this->createValidContainerMockSet(); | ||
34 | $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/']; | ||
35 | |||
36 | $request = $this->createMock(Request::class); | ||
37 | $response = new Response(); | ||
38 | |||
39 | $tags = ['newTag' => 'abc']; | ||
40 | |||
41 | $result = $this->controller->addTag($request, $response, $tags); | ||
42 | |||
43 | static::assertInstanceOf(Response::class, $result); | ||
44 | static::assertSame(302, $result->getStatusCode()); | ||
45 | static::assertSame(['/controller/?searchtags=abc'], $result->getHeader('location')); | ||
46 | } | ||
47 | |||
48 | public function testAddTagWithRefererAndExistingSearch(): void | ||
49 | { | ||
50 | $this->createValidContainerMockSet(); | ||
51 | $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def']; | ||
52 | |||
53 | $request = $this->createMock(Request::class); | ||
54 | $response = new Response(); | ||
55 | |||
56 | $tags = ['newTag' => 'abc']; | ||
57 | |||
58 | $result = $this->controller->addTag($request, $response, $tags); | ||
59 | |||
60 | static::assertInstanceOf(Response::class, $result); | ||
61 | static::assertSame(302, $result->getStatusCode()); | ||
62 | static::assertSame(['/controller/?searchtags=def+abc'], $result->getHeader('location')); | ||
63 | } | ||
64 | |||
65 | public function testAddTagWithoutRefererAndExistingSearch(): void | ||
66 | { | ||
67 | $this->createValidContainerMockSet(); | ||
68 | |||
69 | $request = $this->createMock(Request::class); | ||
70 | $response = new Response(); | ||
71 | |||
72 | $tags = ['newTag' => 'abc']; | ||
73 | |||
74 | $result = $this->controller->addTag($request, $response, $tags); | ||
75 | |||
76 | static::assertInstanceOf(Response::class, $result); | ||
77 | static::assertSame(302, $result->getStatusCode()); | ||
78 | static::assertSame(['./?searchtags=abc'], $result->getHeader('location')); | ||
79 | } | ||
80 | |||
81 | public function testAddTagRemoveLegacyQueryParam(): void | ||
82 | { | ||
83 | $this->createValidContainerMockSet(); | ||
84 | $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def&addtag=abc']; | ||
85 | |||
86 | $request = $this->createMock(Request::class); | ||
87 | $response = new Response(); | ||
88 | |||
89 | $tags = ['newTag' => 'abc']; | ||
90 | |||
91 | $result = $this->controller->addTag($request, $response, $tags); | ||
92 | |||
93 | static::assertInstanceOf(Response::class, $result); | ||
94 | static::assertSame(302, $result->getStatusCode()); | ||
95 | static::assertSame(['/controller/?searchtags=def+abc'], $result->getHeader('location')); | ||
96 | } | ||
97 | |||
98 | public function testAddTagResetPagination(): void | ||
99 | { | ||
100 | $this->createValidContainerMockSet(); | ||
101 | $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def&page=12']; | ||
102 | |||
103 | $request = $this->createMock(Request::class); | ||
104 | $response = new Response(); | ||
105 | |||
106 | $tags = ['newTag' => 'abc']; | ||
107 | |||
108 | $result = $this->controller->addTag($request, $response, $tags); | ||
109 | |||
110 | static::assertInstanceOf(Response::class, $result); | ||
111 | static::assertSame(302, $result->getStatusCode()); | ||
112 | static::assertSame(['/controller/?searchtags=def+abc'], $result->getHeader('location')); | ||
113 | } | ||
114 | |||
115 | public function testAddTagWithRefererAndEmptySearch(): void | ||
116 | { | ||
117 | $this->createValidContainerMockSet(); | ||
118 | $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=']; | ||
119 | |||
120 | $request = $this->createMock(Request::class); | ||
121 | $response = new Response(); | ||
122 | |||
123 | $tags = ['newTag' => 'abc']; | ||
124 | |||
125 | $result = $this->controller->addTag($request, $response, $tags); | ||
126 | |||
127 | static::assertInstanceOf(Response::class, $result); | ||
128 | static::assertSame(302, $result->getStatusCode()); | ||
129 | static::assertSame(['/controller/?searchtags=abc'], $result->getHeader('location')); | ||
130 | } | ||
131 | |||
132 | public function testAddTagWithoutNewTagWithReferer(): void | ||
133 | { | ||
134 | $this->createValidContainerMockSet(); | ||
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 | protected function createValidContainerMockSet(): void | ||
162 | { | ||
163 | // User logged out | ||
164 | $loginManager = $this->createMock(LoginManager::class); | ||
165 | $loginManager->method('isLoggedIn')->willReturn(false); | ||
166 | $loginManager->method('canLogin')->willReturn(true); | ||
167 | $this->container->loginManager = $loginManager; | ||
168 | |||
169 | // Config | ||
170 | $conf = $this->createMock(ConfigManager::class); | ||
171 | $conf->method('get')->willReturnCallback(function (string $parameter, $default) { | ||
172 | return $default; | ||
173 | }); | ||
174 | $this->container->conf = $conf; | ||
175 | |||
176 | // PageBuilder | ||
177 | $pageBuilder = $this->createMock(PageBuilder::class); | ||
178 | $pageBuilder | ||
179 | ->method('render') | ||
180 | ->willReturnCallback(function (string $template): string { | ||
181 | return $template; | ||
182 | }) | ||
183 | ; | ||
184 | $this->container->pageBuilder = $pageBuilder; | ||
185 | |||
186 | $pluginManager = $this->createMock(PluginManager::class); | ||
187 | $this->container->pluginManager = $pluginManager; | ||
188 | $bookmarkService = $this->createMock(BookmarkServiceInterface::class); | ||
189 | $this->container->bookmarkService = $bookmarkService; | ||
190 | } | ||
191 | } | ||