3 namespace Shaarli\Bookmark
;
5 use PHPUnit\Framework\TestCase
;
6 use Shaarli\Config\ConfigManager
;
10 * Class BookmarkInitializerTest
11 * @package Shaarli\Bookmark
13 class BookmarkInitializerTest
extends TestCase
15 /** @var string Path of test data store */
16 protected static $testDatastore = 'sandbox/datastore.php';
18 /** @var string Path of test config file */
19 protected static $testConf = 'sandbox/config';
22 * @var ConfigManager instance.
27 * @var History instance.
31 /** @var BookmarkServiceInterface instance */
32 protected $bookmarkService;
34 /** @var BookmarkInitializer instance */
35 protected $initializer;
38 * Initialize an empty BookmarkFileService
40 public function setUp(): void
42 if (file_exists(self
::$testDatastore)) {
43 unlink(self
::$testDatastore);
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);
52 $this->initializer
= new BookmarkInitializer($this->bookmarkService
);
56 * Test initialize() with a data store containing bookmarks.
58 public function testInitializeNotEmptyDataStore(): void
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
);
65 $this->initializer
->initialize();
67 $this->assertEquals($refDB->countLinks() +
3, $this->bookmarkService
->count());
69 $bookmark = $this->bookmarkService
->get(43);
70 $this->assertStringStartsWith(
71 'Shaarli will automatically pick up the thumbnail for links to a variety of websites.',
72 $bookmark->getDescription()
74 $this->assertTrue($bookmark->isPrivate());
76 $bookmark = $this->bookmarkService
->get(44);
77 $this->assertStringStartsWith(
78 'Adding a shaare without entering a URL creates a text-only "note" post such as this one.',
79 $bookmark->getDescription()
81 $this->assertTrue($bookmark->isPrivate());
83 $bookmark = $this->bookmarkService
->get(45);
84 $this->assertStringStartsWith(
85 'Welcome to Shaarli!',
86 $bookmark->getDescription()
88 $this->assertFalse($bookmark->isPrivate());
90 $this->bookmarkService
->save();
93 $this->bookmarkService
= new BookmarkFileService($this->conf
, $this->history
, true);
94 $this->assertEquals($refDB->countLinks() +
3, $this->bookmarkService
->count());
96 $bookmark = $this->bookmarkService
->get(43);
97 $this->assertStringStartsWith(
98 'Shaarli will automatically pick up the thumbnail for links to a variety of websites.',
99 $bookmark->getDescription()
101 $this->assertTrue($bookmark->isPrivate());
103 $bookmark = $this->bookmarkService
->get(44);
104 $this->assertStringStartsWith(
105 'Adding a shaare without entering a URL creates a text-only "note" post such as this one.',
106 $bookmark->getDescription()
108 $this->assertTrue($bookmark->isPrivate());
110 $bookmark = $this->bookmarkService
->get(45);
111 $this->assertStringStartsWith(
112 'Welcome to Shaarli!',
113 $bookmark->getDescription()
115 $this->assertFalse($bookmark->isPrivate());
119 * Test initialize() with an a non existent datastore file .
121 public function testInitializeNonExistentDataStore(): void
123 $this->conf
->set('resource.datastore', static::$testDatastore . '_empty');
124 $this->bookmarkService
= new BookmarkFileService($this->conf
, $this->history
, true);
126 $this->initializer
->initialize();
128 $this->assertEquals(3, $this->bookmarkService
->count());
129 $bookmark = $this->bookmarkService
->get(0);
130 $this->assertStringStartsWith(
131 'Shaarli will automatically pick up the thumbnail for links to a variety of websites.',
132 $bookmark->getDescription()
134 $this->assertTrue($bookmark->isPrivate());
136 $bookmark = $this->bookmarkService
->get(1);
137 $this->assertStringStartsWith(
138 'Adding a shaare without entering a URL creates a text-only "note" post such as this one.',
139 $bookmark->getDescription()
141 $this->assertTrue($bookmark->isPrivate());
143 $bookmark = $this->bookmarkService
->get(2);
144 $this->assertStringStartsWith(
145 'Welcome to Shaarli!',
146 $bookmark->getDescription()
148 $this->assertFalse($bookmark->isPrivate());