]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/bookmark/BookmarkInitializerTest.php
Compatibility with PHPUnit 9
[github/shaarli/Shaarli.git] / tests / bookmark / BookmarkInitializerTest.php
1 <?php
2
3 namespace Shaarli\Bookmark;
4
5 use Shaarli\Config\ConfigManager;
6 use Shaarli\History;
7 use Shaarli\TestCase;
8
9 /**
10 * Class BookmarkInitializerTest
11 * @package Shaarli\Bookmark
12 */
13 class BookmarkInitializerTest extends TestCase
14 {
15 /** @var string Path of test data store */
16 protected static $testDatastore = 'sandbox/datastore.php';
17
18 /** @var string Path of test config file */
19 protected static $testConf = 'sandbox/config';
20
21 /**
22 * @var ConfigManager instance.
23 */
24 protected $conf;
25
26 /**
27 * @var History instance.
28 */
29 protected $history;
30
31 /** @var BookmarkServiceInterface instance */
32 protected $bookmarkService;
33
34 /** @var BookmarkInitializer instance */
35 protected $initializer;
36
37 /**
38 * Initialize an empty BookmarkFileService
39 */
40 public function setUp(): void
41 {
42 if (file_exists(self::$testDatastore)) {
43 unlink(self::$testDatastore);
44 }
45
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);
51
52 $this->initializer = new BookmarkInitializer($this->bookmarkService);
53 }
54
55 /**
56 * Test initialize() with a data store containing bookmarks.
57 */
58 public function testInitializeNotEmptyDataStore(): void
59 {
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);
64
65 $this->initializer->initialize();
66
67 $this->assertEquals($refDB->countLinks() + 3, $this->bookmarkService->count());
68
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()
73 );
74 $this->assertTrue($bookmark->isPrivate());
75
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()
80 );
81 $this->assertTrue($bookmark->isPrivate());
82
83 $bookmark = $this->bookmarkService->get(45);
84 $this->assertStringStartsWith(
85 'Welcome to Shaarli!',
86 $bookmark->getDescription()
87 );
88 $this->assertFalse($bookmark->isPrivate());
89
90 $this->bookmarkService->save();
91
92 // Reload from file
93 $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true);
94 $this->assertEquals($refDB->countLinks() + 3, $this->bookmarkService->count());
95
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()
100 );
101 $this->assertTrue($bookmark->isPrivate());
102
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()
107 );
108 $this->assertTrue($bookmark->isPrivate());
109
110 $bookmark = $this->bookmarkService->get(45);
111 $this->assertStringStartsWith(
112 'Welcome to Shaarli!',
113 $bookmark->getDescription()
114 );
115 $this->assertFalse($bookmark->isPrivate());
116 }
117
118 /**
119 * Test initialize() with an a non existent datastore file .
120 */
121 public function testInitializeNonExistentDataStore(): void
122 {
123 $this->conf->set('resource.datastore', static::$testDatastore . '_empty');
124 $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true);
125
126 $this->initializer->initialize();
127
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()
133 );
134 $this->assertTrue($bookmark->isPrivate());
135
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()
140 );
141 $this->assertTrue($bookmark->isPrivate());
142
143 $bookmark = $this->bookmarkService->get(2);
144 $this->assertStringStartsWith(
145 'Welcome to Shaarli!',
146 $bookmark->getDescription()
147 );
148 $this->assertFalse($bookmark->isPrivate());
149 }
150 }