]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/bookmark/BookmarkInitializerTest.php
New plugin hook: ability to add custom filters to Shaarli search engine
[github/shaarli/Shaarli.git] / tests / bookmark / BookmarkInitializerTest.php
1 <?php
2
3 namespace Shaarli\Bookmark;
4
5 use malkusch\lock\mutex\NoMutex;
6 use Shaarli\Config\ConfigManager;
7 use Shaarli\History;
8 use Shaarli\Plugin\PluginManager;
9 use Shaarli\TestCase;
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
39 /** @var NoMutex */
40 protected $mutex;
41
42 /** @var PluginManager */
43 protected $pluginManager;
44
45 /**
46 * Initialize an empty BookmarkFileService
47 */
48 public function setUp(): void
49 {
50 $this->mutex = new NoMutex();
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);
58 $this->pluginManager = new PluginManager($this->conf);
59 $this->history = new History('sandbox/history.php');
60 $this->bookmarkService = new BookmarkFileService(
61 $this->conf,
62 $this->pluginManager,
63 $this->history,
64 $this->mutex,
65 true
66 );
67
68 $this->initializer = new BookmarkInitializer($this->bookmarkService);
69 }
70
71 /**
72 * Test initialize() with a data store containing bookmarks.
73 */
74 public function testInitializeNotEmptyDataStore(): void
75 {
76 $refDB = new \ReferenceLinkDB();
77 $refDB->write(self::$testDatastore);
78 $this->bookmarkService = new BookmarkFileService(
79 $this->conf,
80 $this->pluginManager,
81 $this->history,
82 $this->mutex,
83 true
84 );
85 $this->initializer = new BookmarkInitializer($this->bookmarkService);
86
87 $this->initializer->initialize();
88
89 $this->assertEquals($refDB->countLinks() + 3, $this->bookmarkService->count());
90
91 $bookmark = $this->bookmarkService->get(43);
92 $this->assertStringStartsWith(
93 'Shaarli will automatically pick up the thumbnail for links to a variety of websites.',
94 $bookmark->getDescription()
95 );
96 $this->assertTrue($bookmark->isPrivate());
97
98 $bookmark = $this->bookmarkService->get(44);
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()
109 );
110 $this->assertFalse($bookmark->isPrivate());
111
112 $this->bookmarkService->save();
113
114 // Reload from file
115 $this->bookmarkService = new BookmarkFileService(
116 $this->conf,
117 $this->pluginManager,
118 $this->history,
119 $this->mutex,
120 true
121 );
122 $this->assertEquals($refDB->countLinks() + 3, $this->bookmarkService->count());
123
124 $bookmark = $this->bookmarkService->get(43);
125 $this->assertStringStartsWith(
126 'Shaarli will automatically pick up the thumbnail for links to a variety of websites.',
127 $bookmark->getDescription()
128 );
129 $this->assertTrue($bookmark->isPrivate());
130
131 $bookmark = $this->bookmarkService->get(44);
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()
142 );
143 $this->assertFalse($bookmark->isPrivate());
144 }
145
146 /**
147 * Test initialize() with an a non existent datastore file .
148 */
149 public function testInitializeNonExistentDataStore(): void
150 {
151 $this->conf->set('resource.datastore', static::$testDatastore . '_empty');
152 $this->bookmarkService = new BookmarkFileService(
153 $this->conf,
154 $this->pluginManager,
155 $this->history,
156 $this->mutex,
157 true
158 );
159
160 $this->initializer->initialize();
161
162 $this->assertEquals(3, $this->bookmarkService->count());
163 $bookmark = $this->bookmarkService->get(0);
164 $this->assertStringStartsWith(
165 'Shaarli will automatically pick up the thumbnail for links to a variety of websites.',
166 $bookmark->getDescription()
167 );
168 $this->assertTrue($bookmark->isPrivate());
169
170 $bookmark = $this->bookmarkService->get(1);
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()
181 );
182 $this->assertFalse($bookmark->isPrivate());
183 }
184 }