]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/formatter/BookmarkMarkdownFormatterTest.php
Merge pull request #1696 from ArthurHoaro/fix/search-highlight-url
[github/shaarli/Shaarli.git] / tests / formatter / BookmarkMarkdownFormatterTest.php
CommitLineData
e26e2060
A
1<?php
2
3namespace Shaarli\Formatter;
4
5use DateTime;
e26e2060
A
6use Shaarli\Bookmark\Bookmark;
7use Shaarli\Config\ConfigManager;
a5a9cf23 8use Shaarli\TestCase;
e26e2060
A
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 */
8f60e120 28 protected function setUp(): void
e26e2060
A
29 {
30 copy('tests/utils/config/configJson.json.php', self::$testConf .'.json.php');
31 $this->conf = new ConfigManager(self::$testConf);
a39acb25 32 $this->formatter = new BookmarkMarkdownFormatter($this->conf, true);
e26e2060
A
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;
03340c18 128 $description .= 'Also, there is an <a href="./add-tag/hashtag">#hashtag</a> added<br />'. PHP_EOL;
e26e2060
A
129 $description .= 'A N D KEEP SPACES ! ';
130 $description .= '</p></div>';
131
132 $this->assertEquals($description, $link['description']);
133 }
134
9ef8555a
A
135 /**
136 * Make sure that the description is properly formatted by the default formatter.
137 */
138 public function testFormatDescriptionWithSearchHighlight()
139 {
140 $description = 'This a <strong>description</strong>'. PHP_EOL;
141 $description .= 'text https://sub.domain.tld?query=here&for=real#hash more text'. PHP_EOL;
142 $description .= 'Also, there is an #hashtag added'. PHP_EOL;
143 $description .= ' A N D KEEP SPACES ! '. PHP_EOL;
144 $description .= 'And [yet another link](https://other.domain.tld)'. PHP_EOL;
145
146 $bookmark = new Bookmark();
147 $bookmark->setDescription($description);
148 $bookmark->addAdditionalContentEntry(
149 'search_highlight',
150 ['description' => [
151 ['start' => 18, 'end' => 26], // cription
152 ['start' => 49, 'end' => 52], // sub
153 ['start' => 84, 'end' => 88], // hash
154 ['start' => 118, 'end' => 123], // hasht
155 ['start' => 203, 'end' => 215], // other.domain
156 ]]
157 );
158
159 $link = $this->formatter->format($bookmark);
160
161 $description = '<div class="markdown"><p>';
162 $description .= 'This a &lt;strong&gt;des<span class="search-highlight">cription</span>&lt;/strong&gt;<br />' .
163 PHP_EOL;
164 $url = 'https://sub.domain.tld?query=here&amp;for=real#hash';
165 $highlighted = 'https://<span class="search-highlight">sub</span>.domain.tld';
166 $highlighted .= '?query=here&amp;for=real#<span class="search-highlight">hash</span>';
167 $description .= 'text <a href="'. $url .'">'. $highlighted .'</a> more text<br />'. PHP_EOL;
168 $description .= 'Also, there is an <a href="./add-tag/hashtag">#<span class="search-highlight">hasht</span>' .
169 'ag</a> added<br />'. PHP_EOL;
170 $description .= 'A N D KEEP SPACES !<br />' . PHP_EOL;
171 $description .= 'And <a href="https://other.domain.tld">' .
172 '<span class="search-highlight">yet another link</span></a>';
173 $description .= '</p></div>';
174
175 $this->assertEquals($description, $link['description']);
176 }
177
e26e2060
A
178 /**
179 * Test formatting URL with an index_url set
180 * It should prepend relative links.
181 */
182 public function testFormatNoteWithIndexUrl()
183 {
184 $bookmark = new Bookmark();
185 $bookmark->setUrl($short = '?abcdef');
186 $description = 'Text #hashtag more text';
187 $bookmark->setDescription($description);
188
189 $this->formatter->addContextData('index_url', $root = 'https://domain.tld/hithere/');
190
191 $description = '<div class="markdown"><p>';
03340c18 192 $description .= 'Text <a href="'. $root .'./add-tag/hashtag">#hashtag</a> more text';
e26e2060
A
193 $description .= '</p></div>';
194
195 $link = $this->formatter->format($bookmark);
196 $this->assertEquals($root . $short, $link['url']);
197 $this->assertEquals($root . $short, $link['real_url']);
198 $this->assertEquals(
199 $description,
200 $link['description']
201 );
202 }
203}