aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/bookmark/BookmarkInitializerTest.php
blob: 3906cc7f4e3f542531eaa0b53b3b174c07cfc18a (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
<?php

namespace Shaarli\Bookmark;

use PHPUnit\Framework\TestCase;
use Shaarli\Config\ConfigManager;
use Shaarli\History;

/**
 * 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;

    /**
     * Initialize an empty BookmarkFileService
     */
    public function setUp()
    {
        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->history = new History('sandbox/history.php');
        $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, 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->history, true);
        $this->initializer = new BookmarkInitializer($this->bookmarkService);

        $this->initializer->initialize();

        $this->assertEquals($refDB->countLinks() + 2, $this->bookmarkService->count());
        $bookmark = $this->bookmarkService->get(43);
        $this->assertEquals(43, $bookmark->getId());
        $this->assertEquals('My secret stuff... - Pastebin.com', $bookmark->getTitle());
        $this->assertTrue($bookmark->isPrivate());

        $bookmark = $this->bookmarkService->get(44);
        $this->assertEquals(44, $bookmark->getId());
        $this->assertEquals(
            'The personal, minimalist, super-fast, database free, bookmarking service',
            $bookmark->getTitle()
        );
        $this->assertFalse($bookmark->isPrivate());

        $this->bookmarkService->save();

        // Reload from file
        $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true);
        $this->assertEquals($refDB->countLinks() + 2, $this->bookmarkService->count());
        $bookmark = $this->bookmarkService->get(43);
        $this->assertEquals(43, $bookmark->getId());
        $this->assertEquals('My secret stuff... - Pastebin.com', $bookmark->getTitle());
        $this->assertTrue($bookmark->isPrivate());

        $bookmark = $this->bookmarkService->get(44);
        $this->assertEquals(44, $bookmark->getId());
        $this->assertEquals(
            'The personal, minimalist, super-fast, database free, bookmarking service',
            $bookmark->getTitle()
        );
        $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->history, true);

        $this->initializer->initialize();

        $this->assertEquals(2, $this->bookmarkService->count());
        $bookmark = $this->bookmarkService->get(0);
        $this->assertEquals(0, $bookmark->getId());
        $this->assertEquals('My secret stuff... - Pastebin.com', $bookmark->getTitle());
        $this->assertTrue($bookmark->isPrivate());

        $bookmark = $this->bookmarkService->get(1);
        $this->assertEquals(1, $bookmark->getId());
        $this->assertEquals(
            'The personal, minimalist, super-fast, database free, bookmarking service',
            $bookmark->getTitle()
        );
        $this->assertFalse($bookmark->isPrivate());
    }
}