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/BookmarkRawFormatterTest.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/BookmarkRawFormatterTest.php')
-rw-r--r-- | tests/formatter/BookmarkRawFormatterTest.php | 97 |
1 files changed, 97 insertions, 0 deletions
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 | |||
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 BookmarkRawFormatterTest | ||
12 | * @package Shaarli\Formatter | ||
13 | */ | ||
14 | class 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 | } | ||