From 03340c18ead651ef9e11f883745695f2edafbae3 Mon Sep 17 00:00:00 2001
From: ArthurHoaro
Date: Tue, 12 May 2020 12:44:48 +0200
Subject: Slim router: handle add tag route
---
tests/bookmark/LinkUtilsTest.php | 4 +-
tests/feed/FeedBuilderTest.php | 2 +-
tests/formatter/BookmarkDefaultFormatterTest.php | 4 +-
tests/formatter/BookmarkMarkdownFormatterTest.php | 4 +-
tests/front/controller/TagControllerTest.php | 191 ++++++++++++++++++++++
tests/plugins/resources/hashtags.md | 10 --
tests/plugins/resources/hashtags.raw | 10 --
tests/plugins/resources/markdown.html | 33 ----
tests/plugins/resources/markdown.md | 34 ----
9 files changed, 197 insertions(+), 95 deletions(-)
create mode 100644 tests/front/controller/TagControllerTest.php
delete mode 100644 tests/plugins/resources/hashtags.md
delete mode 100644 tests/plugins/resources/hashtags.raw
delete mode 100644 tests/plugins/resources/markdown.html
delete mode 100644 tests/plugins/resources/markdown.md
(limited to 'tests')
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 @@
namespace Shaarli\Bookmark;
use PHPUnit\Framework\TestCase;
-use ReferenceLinkDB;
-use Shaarli\Config\ConfigManager;
require_once 'tests/utils/CurlUtils.php';
@@ -491,7 +489,7 @@ class LinkUtilsTest extends TestCase
*/
private function getHashtagLink($hashtag, $index = '')
{
- $hashtagLink = '#$1';
+ $hashtagLink = '#$1';
return str_replace('$1', $hashtag, $hashtagLink);
}
}
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
$link = $data['links'][array_keys($data['links'])[2]];
$this->assertEquals('http://host.tld:8080/~user/shaarli/?WDWyig', $link['guid']);
$this->assertEquals('http://host.tld:8080/~user/shaarli/?WDWyig', $link['url']);
- $this->assertContains('http://host.tld:8080/~user/shaarli/?addtag=hashtag', $link['description']);
+ $this->assertContains('http://host.tld:8080/~user/shaarli/./add-tag/hashtag', $link['description']);
}
}
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
$description[0] = 'This a <strong>description</strong>
';
$url = 'https://sub.domain.tld?query=here&for=real#hash';
$description[1] = 'text '. $url .' more text
';
- $description[2] = 'Also, there is an #hashtag added
';
$description[3] = ' A N D KEEP '.
'SPACES !
';
@@ -148,7 +148,7 @@ class BookmarkDefaultFormatterTest extends TestCase
$this->assertEquals($root . $short, $link['url']);
$this->assertEquals($root . $short, $link['real_url']);
$this->assertEquals(
- 'Text '.
+ 'Text '.
'#hashtag more text',
$link['description']
);
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
$description .= 'This a <strong>description</strong>
'. PHP_EOL;
$url = 'https://sub.domain.tld?query=here&for=real#hash';
$description .= 'text '. $url .' more text
'. PHP_EOL;
- $description .= 'Also, there is an #hashtag added
'. PHP_EOL;
+ $description .= 'Also, there is an #hashtag added
'. PHP_EOL;
$description .= 'A N D KEEP SPACES ! ';
$description .= '
';
@@ -146,7 +146,7 @@ class BookmarkMarkdownFormatterTest extends TestCase
$this->formatter->addContextData('index_url', $root = 'https://domain.tld/hithere/');
$description = '';
- $description .= 'Text #hashtag more text';
+ $description .= 'Text #hashtag more text';
$description .= '
';
$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 @@
+container = $this->createMock(ShaarliContainer::class);
+ $this->controller = new TagController($this->container);
+ }
+
+ public function testAddTagWithReferer(): void
+ {
+ $this->createValidContainerMockSet();
+ $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/'];
+
+ $request = $this->createMock(Request::class);
+ $response = new Response();
+
+ $tags = ['newTag' => 'abc'];
+
+ $result = $this->controller->addTag($request, $response, $tags);
+
+ static::assertInstanceOf(Response::class, $result);
+ static::assertSame(302, $result->getStatusCode());
+ static::assertSame(['/controller/?searchtags=abc'], $result->getHeader('location'));
+ }
+
+ public function testAddTagWithRefererAndExistingSearch(): void
+ {
+ $this->createValidContainerMockSet();
+ $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def'];
+
+ $request = $this->createMock(Request::class);
+ $response = new Response();
+
+ $tags = ['newTag' => 'abc'];
+
+ $result = $this->controller->addTag($request, $response, $tags);
+
+ static::assertInstanceOf(Response::class, $result);
+ static::assertSame(302, $result->getStatusCode());
+ static::assertSame(['/controller/?searchtags=def+abc'], $result->getHeader('location'));
+ }
+
+ public function testAddTagWithoutRefererAndExistingSearch(): void
+ {
+ $this->createValidContainerMockSet();
+
+ $request = $this->createMock(Request::class);
+ $response = new Response();
+
+ $tags = ['newTag' => 'abc'];
+
+ $result = $this->controller->addTag($request, $response, $tags);
+
+ static::assertInstanceOf(Response::class, $result);
+ static::assertSame(302, $result->getStatusCode());
+ static::assertSame(['./?searchtags=abc'], $result->getHeader('location'));
+ }
+
+ public function testAddTagRemoveLegacyQueryParam(): void
+ {
+ $this->createValidContainerMockSet();
+ $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def&addtag=abc'];
+
+ $request = $this->createMock(Request::class);
+ $response = new Response();
+
+ $tags = ['newTag' => 'abc'];
+
+ $result = $this->controller->addTag($request, $response, $tags);
+
+ static::assertInstanceOf(Response::class, $result);
+ static::assertSame(302, $result->getStatusCode());
+ static::assertSame(['/controller/?searchtags=def+abc'], $result->getHeader('location'));
+ }
+
+ public function testAddTagResetPagination(): void
+ {
+ $this->createValidContainerMockSet();
+ $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def&page=12'];
+
+ $request = $this->createMock(Request::class);
+ $response = new Response();
+
+ $tags = ['newTag' => 'abc'];
+
+ $result = $this->controller->addTag($request, $response, $tags);
+
+ static::assertInstanceOf(Response::class, $result);
+ static::assertSame(302, $result->getStatusCode());
+ static::assertSame(['/controller/?searchtags=def+abc'], $result->getHeader('location'));
+ }
+
+ public function testAddTagWithRefererAndEmptySearch(): void
+ {
+ $this->createValidContainerMockSet();
+ $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags='];
+
+ $request = $this->createMock(Request::class);
+ $response = new Response();
+
+ $tags = ['newTag' => 'abc'];
+
+ $result = $this->controller->addTag($request, $response, $tags);
+
+ static::assertInstanceOf(Response::class, $result);
+ static::assertSame(302, $result->getStatusCode());
+ static::assertSame(['/controller/?searchtags=abc'], $result->getHeader('location'));
+ }
+
+ public function testAddTagWithoutNewTagWithReferer(): void
+ {
+ $this->createValidContainerMockSet();
+ $this->container->environment = ['HTTP_REFERER' => 'http://shaarli/controller/?searchtags=def'];
+
+ $request = $this->createMock(Request::class);
+ $response = new Response();
+
+ $result = $this->controller->addTag($request, $response, []);
+
+ static::assertInstanceOf(Response::class, $result);
+ static::assertSame(302, $result->getStatusCode());
+ static::assertSame(['/controller/?searchtags=def'], $result->getHeader('location'));
+ }
+
+ public function testAddTagWithoutNewTagWithoutReferer(): void
+ {
+ $this->createValidContainerMockSet();
+
+ $request = $this->createMock(Request::class);
+ $response = new Response();
+
+ $result = $this->controller->addTag($request, $response, []);
+
+ static::assertInstanceOf(Response::class, $result);
+ static::assertSame(302, $result->getStatusCode());
+ static::assertSame(['./'], $result->getHeader('location'));
+ }
+
+ protected function createValidContainerMockSet(): void
+ {
+ // User logged out
+ $loginManager = $this->createMock(LoginManager::class);
+ $loginManager->method('isLoggedIn')->willReturn(false);
+ $loginManager->method('canLogin')->willReturn(true);
+ $this->container->loginManager = $loginManager;
+
+ // Config
+ $conf = $this->createMock(ConfigManager::class);
+ $conf->method('get')->willReturnCallback(function (string $parameter, $default) {
+ return $default;
+ });
+ $this->container->conf = $conf;
+
+ // PageBuilder
+ $pageBuilder = $this->createMock(PageBuilder::class);
+ $pageBuilder
+ ->method('render')
+ ->willReturnCallback(function (string $template): string {
+ return $template;
+ })
+ ;
+ $this->container->pageBuilder = $pageBuilder;
+
+ $pluginManager = $this->createMock(PluginManager::class);
+ $this->container->pluginManager = $pluginManager;
+ $bookmarkService = $this->createMock(BookmarkServiceInterface::class);
+ $this->container->bookmarkService = $bookmarkService;
+ }
+}
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 @@
-[#lol](?addtag=lol)
-
- #test
-
-`#test2`
-
-```
-bla #bli blo
-#bla
-```
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 @@
-#lol
-
- #test
-
-`#test2`
-
-```
-bla #bli blo
-#bla
-```
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 @@
-
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 @@
-* test:
- * [zero](http://link.tld)
- + [two](http://link.tld)
- - [three](http://link.tld)
-
-1. [zero](http://link.tld)
- 2. [two](http://link.tld)
- 3. [three](http://link.tld)
- 4. [four](http://link.tld)
- 5. foo #foobar
-
-#foobar foo `lol #foo` #bar
-
-fsdfs http://link.tld #foobar `http://link.tld`
-
- http://link.tld #foobar
- next #foo
-
-Block:
-
-```
-lorem ipsum #foobar http://link.tld
-#foobar http://link.tld
-```
-
-[link](?123456)
-![link](/img/train.png)
-[link](test.tld/path/?query=value#hash)
-[link](http://test.tld/path/?query=value#hash)
-[link](https://test.tld/path/?query=value#hash)
-[link](ftp://test.tld/path/?query=value#hash)
-[link](magnet:test.tld/path/?query=value#hash)
-[link](javascript:alert('xss'))
-[link](other://test.tld/path/?query=value#hash)
--
cgit v1.2.3