3 namespace Shaarli\Bookmark
;
5 use malkusch\lock\mutex\NoMutex
;
6 use Shaarli\Config\ConfigManager
;
11 * Class BookmarkInitializerTest
12 * @package Shaarli\Bookmark
14 class BookmarkInitializerTest
extends TestCase
16 /** @var string Path of test data store */
17 protected static $testDatastore = 'sandbox/datastore.php';
19 /** @var string Path of test config file */
20 protected static $testConf = 'sandbox/config';
23 * @var ConfigManager instance.
28 * @var History instance.
32 /** @var BookmarkServiceInterface instance */
33 protected $bookmarkService;
35 /** @var BookmarkInitializer instance */
36 protected $initializer;
42 * Initialize an empty BookmarkFileService
44 public function setUp(): void
46 $this->mutex
= new NoMutex();
47 if (file_exists(self
::$testDatastore)) {
48 unlink(self
::$testDatastore);
51 copy('tests/utils/config/configJson.json.php', self
::$testConf .'.json.php');
52 $this->conf
= new ConfigManager(self
::$testConf);
53 $this->conf
->set('resource.datastore', self
::$testDatastore);
54 $this->history
= new History('sandbox/history.php');
55 $this->bookmarkService
= new BookmarkFileService($this->conf
, $this->history
, $this->mutex
, true);
57 $this->initializer
= new BookmarkInitializer($this->bookmarkService
);
61 * Test initialize() with a data store containing bookmarks.
63 public function testInitializeNotEmptyDataStore(): void
65 $refDB = new \
ReferenceLinkDB();
66 $refDB->write(self
::$testDatastore);
67 $this->bookmarkService
= new BookmarkFileService($this->conf
, $this->history
, $this->mutex
, true);
68 $this->initializer
= new BookmarkInitializer($this->bookmarkService
);
70 $this->initializer
->initialize();
72 $this->assertEquals($refDB->countLinks() +
3, $this->bookmarkService
->count());
74 $bookmark = $this->bookmarkService
->get(43);
75 $this->assertStringStartsWith(
76 'Shaarli will automatically pick up the thumbnail for links to a variety of websites.',
77 $bookmark->getDescription()
79 $this->assertTrue($bookmark->isPrivate());
81 $bookmark = $this->bookmarkService
->get(44);
82 $this->assertStringStartsWith(
83 'Adding a shaare without entering a URL creates a text-only "note" post such as this one.',
84 $bookmark->getDescription()
86 $this->assertTrue($bookmark->isPrivate());
88 $bookmark = $this->bookmarkService
->get(45);
89 $this->assertStringStartsWith(
90 'Welcome to Shaarli!',
91 $bookmark->getDescription()
93 $this->assertFalse($bookmark->isPrivate());
95 $this->bookmarkService
->save();
98 $this->bookmarkService
= new BookmarkFileService($this->conf
, $this->history
, $this->mutex
, true);
99 $this->assertEquals($refDB->countLinks() +
3, $this->bookmarkService
->count());
101 $bookmark = $this->bookmarkService
->get(43);
102 $this->assertStringStartsWith(
103 'Shaarli will automatically pick up the thumbnail for links to a variety of websites.',
104 $bookmark->getDescription()
106 $this->assertTrue($bookmark->isPrivate());
108 $bookmark = $this->bookmarkService
->get(44);
109 $this->assertStringStartsWith(
110 'Adding a shaare without entering a URL creates a text-only "note" post such as this one.',
111 $bookmark->getDescription()
113 $this->assertTrue($bookmark->isPrivate());
115 $bookmark = $this->bookmarkService
->get(45);
116 $this->assertStringStartsWith(
117 'Welcome to Shaarli!',
118 $bookmark->getDescription()
120 $this->assertFalse($bookmark->isPrivate());
124 * Test initialize() with an a non existent datastore file .
126 public function testInitializeNonExistentDataStore(): void
128 $this->conf
->set('resource.datastore', static::$testDatastore . '_empty');
129 $this->bookmarkService
= new BookmarkFileService($this->conf
, $this->history
, $this->mutex
, true);
131 $this->initializer
->initialize();
133 $this->assertEquals(3, $this->bookmarkService
->count());
134 $bookmark = $this->bookmarkService
->get(0);
135 $this->assertStringStartsWith(
136 'Shaarli will automatically pick up the thumbnail for links to a variety of websites.',
137 $bookmark->getDescription()
139 $this->assertTrue($bookmark->isPrivate());
141 $bookmark = $this->bookmarkService
->get(1);
142 $this->assertStringStartsWith(
143 'Adding a shaare without entering a URL creates a text-only "note" post such as this one.',
144 $bookmark->getDescription()
146 $this->assertTrue($bookmark->isPrivate());
148 $bookmark = $this->bookmarkService
->get(2);
149 $this->assertStringStartsWith(
150 'Welcome to Shaarli!',
151 $bookmark->getDescription()
153 $this->assertFalse($bookmark->isPrivate());