]>
Commit | Line | Data |
---|---|---|
e26e2060 A |
1 | <?php |
2 | ||
3 | namespace Shaarli\Bookmark; | |
4 | ||
5 | use PHPUnit\Framework\TestCase; | |
e26e2060 A |
6 | use Shaarli\Config\ConfigManager; |
7 | use Shaarli\History; | |
8 | ||
9 | /** | |
10 | * Class BookmarkInitializerTest | |
11 | * @package Shaarli\Bookmark | |
12 | */ | |
13 | class BookmarkInitializerTest extends TestCase | |
14 | { | |
15 | /** @var string Path of test data store */ | |
16 | protected static $testDatastore = 'sandbox/datastore.php'; | |
17 | ||
18 | /** @var string Path of test config file */ | |
19 | protected static $testConf = 'sandbox/config'; | |
20 | ||
21 | /** | |
22 | * @var ConfigManager instance. | |
23 | */ | |
24 | protected $conf; | |
25 | ||
26 | /** | |
27 | * @var History instance. | |
28 | */ | |
29 | protected $history; | |
30 | ||
31 | /** @var BookmarkServiceInterface instance */ | |
32 | protected $bookmarkService; | |
33 | ||
34 | /** @var BookmarkInitializer instance */ | |
35 | protected $initializer; | |
36 | ||
37 | /** | |
38 | * Initialize an empty BookmarkFileService | |
39 | */ | |
da7acb98 | 40 | public function setUp(): void |
e26e2060 A |
41 | { |
42 | if (file_exists(self::$testDatastore)) { | |
43 | unlink(self::$testDatastore); | |
44 | } | |
45 | ||
46 | copy('tests/utils/config/configJson.json.php', self::$testConf .'.json.php'); | |
47 | $this->conf = new ConfigManager(self::$testConf); | |
48 | $this->conf->set('resource.datastore', self::$testDatastore); | |
49 | $this->history = new History('sandbox/history.php'); | |
50 | $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true); | |
51 | ||
52 | $this->initializer = new BookmarkInitializer($this->bookmarkService); | |
53 | } | |
54 | ||
55 | /** | |
d6e5f04d | 56 | * Test initialize() with a data store containing bookmarks. |
e26e2060 | 57 | */ |
d6e5f04d | 58 | public function testInitializeNotEmptyDataStore(): void |
e26e2060 A |
59 | { |
60 | $refDB = new \ReferenceLinkDB(); | |
61 | $refDB->write(self::$testDatastore); | |
62 | $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true); | |
63 | $this->initializer = new BookmarkInitializer($this->bookmarkService); | |
64 | ||
65 | $this->initializer->initialize(); | |
66 | ||
da7acb98 A |
67 | $this->assertEquals($refDB->countLinks() + 3, $this->bookmarkService->count()); |
68 | ||
e26e2060 | 69 | $bookmark = $this->bookmarkService->get(43); |
da7acb98 A |
70 | $this->assertStringStartsWith( |
71 | 'Shaarli will automatically pick up the thumbnail for links to a variety of websites.', | |
72 | $bookmark->getDescription() | |
73 | ); | |
e26e2060 A |
74 | $this->assertTrue($bookmark->isPrivate()); |
75 | ||
76 | $bookmark = $this->bookmarkService->get(44); | |
da7acb98 A |
77 | $this->assertStringStartsWith( |
78 | 'Adding a shaare without entering a URL creates a text-only "note" post such as this one.', | |
79 | $bookmark->getDescription() | |
80 | ); | |
81 | $this->assertTrue($bookmark->isPrivate()); | |
82 | ||
83 | $bookmark = $this->bookmarkService->get(45); | |
84 | $this->assertStringStartsWith( | |
85 | 'Welcome to Shaarli!', | |
86 | $bookmark->getDescription() | |
e26e2060 A |
87 | ); |
88 | $this->assertFalse($bookmark->isPrivate()); | |
89 | ||
d6e5f04d A |
90 | $this->bookmarkService->save(); |
91 | ||
e26e2060 A |
92 | // Reload from file |
93 | $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true); | |
da7acb98 A |
94 | $this->assertEquals($refDB->countLinks() + 3, $this->bookmarkService->count()); |
95 | ||
e26e2060 | 96 | $bookmark = $this->bookmarkService->get(43); |
da7acb98 A |
97 | $this->assertStringStartsWith( |
98 | 'Shaarli will automatically pick up the thumbnail for links to a variety of websites.', | |
99 | $bookmark->getDescription() | |
100 | ); | |
e26e2060 A |
101 | $this->assertTrue($bookmark->isPrivate()); |
102 | ||
103 | $bookmark = $this->bookmarkService->get(44); | |
da7acb98 A |
104 | $this->assertStringStartsWith( |
105 | 'Adding a shaare without entering a URL creates a text-only "note" post such as this one.', | |
106 | $bookmark->getDescription() | |
107 | ); | |
108 | $this->assertTrue($bookmark->isPrivate()); | |
109 | ||
110 | $bookmark = $this->bookmarkService->get(45); | |
111 | $this->assertStringStartsWith( | |
112 | 'Welcome to Shaarli!', | |
113 | $bookmark->getDescription() | |
e26e2060 A |
114 | ); |
115 | $this->assertFalse($bookmark->isPrivate()); | |
116 | } | |
117 | ||
118 | /** | |
d6e5f04d | 119 | * Test initialize() with an a non existent datastore file . |
e26e2060 | 120 | */ |
d6e5f04d | 121 | public function testInitializeNonExistentDataStore(): void |
e26e2060 | 122 | { |
d6e5f04d A |
123 | $this->conf->set('resource.datastore', static::$testDatastore . '_empty'); |
124 | $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true); | |
125 | ||
e26e2060 A |
126 | $this->initializer->initialize(); |
127 | ||
da7acb98 | 128 | $this->assertEquals(3, $this->bookmarkService->count()); |
e26e2060 | 129 | $bookmark = $this->bookmarkService->get(0); |
da7acb98 A |
130 | $this->assertStringStartsWith( |
131 | 'Shaarli will automatically pick up the thumbnail for links to a variety of websites.', | |
132 | $bookmark->getDescription() | |
133 | ); | |
e26e2060 A |
134 | $this->assertTrue($bookmark->isPrivate()); |
135 | ||
136 | $bookmark = $this->bookmarkService->get(1); | |
da7acb98 A |
137 | $this->assertStringStartsWith( |
138 | 'Adding a shaare without entering a URL creates a text-only "note" post such as this one.', | |
139 | $bookmark->getDescription() | |
140 | ); | |
141 | $this->assertTrue($bookmark->isPrivate()); | |
142 | ||
143 | $bookmark = $this->bookmarkService->get(2); | |
144 | $this->assertStringStartsWith( | |
145 | 'Welcome to Shaarli!', | |
146 | $bookmark->getDescription() | |
e26e2060 A |
147 | ); |
148 | $this->assertFalse($bookmark->isPrivate()); | |
149 | } | |
150 | } |