]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/bookmark/BookmarkInitializerTest.php
Add mutex on datastore I/O operations
[github/shaarli/Shaarli.git] / tests / bookmark / BookmarkInitializerTest.php
CommitLineData
e26e2060
A
1<?php
2
3namespace Shaarli\Bookmark;
4
fd1ddad9 5use malkusch\lock\mutex\NoMutex;
e26e2060
A
6use Shaarli\Config\ConfigManager;
7use Shaarli\History;
a5a9cf23 8use Shaarli\TestCase;
e26e2060
A
9
10/**
11 * Class BookmarkInitializerTest
12 * @package Shaarli\Bookmark
13 */
14class 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
fd1ddad9
A
38 /** @var NoMutex */
39 protected $mutex;
40
e26e2060
A
41 /**
42 * Initialize an empty BookmarkFileService
43 */
da7acb98 44 public function setUp(): void
e26e2060 45 {
fd1ddad9 46 $this->mutex = new NoMutex();
e26e2060
A
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');
fd1ddad9 55 $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, $this->mutex, true);
e26e2060
A
56
57 $this->initializer = new BookmarkInitializer($this->bookmarkService);
58 }
59
60 /**
d6e5f04d 61 * Test initialize() with a data store containing bookmarks.
e26e2060 62 */
d6e5f04d 63 public function testInitializeNotEmptyDataStore(): void
e26e2060
A
64 {
65 $refDB = new \ReferenceLinkDB();
66 $refDB->write(self::$testDatastore);
fd1ddad9 67 $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, $this->mutex, true);
e26e2060
A
68 $this->initializer = new BookmarkInitializer($this->bookmarkService);
69
70 $this->initializer->initialize();
71
da7acb98
A
72 $this->assertEquals($refDB->countLinks() + 3, $this->bookmarkService->count());
73
e26e2060 74 $bookmark = $this->bookmarkService->get(43);
da7acb98
A
75 $this->assertStringStartsWith(
76 'Shaarli will automatically pick up the thumbnail for links to a variety of websites.',
77 $bookmark->getDescription()
78 );
e26e2060
A
79 $this->assertTrue($bookmark->isPrivate());
80
81 $bookmark = $this->bookmarkService->get(44);
da7acb98
A
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()
e26e2060
A
92 );
93 $this->assertFalse($bookmark->isPrivate());
94
d6e5f04d
A
95 $this->bookmarkService->save();
96
e26e2060 97 // Reload from file
fd1ddad9 98 $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, $this->mutex, true);
da7acb98
A
99 $this->assertEquals($refDB->countLinks() + 3, $this->bookmarkService->count());
100
e26e2060 101 $bookmark = $this->bookmarkService->get(43);
da7acb98
A
102 $this->assertStringStartsWith(
103 'Shaarli will automatically pick up the thumbnail for links to a variety of websites.',
104 $bookmark->getDescription()
105 );
e26e2060
A
106 $this->assertTrue($bookmark->isPrivate());
107
108 $bookmark = $this->bookmarkService->get(44);
da7acb98
A
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()
e26e2060
A
119 );
120 $this->assertFalse($bookmark->isPrivate());
121 }
122
123 /**
d6e5f04d 124 * Test initialize() with an a non existent datastore file .
e26e2060 125 */
d6e5f04d 126 public function testInitializeNonExistentDataStore(): void
e26e2060 127 {
d6e5f04d 128 $this->conf->set('resource.datastore', static::$testDatastore . '_empty');
fd1ddad9 129 $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, $this->mutex, true);
d6e5f04d 130
e26e2060
A
131 $this->initializer->initialize();
132
da7acb98 133 $this->assertEquals(3, $this->bookmarkService->count());
e26e2060 134 $bookmark = $this->bookmarkService->get(0);
da7acb98
A
135 $this->assertStringStartsWith(
136 'Shaarli will automatically pick up the thumbnail for links to a variety of websites.',
137 $bookmark->getDescription()
138 );
e26e2060
A
139 $this->assertTrue($bookmark->isPrivate());
140
141 $bookmark = $this->bookmarkService->get(1);
da7acb98
A
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()
e26e2060
A
152 );
153 $this->assertFalse($bookmark->isPrivate());
154 }
155}