diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-01-17 21:34:12 +0100 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2020-01-18 09:56:32 +0100 |
commit | e26e2060f5470ce8bf4c5973284bae07b8af170a (patch) | |
tree | adf8512f93f5559ba87d0c9931969ae4ebea7133 /tests/formatter/BookmarkMarkdownFormatterTest.php | |
parent | cf92b4dd1521241eefc58eaf6dcd202cd83969d8 (diff) | |
download | Shaarli-e26e2060f5470ce8bf4c5973284bae07b8af170a.tar.gz Shaarli-e26e2060f5470ce8bf4c5973284bae07b8af170a.tar.zst Shaarli-e26e2060f5470ce8bf4c5973284bae07b8af170a.zip |
Add and update unit test for the new system (Bookmark + Service)
See #1307
Diffstat (limited to 'tests/formatter/BookmarkMarkdownFormatterTest.php')
-rw-r--r-- | tests/formatter/BookmarkMarkdownFormatterTest.php | 160 |
1 files changed, 160 insertions, 0 deletions
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 | |||
3 | namespace Shaarli\Formatter; | ||
4 | |||
5 | use DateTime; | ||
6 | use PHPUnit\Framework\TestCase; | ||
7 | use Shaarli\Bookmark\Bookmark; | ||
8 | use Shaarli\Config\ConfigManager; | ||
9 | |||
10 | /** | ||
11 | * Class BookmarkMarkdownFormatterTest | ||
12 | * @package Shaarli\Formatter | ||
13 | */ | ||
14 | class 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&for=real#hash', $link['url']); | ||
57 | $this->assertEquals( | ||
58 | 'https://sub.domain.tld?query=here&for=real#hash', | ||
59 | $link['real_url'] | ||
60 | ); | ||
61 | $this->assertEquals('This is a <strong>bookmark</strong>', $link['title']); | ||
62 | $this->assertEquals( | ||
63 | '<div class="markdown"><p>'. | ||
64 | '<h2>Content</h2><p>`Here is some content</p>'. | ||
65 | '</p></div>', | ||
66 | $link['description'] | ||
67 | ); | ||
68 | $tags[3] = '<script>alert("xss");</script>'; | ||
69 | $this->assertEquals($tags, $link['taglist']); | ||
70 | $this->assertEquals(implode(' ', $tags), $link['tags']); | ||
71 | $this->assertEquals( | ||
72 | 'http://domain2.tdl2/?type=img&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 <strong>description</strong><br />'. PHP_EOL; | ||
126 | $url = 'https://sub.domain.tld?query=here&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 | } | ||