aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/utils/ReferenceLinkDB.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests/utils/ReferenceLinkDB.php')
-rw-r--r--tests/utils/ReferenceLinkDB.php119
1 files changed, 70 insertions, 49 deletions
diff --git a/tests/utils/ReferenceLinkDB.php b/tests/utils/ReferenceLinkDB.php
index c12bcb67..fc3cb109 100644
--- a/tests/utils/ReferenceLinkDB.php
+++ b/tests/utils/ReferenceLinkDB.php
@@ -1,30 +1,39 @@
1<?php 1<?php
2 2
3use Shaarli\Bookmark\LinkDB; 3use Shaarli\Bookmark\Bookmark;
4use Shaarli\Bookmark\BookmarkArray;
4 5
5/** 6/**
6 * Populates a reference datastore to test LinkDB 7 * Populates a reference datastore to test Bookmark
7 */ 8 */
8class ReferenceLinkDB 9class ReferenceLinkDB
9{ 10{
10 public static $NB_LINKS_TOTAL = 11; 11 public static $NB_LINKS_TOTAL = 11;
11 12
12 private $_links = array(); 13 private $bookmarks = array();
13 private $_publicCount = 0; 14 private $_publicCount = 0;
14 private $_privateCount = 0; 15 private $_privateCount = 0;
15 16
17 private $isLegacy;
18
16 /** 19 /**
17 * Populates the test DB with reference data 20 * Populates the test DB with reference data
21 *
22 * @param bool $isLegacy Use links as array instead of Bookmark object
18 */ 23 */
19 public function __construct() 24 public function __construct($isLegacy = false)
20 { 25 {
26 $this->isLegacy = $isLegacy;
27 if (! $this->isLegacy) {
28 $this->bookmarks = new BookmarkArray();
29 }
21 $this->addLink( 30 $this->addLink(
22 11, 31 11,
23 'Pined older', 32 'Pined older',
24 '?PCRizQ', 33 '/shaare/PCRizQ',
25 'This is an older pinned link', 34 'This is an older pinned link',
26 0, 35 0,
27 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20100309_101010'), 36 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20100309_101010'),
28 '', 37 '',
29 null, 38 null,
30 'PCRizQ', 39 'PCRizQ',
@@ -34,10 +43,10 @@ class ReferenceLinkDB
34 $this->addLink( 43 $this->addLink(
35 10, 44 10,
36 'Pined', 45 'Pined',
37 '?0gCTjQ', 46 '/shaare/0gCTjQ',
38 'This is a pinned link', 47 'This is a pinned link',
39 0, 48 0,
40 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121207_152312'), 49 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20121207_152312'),
41 '', 50 '',
42 null, 51 null,
43 '0gCTjQ', 52 '0gCTjQ',
@@ -47,10 +56,10 @@ class ReferenceLinkDB
47 $this->addLink( 56 $this->addLink(
48 41, 57 41,
49 'Link title: @website', 58 'Link title: @website',
50 '?WDWyig', 59 '/shaare/WDWyig',
51 'Stallman has a beard and is part of the Free Software Foundation (or not). Seriously, read this. #hashtag', 60 'Stallman has a beard and is part of the Free Software Foundation (or not). Seriously, read this. #hashtag',
52 0, 61 0,
53 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651'), 62 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651'),
54 'sTuff', 63 'sTuff',
55 null, 64 null,
56 'WDWyig' 65 'WDWyig'
@@ -59,10 +68,10 @@ class ReferenceLinkDB
59 $this->addLink( 68 $this->addLink(
60 42, 69 42,
61 'Note: I have a big ID but an old date', 70 'Note: I have a big ID but an old date',
62 '?WDWyig', 71 '/shaare/WDWyig',
63 'Used to test links reordering.', 72 'Used to test bookmarks reordering.',
64 0, 73 0,
65 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20100310_101010'), 74 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20100310_101010'),
66 'ut' 75 'ut'
67 ); 76 );
68 77
@@ -72,7 +81,7 @@ class ReferenceLinkDB
72 'http://www.php-fig.org/psr/psr-2/', 81 'http://www.php-fig.org/psr/psr-2/',
73 'This guide extends and expands on PSR-1, the basic coding standard.', 82 'This guide extends and expands on PSR-1, the basic coding standard.',
74 0, 83 0,
75 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_152312'), 84 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20121206_152312'),
76 '' 85 ''
77 ); 86 );
78 87
@@ -82,9 +91,9 @@ class ReferenceLinkDB
82 'https://static.fsf.org/nosvn/faif-2.0.pdf', 91 'https://static.fsf.org/nosvn/faif-2.0.pdf',
83 'Richard Stallman and the Free Software Revolution. Read this. #hashtag', 92 'Richard Stallman and the Free Software Revolution. Read this. #hashtag',
84 0, 93 0,
85 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114633'), 94 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114633'),
86 'free gnu software stallman -exclude stuff hashtag', 95 'free gnu software stallman -exclude stuff hashtag',
87 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160803_093033') 96 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20160803_093033')
88 ); 97 );
89 98
90 $this->addLink( 99 $this->addLink(
@@ -93,9 +102,9 @@ class ReferenceLinkDB
93 'http://mediagoblin.org/', 102 'http://mediagoblin.org/',
94 'A free software media publishing platform #hashtagOther', 103 'A free software media publishing platform #hashtagOther',
95 0, 104 0,
96 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20130614_184135'), 105 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20130614_184135'),
97 'gnu media web .hidden hashtag', 106 'gnu media web .hidden hashtag',
98 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20130615_184230'), 107 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20130615_184230'),
99 'IuWvgA' 108 'IuWvgA'
100 ); 109 );
101 110
@@ -105,7 +114,7 @@ class ReferenceLinkDB
105 'https://dvcs.w3.org/hg/markup-validator/summary', 114 'https://dvcs.w3.org/hg/markup-validator/summary',
106 'Mercurial repository for the W3C Validator #private', 115 'Mercurial repository for the W3C Validator #private',
107 1, 116 1,
108 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20141125_084734'), 117 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20141125_084734'),
109 'css html w3c web Mercurial' 118 'css html w3c web Mercurial'
110 ); 119 );
111 120
@@ -115,7 +124,7 @@ class ReferenceLinkDB
115 'http://ars.userfriendly.org/cartoons/?id=20121206', 124 'http://ars.userfriendly.org/cartoons/?id=20121206',
116 'Naming conventions... #private', 125 'Naming conventions... #private',
117 0, 126 0,
118 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_142300'), 127 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20121206_142300'),
119 'dev cartoon web' 128 'dev cartoon web'
120 ); 129 );
121 130
@@ -125,7 +134,7 @@ class ReferenceLinkDB
125 'http://ars.userfriendly.org/cartoons/?id=20010306', 134 'http://ars.userfriendly.org/cartoons/?id=20010306',
126 'Tropical printing', 135 'Tropical printing',
127 0, 136 0,
128 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_172539'), 137 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20121206_172539'),
129 'samba cartoon web' 138 'samba cartoon web'
130 ); 139 );
131 140
@@ -135,7 +144,7 @@ class ReferenceLinkDB
135 'http://geek-and-poke.com/', 144 'http://geek-and-poke.com/',
136 '', 145 '',
137 1, 146 1,
138 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_182539'), 147 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20121206_182539'),
139 'dev cartoon tag1 tag2 tag3 tag4 ' 148 'dev cartoon tag1 tag2 tag3 tag4 '
140 ); 149 );
141 } 150 }
@@ -164,10 +173,15 @@ class ReferenceLinkDB
164 'tags' => $tags, 173 'tags' => $tags,
165 'created' => $date, 174 'created' => $date,
166 'updated' => $updated, 175 'updated' => $updated,
167 'shorturl' => $shorturl ? $shorturl : smallHash($date->format(LinkDB::LINK_DATE_FORMAT) . $id), 176 'shorturl' => $shorturl ? $shorturl : smallHash($date->format(Bookmark::LINK_DATE_FORMAT) . $id),
168 'sticky' => $pinned 177 'sticky' => $pinned
169 ); 178 );
170 $this->_links[$id] = $link; 179 if (! $this->isLegacy) {
180 $bookmark = new Bookmark();
181 $this->bookmarks[$id] = $bookmark->fromArray($link);
182 } else {
183 $this->bookmarks[$id] = $link;
184 }
171 185
172 if ($private) { 186 if ($private) {
173 $this->_privateCount++; 187 $this->_privateCount++;
@@ -184,37 +198,38 @@ class ReferenceLinkDB
184 $this->reorder(); 198 $this->reorder();
185 file_put_contents( 199 file_put_contents(
186 $filename, 200 $filename,
187 '<?php /* '.base64_encode(gzdeflate(serialize($this->_links))).' */ ?>' 201 '<?php /* '.base64_encode(gzdeflate(serialize($this->bookmarks))).' */ ?>'
188 ); 202 );
189 } 203 }
190 204
191 /** 205 /**
192 * Reorder links by creation date (newest first). 206 * Reorder links by creation date (newest first).
193 * 207 *
194 * Also update the urls and ids mapping arrays.
195 *
196 * @param string $order ASC|DESC 208 * @param string $order ASC|DESC
197 */ 209 */
198 public function reorder($order = 'DESC') 210 public function reorder($order = 'DESC')
199 { 211 {
200 // backward compatibility: ignore reorder if the the `created` field doesn't exist 212 if (! $this->isLegacy) {
201 if (! isset(array_values($this->_links)[0]['created'])) { 213 $this->bookmarks->reorder($order);
202 return; 214 } else {
203 } 215 $order = $order === 'ASC' ? -1 : 1;
204 216 // backward compatibility: ignore reorder if the the `created` field doesn't exist
205 $order = $order === 'ASC' ? -1 : 1; 217 if (! isset(array_values($this->bookmarks)[0]['created'])) {
206 // Reorder array by dates. 218 return;
207 usort($this->_links, function ($a, $b) use ($order) {
208 if (isset($a['sticky']) && isset($b['sticky']) && $a['sticky'] !== $b['sticky']) {
209 return $a['sticky'] ? -1 : 1;
210 } 219 }
211 220
212 return $a['created'] < $b['created'] ? 1 * $order : -1 * $order; 221 usort($this->bookmarks, function ($a, $b) use ($order) {
213 }); 222 if (isset($a['sticky']) && isset($b['sticky']) && $a['sticky'] !== $b['sticky']) {
223 return $a['sticky'] ? -1 : 1;
224 }
225
226 return $a['created'] < $b['created'] ? 1 * $order : -1 * $order;
227 });
228 }
214 } 229 }
215 230
216 /** 231 /**
217 * Returns the number of links in the reference data 232 * Returns the number of bookmarks in the reference data
218 */ 233 */
219 public function countLinks() 234 public function countLinks()
220 { 235 {
@@ -222,7 +237,7 @@ class ReferenceLinkDB
222 } 237 }
223 238
224 /** 239 /**
225 * Returns the number of public links in the reference data 240 * Returns the number of public bookmarks in the reference data
226 */ 241 */
227 public function countPublicLinks() 242 public function countPublicLinks()
228 { 243 {
@@ -230,7 +245,7 @@ class ReferenceLinkDB
230 } 245 }
231 246
232 /** 247 /**
233 * Returns the number of private links in the reference data 248 * Returns the number of private bookmarks in the reference data
234 */ 249 */
235 public function countPrivateLinks() 250 public function countPrivateLinks()
236 { 251 {
@@ -238,14 +253,20 @@ class ReferenceLinkDB
238 } 253 }
239 254
240 /** 255 /**
241 * Returns the number of links without tag 256 * Returns the number of bookmarks without tag
242 */ 257 */
243 public function countUntaggedLinks() 258 public function countUntaggedLinks()
244 { 259 {
245 $cpt = 0; 260 $cpt = 0;
246 foreach ($this->_links as $link) { 261 foreach ($this->bookmarks as $link) {
247 if (empty($link['tags'])) { 262 if (! $this->isLegacy) {
248 ++$cpt; 263 if (empty($link->getTags())) {
264 ++$cpt;
265 }
266 } else {
267 if (empty($link['tags'])) {
268 ++$cpt;
269 }
249 } 270 }
250 } 271 }
251 return $cpt; 272 return $cpt;
@@ -254,16 +275,16 @@ class ReferenceLinkDB
254 public function getLinks() 275 public function getLinks()
255 { 276 {
256 $this->reorder(); 277 $this->reorder();
257 return $this->_links; 278 return $this->bookmarks;
258 } 279 }
259 280
260 /** 281 /**
261 * Setter to override link creation. 282 * Setter to override link creation.
262 * 283 *
263 * @param array $links List of links. 284 * @param array $links List of bookmarks.
264 */ 285 */
265 public function setLinks($links) 286 public function setLinks($links)
266 { 287 {
267 $this->_links = $links; 288 $this->bookmarks = $links;
268 } 289 }
269} 290}