|
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
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
function testAddBookmarkWithDate() { |
|
|
|
|
|
|
25
|
|
|
$added = 1143823532; |
|
26
|
|
|
$this->cleanDB(); |
|
27
|
|
|
$this->assertCount(0, Bookmarks::findBookmarks($this->userid, $this->db, 0, 'id', array(), true, -1)); |
|
28
|
|
|
$id = Bookmarks::addBookmark($this->userid, $this->db, 'http://owncloud.org', 'Owncloud project', array('oc', 'cloud'), 'An Awesome project', $added); |
|
|
|
|
|
|
29
|
|
|
$this->assertCount(1, Bookmarks::findBookmarks($this->userid, $this->db, 0, 'id', array(), true, -1)); |
|
30
|
|
|
$bookmark = Bookmarks::findUniqueBookmark($id, $this->userid, $this->db); |
|
31
|
|
|
$this->assertEquals($added, $bookmark['added']); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
function testFindBookmarks() { |
|
|
|
|
|
|
35
|
|
|
$this->cleanDB(); |
|
36
|
|
|
Bookmarks::addBookmark($this->userid, $this->db, "http://www.google.de", "Google", array("one"), "PrivateNoTag", false); |
|
37
|
|
|
Bookmarks::addBookmark($this->userid, $this->db, "http://www.heise.de", "Heise", array("one", "two"), "PrivatTag", false); |
|
38
|
|
|
Bookmarks::addBookmark($this->userid, $this->db, "http://www.golem.de", "Golem", array("one"), "PublicNoTag", true); |
|
39
|
|
|
Bookmarks::addBookmark($this->userid, $this->db, "http://www.9gag.com", "9gag", array("two", "three"), "PublicTag", true); |
|
40
|
|
|
$outputPrivate = Bookmarks::findBookmarks($this->userid, $this->db, 0, "", array(), true, -1, false); |
|
41
|
|
|
$this->assertCount(4, $outputPrivate); |
|
42
|
|
|
$outputPrivateFiltered = Bookmarks::findBookmarks($this->userid, $this->db, 0, "", array("one"), true, -1, false); |
|
43
|
|
|
$this->assertCount(3, $outputPrivateFiltered); |
|
44
|
|
|
$outputPublic = Bookmarks::findBookmarks($this->userid, $this->db, 0, "", array(), true, -1, true); |
|
45
|
|
|
$this->assertCount(2, $outputPublic); |
|
46
|
|
|
$outputPublicFiltered = Bookmarks::findBookmarks($this->userid, $this->db, 0, "", array("two"), true, -1, true); |
|
47
|
|
|
$this->assertCount(1, $outputPublicFiltered); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
function testFindBookmarksSelectAndOrFilteredTags() { |
|
|
|
|
|
|
51
|
|
|
$this->cleanDB(); |
|
52
|
|
|
$secondUser = $this->userid . "andHisClone435"; |
|
53
|
|
|
Bookmarks::addBookmark($this->userid, $this->db, "http://www.google.de", "Google", array("one"), "PrivateNoTag", false); |
|
54
|
|
|
Bookmarks::addBookmark($this->userid, $this->db, "http://www.heise.de", "Heise", array("one", "two"), "PrivatTag", false); |
|
55
|
|
|
Bookmarks::addBookmark($this->userid, $this->db, "http://www.golem.de", "Golem", array("four"), "PublicNoTag", true); |
|
56
|
|
|
Bookmarks::addBookmark($this->userid, $this->db, "http://www.9gag.com", "9gag", array("two", "three"), "PublicTag", true); |
|
57
|
|
|
Bookmarks::addBookmark($secondUser, $this->db, "http://www.google.de", "Google", array("one"), "PrivateNoTag", false); |
|
58
|
|
|
Bookmarks::addBookmark($secondUser, $this->db, "http://www.heise.de", "Heise", array("one", "two"), "PrivatTag", false); |
|
59
|
|
|
Bookmarks::addBookmark($secondUser, $this->db, "http://www.golem.de", "Golem", array("four"), "PublicNoTag", true); |
|
60
|
|
|
Bookmarks::addBookmark($secondUser, $this->db, "http://www.9gag.com", "9gag", array("two", "three"), "PublicTag", true); |
|
61
|
|
|
$resultSetOne = Bookmarks::findBookmarks($this->userid, $this->db, 0, 'lastmodified', array('one', 'three'), true, -1, false, array('url', 'title'), 'or'); |
|
62
|
|
|
$this->assertEquals(3, count($resultSetOne)); |
|
63
|
|
|
$resultOne = $resultSetOne[0]; |
|
64
|
|
|
$this->assertFalse(isset($resultOne['lastmodified'])); |
|
65
|
|
|
$this->assertFalse(isset($resultOne['tags'])); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
function testFindTags() { |
|
|
|
|
|
|
69
|
|
|
$this->cleanDB(); |
|
70
|
|
|
$this->assertEquals(Bookmarks::findTags($this->userid, $this->db), array()); |
|
71
|
|
|
Bookmarks::addBookmark($this->userid, $this->db, 'http://owncloud.org', 'Owncloud project', array('oc', 'cloud'), 'An Awesome project'); |
|
72
|
|
|
$this->assertEquals(array(0 => array('tag' => 'cloud', 'nbr' => 1), 1 => array('tag' => 'oc', 'nbr' => 1)), Bookmarks::findTags($this->userid, $this->db)); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
function testFindUniqueBookmark() { |
|
|
|
|
|
|
76
|
|
|
$this->cleanDB(); |
|
77
|
|
|
$id = Bookmarks::addBookmark($this->userid, $this->db, "http://www.heise.de", "Heise", array("one", "two"), "PrivatTag", false); |
|
78
|
|
|
$bookmark = Bookmarks::findUniqueBookmark($id, $this->userid, $this->db); |
|
79
|
|
|
$this->assertEquals($id, $bookmark['id']); |
|
80
|
|
|
$this->assertEquals("Heise", $bookmark['title']); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
function testEditBookmark() { |
|
|
|
|
|
|
84
|
|
|
$this->cleanDB(); |
|
85
|
|
|
$id = Bookmarks::addBookmark($this->userid, $this->db, "http://www.heise.de", "Heise", array("one", "two"), "PrivatTag", false); |
|
86
|
|
|
Bookmarks::editBookmark($this->userid, $this->db, $id, "http://www.google.de", "NewTitle", array("three")); |
|
87
|
|
|
$bookmark = Bookmarks::findUniqueBookmark($id, $this->userid, $this->db); |
|
88
|
|
|
$this->assertEquals("NewTitle", $bookmark['title']); |
|
89
|
|
|
$this->assertEquals("http://www.google.de", $bookmark['url']); |
|
90
|
|
|
$this->assertEquals(1, count($bookmark['tags'])); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
function testDeleteBookmark() { |
|
|
|
|
|
|
94
|
|
|
$this->cleanDB(); |
|
95
|
|
|
Bookmarks::addBookmark($this->userid, $this->db, "http://www.google.de", "Google", array("one"), "PrivateNoTag", false); |
|
96
|
|
|
$id = Bookmarks::addBookmark($this->userid, $this->db, "http://www.heise.de", "Heise", array("one", "two"), "PrivatTag", false); |
|
97
|
|
|
$this->assertNotEquals(false, Bookmarks::bookmarkExists("http://www.google.de", $this->userid, $this->db)); |
|
98
|
|
|
$this->assertNotEquals(false, Bookmarks::bookmarkExists("http://www.heise.de", $this->userid, $this->db)); |
|
99
|
|
|
Bookmarks::deleteUrl($this->userid, $this->db, $id); |
|
100
|
|
|
$this->assertFalse(Bookmarks::bookmarkExists("http://www.heise.de", $this->userid, $this->db)); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
function testGetURLMetadata() { |
|
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
$config = $this->getMockBuilder('\OCP\IConfig') |
|
106
|
|
|
->disableOriginalConstructor()->getMock(); |
|
107
|
|
|
$clientService = $this->getMock('OCP\Http\Client\IClientService'); |
|
108
|
|
|
$httpHelperMock = $this->getMockBuilder('\OC\HTTPHelper') |
|
109
|
|
|
->setConstructorArgs(array($config, $clientService)) |
|
110
|
|
|
->getMock(); |
|
111
|
|
|
$returnAmazonDe = file_get_contents(__DIR__ . '/res/amazonHtml.file'); |
|
112
|
|
|
$returnGolemDe = file_get_contents(__DIR__ . '/res/golemHtml.file'); |
|
113
|
|
|
$httpHelperMock->expects($this->any())->method('getUrlContent')->with($this->anything())->will($this->onConsecutiveCalls($returnAmazonDe, $returnGolemDe)); |
|
114
|
|
|
$this->registerHttpHelper($httpHelperMock); |
|
115
|
|
|
|
|
116
|
|
|
$metadataAmazon = Bookmarks::getURLMetadata('amazonHtml'); |
|
117
|
|
|
$this->assertTrue($metadataAmazon['url'] == 'amazonHtml'); |
|
118
|
|
|
$this->assertTrue(strpos($metadataAmazon['title'], 'ü') !== false); |
|
119
|
|
|
|
|
120
|
|
|
$metadataGolem = Bookmarks::getURLMetadata('golemHtml'); |
|
121
|
|
|
$this->assertTrue($metadataGolem['url'] == 'golemHtml'); |
|
122
|
|
|
$this->assertTrue(strpos($metadataGolem['title'], 'für') == false); |
|
|
|
|
|
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
protected function tearDown() { |
|
126
|
|
|
$this->cleanDB(); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
function cleanDB() { |
|
|
|
|
|
|
130
|
|
|
$query1 = OC_DB::prepare('DELETE FROM *PREFIX*bookmarks'); |
|
131
|
|
|
$query1->execute(); |
|
132
|
|
|
$query2 = OC_DB::prepare('DELETE FROM *PREFIX*bookmarks_tags'); |
|
133
|
|
|
$query2->execute(); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Register an http helper mock for testing purposes. |
|
138
|
|
|
* @param $httpHelper http helper mock |
|
139
|
|
|
*/ |
|
140
|
|
|
private function registerHttpHelper($httpHelper) { |
|
141
|
|
|
$this->oldHttpHelper = \OC::$server->query('HTTPHelper'); |
|
142
|
|
|
\OC::$server->registerService('HTTPHelper', function () use ($httpHelper) { |
|
143
|
|
|
return $httpHelper; |
|
144
|
|
|
}); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
} |
|
148
|
|
|
|
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.