summaryrefslogtreecommitdiff
path: root/tests/tag2TagTest.php
blob: 3a8466facae411ddeddee18b0baf5bcf6d2d03df (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
<?php
require_once 'PHPUnit/Framework.php';

/*
To launch this test, type the following line into a shell
at the root of the scuttlePlus directory :
     phpunit Tag2TagTest tests/tag2TagTest.php
*/

class Tag2TagTest extends PHPUnit_Framework_TestCase
{
    protected $us;
    protected $bs;
    protected $ts;
    protected $tts;
 
    protected function setUp()
    {
        global $dbhost, $dbuser, $dbpass, $dbname, $dbport, $dbpersist, $dbtype, $tableprefix;
	require_once('./header.inc.php');

	$this->us =& ServiceFactory::getServiceInstance('UserService');
	$this->bs =& ServiceFactory::getServiceInstance('BookmarkService');
	$this->bs->deleteAll();
	$this->ts =& ServiceFactory::getServiceInstance('TagService');
	$this->ts->deleteAll();
	$this->tts =& ServiceFactory::getServiceInstance('Tag2TagService');
	$this->tts->deleteAll(); 
    }
 
    public function testManipulateTag2TagRelations()
    {
	$tts = $this->tts;

	$tts->addLinkedTags('a', 'b', '>', 1);
	$tts->addLinkedTags('a', 'c', '>', 1);
	$tts->addLinkedTags('a', 'd', '>', 20);
	$tts->addLinkedTags('b', 'a', '>', 1); //warning: recursive link
 	$tts->addLinkedTags('b', 'd', '>', 1);
 	$tts->addLinkedTags('d', 'e', '>', 1);
	$tts->addLinkedTags('d', 'e', '>', 20);
	$tts->addLinkedTags('f', 'g', '>', 20);

	$allLinkedTags = $tts->getAllLinkedTags('a', '>', 1, false); //as tree
	$this->assertSame(array('node'=>'a', array('node'=>'b', 'a', array('node'=>'d', 'e')), 'c'), $allLinkedTags);
	$allLinkedTags = $tts->getAllLinkedTags('a', '>', 1, true); // as flat list
	$this->assertEquals(5, sizeof($allLinkedTags));
	$this->assertTrue(in_array('a', $allLinkedTags));
	$this->assertTrue(in_array('b', $allLinkedTags));
	$this->assertTrue(in_array('c', $allLinkedTags));
	$this->assertTrue(in_array('d', $allLinkedTags));
	$this->assertTrue(in_array('e', $allLinkedTags));

	$orphewTags = $tts->getOrphewTags('>', 1);
	$this->assertEquals(0, sizeof($orphewTags));
	$orphewTags = $tts->getOrphewTags('>', 20);
	$this->assertEquals(2, sizeof($orphewTags));
	$this->assertSame('a', $orphewTags[0]['tag']);
	$this->assertSame('f', $orphewTags[1]['tag']);
	$orphewTags = $tts->getOrphewTags('>');
	$this->assertEquals(2, sizeof($orphewTags));
	$this->assertSame('f', $orphewTags[0]['tag']);
	
	$linkedTags = $tts->getLinkedTags('a', '>', 1);
	$this->assertSame(array('b', 'c'), $linkedTags);
	$tts->removeLinkedTags('a', 'b', '>', 1);
	$linkedTags = $tts->getLinkedTags('a', '>', 1);
	$this->assertSame(array('c'), $linkedTags);
	$tts->removeLinkedTags('a', 'c', '>', 1);
	$linkedTags = $tts->getLinkedTags('a', '>', 1);
	$this->assertEquals(0, sizeof($linkedTags));
	$linkedTags = $tts->getLinkedTags('a', '>');
	$this->assertSame(array('b', 'c', 'd'), $linkedTags);
    }

   public function testAddLinkedTagsThroughBookmarking()
    {
	$bs = $this->bs;
	$tags = array('a>b', 'b>c', 'a>d>e', 'a>a', 'a');
	$bs->addBookmark("http://google.com", "title", "description", "status", $tags, null, false, false, 1);
	$bookmark = $bs->getBookmarkByAddress("http://google.com");

	$ts = $this->ts;
	$savedTags = $ts->getTagsForBookmark(intval($bookmark['bId']));
	$this->assertEquals(4, sizeof($savedTags));
	$this->assertContains('b', $savedTags);
	$this->assertContains('c', $savedTags);
	$this->assertContains('e', $savedTags);
	$this->assertContains('a', $savedTags);

	$tts = $this->tts;
	$linkedTags = $tts->getLinkedTags('a', '>', 1);
	$this->assertEquals(2, sizeof($linkedTags));
	$this->assertSame('b', $linkedTags[0]['tag']);
	$this->assertSame('d', $linkedTags[1]['tag']);
	$linkedTags = $tts->getLinkedTags('b', '>', 1);
	$this->assertEquals(1, sizeof($linkedTags));
	$this->assertSame('c', $linkedTags[0]['tag']);
	$this->assertTrue($tts->existsLinkedTags('d', 'e', '>', 1));
    }

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

	$tags1 = array('aa>bb>cc', 'dd');
	$bs->addBookmark("web.com", "B1", "description", "status", $tags1, null, false, false, 1);
	$tags = array('bb>gg', 'ee>ff');
	$bs->addBookmark("web.com", "B2", "description", "status", $tags, null, false, false, 1);
	$tags = array('ee');
	$bs->addBookmark("web.com", "B3", "description", "status", $tags, null, false, false, 1);

	// Query format:
	// $bs->getBookmarks($start = 0, $perpage = NULL, $user = NULL, $tags = NULL, $terms = NULL, $sortOrder = NULL, $watched = NULL, $startdate = NULL, $enddate = NULL, $hash = NULL);

	// basic queries
	$results = $bs->getBookmarks(0, NULL, 1, 'dd');
	$this->assertSame(1, intval($results['total']));
	$this->assertSame('B1', $results['bookmarks'][0]['bTitle']);

	$results = $bs->getBookmarks(0, NULL, 1, 'cc');
	$this->assertSame(1, intval($results['total']));
	$this->assertSame('B1', $results['bookmarks'][0]['bTitle']);

	//advanced queries
	$results = $bs->getBookmarks(0, NULL, 1, 'aa');
	$this->assertSame(2, intval($results['total']));
	$this->assertSame('B1', $results['bookmarks'][0]['bTitle']);
	$this->assertSame('B2', $results['bookmarks'][1]['bTitle']);

	$results = $bs->getBookmarks(0, NULL, 1, 'ee');
	$this->assertSame(2, intval($results['total']));
	$this->assertSame('B2', $results['bookmarks'][0]['bTitle']);
	$this->assertSame('B3', $results['bookmarks'][1]['bTitle']);

	$results = $bs->getBookmarks(0, NULL, 1, 'aa+ee');
	$this->assertSame(1, intval($results['total']));
	$this->assertSame('B2', $results['bookmarks'][0]['bTitle']);

    }

}
?>