From d592daea8343bb4dfecff5d97e93699581ccc58c Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Mon, 28 Nov 2016 18:24:15 +0100 Subject: [PATCH] Add a persistent 'shorturl' key to all links All existing link will keep their permalinks. New links will have smallhash generated with date+id. The purpose of this is to avoid collision between links due to their creation date. --- application/FeedBuilder.php | 2 +- application/LinkDB.php | 12 ++-- application/LinkFilter.php | 2 +- application/LinkUtils.php | 13 +++++ application/NetscapeBookmarkUtils.php | 1 + application/Updater.php | 3 + application/Utils.php | 6 +- index.php | 56 ++++++++----------- plugins/isso/isso.php | 4 +- tests/FeedBuilderTest.php | 10 ++-- tests/LinkDBTest.php | 22 ++++---- .../BookmarkImportTest.php | 26 ++++----- tests/Updater/UpdaterTest.php | 8 +-- tests/plugins/PluginIssoTest.php | 21 +++++-- tests/utils/ReferenceLinkDB.php | 20 +++---- 15 files changed, 115 insertions(+), 91 deletions(-) diff --git a/application/FeedBuilder.php b/application/FeedBuilder.php index bfdf2fd3..fedd90e6 100644 --- a/application/FeedBuilder.php +++ b/application/FeedBuilder.php @@ -143,7 +143,7 @@ class FeedBuilder */ protected function buildItem($link, $pageaddr) { - $link['guid'] = $pageaddr .'?'. smallHash($link['created']->format('Ymd_His')); + $link['guid'] = $pageaddr .'?'. $link['shorturl']; // Check for both signs of a note: starting with ? and 7 chars long. if ($link['url'][0] === '?' && strlen($link['url']) === 7) { $link['url'] = $pageaddr . $link['url']; diff --git a/application/LinkDB.php b/application/LinkDB.php index e429ab4f..1e13286a 100644 --- a/application/LinkDB.php +++ b/application/LinkDB.php @@ -22,6 +22,7 @@ * Can be absolute or relative. * Relative URLs are permalinks (e.g.'?m-ukcw') * - real_url Absolute processed URL. + * - shorturl Permalink smallhash * * Implements 3 interfaces: * - ArrayAccess: behaves like an associative array; @@ -264,6 +265,7 @@ You use the community supported version of the original Shaarli project, by Seba 'created'=> new DateTime(), 'tags'=>'opensource software' ); + $link['shorturl'] = link_small_hash($link['created'], $link['id']); $this->links[1] = $link; $link = array( @@ -273,8 +275,9 @@ You use the community supported version of the original Shaarli project, by Seba 'description'=>'Shhhh! I\'m a private link only YOU can see. You can delete me too.', 'private'=>1, 'created'=> new DateTime('1 minute ago'), - 'tags'=>'secretstuff' + 'tags'=>'secretstuff', ); + $link['shorturl'] = link_small_hash($link['created'], $link['id']); $this->links[0] = $link; // Write database to disk @@ -335,10 +338,11 @@ You use the community supported version of the original Shaarli project, by Seba // To be able to load links before running the update, and prepare the update if (! isset($link['created'])) { $link['id'] = $link['linkdate']; - $link['created'] = DateTime::createFromFormat('Ymd_His', $link['linkdate']); + $link['created'] = DateTime::createFromFormat(self::LINK_DATE_FORMAT, $link['linkdate']); if (! empty($link['updated'])) { - $link['updated'] = DateTime::createFromFormat('Ymd_His', $link['updated']); + $link['updated'] = DateTime::createFromFormat(self::LINK_DATE_FORMAT, $link['updated']); } + $link['shorturl'] = smallHash($link['linkdate']); } } @@ -558,7 +562,7 @@ You use the community supported version of the original Shaarli project, by Seba * * @param int $id Persistent ID of a link. * - * @return int Real offset in local array, or null if doesn't exists. + * @return int Real offset in local array, or null if doesn't exist. */ protected function getLinkOffset($id) { diff --git a/application/LinkFilter.php b/application/LinkFilter.php index 7bab46ba..daa6d9cc 100644 --- a/application/LinkFilter.php +++ b/application/LinkFilter.php @@ -120,7 +120,7 @@ class LinkFilter { $filtered = array(); foreach ($this->links as $key => $l) { - if ($smallHash == smallHash($l['created']->format('Ymd_His'))) { + if ($smallHash == $l['shorturl']) { // Yes, this is ugly and slow $filtered[$key] = $l; return $filtered; diff --git a/application/LinkUtils.php b/application/LinkUtils.php index 9d9ae3cb..cf58f808 100644 --- a/application/LinkUtils.php +++ b/application/LinkUtils.php @@ -169,3 +169,16 @@ function space2nbsp($text) function format_description($description, $redirector = '', $indexUrl = '') { return nl2br(space2nbsp(hashtag_autolink(text2clickable($description, $redirector), $indexUrl))); } + +/** + * Generate a small hash for a link. + * + * @param DateTime $date Link creation date. + * @param int $id Link ID. + * + * @return string the small hash generated from link data. + */ +function link_small_hash($date, $id) +{ + return smallHash($date->format(LinkDB::LINK_DATE_FORMAT) . $id); +} diff --git a/application/NetscapeBookmarkUtils.php b/application/NetscapeBookmarkUtils.php index 8a939adb..e7148d00 100644 --- a/application/NetscapeBookmarkUtils.php +++ b/application/NetscapeBookmarkUtils.php @@ -174,6 +174,7 @@ class NetscapeBookmarkUtils $newLinkDate->setTimezone(new DateTimeZone(date_default_timezone_get())); $newLink['created'] = $newLinkDate; $newLink['id'] = $linkDb->getNextId(); + $newLink['shorturl'] = link_small_hash($newLink['created'], $newLink['id']); $linkDb[$newLink['id']] = $newLink; $importCount++; } diff --git a/application/Updater.php b/application/Updater.php index 16c8c376..f0d02814 100644 --- a/application/Updater.php +++ b/application/Updater.php @@ -223,6 +223,9 @@ class Updater * Since this update is very sensitve (changing the whole database), the datastore will be * automatically backed up into the file datastore..php. * + * LinkDB also adds the field 'shorturl' with the precedent format (linkdate smallhash), + * which will be saved by this method. + * * @return bool true if the update is successful, false otherwise. */ public function updateMethodDatastoreIds() diff --git a/application/Utils.php b/application/Utils.php index 0166ee2a..0a5b476e 100644 --- a/application/Utils.php +++ b/application/Utils.php @@ -31,7 +31,11 @@ function logm($logFile, $clientIp, $message) * - are NOT cryptographically secure (they CAN be forged) * * In Shaarli, they are used as a tinyurl-like link to individual entries, - * e.g. smallHash('20111006_131924') --> yZH23w + * built once with the combination of the date and item ID. + * e.g. smallHash('20111006_131924' . 142) --> eaWxtQ + * + * @warning before v0.8.1, smallhashes were built only with the date, + * and their value has been preserved. * * @param string $text Create a hash from this text. * diff --git a/index.php b/index.php index 05f06452..fdbdfaa2 100644 --- a/index.php +++ b/index.php @@ -566,21 +566,17 @@ function showDailyRSS($conf) { /* Some Shaarlies may have very few links, so we need to look back in time until we have enough days ($nb_of_days). */ - $ids = array(); - foreach ($LINKSDB as $id => $value) { - $ids[] = $id; - } $nb_of_days = 7; // We take 7 days. $today = date('Ymd'); $days = array(); - foreach ($ids as $id) { - $day = $LINKSDB[$id]['created']->format('Ymd'); // Extract day (without time) + foreach ($LINKSDB as $link) { + $day = $link['created']->format('Ymd'); // Extract day (without time) if (strcmp($day, $today) < 0) { if (empty($days[$day])) { $days[$day] = array(); } - $days[$day][] = $id; + $days[$day][] = $link; } if (count($days) > $nb_of_days) { @@ -600,23 +596,18 @@ function showDailyRSS($conf) { echo ''. $pageaddr .''. PHP_EOL; // For each day. - foreach ($days as $day => $ids) { + foreach ($days as $day => $links) { $dayDate = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $day.'_000000'); $absurl = escape(index_url($_SERVER).'?do=daily&day='.$day); // Absolute URL of the corresponding "Daily" page. - // Build the HTML body of this RSS entry. - $links = array(); - // We pre-format some fields for proper output. - foreach ($ids as $id) { - $l = $LINKSDB[$id]; - $l['formatedDescription'] = format_description($l['description'], $conf->get('redirector.url')); - $l['thumbnail'] = thumbnail($conf, $l['url']); - $l['timestamp'] = $l['created']->getTimestamp(); - if (startsWith($l['url'], '?')) { - $l['url'] = index_url($_SERVER) . $l['url']; // make permalink URL absolute + foreach ($links as &$link) { + $link['formatedDescription'] = format_description($link['description'], $conf->get('redirector.url')); + $link['thumbnail'] = thumbnail($conf, $link['url']); + $link['timestamp'] = $link['created']->getTimestamp(); + if (startsWith($link['url'], '?')) { + $link['url'] = index_url($_SERVER) . $link['url']; // make permalink URL absolute } - $links[$id] = $l; } // Then build the HTML for this day: @@ -675,7 +666,6 @@ function showDaily($pageBuilder, $LINKSDB, $conf, $pluginManager) $taglist = explode(' ',$link['tags']); uasort($taglist, 'strcasecmp'); - $linksToDisplay[$key]['shorturl'] = smallHash($link['created']->format('Ymd_His')); $linksToDisplay[$key]['taglist']=$taglist; $linksToDisplay[$key]['formatedDescription'] = format_description($link['description'], $conf->get('redirector.url')); $linksToDisplay[$key]['thumbnail'] = thumbnail($conf, $link['url']); @@ -829,7 +819,7 @@ function renderPage($conf, $pluginManager) // Get only links which have a thumbnail. foreach($links as $link) { - $permalink='?'.escape(smallHash($link['created']->format('Ymd_His'))); + $permalink='?'.$link['shorturl']; $thumb=lazyThumbnail($conf, $link['url'],$permalink); if ($thumb!='') // Only output links which have a thumbnail. { @@ -1249,7 +1239,7 @@ function renderPage($conf, $pluginManager) } // lf_id should only be present if the link exists. - $id = !empty($_POST['lf_id']) ? (int) escape($_POST['lf_id']) : $LINKSDB->getNextId(); + $id = !empty($_POST['lf_id']) ? intval(escape($_POST['lf_id'])) : $LINKSDB->getNextId(); // Linkdate is kept here to: // - use the same permalink for notes as they're displayed when creating them // - let users hack creation date of their posts @@ -1257,11 +1247,11 @@ function renderPage($conf, $pluginManager) $linkdate = escape($_POST['lf_linkdate']); if (isset($LINKSDB[$id])) { // Edit - $created = DateTime::createFromFormat('Ymd_His', $linkdate); + $created = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $linkdate); $updated = new DateTime(); } else { // New link - $created = DateTime::createFromFormat('Ymd_His', $linkdate); + $created = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $linkdate); $updated = null; } @@ -1288,7 +1278,8 @@ function renderPage($conf, $pluginManager) 'private' => (isset($_POST['lf_private']) ? 1 : 0), 'created' => $created, 'updated' => $updated, - 'tags' => str_replace(',', ' ', $tags) + 'tags' => str_replace(',', ' ', $tags), + 'shorturl' => link_small_hash($created, $id), ); // If title is empty, use the URL as title. @@ -1311,7 +1302,7 @@ function renderPage($conf, $pluginManager) $returnurl = !empty($_POST['returnurl']) ? $_POST['returnurl'] : '?'; $location = generateLocation($returnurl, $_SERVER['HTTP_HOST'], array('addlink', 'post', 'edit_link')); // Scroll to the link which has been edited. - $location .= '#' . smallHash($created->format('Ymd_His')); + $location .= '#' . $link['shorturl']; // After saving the link, redirect to the page the user was on. header('Location: '. $location); exit; @@ -1325,7 +1316,7 @@ function renderPage($conf, $pluginManager) $link = $LINKSDB[(int) escape($_POST['lf_id'])]; $returnurl = ( isset($_POST['returnurl']) ? $_POST['returnurl'] : '?' ); // Scroll to the link which has been edited. - $returnurl .= '#'.smallHash($link['created']->format('Ymd_His')); + $returnurl .= '#'. $link['shorturl']; $returnurl = generateLocation($returnurl, $_SERVER['HTTP_HOST'], array('addlink', 'post', 'edit_link')); header('Location: '.$returnurl); // After canceling, redirect to the page the user was on. exit; @@ -1341,7 +1332,7 @@ function renderPage($conf, $pluginManager) // - we are protected from XSRF by the token. // FIXME! We keep `lf_linkdate` for consistency before a proper API. To be removed. - $id = isset($_POST['lf_id']) ? (int) escape($_POST['lf_id']) : (int) escape($_POST['lf_linkdate']); + $id = isset($_POST['lf_id']) ? intval(escape($_POST['lf_id'])) : intval(escape($_POST['lf_linkdate'])); $pluginManager->executeHooks('delete_link', $LINKSDB[$id]); @@ -1387,7 +1378,7 @@ function renderPage($conf, $pluginManager) $id = (int) escape($_GET['edit_link']); $link = $LINKSDB[$id]; // Read database if (!$link) { header('Location: ?'); exit; } // Link not found in database. - $link['linkdate'] = $link['created']->format('Ymd_His'); + $link['linkdate'] = $link['created']->format(LinkDB::LINK_DATE_FORMAT); $data = array( 'link' => $link, 'link_is_new' => false, @@ -1414,7 +1405,7 @@ function renderPage($conf, $pluginManager) if (! $link) { $link_is_new = true; - $linkdate = strval(date('Ymd_His')); + $linkdate = strval(date(LinkDB::LINK_DATE_FORMAT)); // Get title if it was provided in URL (by the bookmarklet). $title = empty($_GET['title']) ? '' : escape($_GET['title']); // Get description if it was provided in URL (by the bookmarklet). [Bronco added that] @@ -1438,7 +1429,7 @@ function renderPage($conf, $pluginManager) } if ($url == '') { - $url = '?' . smallHash($linkdate); + $url = '?' . smallHash($linkdate . $LINKSDB->getNextId()); $title = 'Note: '; } $url = escape($url); @@ -1453,7 +1444,7 @@ function renderPage($conf, $pluginManager) 'private' => $private ); } else { - $link['linkdate'] = $link['created']->format('Ymd_His'); + $link['linkdate'] = $link['created']->format(LinkDB::LINK_DATE_FORMAT); } $data = array( @@ -1668,7 +1659,6 @@ function buildLinkList($PAGE,$LINKSDB, $conf, $pluginManager) $taglist = explode(' ', $link['tags']); uasort($taglist, 'strcasecmp'); $link['taglist'] = $taglist; - $link['shorturl'] = smallHash($link['created']->format('Ymd_His')); // Check for both signs of a note: starting with ? and 7 chars long. if ($link['url'][0] === '?' && strlen($link['url']) === 7) { diff --git a/plugins/isso/isso.php b/plugins/isso/isso.php index c44f3c09..ce16645f 100644 --- a/plugins/isso/isso.php +++ b/plugins/isso/isso.php @@ -43,9 +43,7 @@ function hook_isso_render_linklist($data, $conf) $link = reset($data['links']); $issoHtml = file_get_contents(PluginManager::$PLUGINS_PATH . '/isso/isso.html'); - // FIXME! KO thread unique si même date - $linkDate = $link['created']->format('Ymd_His'); - $isso = sprintf($issoHtml, $issoUrl, $issoUrl, $linkDate, $linkDate); + $isso = sprintf($issoHtml, $issoUrl, $issoUrl, $link['id'], $link['id']); $data['plugin_end_zone'][] = $isso; // Hackish way to include this CSS file only when necessary. diff --git a/tests/FeedBuilderTest.php b/tests/FeedBuilderTest.php index ea1dde25..06a44506 100644 --- a/tests/FeedBuilderTest.php +++ b/tests/FeedBuilderTest.php @@ -86,7 +86,7 @@ class FeedBuilderTest extends PHPUnit_Framework_TestCase // Test first link (note link) $link = reset($data['links']); $this->assertEquals(41, $link['id']); - $this->assertEquals(DateTime::createFromFormat('Ymd_His', '20150310_114651'), $link['created']); + $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651'), $link['created']); $this->assertEquals('http://host.tld/?WDWyig', $link['guid']); $this->assertEquals('http://host.tld/?WDWyig', $link['url']); $this->assertRegExp('/Tue, 10 Mar 2015 11:46:51 \+\d{4}/', $link['pub_iso_date']); @@ -140,7 +140,7 @@ class FeedBuilderTest extends PHPUnit_Framework_TestCase $this->assertEquals(1, count($data['links'])); $link = array_shift($data['links']); $this->assertEquals(41, $link['id']); - $this->assertEquals(DateTime::createFromFormat('Ymd_His', '20150310_114651'), $link['created']); + $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651'), $link['created']); } /** @@ -157,7 +157,7 @@ class FeedBuilderTest extends PHPUnit_Framework_TestCase $this->assertEquals(1, count($data['links'])); $link = array_shift($data['links']); $this->assertEquals(41, $link['id']); - $this->assertEquals(DateTime::createFromFormat('Ymd_His', '20150310_114651'), $link['created']); + $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651'), $link['created']); } /** @@ -174,7 +174,7 @@ class FeedBuilderTest extends PHPUnit_Framework_TestCase // First link is a permalink $link = array_shift($data['links']); $this->assertEquals(41, $link['id']); - $this->assertEquals(DateTime::createFromFormat('Ymd_His', '20150310_114651'), $link['created']); + $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651'), $link['created']); $this->assertEquals('http://host.tld/?WDWyig', $link['guid']); $this->assertEquals('http://host.tld/?WDWyig', $link['url']); $this->assertContains('Direct link', $link['description']); @@ -182,7 +182,7 @@ class FeedBuilderTest extends PHPUnit_Framework_TestCase // Second link is a direct link $link = array_shift($data['links']); $this->assertEquals(8, $link['id']); - $this->assertEquals(DateTime::createFromFormat('Ymd_His', '20150310_114633'), $link['created']); + $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114633'), $link['created']); $this->assertEquals('http://host.tld/?RttfEw', $link['guid']); $this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['url']); $this->assertContains('Direct link', $link['description']); diff --git a/tests/LinkDBTest.php b/tests/LinkDBTest.php index bedc680e..1f62a34a 100644 --- a/tests/LinkDBTest.php +++ b/tests/LinkDBTest.php @@ -191,7 +191,7 @@ class LinkDBTest extends PHPUnit_Framework_TestCase 'url'=>'http://dum.my', 'description'=>'One more', 'private'=>0, - 'created'=> DateTime::createFromFormat('Ymd_His', '20150518_190000'), + 'created'=> DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150518_190000'), 'tags'=>'unit test' ); $testDB[$link['id']] = $link; @@ -447,17 +447,17 @@ class LinkDBTest extends PHPUnit_Framework_TestCase */ public function testReorderLinksDesc() { - self::$publicLinkDB->reorder('ASC'); - $linkIdToTest = 42; - foreach (self::$publicLinkDB as $key => $value) { - $this->assertEquals($linkIdToTest, $key); - break; + self::$privateLinkDB->reorder('ASC'); + $linkIds = array(42, 4, 1, 0, 7, 6, 8, 41); + $cpt = 0; + foreach (self::$privateLinkDB as $key => $value) { + $this->assertEquals($linkIds[$cpt++], $key); } - self::$publicLinkDB->reorder('DESC'); - $linkIdToTest = 41; - foreach (self::$publicLinkDB as $key => $value) { - $this->assertEquals($linkIdToTest, $key); - break; + self::$privateLinkDB->reorder('DESC'); + $linkIds = array_reverse($linkIds); + $cpt = 0; + foreach (self::$privateLinkDB as $key => $value) { + $this->assertEquals($linkIds[$cpt++], $key); } } } diff --git a/tests/NetscapeBookmarkUtils/BookmarkImportTest.php b/tests/NetscapeBookmarkUtils/BookmarkImportTest.php index 96895b00..0ca07eac 100644 --- a/tests/NetscapeBookmarkUtils/BookmarkImportTest.php +++ b/tests/NetscapeBookmarkUtils/BookmarkImportTest.php @@ -116,7 +116,7 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase $this->assertEquals( array( 'id' => 0, - 'created' => DateTime::createFromFormat('Ymd_His', '20160618_203944'), + 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160618_203944'), 'title' => 'Hg Init a Mercurial tutorial by Joel Spolsky', 'url' => 'http://hginit.com/', 'description' => '', @@ -145,7 +145,7 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase $this->assertEquals( array( 'id' => 0, - 'created' => DateTime::createFromFormat('Ymd_His', '20160225_235541'), + 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160225_235541'), 'title' => 'Nested 1', 'url' => 'http://nest.ed/1', 'description' => '', @@ -158,7 +158,7 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase $this->assertEquals( array( 'id' => 1, - 'created' => DateTime::createFromFormat('Ymd_His', '20160225_235542'), + 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160225_235542'), 'title' => 'Nested 1-1', 'url' => 'http://nest.ed/1-1', 'description' => '', @@ -171,7 +171,7 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase $this->assertEquals( array( 'id' => 2, - 'created' => DateTime::createFromFormat('Ymd_His', '20160225_235547'), + 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160225_235547'), 'title' => 'Nested 1-2', 'url' => 'http://nest.ed/1-2', 'description' => '', @@ -184,7 +184,7 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase $this->assertEquals( array( 'id' => 3, - 'created' => DateTime::createFromFormat('Ymd_His', '20160202_202222'), + 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160202_202222'), 'title' => 'Nested 2-1', 'url' => 'http://nest.ed/2-1', 'description' => 'First link of the second section', @@ -197,7 +197,7 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase $this->assertEquals( array( 'id' => 4, - 'created' => DateTime::createFromFormat('Ymd_His', '20160119_230227'), + 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160119_230227'), 'title' => 'Nested 2-2', 'url' => 'http://nest.ed/2-2', 'description' => 'Second link of the second section', @@ -210,7 +210,7 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase $this->assertEquals( array( 'id' => 5, - 'created' => DateTime::createFromFormat('Ymd_His', '20160202_202222'), + 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160202_202222'), 'title' => 'Nested 3-1', 'url' => 'http://nest.ed/3-1', 'description' => '', @@ -223,7 +223,7 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase $this->assertEquals( array( 'id' => 6, - 'created' => DateTime::createFromFormat('Ymd_His', '20160119_230227'), + 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160119_230227'), 'title' => 'Nested 3-2', 'url' => 'http://nest.ed/3-2', 'description' => '', @@ -236,7 +236,7 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase $this->assertEquals( array( 'id' => 7, - 'created' => DateTime::createFromFormat('Ymd_His', '20160229_111541'), + 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160229_111541'), 'title' => 'Nested 2', 'url' => 'http://nest.ed/2', 'description' => '', @@ -269,7 +269,7 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase array( 'id' => 0, // Old link - UTC+4 (note that TZ in the import file is ignored). - 'created' => DateTime::createFromFormat('Ymd_His', '20001010_135536'), + 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20001010_135536'), 'title' => 'Secret stuff', 'url' => 'https://private.tld', 'description' => "Super-secret stuff you're not supposed to know about", @@ -282,7 +282,7 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase $this->assertEquals( array( 'id' => 1, - 'created' => DateTime::createFromFormat('Ymd_His', '20160225_235548'), + 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160225_235548'), 'title' => 'Public stuff', 'url' => 'http://public.tld', 'description' => '', @@ -313,7 +313,7 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase array( 'id' => 0, // Note that TZ in the import file is ignored. - 'created' => DateTime::createFromFormat('Ymd_His', '20001010_135536'), + 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20001010_135536'), 'title' => 'Secret stuff', 'url' => 'https://private.tld', 'description' => "Super-secret stuff you're not supposed to know about", @@ -326,7 +326,7 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase $this->assertEquals( array( 'id' => 1, - 'created' => DateTime::createFromFormat('Ymd_His', '20160225_235548'), + 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160225_235548'), 'title' => 'Public stuff', 'url' => 'http://public.tld', 'description' => '', diff --git a/tests/Updater/UpdaterTest.php b/tests/Updater/UpdaterTest.php index b8a050b0..4948fe52 100644 --- a/tests/Updater/UpdaterTest.php +++ b/tests/Updater/UpdaterTest.php @@ -352,20 +352,20 @@ $GLOBALS[\'privateLinkByDefault\'] = true;'; $this->assertEquals('Naming conventions... #private', $linkDB[0]['description']); $this->assertEquals('samba cartoon web', $linkDB[0]['tags']); $this->assertTrue($linkDB[0]['private']); - $this->assertEquals(DateTime::createFromFormat('Ymd_His', '20121206_142300'), $linkDB[0]['created']); + $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_142300'), $linkDB[0]['created']); $this->assertTrue(isset($linkDB[1])); $this->assertFalse(isset($linkDB[1]['linkdate'])); $this->assertEquals(1, $linkDB[1]['id']); $this->assertEquals('UserFriendly - Samba', $linkDB[1]['title']); - $this->assertEquals(DateTime::createFromFormat('Ymd_His', '20121206_172539'), $linkDB[1]['created']); + $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_172539'), $linkDB[1]['created']); $this->assertTrue(isset($linkDB[2])); $this->assertFalse(isset($linkDB[2]['linkdate'])); $this->assertEquals(2, $linkDB[2]['id']); $this->assertEquals('Geek and Poke', $linkDB[2]['title']); - $this->assertEquals(DateTime::createFromFormat('Ymd_His', '20121206_182539'), $linkDB[2]['created']); - $this->assertEquals(DateTime::createFromFormat('Ymd_His', '20121206_190301'), $linkDB[2]['updated']); + $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_182539'), $linkDB[2]['created']); + $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_190301'), $linkDB[2]['updated']); } /** diff --git a/tests/plugins/PluginIssoTest.php b/tests/plugins/PluginIssoTest.php index ea86a05c..6b7904dd 100644 --- a/tests/plugins/PluginIssoTest.php +++ b/tests/plugins/PluginIssoTest.php @@ -52,8 +52,9 @@ class PluginIssoTest extends PHPUnit_Framework_TestCase 'title' => $str, 'links' => array( array( + 'id' => 12, 'url' => $str, - 'created' => DateTime::createFromFormat('Ymd_His', $date), + 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $date), ) ) ); @@ -66,7 +67,14 @@ class PluginIssoTest extends PHPUnit_Framework_TestCase // plugin data $this->assertEquals(1, count($data['plugin_end_zone'])); - $this->assertNotFalse(strpos($data['plugin_end_zone'][0], $date)); + $this->assertNotFalse(strpos( + $data['plugin_end_zone'][0], + 'data-isso-id="'. $data['links'][0]['id'] .'"' + )); + $this->assertNotFalse(strpos( + $data['plugin_end_zone'][0], + 'data-title="'. $data['links'][0]['id'] .'"' + )); $this->assertNotFalse(strpos($data['plugin_end_zone'][0], 'embed.min.js')); } @@ -85,12 +93,14 @@ class PluginIssoTest extends PHPUnit_Framework_TestCase 'title' => $str, 'links' => array( array( + 'id' => 12, 'url' => $str, - 'created' => DateTime::createFromFormat('Ymd_His', $date1), + 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $date1), ), array( + 'id' => 13, 'url' => $str . '2', - 'created' => DateTime::createFromFormat('Ymd_His', $date2), + 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $date2), ), ) ); @@ -114,8 +124,9 @@ class PluginIssoTest extends PHPUnit_Framework_TestCase 'title' => $str, 'links' => array( array( + 'id' => 12, 'url' => $str, - 'created' => DateTime::createFromFormat('Ymd_His', $date), + 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $date), ) ), 'search_term' => $str diff --git a/tests/utils/ReferenceLinkDB.php b/tests/utils/ReferenceLinkDB.php index 6b16c9e8..36d58c68 100644 --- a/tests/utils/ReferenceLinkDB.php +++ b/tests/utils/ReferenceLinkDB.php @@ -21,7 +21,7 @@ class ReferenceLinkDB '?WDWyig', 'Stallman has a beard and is part of the Free Software Foundation (or not). Seriously, read this. #hashtag', 0, - DateTime::createFromFormat('Ymd_His', '20150310_114651'), + DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651'), 'sTuff', null, 'WDWyig' @@ -33,7 +33,7 @@ class ReferenceLinkDB '?WDWyig', 'Used to test links reordering.', 0, - DateTime::createFromFormat('Ymd_His', '20100310_101010'), + DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20100310_101010'), 'ut' ); @@ -43,9 +43,9 @@ class ReferenceLinkDB 'https://static.fsf.org/nosvn/faif-2.0.pdf', 'Richard Stallman and the Free Software Revolution. Read this. #hashtag', 0, - DateTime::createFromFormat('Ymd_His', '20150310_114633'), + DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114633'), 'free gnu software stallman -exclude stuff hashtag', - DateTime::createFromFormat('Ymd_His', '20160803_093033') + DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160803_093033') ); $this->addLink( @@ -54,7 +54,7 @@ class ReferenceLinkDB 'http://mediagoblin.org/', 'A free software media publishing platform #hashtagOther', 0, - DateTime::createFromFormat('Ymd_His', '20130614_184135'), + DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20130614_184135'), 'gnu media web .hidden hashtag', null, 'IuWvgA' @@ -66,7 +66,7 @@ class ReferenceLinkDB 'https://dvcs.w3.org/hg/markup-validator/summary', 'Mercurial repository for the W3C Validator #private', 1, - DateTime::createFromFormat('Ymd_His', '20141125_084734'), + DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20141125_084734'), 'css html w3c web Mercurial' ); @@ -76,7 +76,7 @@ class ReferenceLinkDB 'http://ars.userfriendly.org/cartoons/?id=20121206', 'Naming conventions... #private', 0, - DateTime::createFromFormat('Ymd_His', '20121206_142300'), + DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_142300'), 'dev cartoon web' ); @@ -86,7 +86,7 @@ class ReferenceLinkDB 'http://ars.userfriendly.org/cartoons/?id=20010306', 'Tropical printing', 0, - DateTime::createFromFormat('Ymd_His', '20121206_172539'), + DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_172539'), 'samba cartoon web' ); @@ -96,7 +96,7 @@ class ReferenceLinkDB 'http://geek-and-poke.com/', '', 1, - DateTime::createFromFormat('Ymd_His', '20121206_182539'), + DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_182539'), 'dev cartoon tag1 tag2 tag3 tag4 ' ); } @@ -115,7 +115,7 @@ class ReferenceLinkDB 'tags' => $tags, 'created' => $date, 'updated' => $updated, - 'shorturl' => $shorturl ? $shorturl : smallHash($date->format('Ymd_His') . $id), + 'shorturl' => $shorturl ? $shorturl : smallHash($date->format(LinkDB::LINK_DATE_FORMAT) . $id), ); $this->_links[$id] = $link; -- 2.41.0