]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/utils/ReferenceLinkDB.php
lint: apply phpcbf to tests/
[github/shaarli/Shaarli.git] / tests / utils / ReferenceLinkDB.php
CommitLineData
ca74886f
V
1<?php
2/**
3 * Populates a reference datastore to test LinkDB
4 */
5class ReferenceLinkDB
6{
4154c25b 7 public static $NB_LINKS_TOTAL = 11;
89baf23d 8
07b6fa75
V
9 private $_links = array();
10 private $_publicCount = 0;
11 private $_privateCount = 0;
ca74886f
V
12
13 /**
14 * Populates the test DB with reference data
15 */
735ed4a9 16 public function __construct()
ca74886f 17 {
4154c25b
A
18 $this->addLink(
19 11,
20 'Pined older',
21 '?PCRizQ',
22 'This is an older pinned link',
23 0,
24 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20100309_101010'),
25 '',
26 null,
27 'PCRizQ',
28 true
29 );
30
31 $this->addLink(
32 10,
33 'Pined',
34 '?0gCTjQ',
35 'This is a pinned link',
36 0,
37 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121207_152312'),
38 '',
39 null,
40 '0gCTjQ',
41 true
42 );
43
89baf23d 44 $this->addLink(
c3dfd899 45 41,
89baf23d
A
46 'Link title: @website',
47 '?WDWyig',
9ccca401 48 'Stallman has a beard and is part of the Free Software Foundation (or not). Seriously, read this. #hashtag',
89baf23d 49 0,
d592daea 50 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651'),
c3dfd899
A
51 'sTuff',
52 null,
53 'WDWyig'
89baf23d
A
54 );
55
ca74886f 56 $this->addLink(
c3dfd899
A
57 42,
58 'Note: I have a big ID but an old date',
59 '?WDWyig',
60 'Used to test links reordering.',
61 0,
d592daea 62 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20100310_101010'),
c3dfd899
A
63 'ut'
64 );
65
7d86f40b
A
66 $this->addLink(
67 9,
68 'PSR-2: Coding Style Guide',
69 'http://www.php-fig.org/psr/psr-2/',
70 'This guide extends and expands on PSR-1, the basic coding standard.',
71 0,
72 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_152312'),
73 ''
74 );
75
c3dfd899
A
76 $this->addLink(
77 8,
522b278b 78 'Free as in Freedom 2.0 @website',
ca74886f 79 'https://static.fsf.org/nosvn/faif-2.0.pdf',
9ccca401 80 'Richard Stallman and the Free Software Revolution. Read this. #hashtag',
ca74886f 81 0,
d592daea 82 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114633'),
c6d876bb 83 'free gnu software stallman -exclude stuff hashtag',
d592daea 84 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160803_093033')
ca74886f
V
85 );
86
87 $this->addLink(
c3dfd899 88 7,
ca74886f
V
89 'MediaGoblin',
90 'http://mediagoblin.org/',
9ccca401 91 'A free software media publishing platform #hashtagOther',
ca74886f 92 0,
d592daea 93 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20130614_184135'),
c3dfd899 94 'gnu media web .hidden hashtag',
68016e37 95 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20130615_184230'),
c3dfd899 96 'IuWvgA'
ca74886f
V
97 );
98
99 $this->addLink(
c3dfd899 100 6,
ca74886f
V
101 'w3c-markup-validator',
102 'https://dvcs.w3.org/hg/markup-validator/summary',
9ccca401 103 'Mercurial repository for the W3C Validator #private',
ca74886f 104 1,
d592daea 105 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20141125_084734'),
ca74886f
V
106 'css html w3c web Mercurial'
107 );
108
109 $this->addLink(
c3dfd899 110 4,
ca74886f
V
111 'UserFriendly - Web Designer',
112 'http://ars.userfriendly.org/cartoons/?id=20121206',
9ccca401 113 'Naming conventions... #private',
ca74886f 114 0,
d592daea 115 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_142300'),
ca74886f
V
116 'dev cartoon web'
117 );
118
119 $this->addLink(
c3dfd899 120 1,
ca74886f
V
121 'UserFriendly - Samba',
122 'http://ars.userfriendly.org/cartoons/?id=20010306',
123 'Tropical printing',
124 0,
d592daea 125 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_172539'),
ca74886f
V
126 'samba cartoon web'
127 );
128
129 $this->addLink(
c3dfd899 130 0,
ca74886f
V
131 'Geek and Poke',
132 'http://geek-and-poke.com/',
133 '',
134 1,
d592daea 135 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_182539'),
9866b408 136 'dev cartoon tag1 tag2 tag3 tag4 '
ca74886f
V
137 );
138 }
139
140 /**
141 * Adds a new link
142 */
4154c25b
A
143 protected function addLink(
144 $id,
145 $title,
146 $url,
147 $description,
148 $private,
149 $date,
150 $tags,
151 $updated = '',
152 $shorturl = '',
067c2dd8
V
153 $pinned = false
154 ) {
ca74886f 155 $link = array(
c3dfd899 156 'id' => $id,
ca74886f
V
157 'title' => $title,
158 'url' => $url,
159 'description' => $description,
160 'private' => $private,
ca74886f 161 'tags' => $tags,
c3dfd899 162 'created' => $date,
c6d876bb 163 'updated' => $updated,
d592daea 164 'shorturl' => $shorturl ? $shorturl : smallHash($date->format(LinkDB::LINK_DATE_FORMAT) . $id),
4154c25b 165 'sticky' => $pinned
ca74886f 166 );
c3dfd899 167 $this->_links[$id] = $link;
ca74886f
V
168
169 if ($private) {
07b6fa75 170 $this->_privateCount++;
ca74886f
V
171 return;
172 }
07b6fa75 173 $this->_publicCount++;
ca74886f
V
174 }
175
176 /**
177 * Writes data to the datastore
178 */
9c8752a2 179 public function write($filename)
ca74886f 180 {
9ec0a611 181 $this->reorder();
ca74886f
V
182 file_put_contents(
183 $filename,
07b6fa75 184 '<?php /* '.base64_encode(gzdeflate(serialize($this->_links))).' */ ?>'
ca74886f
V
185 );
186 }
187
9ec0a611
A
188 /**
189 * Reorder links by creation date (newest first).
190 *
191 * Also update the urls and ids mapping arrays.
192 *
193 * @param string $order ASC|DESC
194 */
195 public function reorder($order = 'DESC')
196 {
197 // backward compatibility: ignore reorder if the the `created` field doesn't exist
198 if (! isset(array_values($this->_links)[0]['created'])) {
199 return;
200 }
201
202 $order = $order === 'ASC' ? -1 : 1;
203 // Reorder array by dates.
067c2dd8 204 usort($this->_links, function ($a, $b) use ($order) {
4154c25b
A
205 if (isset($a['sticky']) && isset($b['sticky']) && $a['sticky'] !== $b['sticky']) {
206 return $a['sticky'] ? -1 : 1;
207 }
208
9ec0a611
A
209 return $a['created'] < $b['created'] ? 1 * $order : -1 * $order;
210 });
211 }
212
ca74886f
V
213 /**
214 * Returns the number of links in the reference data
215 */
216 public function countLinks()
217 {
07b6fa75 218 return $this->_publicCount + $this->_privateCount;
ca74886f
V
219 }
220
221 /**
222 * Returns the number of public links in the reference data
223 */
224 public function countPublicLinks()
225 {
07b6fa75 226 return $this->_publicCount;
ca74886f
V
227 }
228
229 /**
230 * Returns the number of private links in the reference data
231 */
232 public function countPrivateLinks()
233 {
07b6fa75 234 return $this->_privateCount;
ca74886f 235 }
822bffce 236
7d86f40b
A
237 /**
238 * Returns the number of links without tag
239 */
240 public function countUntaggedLinks()
241 {
242 $cpt = 0;
243 foreach ($this->_links as $link) {
244 if (empty($link['tags'])) {
245 ++$cpt;
246 }
247 }
248 return $cpt;
249 }
250
822bffce
A
251 public function getLinks()
252 {
9ec0a611 253 $this->reorder();
822bffce
A
254 return $this->_links;
255 }
c3dfd899
A
256
257 /**
258 * Setter to override link creation.
259 *
260 * @param array $links List of links.
261 */
262 public function setLinks($links)
263 {
264 $this->_links = $links;
265 }
ca74886f 266}