diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-05-12 12:44:48 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2020-07-23 21:19:21 +0200 |
commit | 03340c18ead651ef9e11f883745695f2edafbae3 (patch) | |
tree | 619822bfb909f68f78f4a266a89e7249644b9d4c /tests | |
parent | 8e47af2b3620c920116ec056173277c039163ec1 (diff) | |
download | Shaarli-03340c18ead651ef9e11f883745695f2edafbae3.tar.gz Shaarli-03340c18ead651ef9e11f883745695f2edafbae3.tar.zst Shaarli-03340c18ead651ef9e11f883745695f2edafbae3.zip |
Slim router: handle add tag route
Diffstat (limited to 'tests')
-rw-r--r-- | tests/bookmark/LinkUtilsTest.php | 4 | ||||
-rw-r--r-- | tests/feed/FeedBuilderTest.php | 2 | ||||
-rw-r--r-- | tests/formatter/BookmarkDefaultFormatterTest.php | 4 | ||||
-rw-r--r-- | tests/formatter/BookmarkMarkdownFormatterTest.php | 4 | ||||
-rw-r--r-- | tests/front/controller/TagControllerTest.php | 191 | ||||
-rw-r--r-- | tests/plugins/resources/hashtags.md | 10 | ||||
-rw-r--r-- | tests/plugins/resources/hashtags.raw | 10 | ||||
-rw-r--r-- | tests/plugins/resources/markdown.html | 33 | ||||
-rw-r--r-- | tests/plugins/resources/markdown.md | 34 |
9 files changed, 197 insertions, 95 deletions
diff --git a/tests/bookmark/LinkUtilsTest.php b/tests/bookmark/LinkUtilsTest.php index 591976f2..7d4a7b89 100644 --- a/tests/bookmark/LinkUtilsTest.php +++ b/tests/bookmark/LinkUtilsTest.php | |||
@@ -3,8 +3,6 @@ | |||
3 | namespace Shaarli\Bookmark; | 3 | namespace Shaarli\Bookmark; |
4 | 4 | ||
5 | use PHPUnit\Framework\TestCase; | 5 | use PHPUnit\Framework\TestCase; |
6 | use ReferenceLinkDB; | ||
7 | use Shaarli\Config\ConfigManager; | ||
8 | 6 | ||
9 | require_once 'tests/utils/CurlUtils.php'; | 7 | require_once 'tests/utils/CurlUtils.php'; |
10 | 8 | ||
@@ -491,7 +489,7 @@ class LinkUtilsTest extends TestCase | |||
491 | */ | 489 | */ |
492 | private function getHashtagLink($hashtag, $index = '') | 490 | private function getHashtagLink($hashtag, $index = '') |
493 | { | 491 | { |
494 | $hashtagLink = '<a href="' . $index . '?addtag=$1" title="Hashtag $1">#$1</a>'; | 492 | $hashtagLink = '<a href="' . $index . './add-tag/$1" title="Hashtag $1">#$1</a>'; |
495 | return str_replace('$1', $hashtag, $hashtagLink); | 493 | return str_replace('$1', $hashtag, $hashtagLink); |
496 | } | 494 | } |
497 | } | 495 | } |
diff --git a/tests/feed/FeedBuilderTest.php b/tests/feed/FeedBuilderTest.php index 54671891..535207aa 100644 --- a/tests/feed/FeedBuilderTest.php +++ b/tests/feed/FeedBuilderTest.php | |||
@@ -306,6 +306,6 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase | |||
306 | $link = $data['links'][array_keys($data['links'])[2]]; | 306 | $link = $data['links'][array_keys($data['links'])[2]]; |
307 | $this->assertEquals('http://host.tld:8080/~user/shaarli/?WDWyig', $link['guid']); | 307 | $this->assertEquals('http://host.tld:8080/~user/shaarli/?WDWyig', $link['guid']); |
308 | $this->assertEquals('http://host.tld:8080/~user/shaarli/?WDWyig', $link['url']); | 308 | $this->assertEquals('http://host.tld:8080/~user/shaarli/?WDWyig', $link['url']); |
309 | $this->assertContains('http://host.tld:8080/~user/shaarli/?addtag=hashtag', $link['description']); | 309 | $this->assertContains('http://host.tld:8080/~user/shaarli/./add-tag/hashtag', $link['description']); |
310 | } | 310 | } |
311 | } | 311 | } |
diff --git a/tests/formatter/BookmarkDefaultFormatterTest.php b/tests/formatter/BookmarkDefaultFormatterTest.php index 382a560e..cf48b00b 100644 --- a/tests/formatter/BookmarkDefaultFormatterTest.php +++ b/tests/formatter/BookmarkDefaultFormatterTest.php | |||
@@ -123,7 +123,7 @@ class BookmarkDefaultFormatterTest extends TestCase | |||
123 | $description[0] = 'This a <strong>description</strong><br />'; | 123 | $description[0] = 'This a <strong>description</strong><br />'; |
124 | $url = 'https://sub.domain.tld?query=here&for=real#hash'; | 124 | $url = 'https://sub.domain.tld?query=here&for=real#hash'; |
125 | $description[1] = 'text <a href="'. $url .'">'. $url .'</a> more text<br />'; | 125 | $description[1] = 'text <a href="'. $url .'">'. $url .'</a> more text<br />'; |
126 | $description[2] = 'Also, there is an <a href="?addtag=hashtag" '. | 126 | $description[2] = 'Also, there is an <a href="./add-tag/hashtag" '. |
127 | 'title="Hashtag hashtag">#hashtag</a> added<br />'; | 127 | 'title="Hashtag hashtag">#hashtag</a> added<br />'; |
128 | $description[3] = ' A N D KEEP '. | 128 | $description[3] = ' A N D KEEP '. |
129 | 'SPACES ! <br />'; | 129 | 'SPACES ! <br />'; |
@@ -148,7 +148,7 @@ class BookmarkDefaultFormatterTest extends TestCase | |||
148 | $this->assertEquals($root . $short, $link['url']); | 148 | $this->assertEquals($root . $short, $link['url']); |
149 | $this->assertEquals($root . $short, $link['real_url']); | 149 | $this->assertEquals($root . $short, $link['real_url']); |
150 | $this->assertEquals( | 150 | $this->assertEquals( |
151 | 'Text <a href="'. $root .'?addtag=hashtag" title="Hashtag hashtag">'. | 151 | 'Text <a href="'. $root .'./add-tag/hashtag" title="Hashtag hashtag">'. |
152 | '#hashtag</a> more text', | 152 | '#hashtag</a> more text', |
153 | $link['description'] | 153 | $link['description'] |
154 | ); | 154 | ); |
diff --git a/tests/formatter/BookmarkMarkdownFormatterTest.php b/tests/formatter/BookmarkMarkdownFormatterTest.php index f1f12c04..3e72d1ee 100644 --- a/tests/formatter/BookmarkMarkdownFormatterTest.php +++ b/tests/formatter/BookmarkMarkdownFormatterTest.php | |||
@@ -125,7 +125,7 @@ class BookmarkMarkdownFormatterTest extends TestCase | |||
125 | $description .= 'This a <strong>description</strong><br />'. PHP_EOL; | 125 | $description .= 'This a <strong>description</strong><br />'. PHP_EOL; |
126 | $url = 'https://sub.domain.tld?query=here&for=real#hash'; | 126 | $url = 'https://sub.domain.tld?query=here&for=real#hash'; |
127 | $description .= 'text <a href="'. $url .'">'. $url .'</a> more text<br />'. PHP_EOL; | 127 | $description .= 'text <a href="'. $url .'">'. $url .'</a> more text<br />'. PHP_EOL; |
128 | $description .= 'Also, there is an <a href="?addtag=hashtag">#hashtag</a> added<br />'. PHP_EOL; | 128 | $description .= 'Also, there is an <a href="./add-tag/hashtag">#hashtag</a> added<br />'. PHP_EOL; |
129 | $description .= 'A N D KEEP SPACES ! '; | 129 | $description .= 'A N D KEEP SPACES ! '; |
130 | $description .= '</p></div>'; | 130 | $description .= '</p></div>'; |
131 | 131 | ||
@@ -146,7 +146,7 @@ class BookmarkMarkdownFormatterTest extends TestCase | |||
146 | $this->formatter->addContextData('index_url', $root = 'https://domain.tld/hithere/'); | 146 | $this->formatter->addContextData('index_url', $root = 'https://domain.tld/hithere/'); |
147 | 147 | ||
148 | $description = '<div class="markdown"><p>'; | 148 | $description = '<div class="markdown"><p>'; |
149 | $description .= 'Text <a href="'. $root .'?addtag=hashtag">#hashtag</a> more text'; | 149 | $description .= 'Text <a href="'. $root .'./add-tag/hashtag">#hashtag</a> more text'; |
150 | $description .= '</p></div>'; | 150 | $description .= '</p></div>'; |
151 | 151 | ||
152 | $link = $this->formatter->format($bookmark); | 152 | $link = $this->formatter->format($bookmark); |
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 | } | ||
diff --git a/tests/plugins/resources/hashtags.md b/tests/plugins/resources/hashtags.md deleted file mode 100644 index 46326de3..00000000 --- a/tests/plugins/resources/hashtags.md +++ /dev/null | |||
@@ -1,10 +0,0 @@ | |||
1 | [#lol](?addtag=lol) | ||
2 | |||
3 | #test | ||
4 | |||
5 | `#test2` | ||
6 | |||
7 | ``` | ||
8 | bla #bli blo | ||
9 | #bla | ||
10 | ``` | ||
diff --git a/tests/plugins/resources/hashtags.raw b/tests/plugins/resources/hashtags.raw deleted file mode 100644 index 9d2dc98a..00000000 --- a/tests/plugins/resources/hashtags.raw +++ /dev/null | |||
@@ -1,10 +0,0 @@ | |||
1 | #lol | ||
2 | |||
3 | #test | ||
4 | |||
5 | `#test2` | ||
6 | |||
7 | ``` | ||
8 | bla #bli blo | ||
9 | #bla | ||
10 | ``` | ||
diff --git a/tests/plugins/resources/markdown.html b/tests/plugins/resources/markdown.html deleted file mode 100644 index c3460bf7..00000000 --- a/tests/plugins/resources/markdown.html +++ /dev/null | |||
@@ -1,33 +0,0 @@ | |||
1 | <div class="markdown"><ul> | ||
2 | <li>test: | ||
3 | <ul> | ||
4 | <li><a href="http://link.tld">zero</a></li> | ||
5 | <li><a href="http://link.tld">two</a></li> | ||
6 | <li><a href="http://link.tld">three</a></li> | ||
7 | </ul></li> | ||
8 | </ul> | ||
9 | <ol> | ||
10 | <li><a href="http://link.tld">zero</a> | ||
11 | <ol start="2"> | ||
12 | <li><a href="http://link.tld">two</a></li> | ||
13 | <li><a href="http://link.tld">three</a></li> | ||
14 | <li><a href="http://link.tld">four</a></li> | ||
15 | <li>foo <a href="?addtag=foobar">#foobar</a></li> | ||
16 | </ol></li> | ||
17 | </ol> | ||
18 | <p><a href="?addtag=foobar">#foobar</a> foo <code>lol #foo</code> <a href="?addtag=bar">#bar</a></p> | ||
19 | <p>fsdfs <a href="http://link.tld">http://link.tld</a> <a href="?addtag=foobar">#foobar</a> <code>http://link.tld</code></p> | ||
20 | <pre><code>http://link.tld #foobar | ||
21 | next #foo</code></pre> | ||
22 | <p>Block:</p> | ||
23 | <pre><code>lorem ipsum #foobar http://link.tld | ||
24 | #foobar http://link.tld</code></pre> | ||
25 | <p><a href="?123456">link</a><br /> | ||
26 | <img src="/img/train.png" alt="link" /><br /> | ||
27 | <a href="http://test.tld/path/?query=value#hash">link</a><br /> | ||
28 | <a href="http://test.tld/path/?query=value#hash">link</a><br /> | ||
29 | <a href="https://test.tld/path/?query=value#hash">link</a><br /> | ||
30 | <a href="ftp://test.tld/path/?query=value#hash">link</a><br /> | ||
31 | <a href="magnet:test.tld/path/?query=value#hash">link</a><br /> | ||
32 | <a href="http://alert('xss')">link</a><br /> | ||
33 | <a href="http://test.tld/path/?query=value#hash">link</a></p></div> | ||
diff --git a/tests/plugins/resources/markdown.md b/tests/plugins/resources/markdown.md deleted file mode 100644 index 9350a8c7..00000000 --- a/tests/plugins/resources/markdown.md +++ /dev/null | |||
@@ -1,34 +0,0 @@ | |||
1 | * test: | ||
2 | * [zero](http://link.tld) | ||
3 | + [two](http://link.tld) | ||
4 | - [three](http://link.tld) | ||
5 | |||
6 | 1. [zero](http://link.tld) | ||
7 | 2. [two](http://link.tld) | ||
8 | 3. [three](http://link.tld) | ||
9 | 4. [four](http://link.tld) | ||
10 | 5. foo #foobar | ||
11 | |||
12 | #foobar foo `lol #foo` #bar | ||
13 | |||
14 | fsdfs http://link.tld #foobar `http://link.tld` | ||
15 | |||
16 | http://link.tld #foobar | ||
17 | next #foo | ||
18 | |||
19 | Block: | ||
20 | |||
21 | ``` | ||
22 | lorem ipsum #foobar http://link.tld | ||
23 | #foobar http://link.tld | ||
24 | ``` | ||
25 | |||
26 | [link](?123456) | ||
27 | ![link](/img/train.png) | ||
28 | [link](test.tld/path/?query=value#hash) | ||
29 | [link](http://test.tld/path/?query=value#hash) | ||
30 | [link](https://test.tld/path/?query=value#hash) | ||
31 | [link](ftp://test.tld/path/?query=value#hash) | ||
32 | [link](magnet:test.tld/path/?query=value#hash) | ||
33 | [link](javascript:alert('xss')) | ||
34 | [link](other://test.tld/path/?query=value#hash) | ||