summaryrefslogtreecommitdiff
path: root/tests/Api/ExportCsvTest.php
blob: ee7db4b0cb841770f1ee0cc3e4da810bcafd4cb3 (plain)
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
/**
 * SemanticScuttle - your social bookmark manager.
 *
 * PHP version 5.
 *
 * @category Bookmarking
 * @package  SemanticScuttle
 * @author   Benjamin Huynh-Kim-Bang <mensonge@users.sourceforge.net>
 * @author   Christian Weiske <cweiske@cweiske.de>
 * @author   Eric Dane <ericdane@users.sourceforge.net>
 * @license  GPL http://www.gnu.org/licenses/gpl.html
 * @link     http://sourceforge.net/projects/semanticscuttle
 */

require_once dirname(__FILE__) . '/../prepare.php';
require_once 'HTTP/Request2.php';

if (!defined('PHPUnit_MAIN_METHOD')) {
    define('PHPUnit_MAIN_METHOD', 'Api_ExportCsvTest::main');
}

/**
 * Unit tests for the SemanticScuttle csv export API
 *
 * @category Bookmarking
 * @package  SemanticScuttle
 * @author   Benjamin Huynh-Kim-Bang <mensonge@users.sourceforge.net>
 * @author   Christian Weiske <cweiske@cweiske.de>
 * @author   Eric Dane <ericdane@users.sourceforge.net>
 * @license  GPL http://www.gnu.org/licenses/gpl.html
 * @link     http://sourceforge.net/projects/semanticscuttle
 */
class Api_ExportCsvTest extends TestBaseApi
{
    protected $us;
    protected $bs;
    protected $urlPart = 'api/export_csv.php';



    /**
     * Used to run this test class standalone
     *
     * @return void
     */
    public static function main()
    {
        require_once 'PHPUnit/TextUI/TestRunner.php';
        PHPUnit_TextUI_TestRunner::run(
            new PHPUnit_Framework_TestSuite(__CLASS__)
        );
    }



    /**
     * Test if authentication is required when sending no auth data
     */
    public function testAuthWithoutAuthData()
    {
        $req = $this->getRequest(null, false);
        $res = $req->send();
        $this->assertEquals(401, $res->getStatus());
    }



    /**
     * Test if authentication is required when sending wrong user data

     */
    public function testAuthWrongCredentials()
    {
        $req = $this->getRequest(null, false);
        $req->setAuth('user', 'password', HTTP_Request2::AUTH_BASIC);
        $res = $req->send();
        $this->assertEquals(401, $res->getStatus());
    }



    /**
     * Test MIME content type and filename header fields
     */
    public function testMimeTypeFilename()
    {
        $res = reset($this->getAuthRequest())->send();

        $this->assertEquals(200, $res->getStatus());
        //verify MIME content type
        $this->assertEquals(
            'application/csv-tab-delimited-table; charset=utf-8',
            $res->getHeader('content-type')
        );
        //we need a file name
        $this->assertNotNull($res->getHeader('content-disposition'));
    }



    /**
     * Test CSV export without bookmarks
     */
    public function testNoBookmarks()
    {
        list($req, $uid) = $this->getAuthRequest();
        $body = $req->send()->getBody();
        $csv  = $this->getCsvArray($body);

        $this->assertEquals(1, count($csv));
        $this->assertCsvHeader($csv);
    }



    /**
     * Test CSV export with some bookmarks
     */
    public function testBookmarks()
    {
        list($req, $uid) = $this->getAuthRequest();
        //public
        $this->addBookmark(
            $uid, 'http://example.org/testBookmarks', 0,
            array('unittest', 'testBookmarks'), 'mytitle'
        );
        //shared
        $this->addBookmark(
            $uid, 'http://example.org/testBookmarks-shared', 1,
            array('unittest', 'testBookmarks'), 'mytitle-shared'
        );
        //private
        $this->addBookmark(
            $uid, 'http://example.org/testBookmarks-private', 2,
            array('unittest', 'testBookmarks'), 'mytitle-private'
        );

        //private other that should not in the export
        $this->addBookmark(
            null, 'http://example.org/testBookmarks-private2', 2
        );

        $body = $req->send()->getBody();
        $csv  = $this->getCsvArray($body);

        $this->assertEquals(4, count($csv));
        $this->assertCsvHeader($csv);

        $this->assertEquals('http://example.org/testBookmarks', $csv[1][0]);
        $this->assertEquals('mytitle', $csv[1][1]);
        $this->assertEquals('unittest,testbookmarks', $csv[1][2]);

        $this->assertEquals('http://example.org/testBookmarks-shared', $csv[2][0]);
        $this->assertEquals('mytitle-shared', $csv[2][1]);
        $this->assertEquals('unittest,testbookmarks', $csv[2][2]);

        $this->assertEquals('http://example.org/testBookmarks-private', $csv[3][0]);
        $this->assertEquals('mytitle-private', $csv[3][1]);
        $this->assertEquals('unittest,testbookmarks', $csv[3][2]);
    }



    /**
     * Asserts that the CSV array contains the correct header
     *
     * @param array $csv CSV array from getCsvArray()
     *
     * @return void
     */
    protected function assertCsvHeader($csv)
    {
        $this->assertEquals(
            array('url', 'title', 'tags', 'description'),
            $csv[0]
        );
    }



    /**
     * Converts a string of CSV data to an array
     *
     * @param string $body String containing the full CSV file
     *
     * @return array Array of CSV data
     */
    protected function getCsvArray($body)
    {
        $v53 = (version_compare(PHP_VERSION, '5.3.0') === 1);

        //dead simple implementation that does not work with
        // advanced CSV files
        $ar = array();
        foreach (explode("\n", $body) as $line) {
            if ($v53) {
                $ar[] = str_getcsv($line, ';');
            } else {
                $arl = explode(';', $line);
                foreach ($arl as &$str) {
                    if (substr($str, 0, 1) == '"'
                        && substr($str, -1) == '"'
                    ) {
                        $str = substr($str, 1, -1);
                    }
                }
                $ar[] = $arl;
            }
        }
        if (count(end($ar)) == 1 && reset(end($ar)) == '') {
            unset($ar[key($ar)]);
        }
        return $ar;
    }
}

if (PHPUnit_MAIN_METHOD == 'Api_ExportCsvTest::main') {
    Api_ExportCsvTest::main();
}
?>