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