aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/NetscapeBookmarkUtilsTest.php
blob: b7472d92e38412455d2a8c8e9514f51d64099a92 (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
<?php

require_once 'application/NetscapeBookmarkUtils.php';

/**
 * Netscape bookmark import and export
 */
class NetscapeBookmarkUtilsTest extends PHPUnit_Framework_TestCase
{
    /**
     * @var string datastore to test write operations
     */
    protected static $testDatastore = 'sandbox/datastore.php';

    /**
     * @var ReferenceLinkDB instance.
     */
    protected static $refDb = null;

    /**
     * @var LinkDB private LinkDB instance.
     */
    protected static $linkDb = null;

    /**
     * Instantiate reference data
     */
    public static function setUpBeforeClass()
    {
        self::$refDb = new ReferenceLinkDB();
        self::$refDb->write(self::$testDatastore);
        self::$linkDb = new LinkDB(self::$testDatastore, true, false);
    }

    /**
     * Attempt to export an invalid link selection
     * @expectedException              Exception
     * @expectedExceptionMessageRegExp /Invalid export selection/
     */
    public function testFilterAndFormatInvalid()
    {
        NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'derp');
    }

    /**
     * Prepare all links for export
     */
    public function testFilterAndFormatAll()
    {
        $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'all');
        $this->assertEquals(self::$refDb->countLinks(), sizeof($links));
        foreach ($links as $link) {
            $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']);
            $this->assertEquals(
                $date->getTimestamp(),
                $link['timestamp']
            );
            $this->assertEquals(
                str_replace(' ', ',', $link['tags']),
                $link['taglist']
            );
        }
    }

    /**
     * Prepare private links for export
     */
    public function testFilterAndFormatPrivate()
    {
        $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'private');
        $this->assertEquals(self::$refDb->countPrivateLinks(), sizeof($links));
        foreach ($links as $link) {
            $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']);
            $this->assertEquals(
                $date->getTimestamp(),
                $link['timestamp']
            );
            $this->assertEquals(
                str_replace(' ', ',', $link['tags']),
                $link['taglist']
            );
        }
    }

    /**
     * Prepare public links for export
     */
    public function testFilterAndFormatPublic()
    {
        $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'public');
        $this->assertEquals(self::$refDb->countPublicLinks(), sizeof($links));
        foreach ($links as $link) {
            $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']);
            $this->assertEquals(
                $date->getTimestamp(),
                $link['timestamp']
            );
            $this->assertEquals(
                str_replace(' ', ',', $link['tags']),
                $link['taglist']
            );
        }
    }
}