]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/bookmark/BookmarkInitializerTest.php
Added PATCH to the allowed Apache request methods.
[github/shaarli/Shaarli.git] / tests / bookmark / BookmarkInitializerTest.php
1 <?php
2
3 namespace Shaarli\Bookmark;
4
5 use PHPUnit\Framework\TestCase;
6 use Shaarli\Config\ConfigManager;
7 use Shaarli\History;
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()
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() + 2, $this->bookmarkService->count());
68 $bookmark = $this->bookmarkService->get(43);
69 $this->assertEquals(43, $bookmark->getId());
70 $this->assertEquals('My secret stuff... - Pastebin.com', $bookmark->getTitle());
71 $this->assertTrue($bookmark->isPrivate());
72
73 $bookmark = $this->bookmarkService->get(44);
74 $this->assertEquals(44, $bookmark->getId());
75 $this->assertEquals(
76 'The personal, minimalist, super-fast, database free, bookmarking service',
77 $bookmark->getTitle()
78 );
79 $this->assertFalse($bookmark->isPrivate());
80
81 $this->bookmarkService->save();
82
83 // Reload from file
84 $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true);
85 $this->assertEquals($refDB->countLinks() + 2, $this->bookmarkService->count());
86 $bookmark = $this->bookmarkService->get(43);
87 $this->assertEquals(43, $bookmark->getId());
88 $this->assertEquals('My secret stuff... - Pastebin.com', $bookmark->getTitle());
89 $this->assertTrue($bookmark->isPrivate());
90
91 $bookmark = $this->bookmarkService->get(44);
92 $this->assertEquals(44, $bookmark->getId());
93 $this->assertEquals(
94 'The personal, minimalist, super-fast, database free, bookmarking service',
95 $bookmark->getTitle()
96 );
97 $this->assertFalse($bookmark->isPrivate());
98 }
99
100 /**
101 * Test initialize() with an a non existent datastore file .
102 */
103 public function testInitializeNonExistentDataStore(): void
104 {
105 $this->conf->set('resource.datastore', static::$testDatastore . '_empty');
106 $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true);
107
108 $this->initializer->initialize();
109
110 $this->assertEquals(2, $this->bookmarkService->count());
111 $bookmark = $this->bookmarkService->get(0);
112 $this->assertEquals(0, $bookmark->getId());
113 $this->assertEquals('My secret stuff... - Pastebin.com', $bookmark->getTitle());
114 $this->assertTrue($bookmark->isPrivate());
115
116 $bookmark = $this->bookmarkService->get(1);
117 $this->assertEquals(1, $bookmark->getId());
118 $this->assertEquals(
119 'The personal, minimalist, super-fast, database free, bookmarking service',
120 $bookmark->getTitle()
121 );
122 $this->assertFalse($bookmark->isPrivate());
123 }
124 }