summaryrefslogtreecommitdiff
path: root/tests/BookmarkTest.php
blob: caee84b022bd597a356213bdb0841d99e28fe125 (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
<?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 'prepare.php';

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

/**
 * Unit tests for the SemanticScuttle bookmark service.
 *
 * @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 BookmarkTest extends TestBase
{
    protected $us;
    protected $bs;
    protected $ts;
    protected $tts;



    /**
     * 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__)
        );
    }



    protected function setUp()
    {
        $this->us = SemanticScuttle_Service_Factory::get('User');
        $this->bs = SemanticScuttle_Service_Factory::get('Bookmark');
        $this->bs->deleteAll();
        $this->b2ts= SemanticScuttle_Service_Factory::get('Bookmark2Tag');
        $this->b2ts->deleteAll();
        $this->tts = SemanticScuttle_Service_Factory::get('Tag2Tag');
        $this->tts->deleteAll();
        $this->tsts = SemanticScuttle_Service_Factory::get('TagStat');
        $this->tsts->deleteAll();
        $this->vs = SemanticScuttle_Service_Factory::get('Vote');
        $this->vs->deleteAll();
    }

    public function testHardCharactersInBookmarks()
    {
        $bs = $this->bs;
        $title = "title&é\"'(-è_çà)=";
        $desc = "description#{[|`\^@]}³<> ¹¡÷׿&é\"'(-è\\_çà)=";
        $tag1 = "#{|`^@]³¹¡¿<&é\"'(-è\\_çà)";
        $tag2 = "&é\"'(-è.[?./§!_çà)";

        $uid = $this->addUser();
        $bid = $bs->addBookmark(
            'http://site1.com', $title, $desc, 'note',
            0, array($tag1, $tag2),
            null, false, false, $uid
        );

        $bookmarks = $bs->getBookmarks(0, 1);

        $b0 = $bookmarks['bookmarks'][0];
        $this->assertEquals($title, $b0['bTitle']);
        $this->assertEquals($desc, $b0['bDescription']);
        $this->assertEquals(
            str_replace(array('"', '\'', '/'), "_", $tag1),
            $b0['tags'][0]
        );
        $this->assertEquals(
            str_replace(array('"', '\'', '/'), "_", $tag2),
            $b0['tags'][1]
        );
    }

    public function testUnificationOfBookmarks()
    {
        $bs = $this->bs;

        $uid  = $this->addUser();
        $uid2 = $this->addUser();

        $bs->addBookmark(
            'http://site1.com', "title", "description", 'note',
            0, array('tag1'), null, false, false,
            $uid
        );
        $bs->addBookmark(
            "http://site1.com", "title2", "description2", 'note',
            0, array('tag2'), null, false, false,
            $uid2
        );

        $bookmarks = $bs->getBookmarks();
        $this->assertEquals(1, $bookmarks['total']);
    }

    /*public function testSearchingBookmarksAccentsInsensible()
     {
     $bs = $this->bs;

     $bs->addBookmark("http://site1.com", "title", "éèüaàê", "status", array('tag1'), null, false, false, 1);
     $bookmarks =& $bs->getBookmarks(0, NULL, NULL, NULL, $terms = "eeaae"); //void
     $this->assertEquals(0, $bookmarks['total']);
     $bookmarks =& $bs->getBookmarks(0, NULL, NULL, NULL, $terms = "eeuaae");
     $this->assertEquals(1, $bookmarks['total']);
     }*/



    /**
     * Test if countBookmarks() works with no bookmarks
     *
     * @return void
     */
    public function testCountBookmarksNone()
    {
        $uid = $this->addUser();
        $this->assertEquals(0, $this->bs->countBookmarks($uid));
        $this->assertEquals(0, $this->bs->countBookmarks($uid, 'public'));
        $this->assertEquals(0, $this->bs->countBookmarks($uid, 'private'));
        $this->assertEquals(0, $this->bs->countBookmarks($uid, 'shared'));
        $this->assertEquals(0, $this->bs->countBookmarks($uid, 'all'));
    }



    /**
     * Test if countBookmarks() works with one public bookmark
     *
     * @return void
     */
    public function testCountBookmarksOnePublic()
    {
        $uid = $this->addUser();
        $this->addBookmark($uid);
        $this->assertEquals(1, $this->bs->countBookmarks($uid));
        $this->assertEquals(1, $this->bs->countBookmarks($uid, 'public'));
        $this->assertEquals(0, $this->bs->countBookmarks($uid, 'private'));
        $this->assertEquals(0, $this->bs->countBookmarks($uid, 'shared'));
        $this->assertEquals(1, $this->bs->countBookmarks($uid, 'all'));
    }



    /**
     * Test if countBookmarks() works with one private bookmark
     *
     * @return void
     */
    public function testCountBookmarksOnePrivate()
    {
        $uid = $this->addUser();
        $this->bs->addBookmark(
            'http://test', 'test', 'desc', 'note',
            2,//private
            array(), null, false, false, $uid
        );
        $this->assertEquals(0, $this->bs->countBookmarks($uid));
        $this->assertEquals(0, $this->bs->countBookmarks($uid, 'public'));
        $this->assertEquals(1, $this->bs->countBookmarks($uid, 'private'));
        $this->assertEquals(0, $this->bs->countBookmarks($uid, 'shared'));
        $this->assertEquals(1, $this->bs->countBookmarks($uid, 'all'));
    }



    /**
     * Test if countBookmarks() works with one shared bookmark
     *
     * @return void
     */
    public function testCountBookmarksOneShared()
    {
        $uid = $this->addUser();
        $this->bs->addBookmark(
            'http://test', 'test', 'desc', 'note',
            1,//shared
            array(), null, false, false, $uid
        );
        $this->assertEquals(0, $this->bs->countBookmarks($uid));
        $this->assertEquals(0, $this->bs->countBookmarks($uid, 'public'));
        $this->assertEquals(0, $this->bs->countBookmarks($uid, 'private'));
        $this->assertEquals(1, $this->bs->countBookmarks($uid, 'shared'));
        $this->assertEquals(1, $this->bs->countBookmarks($uid, 'all'));
    }



    /**
     * Test if deleting a bookmark works.
     *
     * @return void
     */
    public function testDeleteBookmark()
    {
        $bookmarks = $this->bs->getBookmarks();
        $this->assertEquals(0, $bookmarks['total']);

        $bid = $this->addBookmark();
        $bookmarks = $this->bs->getBookmarks();
        $this->assertEquals(1, $bookmarks['total']);

        $bid2 = $this->addBookmark();
        $bookmarks = $this->bs->getBookmarks();
        $this->assertEquals(2, $bookmarks['total']);

        $this->assertTrue($this->bs->deleteBookmark($bid));
        $bookmarks = $this->bs->getBookmarks();
        $this->assertEquals(1, $bookmarks['total']);

        $this->assertTrue($this->bs->deleteBookmark($bid2));
        $bookmarks = $this->bs->getBookmarks();
        $this->assertEquals(0, $bookmarks['total']);
    }



    /**
     * Test if deleting a bookmark with a vote works.
     *
     * @return void
     */
    public function testDeleteBookmarkWithVote()
    {
        $GLOBALS['enableVoting'] = true;

        $uid = $this->addUser();
        $bid = $this->addBookmark();

        $bid = $this->addBookmark();
        $this->vs->vote($bid, $uid, 1);
        $this->assertTrue($this->vs->hasVoted($bid, $uid));

        $bid2 = $this->addBookmark();
        $this->vs->vote($bid2, $uid, 1);
        $this->assertTrue($this->vs->hasVoted($bid2, $uid));

        $this->assertTrue($this->bs->deleteBookmark($bid));
        $this->assertFalse($this->vs->hasVoted($bid, $uid));
        $this->assertTrue($this->vs->hasVoted($bid2, $uid));
    }



    /**
     * Test if editAllowed() returns false when the bookmark
     * id is invalid.
     *
     * @return void
     */
    public function testEditAllowedInvalidBookmarkId()
    {
        $this->assertFalse($this->bs->editAllowed('invalid'));
        $this->assertFalse($this->bs->editAllowed(array()));
        $this->assertFalse($this->bs->editAllowed(array('some', 'where')));
        $this->assertFalse($this->bs->editAllowed(array('bId' => false)));
        $this->assertFalse($this->bs->editAllowed(array('bId' => 'foo')));
    }



    /**
     * Test if editAllowed() works when passing the ID of
     * an existing bookmark.
     *
     * @return void
     */
    public function testEditAllowedBookmarkId()
    {
        $uid = $this->addUser();
        $bid = $this->addBookmark($uid);
        $this->us->setCurrentUserId($uid);
        $this->assertTrue($this->bs->editAllowed($bid));
    }



    /**
     * Test if editAllowed() works when passing the ID of
     * an existing bookmark that does not belong to the current
     * user.
     *
     * @return void
     */
    public function testEditAllowedBookmarkIdNotOwn()
    {
        $uid = $this->addUser();
        $bid = $this->addBookmark();
        $this->us->setCurrentUserId($uid);
        $this->assertFalse($this->bs->editAllowed($bid));
    }



    /**
     * Test if editAllowed() works when passing the ID of
     * an existing bookmark that does not belong to the current
     * user.
     *
     * @return void
     */
    public function testEditAllowedBookmarkIdNoUser()
    {
        $bid = $this->addBookmark();
        $this->us->setCurrentUserId(null);
        $this->assertFalse($this->bs->editAllowed($bid));
    }



    /**
     * Test if editAllowed() works when passing a bookmark
     * row.
     *
     * @return void
     */
    public function testEditAllowedBookmarkRow()
    {
        $uid = $this->addUser();
        $this->us->setCurrentUserId($uid);

        $bid = $this->addBookmark($uid);
        $bookmark = $this->bs->getBookmark($bid);
        $this->assertTrue($this->bs->editAllowed($bookmark));
    }



    /**
     * Test if editAllowed() returns false when the bookmark
     * specified by the ID does not exist.
     *
     * @return void
     */
    public function testEditAllowedIdNotFound()
    {
        $this->assertFalse($this->bs->editAllowed(98765));
    }



    /**
     * Test if editAllowed() works when the user is an administrator.
     *
     * @return void
     */
    public function testEditAllowedBookmarkAdmin()
    {
        //make the user admin
        $uid = $this->addUser();
        $user = $this->us->getUser($uid);
        $GLOBALS['admin_users'][] = $user['username'];

        $bid = $this->addBookmark($uid);
        $this->us->setCurrentUserId($uid);
        $this->assertTrue($this->bs->editAllowed($bid));
    }



    /**
     * Verify that getBookmark() returns false when the
     * bookmark cannot be found.
     *
     * @return void
     */
    public function testGetBookmarkNotFound()
    {
        $this->assertFalse($this->bs->getBookmark(987654));
    }



    /**
     * Verify that getBookmark() returns false when the
     * bookmark ID is not numeric
     *
     * @return void
     */
    public function testGetBookmarkInvalidParam()
    {
        $this->assertFalse($this->bs->getBookmark('foo'));
    }



    /**
     * Check tag loading functionality of getBookmark()
     *
     * @return void
     */
    public function testGetBookmarkIncludeTags()
    {
        $uid = $this->addUser();
        $bid = $this->addBookmark($uid);
        $this->b2ts->attachTags($bid, array('foo', 'bar'));
        $bid2 = $this->addBookmark($uid);
        $this->b2ts->attachTags($bid2, array('fuu', 'baz'));

        $bm = $this->bs->getBookmark($bid, true);
        $this->assertArrayHasKey('tags', $bm);
        $this->assertType('array', $bm['tags']);
        $this->assertContains('foo', $bm['tags']);
        $this->assertContains('bar', $bm['tags']);
    }



    /**
     * Verify that getBookmark() does not include user voting
     * data when no user is logged on.
     *
     * @return void
     */
    public function testGetBookmarkUserVotingNoUser()
    {
        $GLOBALS['enableVoting'] = true;

        $uid = $this->addUser();
        $bid = $this->addBookmark($uid);
        //no user
        $this->us->setCurrentUserId(null);

        $bm = $this->bs->getBookmark($bid);
        $this->assertArrayNotHasKey('hasVoted', $bm);
        $this->assertArrayNotHasKey('vote', $bm);
    }



    /**
     * Verify that getBookmark() automatically includes
     * voting data of the currently logged on user,
     * even if he did not vote yet.
     *
     * @return void
     */
    public function testGetBookmarkUserVotingWithUserNoVote()
    {
        $GLOBALS['enableVoting'] = true;

        $uid = $this->addUser();
        $bid = $this->addBookmark($uid);
        //log user in
        $this->us->setCurrentUserId($uid);

        $bm = $this->bs->getBookmark($bid);
        $this->assertArrayHasKey('hasVoted', $bm);
        $this->assertArrayHasKey('vote', $bm);
        $this->assertEquals(0, $bm['hasVoted']);
        $this->assertEquals(null, $bm['vote']);
    }



    /**
     * Verify that getBookmark() automatically includes
     * voting data of the currently logged on user
     * when he voted positive.
     *
     * @return void
     */
    public function testGetBookmarkUserVotingWithUserPositiveVote()
    {
        $GLOBALS['enableVoting'] = true;

        $uid = $this->addUser();
        $bid = $this->addBookmark($uid);
        //log user in
        $this->us->setCurrentUserId($uid);
        $this->assertTrue($this->vs->vote($bid, $uid, 1));

        $bm = $this->bs->getBookmark($bid);
        $this->assertArrayHasKey('hasVoted', $bm);
        $this->assertArrayHasKey('vote', $bm);
        $this->assertEquals(1, $bm['hasVoted']);
        $this->assertEquals(1, $bm['vote']);
    }



    /**
     * Verify that getBookmark() automatically includes
     * voting data of the currently logged on user
     * when he voted positive.
     *
     * @return void
     */
    public function testGetBookmarkUserVotingWithUserNegativeVote()
    {
        $GLOBALS['enableVoting'] = true;

        $uid = $this->addUser();
        $bid = $this->addBookmark($uid);
        //log user in
        $this->us->setCurrentUserId($uid);
        $this->assertTrue($this->vs->vote($bid, $uid, -1));

        $bm = $this->bs->getBookmark($bid);
        $this->assertArrayHasKey('hasVoted', $bm);
        $this->assertArrayHasKey('vote', $bm);
        $this->assertEquals(1, $bm['hasVoted']);
        $this->assertEquals(-1, $bm['vote']);
    }

}


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