]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/bookmark/BookmarkInitializerTest.php
0c8420ce5ffceac84e4dd471cdc8f8fa567113d1
[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\TestCase;
9
10 /**
11 * Class BookmarkInitializerTest
12 * @package Shaarli\Bookmark
13 */
14 class BookmarkInitializerTest extends TestCase
15 {
16 /** @var string Path of test data store */
17 protected static $testDatastore = 'sandbox/datastore.php';
18
19 /** @var string Path of test config file */
20 protected static $testConf = 'sandbox/config';
21
22 /**
23 * @var ConfigManager instance.
24 */
25 protected $conf;
26
27 /**
28 * @var History instance.
29 */
30 protected $history;
31
32 /** @var BookmarkServiceInterface instance */
33 protected $bookmarkService;
34
35 /** @var BookmarkInitializer instance */
36 protected $initializer;
37
38 /** @var NoMutex */
39 protected $mutex;
40
41 /**
42 * Initialize an empty BookmarkFileService
43 */
44 public function setUp(): void
45 {
46 $this->mutex = new NoMutex();
47 if (file_exists(self::$testDatastore)) {
48 unlink(self::$testDatastore);
49 }
50
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);
56
57 $this->initializer = new BookmarkInitializer($this->bookmarkService);
58 }
59
60 /**
61 * Test initialize() with a data store containing bookmarks.
62 */
63 public function testInitializeNotEmptyDataStore(): void
64 {
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);
69
70 $this->initializer->initialize();
71
72 $this->assertEquals($refDB->countLinks() + 3, $this->bookmarkService->count());
73
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()
78 );
79 $this->assertTrue($bookmark->isPrivate());
80
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()
85 );
86 $this->assertTrue($bookmark->isPrivate());
87
88 $bookmark = $this->bookmarkService->get(45);
89 $this->assertStringStartsWith(
90 'Welcome to Shaarli!',
91 $bookmark->getDescription()
92 );
93 $this->assertFalse($bookmark->isPrivate());
94
95 $this->bookmarkService->save();
96
97 // Reload from file
98 $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, $this->mutex, true);
99 $this->assertEquals($refDB->countLinks() + 3, $this->bookmarkService->count());
100
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()
105 );
106 $this->assertTrue($bookmark->isPrivate());
107
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()
112 );
113 $this->assertTrue($bookmark->isPrivate());
114
115 $bookmark = $this->bookmarkService->get(45);
116 $this->assertStringStartsWith(
117 'Welcome to Shaarli!',
118 $bookmark->getDescription()
119 );
120 $this->assertFalse($bookmark->isPrivate());
121 }
122
123 /**
124 * Test initialize() with an a non existent datastore file .
125 */
126 public function testInitializeNonExistentDataStore(): void
127 {
128 $this->conf->set('resource.datastore', static::$testDatastore . '_empty');
129 $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, $this->mutex, true);
130
131 $this->initializer->initialize();
132
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()
138 );
139 $this->assertTrue($bookmark->isPrivate());
140
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()
145 );
146 $this->assertTrue($bookmark->isPrivate());
147
148 $bookmark = $this->bookmarkService->get(2);
149 $this->assertStringStartsWith(
150 'Welcome to Shaarli!',
151 $bookmark->getDescription()
152 );
153 $this->assertFalse($bookmark->isPrivate());
154 }
155 }