aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/bookmark/BookmarkInitializerTest.php
blob: 351807c16d5a5a574e0e4d6f92efba35bf07fe47 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php

namespace Shaarli\Bookmark;

use malkusch\lock\mutex\NoMutex;
use Shaarli\Config\ConfigManager;
use Shaarli\History;
use Shaarli\Plugin\PluginManager;
use Shaarli\TestCase;

/**
 * Class BookmarkInitializerTest
 * @package Shaarli\Bookmark
 */
class BookmarkInitializerTest extends TestCase
{
    /** @var string Path of test data store */
    protected static $testDatastore = 'sandbox/datastore.php';

    /** @var string Path of test config file */
    protected static $testConf = 'sandbox/config';

    /**
     * @var ConfigManager instance.
     */
    protected $conf;

    /**
     * @var History instance.
     */
    protected $history;

    /** @var BookmarkServiceInterface instance */
    protected $bookmarkService;

    /** @var BookmarkInitializer instance */
    protected $initializer;

    /** @var NoMutex */
    protected $mutex;

    /** @var PluginManager */
    protected $pluginManager;

    /**
     * Initialize an empty BookmarkFileService
     */
    public function setUp(): void
    {
        $this->mutex = new NoMutex();
        if (file_exists(self::$testDatastore)) {
            unlink(self::$testDatastore);
        }

        copy('tests/utils/config/configJson.json.php', self::$testConf .'.json.php');
        $this->conf = new ConfigManager(self::$testConf);
        $this->conf->set('resource.datastore', self::$testDatastore);
        $this->pluginManager = new PluginManager($this->conf);
        $this->history = new History('sandbox/history.php');
        $this->bookmarkService = new BookmarkFileService(
            $this->conf,
            $this->pluginManager,
            $this->history,
            $this->mutex,
            true
        );

        $this->initializer = new BookmarkInitializer($this->bookmarkService);
    }

    /**
     * Test initialize() with a data store containing bookmarks.
     */
    public function testInitializeNotEmptyDataStore(): void
    {
        $refDB = new \ReferenceLinkDB();
        $refDB->write(self::$testDatastore);
        $this->bookmarkService = new BookmarkFileService(
            $this->conf,
            $this->pluginManager,
            $this->history,
            $this->mutex,
            true
        );
        $this->initializer = new BookmarkInitializer($this->bookmarkService);

        $this->initializer->initialize();

        $this->assertEquals($refDB->countLinks() + 3, $this->bookmarkService->count());

        $bookmark = $this->bookmarkService->get(43);
        $this->assertStringStartsWith(
            'Shaarli will automatically pick up the thumbnail for links to a variety of websites.',
            $bookmark->getDescription()
        );
        $this->assertTrue($bookmark->isPrivate());

        $bookmark = $this->bookmarkService->get(44);
        $this->assertStringStartsWith(
            'Adding a shaare without entering a URL creates a text-only "note" post such as this one.',
            $bookmark->getDescription()
        );
        $this->assertTrue($bookmark->isPrivate());

        $bookmark = $this->bookmarkService->get(45);
        $this->assertStringStartsWith(
            'Welcome to Shaarli!',
            $bookmark->getDescription()
        );
        $this->assertFalse($bookmark->isPrivate());

        $this->bookmarkService->save();

        // Reload from file
        $this->bookmarkService = new BookmarkFileService(
            $this->conf,
            $this->pluginManager,
            $this->history,
            $this->mutex,
            true
        );
        $this->assertEquals($refDB->countLinks() + 3, $this->bookmarkService->count());

        $bookmark = $this->bookmarkService->get(43);
        $this->assertStringStartsWith(
            'Shaarli will automatically pick up the thumbnail for links to a variety of websites.',
            $bookmark->getDescription()
        );
        $this->assertTrue($bookmark->isPrivate());

        $bookmark = $this->bookmarkService->get(44);
        $this->assertStringStartsWith(
            'Adding a shaare without entering a URL creates a text-only "note" post such as this one.',
            $bookmark->getDescription()
        );
        $this->assertTrue($bookmark->isPrivate());

        $bookmark = $this->bookmarkService->get(45);
        $this->assertStringStartsWith(
            'Welcome to Shaarli!',
            $bookmark->getDescription()
        );
        $this->assertFalse($bookmark->isPrivate());
    }

    /**
     * Test initialize() with an a non existent datastore file .
     */
    public function testInitializeNonExistentDataStore(): void
    {
        $this->conf->set('resource.datastore', static::$testDatastore . '_empty');
        $this->bookmarkService = new BookmarkFileService(
            $this->conf,
            $this->pluginManager,
            $this->history,
            $this->mutex,
            true
        );

        $this->initializer->initialize();

        $this->assertEquals(3, $this->bookmarkService->count());
        $bookmark = $this->bookmarkService->get(0);
        $this->assertStringStartsWith(
            'Shaarli will automatically pick up the thumbnail for links to a variety of websites.',
            $bookmark->getDescription()
        );
        $this->assertTrue($bookmark->isPrivate());

        $bookmark = $this->bookmarkService->get(1);
        $this->assertStringStartsWith(
            'Adding a shaare without entering a URL creates a text-only "note" post such as this one.',
            $bookmark->getDescription()
        );
        $this->assertTrue($bookmark->isPrivate());

        $bookmark = $this->bookmarkService->get(2);
        $this->assertStringStartsWith(
            'Welcome to Shaarli!',
            $bookmark->getDescription()
        );
        $this->assertFalse($bookmark->isPrivate());
    }
}