1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
OC_App::loadApp('bookmarks'); |
4
|
|
|
|
5
|
|
|
use \OCA\Bookmarks\Controller\Lib\Bookmarks; |
6
|
|
|
|
7
|
|
|
class Test_LibBookmarks_Bookmarks extends PHPUnit_Framework_TestCase { |
8
|
|
|
|
9
|
|
|
private $userid; |
10
|
|
|
private $db; |
11
|
|
|
|
12
|
|
|
protected function setUp() { |
13
|
|
|
$this->userid = \OCP\User::getUser(); |
14
|
|
|
$this->db = \OC::$server->getDb(); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
function testAddBookmark() { |
|
|
|
|
18
|
|
|
$this->cleanDB(); |
19
|
|
|
$this->assertCount(0, Bookmarks::findBookmarks($this->userid, $this->db, 0, 'id', array(), true, -1)); |
20
|
|
|
Bookmarks::addBookmark($this->userid, $this->db, 'http://owncloud.org', 'owncloud project', array('oc', 'cloud'), 'An Awesome project'); |
21
|
|
|
$this->assertCount(1, Bookmarks::findBookmarks($this->userid, $this->db, 0, 'id', array(), true, -1)); |
22
|
|
|
Bookmarks::addBookmark($this->userid, $this->db, 'http://de.wikipedia.org/Ü', 'Das Ü', array('encyclopedia', 'lang'), 'A terrific letter'); |
23
|
|
|
$this->assertCount(2, Bookmarks::findBookmarks($this->userid, $this->db, 0, 'id', array(), true, -1)); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
function testFindBookmarks() { |
|
|
|
|
27
|
|
|
$this->cleanDB(); |
28
|
|
|
Bookmarks::addBookmark($this->userid, $this->db, "http://www.google.de", "Google", array("one"), "PrivateNoTag", false); |
29
|
|
|
Bookmarks::addBookmark($this->userid, $this->db, "http://www.heise.de", "Heise", array("one", "two"), "PrivatTag", false); |
30
|
|
|
Bookmarks::addBookmark($this->userid, $this->db, "http://www.golem.de", "Golem", array("one"), "PublicNoTag", true); |
31
|
|
|
Bookmarks::addBookmark($this->userid, $this->db, "http://www.9gag.com", "9gag", array("two", "three"), "PublicTag", true); |
32
|
|
|
$outputPrivate = Bookmarks::findBookmarks($this->userid, $this->db, 0, "", array(), true, -1, false); |
33
|
|
|
$this->assertCount(4, $outputPrivate); |
34
|
|
|
$outputPrivateFiltered = Bookmarks::findBookmarks($this->userid, $this->db, 0, "", array("one"), true, -1, false); |
35
|
|
|
$this->assertCount(3, $outputPrivateFiltered); |
36
|
|
|
$outputPublic = Bookmarks::findBookmarks($this->userid, $this->db, 0, "", array(), true, -1, true); |
37
|
|
|
$this->assertCount(2, $outputPublic); |
38
|
|
|
$outputPublicFiltered = Bookmarks::findBookmarks($this->userid, $this->db, 0, "", array("two"), true, -1, true); |
39
|
|
|
$this->assertCount(1, $outputPublicFiltered); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
function testFindBookmarksSelectAndOrFilteredTags() { |
|
|
|
|
43
|
|
|
$this->cleanDB(); |
44
|
|
|
$secondUser = $this->userid . "andHisClone435"; |
45
|
|
|
Bookmarks::addBookmark($this->userid, $this->db, "http://www.google.de", "Google", array("one"), "PrivateNoTag", false); |
46
|
|
|
Bookmarks::addBookmark($this->userid, $this->db, "http://www.heise.de", "Heise", array("one", "two"), "PrivatTag", false); |
47
|
|
|
Bookmarks::addBookmark($this->userid, $this->db, "http://www.golem.de", "Golem", array("four"), "PublicNoTag", true); |
48
|
|
|
Bookmarks::addBookmark($this->userid, $this->db, "http://www.9gag.com", "9gag", array("two", "three"), "PublicTag", true); |
49
|
|
|
Bookmarks::addBookmark($secondUser, $this->db, "http://www.google.de", "Google", array("one"), "PrivateNoTag", false); |
50
|
|
|
Bookmarks::addBookmark($secondUser, $this->db, "http://www.heise.de", "Heise", array("one", "two"), "PrivatTag", false); |
51
|
|
|
Bookmarks::addBookmark($secondUser, $this->db, "http://www.golem.de", "Golem", array("four"), "PublicNoTag", true); |
52
|
|
|
Bookmarks::addBookmark($secondUser, $this->db, "http://www.9gag.com", "9gag", array("two", "three"), "PublicTag", true); |
53
|
|
|
$resultSetOne = Bookmarks::findBookmarks($this->userid, $this->db, 0, 'lastmodified', array('one', 'three'), true, -1, false, array('url', 'title'), 'or'); |
54
|
|
|
$this->assertEquals(3, count($resultSetOne)); |
55
|
|
|
$resultOne = $resultSetOne[0]; |
56
|
|
|
$this->assertFalse(isset($resultOne['lastmodified'])); |
57
|
|
|
$this->assertFalse(isset($resultOne['tags'])); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
function testFindTags() { |
|
|
|
|
61
|
|
|
$this->cleanDB(); |
62
|
|
|
$this->assertEquals(Bookmarks::findTags($this->userid, $this->db), array()); |
63
|
|
|
Bookmarks::addBookmark($this->userid, $this->db, 'http://owncloud.org', 'Owncloud project', array('oc', 'cloud'), 'An Awesome project'); |
64
|
|
|
$this->assertEquals(array(0 => array('tag' => 'cloud', 'nbr' => 1), 1 => array('tag' => 'oc', 'nbr' => 1)), Bookmarks::findTags($this->userid, $this->db)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
function testFindUniqueBookmark() { |
|
|
|
|
68
|
|
|
$this->cleanDB(); |
69
|
|
|
$id = Bookmarks::addBookmark($this->userid, $this->db, "http://www.heise.de", "Heise", array("one", "two"), "PrivatTag", false); |
70
|
|
|
$bookmark = Bookmarks::findUniqueBookmark($id, $this->userid, $this->db); |
71
|
|
|
$this->assertEquals($id, $bookmark['id']); |
72
|
|
|
$this->assertEquals("Heise", $bookmark['title']); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
function testEditBookmark() { |
|
|
|
|
76
|
|
|
$this->cleanDB(); |
77
|
|
|
$id = Bookmarks::addBookmark($this->userid, $this->db, "http://www.heise.de", "Heise", array("one", "two"), "PrivatTag", false); |
78
|
|
|
Bookmarks::editBookmark($this->userid, $this->db, $id, "http://www.google.de", "NewTitle", array("three")); |
79
|
|
|
$bookmark = Bookmarks::findUniqueBookmark($id, $this->userid, $this->db); |
80
|
|
|
$this->assertEquals("NewTitle", $bookmark['title']); |
81
|
|
|
$this->assertEquals("http://www.google.de", $bookmark['url']); |
82
|
|
|
$this->assertEquals(1, count($bookmark['tags'])); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
function testDeleteBookmark() { |
|
|
|
|
86
|
|
|
$this->cleanDB(); |
87
|
|
|
Bookmarks::addBookmark($this->userid, $this->db, "http://www.google.de", "Google", array("one"), "PrivateNoTag", false); |
88
|
|
|
$id = Bookmarks::addBookmark($this->userid, $this->db, "http://www.heise.de", "Heise", array("one", "two"), "PrivatTag", false); |
89
|
|
|
$this->assertNotEquals(false, Bookmarks::bookmarkExists("http://www.google.de", $this->userid, $this->db)); |
90
|
|
|
$this->assertNotEquals(false, Bookmarks::bookmarkExists("http://www.heise.de", $this->userid, $this->db)); |
91
|
|
|
Bookmarks::deleteUrl($this->userid, $this->db, $id); |
92
|
|
|
$this->assertFalse(Bookmarks::bookmarkExists("http://www.heise.de", $this->userid, $this->db)); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
function testGetURLMetadata() { |
|
|
|
|
96
|
|
|
|
97
|
|
|
$config = $this->getMockBuilder('\OCP\IConfig') |
|
|
|
|
98
|
|
|
->disableOriginalConstructor()->getMock(); |
99
|
|
|
$amazonResponse = $this->getMock('OCP\Http\Client\IResponse'); |
100
|
|
|
$amazonResponse->expects($this->once()) |
101
|
|
|
->method('getBody') |
102
|
|
|
->will($this->returnValue(file_get_contents(__DIR__ . '/res/amazonHtml.file'))); |
103
|
|
|
$amazonResponse->expects($this->once()) |
104
|
|
|
->method('getHeader') |
105
|
|
|
->with('Content-Type') |
106
|
|
|
->will($this->returnValue('')); |
107
|
|
|
|
108
|
|
|
$golemResponse = $this->getMock('OCP\Http\Client\IResponse'); |
109
|
|
|
$golemResponse->expects($this->once()) |
110
|
|
|
->method('getBody') |
111
|
|
|
->will($this->returnValue(file_get_contents(__DIR__ . '/res/golemHtml.file'))); |
112
|
|
|
$golemResponse->expects($this->once()) |
113
|
|
|
->method('getHeader') |
114
|
|
|
->with('Content-Type') |
115
|
|
|
->will($this->returnValue('text/html; charset=UTF-8')); |
116
|
|
|
|
117
|
|
|
$clientMock = $this->getMock('OCP\Http\Client\IClient'); |
118
|
|
|
$clientMock->expects($this->exactly(2)) |
119
|
|
|
->method('get') |
120
|
|
|
->will($this->returnCallback(function ($page) use($amazonResponse, $golemResponse) { |
121
|
|
|
if($page === 'amazonHtml') { |
122
|
|
|
return $amazonResponse; |
123
|
|
|
} else if($page === 'golemHtml') { |
124
|
|
|
return $golemResponse; |
125
|
|
|
} |
126
|
|
|
})); |
127
|
|
|
|
128
|
|
|
$clientServiceMock = $this->getMock('OCP\Http\Client\IClientService'); |
129
|
|
|
$clientServiceMock->expects($this->any()) |
130
|
|
|
->method('newClient') |
131
|
|
|
->will($this->returnValue($clientMock)); |
132
|
|
|
|
133
|
|
|
$this->registerHttpService($clientServiceMock); |
134
|
|
|
|
135
|
|
|
$metadataAmazon = Bookmarks::getURLMetadata('amazonHtml'); |
136
|
|
|
$this->assertTrue($metadataAmazon['url'] == 'amazonHtml'); |
137
|
|
|
$this->assertTrue(strpos($metadataAmazon['title'], 'ü') !== false); |
138
|
|
|
|
139
|
|
|
$metadataGolem = Bookmarks::getURLMetadata('golemHtml'); |
140
|
|
|
$this->assertTrue($metadataGolem['url'] == 'golemHtml'); |
141
|
|
|
$this->assertTrue(strpos($metadataGolem['title'], 'für') == false); |
|
|
|
|
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
protected function tearDown() { |
145
|
|
|
$this->cleanDB(); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
function cleanDB() { |
|
|
|
|
149
|
|
|
$query1 = OC_DB::prepare('DELETE FROM *PREFIX*bookmarks'); |
150
|
|
|
$query1->execute(); |
151
|
|
|
$query2 = OC_DB::prepare('DELETE FROM *PREFIX*bookmarks_tags'); |
152
|
|
|
$query2->execute(); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Register an http service mock for testing purposes. |
157
|
|
|
* |
158
|
|
|
* @param \OCP\Http\Client\IClientService $service |
159
|
|
|
*/ |
160
|
|
|
private function registerHttpService($service) { |
161
|
|
|
\OC::$server->registerService('HttpClientService', function () use ($service) { |
162
|
|
|
return $service; |
163
|
|
|
}); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
} |
167
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.