aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/formatter
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-01-18 10:01:06 +0100
committerGitHub <noreply@github.com>2020-01-18 10:01:06 +0100
commit3fb29fdda04ca86e04422d49b86cf646d53c4f9d (patch)
treeadf8512f93f5559ba87d0c9931969ae4ebea7133 /tests/formatter
parent796c4c57d085ae4589b53dfe8369ae9ba30ffdaf (diff)
parente26e2060f5470ce8bf4c5973284bae07b8af170a (diff)
downloadShaarli-3fb29fdda04ca86e04422d49b86cf646d53c4f9d.tar.gz
Shaarli-3fb29fdda04ca86e04422d49b86cf646d53c4f9d.tar.zst
Shaarli-3fb29fdda04ca86e04422d49b86cf646d53c4f9d.zip
Store bookmarks as PHP objects and add a service layer to retriā€¦ (#1307)
Store bookmarks as PHP objects and add a service layer to retrieve them
Diffstat (limited to 'tests/formatter')
-rw-r--r--tests/formatter/BookmarkDefaultFormatterTest.php156
-rw-r--r--tests/formatter/BookmarkMarkdownFormatterTest.php160
-rw-r--r--tests/formatter/BookmarkRawFormatterTest.php97
-rw-r--r--tests/formatter/FormatterFactoryTest.php101
4 files changed, 514 insertions, 0 deletions
diff --git a/tests/formatter/BookmarkDefaultFormatterTest.php b/tests/formatter/BookmarkDefaultFormatterTest.php
new file mode 100644
index 00000000..fe42a208
--- /dev/null
+++ b/tests/formatter/BookmarkDefaultFormatterTest.php
@@ -0,0 +1,156 @@
1<?php
2
3namespace Shaarli\Formatter;
4
5use DateTime;
6use PHPUnit\Framework\TestCase;
7use Shaarli\Bookmark\Bookmark;
8use Shaarli\Config\ConfigManager;
9
10/**
11 * Class BookmarkDefaultFormatterTest
12 * @package Shaarli\Formatter
13 */
14class BookmarkDefaultFormatterTest extends TestCase
15{
16 /** @var string Path of test config file */
17 protected static $testConf = 'sandbox/config';
18
19 /** @var BookmarkFormatter */
20 protected $formatter;
21
22 /** @var ConfigManager instance */
23 protected $conf;
24
25 /**
26 * Initialize formatter instance.
27 */
28 public function setUp()
29 {
30 copy('tests/utils/config/configJson.json.php', self::$testConf .'.json.php');
31 $this->conf = new ConfigManager(self::$testConf);
32 $this->formatter = new BookmarkDefaultFormatter($this->conf);
33 }
34
35 /**
36 * Test formatting a bookmark with all its attribute filled.
37 */
38 public function testFormatFull()
39 {
40 $bookmark = new Bookmark();
41 $bookmark->setId($id = 11);
42 $bookmark->setShortUrl($short = 'abcdef');
43 $bookmark->setUrl('https://sub.domain.tld?query=here&for=real#hash');
44 $bookmark->setTitle($title = 'This is a <strong>bookmark</strong>');
45 $bookmark->setDescription($desc = '<h2>Content</h2><p>`Here is some content</p>');
46 $bookmark->setTags($tags = ['tag1', 'bookmark', 'other', '<script>alert("xss");</script>']);
47 $bookmark->setThumbnail('http://domain2.tdl2/?type=img&name=file.png');
48 $bookmark->setSticky(true);
49 $bookmark->setCreated($created = DateTime::createFromFormat('Ymd_His', '20190521_190412'));
50 $bookmark->setUpdated($updated = DateTime::createFromFormat('Ymd_His', '20190521_191213'));
51 $bookmark->setPrivate(true);
52
53 $link = $this->formatter->format($bookmark);
54 $this->assertEquals($id, $link['id']);
55 $this->assertEquals($short, $link['shorturl']);
56 $this->assertEquals('https://sub.domain.tld?query=here&amp;for=real#hash', $link['url']);
57 $this->assertEquals(
58 'https://sub.domain.tld?query=here&amp;for=real#hash',
59 $link['real_url']
60 );
61 $this->assertEquals('This is a &lt;strong&gt;bookmark&lt;/strong&gt;', $link['title']);
62 $this->assertEquals(
63 '&lt;h2&gt;Content&lt;/h2&gt;&lt;p&gt;`Here is some content&lt;/p&gt;',
64 $link['description']
65 );
66 $tags[3] = '&lt;script&gt;alert(&quot;xss&quot;);&lt;/script&gt;';
67 $this->assertEquals($tags, $link['taglist']);
68 $this->assertEquals(implode(' ', $tags), $link['tags']);
69 $this->assertEquals(
70 'http://domain2.tdl2/?type=img&amp;name=file.png',
71 $link['thumbnail']
72 );
73 $this->assertEquals($created, $link['created']);
74 $this->assertEquals($created->getTimestamp(), $link['timestamp']);
75 $this->assertEquals($updated, $link['updated']);
76 $this->assertEquals($updated->getTimestamp(), $link['updated_timestamp']);
77 $this->assertTrue($link['private']);
78 $this->assertTrue($link['sticky']);
79 $this->assertEquals('private', $link['class']);
80 }
81
82 /**
83 * Test formatting a bookmark with all its attribute filled.
84 */
85 public function testFormatMinimal()
86 {
87 $bookmark = new Bookmark();
88
89 $link = $this->formatter->format($bookmark);
90 $this->assertEmpty($link['id']);
91 $this->assertEmpty($link['shorturl']);
92 $this->assertEmpty($link['url']);
93 $this->assertEmpty($link['real_url']);
94 $this->assertEmpty($link['title']);
95 $this->assertEmpty($link['description']);
96 $this->assertEmpty($link['taglist']);
97 $this->assertEmpty($link['tags']);
98 $this->assertEmpty($link['thumbnail']);
99 $this->assertEmpty($link['created']);
100 $this->assertEmpty($link['timestamp']);
101 $this->assertEmpty($link['updated']);
102 $this->assertEmpty($link['updated_timestamp']);
103 $this->assertFalse($link['private']);
104 $this->assertFalse($link['sticky']);
105 $this->assertEmpty($link['class']);
106 }
107
108 /**
109 * Make sure that the description is properly formatted by the default formatter.
110 */
111 public function testFormatDescription()
112 {
113 $description = [];
114 $description[] = 'This a <strong>description</strong>' . PHP_EOL;
115 $description[] = 'text https://sub.domain.tld?query=here&for=real#hash more text'. PHP_EOL;
116 $description[] = 'Also, there is an #hashtag added'. PHP_EOL;
117 $description[] = ' A N D KEEP SPACES ! '. PHP_EOL;
118
119 $bookmark = new Bookmark();
120 $bookmark->setDescription(implode('', $description));
121 $link = $this->formatter->format($bookmark);
122
123 $description[0] = 'This a &lt;strong&gt;description&lt;/strong&gt;<br />';
124 $url = 'https://sub.domain.tld?query=here&amp;for=real#hash';
125 $description[1] = 'text <a href="'. $url .'">'. $url .'</a> more text<br />';
126 $description[2] = 'Also, there is an <a href="?addtag=hashtag" '.
127 'title="Hashtag hashtag">#hashtag</a> added<br />';
128 $description[3] = '&nbsp; &nbsp; A &nbsp;N &nbsp;D KEEP &nbsp; &nbsp; '.
129 'SPACES &nbsp; &nbsp;! &nbsp; <br />';
130
131 $this->assertEquals(implode(PHP_EOL, $description) . PHP_EOL, $link['description']);
132 }
133
134 /**
135 * Test formatting URL with an index_url set
136 * It should prepend relative links.
137 */
138 public function testFormatNoteWithIndexUrl()
139 {
140 $bookmark = new Bookmark();
141 $bookmark->setUrl($short = '?abcdef');
142 $description = 'Text #hashtag more text';
143 $bookmark->setDescription($description);
144
145 $this->formatter->addContextData('index_url', $root = 'https://domain.tld/hithere/');
146
147 $link = $this->formatter->format($bookmark);
148 $this->assertEquals($root . $short, $link['url']);
149 $this->assertEquals($root . $short, $link['real_url']);
150 $this->assertEquals(
151 'Text <a href="'. $root .'?addtag=hashtag" title="Hashtag hashtag">'.
152 '#hashtag</a> more text',
153 $link['description']
154 );
155 }
156}
diff --git a/tests/formatter/BookmarkMarkdownFormatterTest.php b/tests/formatter/BookmarkMarkdownFormatterTest.php
new file mode 100644
index 00000000..0ca7f802
--- /dev/null
+++ b/tests/formatter/BookmarkMarkdownFormatterTest.php
@@ -0,0 +1,160 @@
1<?php
2
3namespace Shaarli\Formatter;
4
5use DateTime;
6use PHPUnit\Framework\TestCase;
7use Shaarli\Bookmark\Bookmark;
8use Shaarli\Config\ConfigManager;
9
10/**
11 * Class BookmarkMarkdownFormatterTest
12 * @package Shaarli\Formatter
13 */
14class BookmarkMarkdownFormatterTest extends TestCase
15{
16 /** @var string Path of test config file */
17 protected static $testConf = 'sandbox/config';
18
19 /** @var BookmarkFormatter */
20 protected $formatter;
21
22 /** @var ConfigManager instance */
23 protected $conf;
24
25 /**
26 * Initialize formatter instance.
27 */
28 public function setUp()
29 {
30 copy('tests/utils/config/configJson.json.php', self::$testConf .'.json.php');
31 $this->conf = new ConfigManager(self::$testConf);
32 $this->formatter = new BookmarkMarkdownFormatter($this->conf);
33 }
34
35 /**
36 * Test formatting a bookmark with all its attribute filled.
37 */
38 public function testFormatFull()
39 {
40 $bookmark = new Bookmark();
41 $bookmark->setId($id = 11);
42 $bookmark->setShortUrl($short = 'abcdef');
43 $bookmark->setUrl('https://sub.domain.tld?query=here&for=real#hash');
44 $bookmark->setTitle($title = 'This is a <strong>bookmark</strong>');
45 $bookmark->setDescription('<h2>Content</h2><p>`Here is some content</p>');
46 $bookmark->setTags($tags = ['tag1', 'bookmark', 'other', '<script>alert("xss");</script>']);
47 $bookmark->setThumbnail('http://domain2.tdl2/?type=img&name=file.png');
48 $bookmark->setSticky(true);
49 $bookmark->setCreated($created = DateTime::createFromFormat('Ymd_His', '20190521_190412'));
50 $bookmark->setUpdated($updated = DateTime::createFromFormat('Ymd_His', '20190521_191213'));
51 $bookmark->setPrivate(true);
52
53 $link = $this->formatter->format($bookmark);
54 $this->assertEquals($id, $link['id']);
55 $this->assertEquals($short, $link['shorturl']);
56 $this->assertEquals('https://sub.domain.tld?query=here&amp;for=real#hash', $link['url']);
57 $this->assertEquals(
58 'https://sub.domain.tld?query=here&amp;for=real#hash',
59 $link['real_url']
60 );
61 $this->assertEquals('This is a &lt;strong&gt;bookmark&lt;/strong&gt;', $link['title']);
62 $this->assertEquals(
63 '<div class="markdown"><p>'.
64 '&lt;h2&gt;Content&lt;/h2&gt;&lt;p&gt;`Here is some content&lt;/p&gt;'.
65 '</p></div>',
66 $link['description']
67 );
68 $tags[3] = '&lt;script&gt;alert(&quot;xss&quot;);&lt;/script&gt;';
69 $this->assertEquals($tags, $link['taglist']);
70 $this->assertEquals(implode(' ', $tags), $link['tags']);
71 $this->assertEquals(
72 'http://domain2.tdl2/?type=img&amp;name=file.png',
73 $link['thumbnail']
74 );
75 $this->assertEquals($created, $link['created']);
76 $this->assertEquals($created->getTimestamp(), $link['timestamp']);
77 $this->assertEquals($updated, $link['updated']);
78 $this->assertEquals($updated->getTimestamp(), $link['updated_timestamp']);
79 $this->assertTrue($link['private']);
80 $this->assertTrue($link['sticky']);
81 $this->assertEquals('private', $link['class']);
82 }
83
84 /**
85 * Test formatting a bookmark with all its attribute filled.
86 */
87 public function testFormatMinimal()
88 {
89 $bookmark = new Bookmark();
90
91 $link = $this->formatter->format($bookmark);
92 $this->assertEmpty($link['id']);
93 $this->assertEmpty($link['shorturl']);
94 $this->assertEmpty($link['url']);
95 $this->assertEmpty($link['real_url']);
96 $this->assertEmpty($link['title']);
97 $this->assertEmpty($link['description']);
98 $this->assertEmpty($link['taglist']);
99 $this->assertEmpty($link['tags']);
100 $this->assertEmpty($link['thumbnail']);
101 $this->assertEmpty($link['created']);
102 $this->assertEmpty($link['timestamp']);
103 $this->assertEmpty($link['updated']);
104 $this->assertEmpty($link['updated_timestamp']);
105 $this->assertFalse($link['private']);
106 $this->assertFalse($link['sticky']);
107 $this->assertEmpty($link['class']);
108 }
109
110 /**
111 * Make sure that the description is properly formatted by the default formatter.
112 */
113 public function testFormatDescription()
114 {
115 $description = 'This a <strong>description</strong>'. PHP_EOL;
116 $description .= 'text https://sub.domain.tld?query=here&for=real#hash more text'. PHP_EOL;
117 $description .= 'Also, there is an #hashtag added'. PHP_EOL;
118 $description .= ' A N D KEEP SPACES ! '. PHP_EOL;
119
120 $bookmark = new Bookmark();
121 $bookmark->setDescription($description);
122 $link = $this->formatter->format($bookmark);
123
124 $description = '<div class="markdown"><p>';
125 $description .= 'This a &lt;strong&gt;description&lt;/strong&gt;<br />'. PHP_EOL;
126 $url = 'https://sub.domain.tld?query=here&amp;for=real#hash';
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;
129 $description .= 'A N D KEEP SPACES ! ';
130 $description .= '</p></div>';
131
132 $this->assertEquals($description, $link['description']);
133 }
134
135 /**
136 * Test formatting URL with an index_url set
137 * It should prepend relative links.
138 */
139 public function testFormatNoteWithIndexUrl()
140 {
141 $bookmark = new Bookmark();
142 $bookmark->setUrl($short = '?abcdef');
143 $description = 'Text #hashtag more text';
144 $bookmark->setDescription($description);
145
146 $this->formatter->addContextData('index_url', $root = 'https://domain.tld/hithere/');
147
148 $description = '<div class="markdown"><p>';
149 $description .= 'Text <a href="'. $root .'?addtag=hashtag">#hashtag</a> more text';
150 $description .= '</p></div>';
151
152 $link = $this->formatter->format($bookmark);
153 $this->assertEquals($root . $short, $link['url']);
154 $this->assertEquals($root . $short, $link['real_url']);
155 $this->assertEquals(
156 $description,
157 $link['description']
158 );
159 }
160}
diff --git a/tests/formatter/BookmarkRawFormatterTest.php b/tests/formatter/BookmarkRawFormatterTest.php
new file mode 100644
index 00000000..ceb6fb73
--- /dev/null
+++ b/tests/formatter/BookmarkRawFormatterTest.php
@@ -0,0 +1,97 @@
1<?php
2
3namespace Shaarli\Formatter;
4
5use DateTime;
6use PHPUnit\Framework\TestCase;
7use Shaarli\Bookmark\Bookmark;
8use Shaarli\Config\ConfigManager;
9
10/**
11 * Class BookmarkRawFormatterTest
12 * @package Shaarli\Formatter
13 */
14class BookmarkRawFormatterTest extends TestCase
15{
16 /** @var string Path of test config file */
17 protected static $testConf = 'sandbox/config';
18
19 /** @var BookmarkFormatter */
20 protected $formatter;
21
22 /** @var ConfigManager instance */
23 protected $conf;
24
25 /**
26 * Initialize formatter instance.
27 */
28 public function setUp()
29 {
30 copy('tests/utils/config/configJson.json.php', self::$testConf .'.json.php');
31 $this->conf = new ConfigManager(self::$testConf);
32 $this->formatter = new BookmarkRawFormatter($this->conf);
33 }
34
35 /**
36 * Test formatting a bookmark with all its attribute filled.
37 */
38 public function testFormatFull()
39 {
40 $bookmark = new Bookmark();
41 $bookmark->setId($id = 11);
42 $bookmark->setShortUrl($short = 'abcdef');
43 $bookmark->setUrl($url = 'https://sub.domain.tld?query=here&for=real#hash');
44 $bookmark->setTitle($title = 'This is a <strong>bookmark</strong>');
45 $bookmark->setDescription($desc = '<h2>Content</h2><p>`Here is some content</p>');
46 $bookmark->setTags($tags = ['tag1', 'bookmark', 'other', '<script>alert("xss");</script>']);
47 $bookmark->setThumbnail($thumb = 'http://domain2.tdl2/file.png');
48 $bookmark->setSticky(true);
49 $bookmark->setCreated($created = DateTime::createFromFormat('Ymd_His', '20190521_190412'));
50 $bookmark->setUpdated($updated = DateTime::createFromFormat('Ymd_His', '20190521_191213'));
51 $bookmark->setPrivate(true);
52
53 $link = $this->formatter->format($bookmark);
54 $this->assertEquals($id, $link['id']);
55 $this->assertEquals($short, $link['shorturl']);
56 $this->assertEquals($url, $link['url']);
57 $this->assertEquals($url, $link['real_url']);
58 $this->assertEquals($title, $link['title']);
59 $this->assertEquals($desc, $link['description']);
60 $this->assertEquals($tags, $link['taglist']);
61 $this->assertEquals(implode(' ', $tags), $link['tags']);
62 $this->assertEquals($thumb, $link['thumbnail']);
63 $this->assertEquals($created, $link['created']);
64 $this->assertEquals($created->getTimestamp(), $link['timestamp']);
65 $this->assertEquals($updated, $link['updated']);
66 $this->assertEquals($updated->getTimestamp(), $link['updated_timestamp']);
67 $this->assertTrue($link['private']);
68 $this->assertTrue($link['sticky']);
69 $this->assertEquals('private', $link['class']);
70 }
71
72 /**
73 * Test formatting a bookmark with all its attribute filled.
74 */
75 public function testFormatMinimal()
76 {
77 $bookmark = new Bookmark();
78
79 $link = $this->formatter->format($bookmark);
80 $this->assertEmpty($link['id']);
81 $this->assertEmpty($link['shorturl']);
82 $this->assertEmpty($link['url']);
83 $this->assertEmpty($link['real_url']);
84 $this->assertEmpty($link['title']);
85 $this->assertEmpty($link['description']);
86 $this->assertEmpty($link['taglist']);
87 $this->assertEmpty($link['tags']);
88 $this->assertEmpty($link['thumbnail']);
89 $this->assertEmpty($link['created']);
90 $this->assertEmpty($link['timestamp']);
91 $this->assertEmpty($link['updated']);
92 $this->assertEmpty($link['updated_timestamp']);
93 $this->assertFalse($link['private']);
94 $this->assertFalse($link['sticky']);
95 $this->assertEmpty($link['class']);
96 }
97}
diff --git a/tests/formatter/FormatterFactoryTest.php b/tests/formatter/FormatterFactoryTest.php
new file mode 100644
index 00000000..317c0b2d
--- /dev/null
+++ b/tests/formatter/FormatterFactoryTest.php
@@ -0,0 +1,101 @@
1<?php
2
3namespace Shaarli\Formatter;
4
5use PHPUnit\Framework\TestCase;
6use Shaarli\Config\ConfigManager;
7
8/**
9 * Class FormatterFactoryTest
10 *
11 * @package Shaarli\Formatter
12 */
13class FormatterFactoryTest extends TestCase
14{
15 /** @var string Path of test config file */
16 protected static $testConf = 'sandbox/config';
17
18 /** @var FormatterFactory instance */
19 protected $factory;
20
21 /** @var ConfigManager instance */
22 protected $conf;
23
24 /**
25 * Initialize FormatterFactory instance
26 */
27 public function setUp()
28 {
29 copy('tests/utils/config/configJson.json.php', self::$testConf .'.json.php');
30 $this->conf = new ConfigManager(self::$testConf);
31 $this->factory = new FormatterFactory($this->conf);
32 }
33
34 /**
35 * Test creating an instance of BookmarkFormatter without any setting -> default formatter
36 */
37 public function testCreateInstanceDefault()
38 {
39 $this->assertInstanceOf(BookmarkDefaultFormatter::class, $this->factory->getFormatter());
40 }
41
42 /**
43 * Test creating an instance of BookmarkDefaultFormatter from settings
44 */
45 public function testCreateInstanceDefaultSetting()
46 {
47 $this->conf->set('formatter', 'default');
48 $this->assertInstanceOf(BookmarkDefaultFormatter::class, $this->factory->getFormatter());
49 }
50
51 /**
52 * Test creating an instance of BookmarkDefaultFormatter from parameter
53 */
54 public function testCreateInstanceDefaultParameter()
55 {
56 $this->assertInstanceOf(
57 BookmarkDefaultFormatter::class,
58 $this->factory->getFormatter('default')
59 );
60 }
61
62 /**
63 * Test creating an instance of BookmarkRawFormatter from settings
64 */
65 public function testCreateInstanceRawSetting()
66 {
67 $this->conf->set('formatter', 'raw');
68 $this->assertInstanceOf(BookmarkRawFormatter::class, $this->factory->getFormatter());
69 }
70
71 /**
72 * Test creating an instance of BookmarkRawFormatter from parameter
73 */
74 public function testCreateInstanceRawParameter()
75 {
76 $this->assertInstanceOf(
77 BookmarkRawFormatter::class,
78 $this->factory->getFormatter('raw')
79 );
80 }
81
82 /**
83 * Test creating an instance of BookmarkMarkdownFormatter from settings
84 */
85 public function testCreateInstanceMarkdownSetting()
86 {
87 $this->conf->set('formatter', 'markdown');
88 $this->assertInstanceOf(BookmarkMarkdownFormatter::class, $this->factory->getFormatter());
89 }
90
91 /**
92 * Test creating an instance of BookmarkMarkdownFormatter from parameter
93 */
94 public function testCreateInstanceMarkdownParameter()
95 {
96 $this->assertInstanceOf(
97 BookmarkMarkdownFormatter::class,
98 $this->factory->getFormatter('markdown')
99 );
100 }
101}