From e26e2060f5470ce8bf4c5973284bae07b8af170a Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Fri, 17 Jan 2020 21:34:12 +0100 Subject: Add and update unit test for the new system (Bookmark + Service) See #1307 --- tests/legacy/LegacyDummyUpdater.php | 74 +++ tests/legacy/LegacyLinkDBTest.php | 656 +++++++++++++++++++++++++ tests/legacy/LegacyLinkFilterTest.php | 509 +++++++++++++++++++ tests/legacy/LegacyUpdaterTest.php | 886 ++++++++++++++++++++++++++++++++++ 4 files changed, 2125 insertions(+) create mode 100644 tests/legacy/LegacyDummyUpdater.php create mode 100644 tests/legacy/LegacyLinkDBTest.php create mode 100644 tests/legacy/LegacyLinkFilterTest.php create mode 100644 tests/legacy/LegacyUpdaterTest.php (limited to 'tests/legacy') diff --git a/tests/legacy/LegacyDummyUpdater.php b/tests/legacy/LegacyDummyUpdater.php new file mode 100644 index 00000000..10e0a5b7 --- /dev/null +++ b/tests/legacy/LegacyDummyUpdater.php @@ -0,0 +1,74 @@ +methods = $class->getMethods(ReflectionMethod::IS_FINAL); + } + + /** + * Update method 1. + * + * @return bool true. + */ + final private function updateMethodDummy1() + { + return true; + } + + /** + * Update method 2. + * + * @return bool true. + */ + final private function updateMethodDummy2() + { + return true; + } + + /** + * Update method 3. + * + * @return bool true. + */ + final private function updateMethodDummy3() + { + return true; + } + + /** + * Update method 4, raise an exception. + * + * @throws Exception error. + */ + final private function updateMethodException() + { + throw new Exception('whatever'); + } +} diff --git a/tests/legacy/LegacyLinkDBTest.php b/tests/legacy/LegacyLinkDBTest.php new file mode 100644 index 00000000..17b2b0e6 --- /dev/null +++ b/tests/legacy/LegacyLinkDBTest.php @@ -0,0 +1,656 @@ +write(self::$testDatastore); + self::$publicLinkDB = new LegacyLinkDB(self::$testDatastore, false, false); + self::$privateLinkDB = new LegacyLinkDB(self::$testDatastore, true, false); + } + + /** + * Allows to test LinkDB's private methods + * + * @see + * https://sebastian-bergmann.de/archives/881-Testing-Your-Privates.html + * http://stackoverflow.com/a/2798203 + */ + protected static function getMethod($name) + { + $class = new ReflectionClass('Shaarli\Legacy\LegacyLinkDB'); + $method = $class->getMethod($name); + $method->setAccessible(true); + return $method; + } + + /** + * Instantiate LinkDB objects - logged in user + */ + public function testConstructLoggedIn() + { + new LegacyLinkDB(self::$testDatastore, true, false); + $this->assertFileExists(self::$testDatastore); + } + + /** + * Instantiate LinkDB objects - logged out or public instance + */ + public function testConstructLoggedOut() + { + new LegacyLinkDB(self::$testDatastore, false, false); + $this->assertFileExists(self::$testDatastore); + } + + /** + * Attempt to instantiate a LinkDB whereas the datastore is not writable + * + * @expectedException Shaarli\Exceptions\IOException + * @expectedExceptionMessageRegExp /Error accessing "null"/ + */ + public function testConstructDatastoreNotWriteable() + { + new LegacyLinkDB('null/store.db', false, false); + } + + /** + * The DB doesn't exist, ensure it is created with dummy content + */ + public function testCheckDBNew() + { + $linkDB = new LegacyLinkDB(self::$testDatastore, false, false); + unlink(self::$testDatastore); + $this->assertFileNotExists(self::$testDatastore); + + $checkDB = self::getMethod('check'); + $checkDB->invokeArgs($linkDB, array()); + $this->assertFileExists(self::$testDatastore); + + // ensure the correct data has been written + $this->assertGreaterThan(0, filesize(self::$testDatastore)); + } + + /** + * The DB exists, don't do anything + */ + public function testCheckDBLoad() + { + $linkDB = new LegacyLinkDB(self::$testDatastore, false, false); + $datastoreSize = filesize(self::$testDatastore); + $this->assertGreaterThan(0, $datastoreSize); + + $checkDB = self::getMethod('check'); + $checkDB->invokeArgs($linkDB, array()); + + // ensure the datastore is left unmodified + $this->assertEquals( + $datastoreSize, + filesize(self::$testDatastore) + ); + } + + /** + * Load an empty DB + */ + public function testReadEmptyDB() + { + file_put_contents(self::$testDatastore, ''); + $emptyDB = new LegacyLinkDB(self::$testDatastore, false, false); + $this->assertEquals(0, sizeof($emptyDB)); + $this->assertEquals(0, count($emptyDB)); + } + + /** + * Load public bookmarks from the DB + */ + public function testReadPublicDB() + { + $this->assertEquals( + self::$refDB->countPublicLinks(), + sizeof(self::$publicLinkDB) + ); + } + + /** + * Load public and private bookmarks from the DB + */ + public function testReadPrivateDB() + { + $this->assertEquals( + self::$refDB->countLinks(), + sizeof(self::$privateLinkDB) + ); + } + + /** + * Save the bookmarks to the DB + */ + public function testSave() + { + $testDB = new LegacyLinkDB(self::$testDatastore, true, false); + $dbSize = sizeof($testDB); + + $link = array( + 'id' => 43, + 'title' => 'an additional link', + 'url' => 'http://dum.my', + 'description' => 'One more', + 'private' => 0, + 'created' => DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150518_190000'), + 'tags' => 'unit test' + ); + $testDB[$link['id']] = $link; + $testDB->save('tests'); + + $testDB = new LegacyLinkDB(self::$testDatastore, true, false); + $this->assertEquals($dbSize + 1, sizeof($testDB)); + } + + /** + * Count existing bookmarks + */ + public function testCount() + { + $this->assertEquals( + self::$refDB->countPublicLinks(), + self::$publicLinkDB->count() + ); + $this->assertEquals( + self::$refDB->countLinks(), + self::$privateLinkDB->count() + ); + } + + /** + * Count existing bookmarks - public bookmarks hidden + */ + public function testCountHiddenPublic() + { + $linkDB = new LegacyLinkDB(self::$testDatastore, false, true); + + $this->assertEquals( + 0, + $linkDB->count() + ); + $this->assertEquals( + 0, + $linkDB->count() + ); + } + + /** + * List the days for which bookmarks have been posted + */ + public function testDays() + { + $this->assertEquals( + array('20100309', '20100310', '20121206', '20121207', '20130614', '20150310'), + self::$publicLinkDB->days() + ); + + $this->assertEquals( + array('20100309', '20100310', '20121206', '20121207', '20130614', '20141125', '20150310'), + self::$privateLinkDB->days() + ); + } + + /** + * The URL corresponds to an existing entry in the DB + */ + public function testGetKnownLinkFromURL() + { + $link = self::$publicLinkDB->getLinkFromUrl('http://mediagoblin.org/'); + + $this->assertNotEquals(false, $link); + $this->assertContains( + 'A free software media publishing platform', + $link['description'] + ); + } + + /** + * The URL is not in the DB + */ + public function testGetUnknownLinkFromURL() + { + $this->assertEquals( + false, + self::$publicLinkDB->getLinkFromUrl('http://dev.null') + ); + } + + /** + * Lists all tags + */ + public function testAllTags() + { + $this->assertEquals( + array( + 'web' => 3, + 'cartoon' => 2, + 'gnu' => 2, + 'dev' => 1, + 'samba' => 1, + 'media' => 1, + 'software' => 1, + 'stallman' => 1, + 'free' => 1, + '-exclude' => 1, + 'hashtag' => 2, + // The DB contains a link with `sTuff` and another one with `stuff` tag. + // They need to be grouped with the first case found - order by date DESC: `sTuff`. + 'sTuff' => 2, + 'ut' => 1, + ), + self::$publicLinkDB->linksCountPerTag() + ); + + $this->assertEquals( + array( + 'web' => 4, + 'cartoon' => 3, + 'gnu' => 2, + 'dev' => 2, + 'samba' => 1, + 'media' => 1, + 'software' => 1, + 'stallman' => 1, + 'free' => 1, + 'html' => 1, + 'w3c' => 1, + 'css' => 1, + 'Mercurial' => 1, + 'sTuff' => 2, + '-exclude' => 1, + '.hidden' => 1, + 'hashtag' => 2, + 'tag1' => 1, + 'tag2' => 1, + 'tag3' => 1, + 'tag4' => 1, + 'ut' => 1, + ), + self::$privateLinkDB->linksCountPerTag() + ); + $this->assertEquals( + array( + 'web' => 4, + 'cartoon' => 2, + 'gnu' => 1, + 'dev' => 1, + 'samba' => 1, + 'media' => 1, + 'html' => 1, + 'w3c' => 1, + 'css' => 1, + 'Mercurial' => 1, + '.hidden' => 1, + 'hashtag' => 1, + ), + self::$privateLinkDB->linksCountPerTag(['web']) + ); + $this->assertEquals( + array( + 'web' => 1, + 'html' => 1, + 'w3c' => 1, + 'css' => 1, + 'Mercurial' => 1, + ), + self::$privateLinkDB->linksCountPerTag(['web'], 'private') + ); + } + + /** + * Test filter with string. + */ + public function testFilterString() + { + $tags = 'dev cartoon'; + $request = array('searchtags' => $tags); + $this->assertEquals( + 2, + count(self::$privateLinkDB->filterSearch($request, true, false)) + ); + } + + /** + * Test filter with string. + */ + public function testFilterArray() + { + $tags = array('dev', 'cartoon'); + $request = array('searchtags' => $tags); + $this->assertEquals( + 2, + count(self::$privateLinkDB->filterSearch($request, true, false)) + ); + } + + /** + * Test hidden tags feature: + * tags starting with a dot '.' are only visible when logged in. + */ + public function testHiddenTags() + { + $tags = '.hidden'; + $request = array('searchtags' => $tags); + $this->assertEquals( + 1, + count(self::$privateLinkDB->filterSearch($request, true, false)) + ); + + $this->assertEquals( + 0, + count(self::$publicLinkDB->filterSearch($request, true, false)) + ); + } + + /** + * Test filterHash() with a valid smallhash. + */ + public function testFilterHashValid() + { + $request = smallHash('20150310_114651'); + $this->assertEquals( + 1, + count(self::$publicLinkDB->filterHash($request)) + ); + $request = smallHash('20150310_114633' . 8); + $this->assertEquals( + 1, + count(self::$publicLinkDB->filterHash($request)) + ); + } + + /** + * Test filterHash() with an invalid smallhash. + * + * @expectedException \Shaarli\Bookmark\Exception\BookmarkNotFoundException + */ + public function testFilterHashInValid1() + { + $request = 'blabla'; + self::$publicLinkDB->filterHash($request); + } + + /** + * Test filterHash() with an empty smallhash. + * + * @expectedException \Shaarli\Bookmark\Exception\BookmarkNotFoundException + */ + public function testFilterHashInValid() + { + self::$publicLinkDB->filterHash(''); + } + + /** + * Test reorder with asc/desc parameter. + */ + public function testReorderLinksDesc() + { + self::$privateLinkDB->reorder('ASC'); + $stickyIds = [11, 10]; + $standardIds = [42, 4, 9, 1, 0, 7, 6, 8, 41]; + $linkIds = array_merge($stickyIds, $standardIds); + $cpt = 0; + foreach (self::$privateLinkDB as $key => $value) { + $this->assertEquals($linkIds[$cpt++], $key); + } + self::$privateLinkDB->reorder('DESC'); + $linkIds = array_merge(array_reverse($stickyIds), array_reverse($standardIds)); + $cpt = 0; + foreach (self::$privateLinkDB as $key => $value) { + $this->assertEquals($linkIds[$cpt++], $key); + } + } + + /** + * Test rename tag with a valid value present in multiple bookmarks + */ + public function testRenameTagMultiple() + { + self::$refDB->write(self::$testDatastore); + $linkDB = new LegacyLinkDB(self::$testDatastore, true, false); + + $res = $linkDB->renameTag('cartoon', 'Taz'); + $this->assertEquals(3, count($res)); + $this->assertContains(' Taz ', $linkDB[4]['tags']); + $this->assertContains(' Taz ', $linkDB[1]['tags']); + $this->assertContains(' Taz ', $linkDB[0]['tags']); + } + + /** + * Test rename tag with a valid value + */ + public function testRenameTagCaseSensitive() + { + self::$refDB->write(self::$testDatastore); + $linkDB = new LegacyLinkDB(self::$testDatastore, true, false); + + $res = $linkDB->renameTag('sTuff', 'Taz'); + $this->assertEquals(1, count($res)); + $this->assertEquals('Taz', $linkDB[41]['tags']); + } + + /** + * Test rename tag with invalid values + */ + public function testRenameTagInvalid() + { + $linkDB = new LegacyLinkDB(self::$testDatastore, false, false); + + $this->assertFalse($linkDB->renameTag('', 'test')); + $this->assertFalse($linkDB->renameTag('', '')); + // tag non existent + $this->assertEquals([], $linkDB->renameTag('test', '')); + $this->assertEquals([], $linkDB->renameTag('test', 'retest')); + } + + /** + * Test delete tag with a valid value + */ + public function testDeleteTag() + { + self::$refDB->write(self::$testDatastore); + $linkDB = new LegacyLinkDB(self::$testDatastore, true, false); + + $res = $linkDB->renameTag('cartoon', null); + $this->assertEquals(3, count($res)); + $this->assertNotContains('cartoon', $linkDB[4]['tags']); + } + + /** + * Test linksCountPerTag all tags without filter. + * Equal occurrences should be sorted alphabetically. + */ + public function testCountLinkPerTagAllNoFilter() + { + $expected = [ + 'web' => 4, + 'cartoon' => 3, + 'dev' => 2, + 'gnu' => 2, + 'hashtag' => 2, + 'sTuff' => 2, + '-exclude' => 1, + '.hidden' => 1, + 'Mercurial' => 1, + 'css' => 1, + 'free' => 1, + 'html' => 1, + 'media' => 1, + 'samba' => 1, + 'software' => 1, + 'stallman' => 1, + 'tag1' => 1, + 'tag2' => 1, + 'tag3' => 1, + 'tag4' => 1, + 'ut' => 1, + 'w3c' => 1, + ]; + $tags = self::$privateLinkDB->linksCountPerTag(); + + $this->assertEquals($expected, $tags, var_export($tags, true)); + } + + /** + * Test linksCountPerTag all tags with filter. + * Equal occurrences should be sorted alphabetically. + */ + public function testCountLinkPerTagAllWithFilter() + { + $expected = [ + 'gnu' => 2, + 'hashtag' => 2, + '-exclude' => 1, + '.hidden' => 1, + 'free' => 1, + 'media' => 1, + 'software' => 1, + 'stallman' => 1, + 'stuff' => 1, + 'web' => 1, + ]; + $tags = self::$privateLinkDB->linksCountPerTag(['gnu']); + + $this->assertEquals($expected, $tags, var_export($tags, true)); + } + + /** + * Test linksCountPerTag public tags with filter. + * Equal occurrences should be sorted alphabetically. + */ + public function testCountLinkPerTagPublicWithFilter() + { + $expected = [ + 'gnu' => 2, + 'hashtag' => 2, + '-exclude' => 1, + '.hidden' => 1, + 'free' => 1, + 'media' => 1, + 'software' => 1, + 'stallman' => 1, + 'stuff' => 1, + 'web' => 1, + ]; + $tags = self::$privateLinkDB->linksCountPerTag(['gnu'], 'public'); + + $this->assertEquals($expected, $tags, var_export($tags, true)); + } + + /** + * Test linksCountPerTag public tags with filter. + * Equal occurrences should be sorted alphabetically. + */ + public function testCountLinkPerTagPrivateWithFilter() + { + $expected = [ + 'cartoon' => 1, + 'dev' => 1, + 'tag1' => 1, + 'tag2' => 1, + 'tag3' => 1, + 'tag4' => 1, + ]; + $tags = self::$privateLinkDB->linksCountPerTag(['dev'], 'private'); + + $this->assertEquals($expected, $tags, var_export($tags, true)); + } + + /** + * Make sure that bookmarks with the same timestamp have a consistent order: + * if their creation date is equal, bookmarks are sorted by ID DESC. + */ + public function testConsistentOrder() + { + $nextId = 43; + $creation = DateTime::createFromFormat('Ymd_His', '20190807_130444'); + $linkDB = new LegacyLinkDB(self::$testDatastore, true, false); + for ($i = 0; $i < 4; ++$i) { + $linkDB[$nextId + $i] = [ + 'id' => $nextId + $i, + 'url' => 'http://'. $i, + 'created' => $creation, + 'title' => true, + 'description' => true, + 'tags' => true, + ]; + } + + // Check 4 new links 4 times + for ($i = 0; $i < 4; ++$i) { + $linkDB->save('tests'); + $linkDB = new LegacyLinkDB(self::$testDatastore, true, false); + $count = 3; + foreach ($linkDB as $link) { + if ($link['sticky'] === true) { + continue; + } + $this->assertEquals($nextId + $count, $link['id']); + $this->assertEquals('http://'. $count, $link['url']); + if (--$count < 0) { + break; + } + } + } + } +} diff --git a/tests/legacy/LegacyLinkFilterTest.php b/tests/legacy/LegacyLinkFilterTest.php new file mode 100644 index 00000000..ba9ec529 --- /dev/null +++ b/tests/legacy/LegacyLinkFilterTest.php @@ -0,0 +1,509 @@ +write(self::$testDatastore); + self::$linkDB = new LegacyLinkDB(self::$testDatastore, true, false); + self::$linkFilter = new LegacyLinkFilter(self::$linkDB); + } + + /** + * Blank filter. + */ + public function testFilter() + { + $this->assertEquals( + self::$refDB->countLinks(), + count(self::$linkFilter->filter('', '')) + ); + + $this->assertEquals( + self::$refDB->countLinks(), + count(self::$linkFilter->filter('', '', 'all')) + ); + + $this->assertEquals( + self::$refDB->countLinks(), + count(self::$linkFilter->filter('', '', 'randomstr')) + ); + + // Private only. + $this->assertEquals( + self::$refDB->countPrivateLinks(), + count(self::$linkFilter->filter('', '', false, 'private')) + ); + + // Public only. + $this->assertEquals( + self::$refDB->countPublicLinks(), + count(self::$linkFilter->filter('', '', false, 'public')) + ); + + $this->assertEquals( + ReferenceLinkDB::$NB_LINKS_TOTAL, + count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TAG, '')) + ); + + $this->assertEquals( + self::$refDB->countUntaggedLinks(), + count( + self::$linkFilter->filter( + LegacyLinkFilter::$FILTER_TAG, + /*$request=*/ + '', + /*$casesensitive=*/ + false, + /*$visibility=*/ + 'all', + /*$untaggedonly=*/ + true + ) + ) + ); + + $this->assertEquals( + ReferenceLinkDB::$NB_LINKS_TOTAL, + count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, '')) + ); + } + + /** + * Filter bookmarks using a tag + */ + public function testFilterOneTag() + { + $this->assertEquals( + 4, + count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TAG, 'web', false)) + ); + + $this->assertEquals( + 4, + count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TAG, 'web', false, 'all')) + ); + + $this->assertEquals( + 4, + count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TAG, 'web', false, 'default-blabla')) + ); + + // Private only. + $this->assertEquals( + 1, + count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TAG, 'web', false, 'private')) + ); + + // Public only. + $this->assertEquals( + 3, + count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TAG, 'web', false, 'public')) + ); + } + + /** + * Filter bookmarks using a tag - case-sensitive + */ + public function testFilterCaseSensitiveTag() + { + $this->assertEquals( + 0, + count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TAG, 'mercurial', true)) + ); + + $this->assertEquals( + 1, + count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TAG, 'Mercurial', true)) + ); + } + + /** + * Filter bookmarks using a tag combination + */ + public function testFilterMultipleTags() + { + $this->assertEquals( + 2, + count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TAG, 'dev cartoon', false)) + ); + } + + /** + * Filter bookmarks using a non-existent tag + */ + public function testFilterUnknownTag() + { + $this->assertEquals( + 0, + count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TAG, 'null', false)) + ); + } + + /** + * Return bookmarks for a given day + */ + public function testFilterDay() + { + $this->assertEquals( + 4, + count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_DAY, '20121206')) + ); + } + + /** + * 404 - day not found + */ + public function testFilterUnknownDay() + { + $this->assertEquals( + 0, + count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_DAY, '19700101')) + ); + } + + /** + * Use an invalid date format + * @expectedException Exception + * @expectedExceptionMessageRegExp /Invalid date format/ + */ + public function testFilterInvalidDayWithChars() + { + self::$linkFilter->filter(LegacyLinkFilter::$FILTER_DAY, 'Rainy day, dream away'); + } + + /** + * Use an invalid date format + * @expectedException Exception + * @expectedExceptionMessageRegExp /Invalid date format/ + */ + public function testFilterInvalidDayDigits() + { + self::$linkFilter->filter(LegacyLinkFilter::$FILTER_DAY, '20'); + } + + /** + * Retrieve a link entry with its hash + */ + public function testFilterSmallHash() + { + $links = self::$linkFilter->filter(LegacyLinkFilter::$FILTER_HASH, 'IuWvgA'); + + $this->assertEquals( + 1, + count($links) + ); + + $this->assertEquals( + 'MediaGoblin', + $links[7]['title'] + ); + } + + /** + * No link for this hash + * + * @expectedException \Shaarli\Bookmark\Exception\BookmarkNotFoundException + */ + public function testFilterUnknownSmallHash() + { + self::$linkFilter->filter(LegacyLinkFilter::$FILTER_HASH, 'Iblaah'); + } + + /** + * Full-text search - no result found. + */ + public function testFilterFullTextNoResult() + { + $this->assertEquals( + 0, + count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'azertyuiop')) + ); + } + + /** + * Full-text search - result from a link's URL + */ + public function testFilterFullTextURL() + { + $this->assertEquals( + 2, + count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'ars.userfriendly.org')) + ); + + $this->assertEquals( + 2, + count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'ars org')) + ); + } + + /** + * Full-text search - result from a link's title only + */ + public function testFilterFullTextTitle() + { + // use miscellaneous cases + $this->assertEquals( + 2, + count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'userfriendly -')) + ); + $this->assertEquals( + 2, + count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'UserFriendly -')) + ); + $this->assertEquals( + 2, + count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'uSeRFrIendlY -')) + ); + + // use miscellaneous case and offset + $this->assertEquals( + 2, + count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'RFrIendL')) + ); + } + + /** + * Full-text search - result from the link's description only + */ + public function testFilterFullTextDescription() + { + $this->assertEquals( + 1, + count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'publishing media')) + ); + + $this->assertEquals( + 1, + count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'mercurial w3c')) + ); + + $this->assertEquals( + 3, + count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, '"free software"')) + ); + } + + /** + * Full-text search - result from the link's tags only + */ + public function testFilterFullTextTags() + { + $this->assertEquals( + 6, + count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'web')) + ); + + $this->assertEquals( + 6, + count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'web', 'all')) + ); + + $this->assertEquals( + 6, + count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'web', 'bla')) + ); + + // Private only. + $this->assertEquals( + 1, + count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'web', false, 'private')) + ); + + // Public only. + $this->assertEquals( + 5, + count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'web', false, 'public')) + ); + } + + /** + * Full-text search - result set from mixed sources + */ + public function testFilterFullTextMixed() + { + $this->assertEquals( + 3, + count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'free software')) + ); + } + + /** + * Full-text search - test exclusion with '-'. + */ + public function testExcludeSearch() + { + $this->assertEquals( + 1, + count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'free -gnu')) + ); + + $this->assertEquals( + ReferenceLinkDB::$NB_LINKS_TOTAL - 1, + count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, '-revolution')) + ); + } + + /** + * Full-text search - test AND, exact terms and exclusion combined, across fields. + */ + public function testMultiSearch() + { + $this->assertEquals( + 2, + count(self::$linkFilter->filter( + LegacyLinkFilter::$FILTER_TEXT, + '"Free Software " stallman "read this" @website stuff' + )) + ); + + $this->assertEquals( + 1, + count(self::$linkFilter->filter( + LegacyLinkFilter::$FILTER_TEXT, + '"free software " stallman "read this" -beard @website stuff' + )) + ); + } + + /** + * Full-text search - make sure that exact search won't work across fields. + */ + public function testSearchExactTermMultiFieldsKo() + { + $this->assertEquals( + 0, + count(self::$linkFilter->filter( + LegacyLinkFilter::$FILTER_TEXT, + '"designer naming"' + )) + ); + + $this->assertEquals( + 0, + count(self::$linkFilter->filter( + LegacyLinkFilter::$FILTER_TEXT, + '"designernaming"' + )) + ); + } + + /** + * Tag search with exclusion. + */ + public function testTagFilterWithExclusion() + { + $this->assertEquals( + 1, + count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TAG, 'gnu -free')) + ); + + $this->assertEquals( + ReferenceLinkDB::$NB_LINKS_TOTAL - 1, + count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TAG, '-free')) + ); + } + + /** + * Test crossed search (terms + tags). + */ + public function testFilterCrossedSearch() + { + $terms = '"Free Software " stallman "read this" @website stuff'; + $tags = 'free'; + $this->assertEquals( + 1, + count(self::$linkFilter->filter( + LegacyLinkFilter::$FILTER_TAG | LegacyLinkFilter::$FILTER_TEXT, + array($tags, $terms) + )) + ); + $this->assertEquals( + 2, + count(self::$linkFilter->filter( + LegacyLinkFilter::$FILTER_TAG | LegacyLinkFilter::$FILTER_TEXT, + array('', $terms) + )) + ); + $this->assertEquals( + 1, + count(self::$linkFilter->filter( + LegacyLinkFilter::$FILTER_TAG | LegacyLinkFilter::$FILTER_TEXT, + array(false, 'PSR-2') + )) + ); + $this->assertEquals( + 1, + count(self::$linkFilter->filter( + LegacyLinkFilter::$FILTER_TAG | LegacyLinkFilter::$FILTER_TEXT, + array($tags, '') + )) + ); + $this->assertEquals( + ReferenceLinkDB::$NB_LINKS_TOTAL, + count(self::$linkFilter->filter( + LegacyLinkFilter::$FILTER_TAG | LegacyLinkFilter::$FILTER_TEXT, + '' + )) + ); + } + + /** + * Filter bookmarks by #hashtag. + */ + public function testFilterByHashtag() + { + $hashtag = 'hashtag'; + $this->assertEquals( + 3, + count(self::$linkFilter->filter( + LegacyLinkFilter::$FILTER_TAG, + $hashtag + )) + ); + + $hashtag = 'private'; + $this->assertEquals( + 1, + count(self::$linkFilter->filter( + LegacyLinkFilter::$FILTER_TAG, + $hashtag, + false, + 'private' + )) + ); + } +} diff --git a/tests/legacy/LegacyUpdaterTest.php b/tests/legacy/LegacyUpdaterTest.php new file mode 100644 index 00000000..7c429811 --- /dev/null +++ b/tests/legacy/LegacyUpdaterTest.php @@ -0,0 +1,886 @@ +conf = new ConfigManager(self::$configFile); + } + + /** + * Test UpdaterUtils::read_updates_file with an empty/missing file. + */ + public function testReadEmptyUpdatesFile() + { + $this->assertEquals(array(), UpdaterUtils::read_updates_file('')); + $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt'; + touch($updatesFile); + $this->assertEquals(array(), UpdaterUtils::read_updates_file($updatesFile)); + unlink($updatesFile); + } + + /** + * Test read/write updates file. + */ + public function testReadWriteUpdatesFile() + { + $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt'; + $updatesMethods = array('m1', 'm2', 'm3'); + + UpdaterUtils::write_updates_file($updatesFile, $updatesMethods); + $readMethods = UpdaterUtils::read_updates_file($updatesFile); + $this->assertEquals($readMethods, $updatesMethods); + + // Update + $updatesMethods[] = 'm4'; + UpdaterUtils::write_updates_file($updatesFile, $updatesMethods); + $readMethods = UpdaterUtils::read_updates_file($updatesFile); + $this->assertEquals($readMethods, $updatesMethods); + unlink($updatesFile); + } + + /** + * Test errors in UpdaterUtils::write_updates_file(): empty updates file. + * + * @expectedException Exception + * @expectedExceptionMessageRegExp /Updates file path is not set(.*)/ + */ + public function testWriteEmptyUpdatesFile() + { + UpdaterUtils::write_updates_file('', array('test')); + } + + /** + * Test errors in UpdaterUtils::write_updates_file(): not writable updates file. + * + * @expectedException Exception + * @expectedExceptionMessageRegExp /Unable to write(.*)/ + */ + public function testWriteUpdatesFileNotWritable() + { + $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt'; + touch($updatesFile); + chmod($updatesFile, 0444); + try { + @UpdaterUtils::write_updates_file($updatesFile, array('test')); + } catch (Exception $e) { + unlink($updatesFile); + throw $e; + } + } + + /** + * Test the update() method, with no update to run. + * 1. Everything already run. + * 2. User is logged out. + */ + public function testNoUpdates() + { + $updates = array( + 'updateMethodDummy1', + 'updateMethodDummy2', + 'updateMethodDummy3', + 'updateMethodException', + ); + $updater = new DummyUpdater($updates, array(), $this->conf, true); + $this->assertEquals(array(), $updater->update()); + + $updater = new DummyUpdater(array(), array(), $this->conf, false); + $this->assertEquals(array(), $updater->update()); + } + + /** + * Test the update() method, with all updates to run (except the failing one). + */ + public function testUpdatesFirstTime() + { + $updates = array('updateMethodException',); + $expectedUpdates = array( + 'updateMethodDummy1', + 'updateMethodDummy2', + 'updateMethodDummy3', + ); + $updater = new DummyUpdater($updates, array(), $this->conf, true); + $this->assertEquals($expectedUpdates, $updater->update()); + } + + /** + * Test the update() method, only one update to run. + */ + public function testOneUpdate() + { + $updates = array( + 'updateMethodDummy1', + 'updateMethodDummy3', + 'updateMethodException', + ); + $expectedUpdate = array('updateMethodDummy2'); + + $updater = new DummyUpdater($updates, array(), $this->conf, true); + $this->assertEquals($expectedUpdate, $updater->update()); + } + + /** + * Test Update failed. + * + * @expectedException \Exception + */ + public function testUpdateFailed() + { + $updates = array( + 'updateMethodDummy1', + 'updateMethodDummy2', + 'updateMethodDummy3', + ); + + $updater = new DummyUpdater($updates, array(), $this->conf, true); + $updater->update(); + } + + /** + * Test update mergeDeprecatedConfig: + * 1. init a config file. + * 2. init a options.php file with update value. + * 3. merge. + * 4. check updated value in config file. + */ + public function testUpdateMergeDeprecatedConfig() + { + $this->conf->setConfigFile('tests/utils/config/configPhp'); + $this->conf->reset(); + + $optionsFile = 'tests/updater/options.php'; + $options = 'conf->setConfigFile('tests/updater/config'); + + // merge configs + $updater = new LegacyUpdater(array(), array(), $this->conf, true); + // This writes a new config file in tests/updater/config.php + $updater->updateMethodMergeDeprecatedConfigFile(); + + // make sure updated field is changed + $this->conf->reload(); + $this->assertTrue($this->conf->get('privacy.default_private_links')); + $this->assertFalse(is_file($optionsFile)); + // Delete the generated file. + unlink($this->conf->getConfigFileExt()); + } + + /** + * Test mergeDeprecatedConfig in without options file. + */ + public function testMergeDeprecatedConfigNoFile() + { + $updater = new LegacyUpdater(array(), array(), $this->conf, true); + $updater->updateMethodMergeDeprecatedConfigFile(); + + $this->assertEquals('root', $this->conf->get('credentials.login')); + } + + /** + * Test renameDashTags update method. + */ + public function testRenameDashTags() + { + $refDB = new \ReferenceLinkDB(true); + $refDB->write(self::$testDatastore); + $linkDB = new LegacyLinkDB(self::$testDatastore, true, false); + + $this->assertEmpty($linkDB->filterSearch(array('searchtags' => 'exclude'))); + $updater = new LegacyUpdater(array(), $linkDB, $this->conf, true); + $updater->updateMethodRenameDashTags(); + $this->assertNotEmpty($linkDB->filterSearch(array('searchtags' => 'exclude'))); + } + + /** + * Convert old PHP config file to JSON config. + */ + public function testConfigToJson() + { + $configFile = 'tests/utils/config/configPhp'; + $this->conf->setConfigFile($configFile); + $this->conf->reset(); + + // The ConfigIO is initialized with ConfigPhp. + $this->assertTrue($this->conf->getConfigIO() instanceof ConfigPhp); + + $updater = new LegacyUpdater(array(), array(), $this->conf, false); + $done = $updater->updateMethodConfigToJson(); + $this->assertTrue($done); + + // The ConfigIO has been updated to ConfigJson. + $this->assertTrue($this->conf->getConfigIO() instanceof ConfigJson); + $this->assertTrue(file_exists($this->conf->getConfigFileExt())); + + // Check JSON config data. + $this->conf->reload(); + $this->assertEquals('root', $this->conf->get('credentials.login')); + $this->assertEquals('lala', $this->conf->get('redirector.url')); + $this->assertEquals('data/datastore.php', $this->conf->get('resource.datastore')); + $this->assertEquals('1', $this->conf->get('plugins.WALLABAG_VERSION')); + + rename($configFile . '.save.php', $configFile . '.php'); + unlink($this->conf->getConfigFileExt()); + } + + /** + * Launch config conversion update with an existing JSON file => nothing to do. + */ + public function testConfigToJsonNothingToDo() + { + $filetime = filemtime($this->conf->getConfigFileExt()); + $updater = new LegacyUpdater(array(), array(), $this->conf, false); + $done = $updater->updateMethodConfigToJson(); + $this->assertTrue($done); + $expected = filemtime($this->conf->getConfigFileExt()); + $this->assertEquals($expected, $filetime); + } + + /** + * Test escapeUnescapedConfig with valid data. + */ + public function testEscapeConfig() + { + $sandbox = 'sandbox/config'; + copy(self::$configFile . '.json.php', $sandbox . '.json.php'); + $this->conf = new ConfigManager($sandbox); + $title = ''; + $headerLink = ''; + $this->conf->set('general.title', $title); + $this->conf->set('general.header_link', $headerLink); + $updater = new LegacyUpdater(array(), array(), $this->conf, true); + $done = $updater->updateMethodEscapeUnescapedConfig(); + $this->assertTrue($done); + $this->conf->reload(); + $this->assertEquals(escape($title), $this->conf->get('general.title')); + $this->assertEquals(escape($headerLink), $this->conf->get('general.header_link')); + unlink($sandbox . '.json.php'); + } + + /** + * Test updateMethodApiSettings(): create default settings for the API (enabled + secret). + */ + public function testUpdateApiSettings() + { + $confFile = 'sandbox/config'; + copy(self::$configFile .'.json.php', $confFile .'.json.php'); + $conf = new ConfigManager($confFile); + $updater = new LegacyUpdater(array(), array(), $conf, true); + + $this->assertFalse($conf->exists('api.enabled')); + $this->assertFalse($conf->exists('api.secret')); + $updater->updateMethodApiSettings(); + $conf->reload(); + $this->assertTrue($conf->get('api.enabled')); + $this->assertTrue($conf->exists('api.secret')); + unlink($confFile .'.json.php'); + } + + /** + * Test updateMethodApiSettings(): already set, do nothing. + */ + public function testUpdateApiSettingsNothingToDo() + { + $confFile = 'sandbox/config'; + copy(self::$configFile .'.json.php', $confFile .'.json.php'); + $conf = new ConfigManager($confFile); + $conf->set('api.enabled', false); + $conf->set('api.secret', ''); + $updater = new LegacyUpdater(array(), array(), $conf, true); + $updater->updateMethodApiSettings(); + $this->assertFalse($conf->get('api.enabled')); + $this->assertEmpty($conf->get('api.secret')); + unlink($confFile .'.json.php'); + } + + /** + * Test updateMethodDatastoreIds(). + */ + public function testDatastoreIds() + { + $links = array( + '20121206_182539' => array( + 'linkdate' => '20121206_182539', + 'title' => 'Geek and Poke', + 'url' => 'http://geek-and-poke.com/', + 'description' => 'desc', + 'tags' => 'dev cartoon tag1 tag2 tag3 tag4 ', + 'updated' => '20121206_190301', + 'private' => false, + ), + '20121206_172539' => array( + 'linkdate' => '20121206_172539', + 'title' => 'UserFriendly - Samba', + 'url' => 'http://ars.userfriendly.org/cartoons/?id=20010306', + 'description' => '', + 'tags' => 'samba cartoon web', + 'private' => false, + ), + '20121206_142300' => array( + 'linkdate' => '20121206_142300', + 'title' => 'UserFriendly - Web Designer', + 'url' => 'http://ars.userfriendly.org/cartoons/?id=20121206', + 'description' => 'Naming conventions... #private', + 'tags' => 'samba cartoon web', + 'private' => true, + ), + ); + $refDB = new \ReferenceLinkDB(true); + $refDB->setLinks($links); + $refDB->write(self::$testDatastore); + $linkDB = new LegacyLinkDB(self::$testDatastore, true, false); + + $checksum = hash_file('sha1', self::$testDatastore); + + $this->conf->set('resource.data_dir', 'sandbox'); + $this->conf->set('resource.datastore', self::$testDatastore); + + $updater = new LegacyUpdater(array(), $linkDB, $this->conf, true); + $this->assertTrue($updater->updateMethodDatastoreIds()); + + $linkDB = new LegacyLinkDB(self::$testDatastore, true, false); + + $backupFiles = glob($this->conf->get('resource.data_dir') . '/datastore.'. date('YmdH') .'*.php'); + $backup = null; + foreach ($backupFiles as $backupFile) { + if (strpos($backupFile, '_1') === false) { + $backup = $backupFile; + } + } + $this->assertNotNull($backup); + $this->assertFileExists($backup); + $this->assertEquals($checksum, hash_file('sha1', $backup)); + unlink($backup); + + $this->assertEquals(3, count($linkDB)); + $this->assertTrue(isset($linkDB[0])); + $this->assertFalse(isset($linkDB[0]['linkdate'])); + $this->assertEquals(0, $linkDB[0]['id']); + $this->assertEquals('UserFriendly - Web Designer', $linkDB[0]['title']); + $this->assertEquals('http://ars.userfriendly.org/cartoons/?id=20121206', $linkDB[0]['url']); + $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(Bookmark::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(Bookmark::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(Bookmark::LINK_DATE_FORMAT, '20121206_182539'), + $linkDB[2]['created'] + ); + $this->assertEquals( + DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20121206_190301'), + $linkDB[2]['updated'] + ); + } + + /** + * Test updateMethodDatastoreIds() with the update already applied: nothing to do. + */ + public function testDatastoreIdsNothingToDo() + { + $refDB = new \ReferenceLinkDB(true); + $refDB->write(self::$testDatastore); + $linkDB = new LegacyLinkDB(self::$testDatastore, true, false); + + $this->conf->set('resource.data_dir', 'sandbox'); + $this->conf->set('resource.datastore', self::$testDatastore); + + $checksum = hash_file('sha1', self::$testDatastore); + $updater = new LegacyUpdater(array(), $linkDB, $this->conf, true); + $this->assertTrue($updater->updateMethodDatastoreIds()); + $this->assertEquals($checksum, hash_file('sha1', self::$testDatastore)); + } + + /** + * Test defaultTheme update with default settings: nothing to do. + */ + public function testDefaultThemeWithDefaultSettings() + { + $sandbox = 'sandbox/config'; + copy(self::$configFile . '.json.php', $sandbox . '.json.php'); + $this->conf = new ConfigManager($sandbox); + $updater = new LegacyUpdater([], [], $this->conf, true); + $this->assertTrue($updater->updateMethodDefaultTheme()); + + $this->assertEquals('tpl/', $this->conf->get('resource.raintpl_tpl')); + $this->assertEquals('default', $this->conf->get('resource.theme')); + $this->conf = new ConfigManager($sandbox); + $this->assertEquals('tpl/', $this->conf->get('resource.raintpl_tpl')); + $this->assertEquals('default', $this->conf->get('resource.theme')); + unlink($sandbox . '.json.php'); + } + + /** + * Test defaultTheme update with a custom theme in a subfolder + */ + public function testDefaultThemeWithCustomTheme() + { + $theme = 'iamanartist'; + $sandbox = 'sandbox/config'; + copy(self::$configFile . '.json.php', $sandbox . '.json.php'); + $this->conf = new ConfigManager($sandbox); + mkdir('sandbox/'. $theme); + touch('sandbox/'. $theme .'/linklist.html'); + $this->conf->set('resource.raintpl_tpl', 'sandbox/'. $theme .'/'); + $updater = new LegacyUpdater([], [], $this->conf, true); + $this->assertTrue($updater->updateMethodDefaultTheme()); + + $this->assertEquals('sandbox', $this->conf->get('resource.raintpl_tpl')); + $this->assertEquals($theme, $this->conf->get('resource.theme')); + $this->conf = new ConfigManager($sandbox); + $this->assertEquals('sandbox', $this->conf->get('resource.raintpl_tpl')); + $this->assertEquals($theme, $this->conf->get('resource.theme')); + unlink($sandbox . '.json.php'); + unlink('sandbox/'. $theme .'/linklist.html'); + rmdir('sandbox/'. $theme); + } + + /** + * Test updateMethodEscapeMarkdown with markdown plugin enabled + * => setting markdown_escape set to false. + */ + public function testEscapeMarkdownSettingToFalse() + { + $sandboxConf = 'sandbox/config'; + copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); + $this->conf = new ConfigManager($sandboxConf); + + $this->conf->set('general.enabled_plugins', ['markdown']); + $updater = new LegacyUpdater([], [], $this->conf, true); + $this->assertTrue($updater->updateMethodEscapeMarkdown()); + $this->assertFalse($this->conf->get('security.markdown_escape')); + + // reload from file + $this->conf = new ConfigManager($sandboxConf); + $this->assertFalse($this->conf->get('security.markdown_escape')); + } + + + /** + * Test updateMethodEscapeMarkdown with markdown plugin disabled + * => setting markdown_escape set to true. + */ + public function testEscapeMarkdownSettingToTrue() + { + $sandboxConf = 'sandbox/config'; + copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); + $this->conf = new ConfigManager($sandboxConf); + + $this->conf->set('general.enabled_plugins', []); + $updater = new LegacyUpdater([], [], $this->conf, true); + $this->assertTrue($updater->updateMethodEscapeMarkdown()); + $this->assertTrue($this->conf->get('security.markdown_escape')); + + // reload from file + $this->conf = new ConfigManager($sandboxConf); + $this->assertTrue($this->conf->get('security.markdown_escape')); + } + + /** + * Test updateMethodEscapeMarkdown with nothing to do (setting already enabled) + */ + public function testEscapeMarkdownSettingNothingToDoEnabled() + { + $sandboxConf = 'sandbox/config'; + copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); + $this->conf = new ConfigManager($sandboxConf); + $this->conf->set('security.markdown_escape', true); + $updater = new LegacyUpdater([], [], $this->conf, true); + $this->assertTrue($updater->updateMethodEscapeMarkdown()); + $this->assertTrue($this->conf->get('security.markdown_escape')); + } + + /** + * Test updateMethodEscapeMarkdown with nothing to do (setting already disabled) + */ + public function testEscapeMarkdownSettingNothingToDoDisabled() + { + $this->conf->set('security.markdown_escape', false); + $updater = new LegacyUpdater([], [], $this->conf, true); + $this->assertTrue($updater->updateMethodEscapeMarkdown()); + $this->assertFalse($this->conf->get('security.markdown_escape')); + } + + /** + * Test updateMethodPiwikUrl with valid data + */ + public function testUpdatePiwikUrlValid() + { + $sandboxConf = 'sandbox/config'; + copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); + $this->conf = new ConfigManager($sandboxConf); + $url = 'mypiwik.tld'; + $this->conf->set('plugins.PIWIK_URL', $url); + $updater = new LegacyUpdater([], [], $this->conf, true); + $this->assertTrue($updater->updateMethodPiwikUrl()); + $this->assertEquals('http://'. $url, $this->conf->get('plugins.PIWIK_URL')); + + // reload from file + $this->conf = new ConfigManager($sandboxConf); + $this->assertEquals('http://'. $url, $this->conf->get('plugins.PIWIK_URL')); + } + + /** + * Test updateMethodPiwikUrl without setting + */ + public function testUpdatePiwikUrlEmpty() + { + $updater = new LegacyUpdater([], [], $this->conf, true); + $this->assertTrue($updater->updateMethodPiwikUrl()); + $this->assertEmpty($this->conf->get('plugins.PIWIK_URL')); + } + + /** + * Test updateMethodPiwikUrl: valid URL, nothing to do + */ + public function testUpdatePiwikUrlNothingToDo() + { + $url = 'https://mypiwik.tld'; + $this->conf->set('plugins.PIWIK_URL', $url); + $updater = new LegacyUpdater([], [], $this->conf, true); + $this->assertTrue($updater->updateMethodPiwikUrl()); + $this->assertEquals($url, $this->conf->get('plugins.PIWIK_URL')); + } + + /** + * Test updateMethodAtomDefault with show_atom set to false + * => update to true. + */ + public function testUpdateMethodAtomDefault() + { + $sandboxConf = 'sandbox/config'; + copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); + $this->conf = new ConfigManager($sandboxConf); + $this->conf->set('feed.show_atom', false); + $updater = new LegacyUpdater([], [], $this->conf, true); + $this->assertTrue($updater->updateMethodAtomDefault()); + $this->assertTrue($this->conf->get('feed.show_atom')); + // reload from file + $this->conf = new ConfigManager($sandboxConf); + $this->assertTrue($this->conf->get('feed.show_atom')); + } + /** + * Test updateMethodAtomDefault with show_atom not set. + * => nothing to do + */ + public function testUpdateMethodAtomDefaultNoExist() + { + $sandboxConf = 'sandbox/config'; + copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); + $this->conf = new ConfigManager($sandboxConf); + $updater = new LegacyUpdater([], [], $this->conf, true); + $this->assertTrue($updater->updateMethodAtomDefault()); + $this->assertTrue($this->conf->get('feed.show_atom')); + } + /** + * Test updateMethodAtomDefault with show_atom set to true. + * => nothing to do + */ + public function testUpdateMethodAtomDefaultAlreadyTrue() + { + $sandboxConf = 'sandbox/config'; + copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); + $this->conf = new ConfigManager($sandboxConf); + $this->conf->set('feed.show_atom', true); + $updater = new LegacyUpdater([], [], $this->conf, true); + $this->assertTrue($updater->updateMethodAtomDefault()); + $this->assertTrue($this->conf->get('feed.show_atom')); + } + + /** + * Test updateMethodDownloadSizeAndTimeoutConf, it should be set if none is already defined. + */ + public function testUpdateMethodDownloadSizeAndTimeoutConf() + { + $sandboxConf = 'sandbox/config'; + copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); + $this->conf = new ConfigManager($sandboxConf); + $updater = new LegacyUpdater([], [], $this->conf, true); + $this->assertTrue($updater->updateMethodDownloadSizeAndTimeoutConf()); + $this->assertEquals(4194304, $this->conf->get('general.download_max_size')); + $this->assertEquals(30, $this->conf->get('general.download_timeout')); + + $this->conf = new ConfigManager($sandboxConf); + $this->assertEquals(4194304, $this->conf->get('general.download_max_size')); + $this->assertEquals(30, $this->conf->get('general.download_timeout')); + } + + /** + * Test updateMethodDownloadSizeAndTimeoutConf, it shouldn't be set if it is already defined. + */ + public function testUpdateMethodDownloadSizeAndTimeoutConfIgnore() + { + $sandboxConf = 'sandbox/config'; + copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); + $this->conf = new ConfigManager($sandboxConf); + $this->conf->set('general.download_max_size', 38); + $this->conf->set('general.download_timeout', 70); + $updater = new LegacyUpdater([], [], $this->conf, true); + $this->assertTrue($updater->updateMethodDownloadSizeAndTimeoutConf()); + $this->assertEquals(38, $this->conf->get('general.download_max_size')); + $this->assertEquals(70, $this->conf->get('general.download_timeout')); + } + + /** + * Test updateMethodDownloadSizeAndTimeoutConf, only the maz size should be set here. + */ + public function testUpdateMethodDownloadSizeAndTimeoutConfOnlySize() + { + $sandboxConf = 'sandbox/config'; + copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); + $this->conf = new ConfigManager($sandboxConf); + $this->conf->set('general.download_max_size', 38); + $updater = new LegacyUpdater([], [], $this->conf, true); + $this->assertTrue($updater->updateMethodDownloadSizeAndTimeoutConf()); + $this->assertEquals(38, $this->conf->get('general.download_max_size')); + $this->assertEquals(30, $this->conf->get('general.download_timeout')); + } + + /** + * Test updateMethodDownloadSizeAndTimeoutConf, only the time out should be set here. + */ + public function testUpdateMethodDownloadSizeAndTimeoutConfOnlyTimeout() + { + $sandboxConf = 'sandbox/config'; + copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); + $this->conf = new ConfigManager($sandboxConf); + $this->conf->set('general.download_timeout', 3); + $updater = new LegacyUpdater([], [], $this->conf, true); + $this->assertTrue($updater->updateMethodDownloadSizeAndTimeoutConf()); + $this->assertEquals(4194304, $this->conf->get('general.download_max_size')); + $this->assertEquals(3, $this->conf->get('general.download_timeout')); + } + + /** + * Test updateMethodWebThumbnailer with thumbnails enabled. + */ + public function testUpdateMethodWebThumbnailerEnabled() + { + $this->conf->remove('thumbnails'); + $this->conf->set('thumbnail.enable_thumbnails', true); + $updater = new LegacyUpdater([], [], $this->conf, true, $_SESSION); + $this->assertTrue($updater->updateMethodWebThumbnailer()); + $this->assertFalse($this->conf->exists('thumbnail')); + $this->assertEquals(\Shaarli\Thumbnailer::MODE_ALL, $this->conf->get('thumbnails.mode')); + $this->assertEquals(125, $this->conf->get('thumbnails.width')); + $this->assertEquals(90, $this->conf->get('thumbnails.height')); + $this->assertContains('You have enabled or changed thumbnails', $_SESSION['warnings'][0]); + } + + /** + * Test updateMethodWebThumbnailer with thumbnails disabled. + */ + public function testUpdateMethodWebThumbnailerDisabled() + { + if (isset($_SESSION['warnings'])) { + unset($_SESSION['warnings']); + } + + $this->conf->remove('thumbnails'); + $this->conf->set('thumbnail.enable_thumbnails', false); + $updater = new LegacyUpdater([], [], $this->conf, true, $_SESSION); + $this->assertTrue($updater->updateMethodWebThumbnailer()); + $this->assertFalse($this->conf->exists('thumbnail')); + $this->assertEquals(Thumbnailer::MODE_NONE, $this->conf->get('thumbnails.mode')); + $this->assertEquals(125, $this->conf->get('thumbnails.width')); + $this->assertEquals(90, $this->conf->get('thumbnails.height')); + $this->assertTrue(empty($_SESSION['warnings'])); + } + + /** + * Test updateMethodWebThumbnailer with thumbnails disabled. + */ + public function testUpdateMethodWebThumbnailerNothingToDo() + { + if (isset($_SESSION['warnings'])) { + unset($_SESSION['warnings']); + } + + $updater = new LegacyUpdater([], [], $this->conf, true, $_SESSION); + $this->assertTrue($updater->updateMethodWebThumbnailer()); + $this->assertFalse($this->conf->exists('thumbnail')); + $this->assertEquals(Thumbnailer::MODE_COMMON, $this->conf->get('thumbnails.mode')); + $this->assertEquals(90, $this->conf->get('thumbnails.width')); + $this->assertEquals(53, $this->conf->get('thumbnails.height')); + $this->assertTrue(empty($_SESSION['warnings'])); + } + + /** + * Test updateMethodSetSticky(). + */ + public function testUpdateStickyValid() + { + $blank = [ + 'id' => 1, + 'url' => 'z', + 'title' => '', + 'description' => '', + 'tags' => '', + 'created' => new DateTime(), + ]; + $links = [ + 1 => ['id' => 1] + $blank, + 2 => ['id' => 2] + $blank, + ]; + $refDB = new \ReferenceLinkDB(true); + $refDB->setLinks($links); + $refDB->write(self::$testDatastore); + $linkDB = new LegacyLinkDB(self::$testDatastore, true, false); + + $updater = new LegacyUpdater(array(), $linkDB, $this->conf, true); + $this->assertTrue($updater->updateMethodSetSticky()); + + $linkDB = new LegacyLinkDB(self::$testDatastore, true, false); + foreach ($linkDB as $link) { + $this->assertFalse($link['sticky']); + } + } + + /** + * Test updateMethodSetSticky(). + */ + public function testUpdateStickyNothingToDo() + { + $blank = [ + 'id' => 1, + 'url' => 'z', + 'title' => '', + 'description' => '', + 'tags' => '', + 'created' => new DateTime(), + ]; + $links = [ + 1 => ['id' => 1, 'sticky' => true] + $blank, + 2 => ['id' => 2] + $blank, + ]; + $refDB = new \ReferenceLinkDB(true); + $refDB->setLinks($links); + $refDB->write(self::$testDatastore); + $linkDB = new LegacyLinkDB(self::$testDatastore, true, false); + + $updater = new LegacyUpdater(array(), $linkDB, $this->conf, true); + $this->assertTrue($updater->updateMethodSetSticky()); + + $linkDB = new LegacyLinkDB(self::$testDatastore, true, false); + $this->assertTrue($linkDB[1]['sticky']); + } + + /** + * Test updateMethodRemoveRedirector(). + */ + public function testUpdateRemoveRedirector() + { + $sandboxConf = 'sandbox/config'; + copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); + $this->conf = new ConfigManager($sandboxConf); + $updater = new LegacyUpdater([], null, $this->conf, true); + $this->assertTrue($updater->updateMethodRemoveRedirector()); + $this->assertFalse($this->conf->exists('redirector')); + $this->conf = new ConfigManager($sandboxConf); + $this->assertFalse($this->conf->exists('redirector')); + } + + /** + * Test updateMethodFormatterSetting() + */ + public function testUpdateMethodFormatterSettingDefault() + { + $sandboxConf = 'sandbox/config'; + copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); + $this->conf = new ConfigManager($sandboxConf); + $this->conf->set('formatter', 'default'); + $updater = new LegacyUpdater([], null, $this->conf, true); + $enabledPlugins = $this->conf->get('general.enabled_plugins'); + $this->assertFalse(in_array('markdown', $enabledPlugins)); + $this->assertTrue($updater->updateMethodFormatterSetting()); + $this->assertEquals('default', $this->conf->get('formatter')); + $this->assertEquals($enabledPlugins, $this->conf->get('general.enabled_plugins')); + + $this->conf = new ConfigManager($sandboxConf); + $this->assertEquals('default', $this->conf->get('formatter')); + $this->assertEquals($enabledPlugins, $this->conf->get('general.enabled_plugins')); + } + + /** + * Test updateMethodFormatterSetting() + */ + public function testUpdateMethodFormatterSettingMarkdown() + { + $sandboxConf = 'sandbox/config'; + copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); + $this->conf = new ConfigManager($sandboxConf); + $this->conf->set('formatter', 'default'); + $updater = new LegacyUpdater([], null, $this->conf, true); + $enabledPlugins = $this->conf->get('general.enabled_plugins'); + $enabledPlugins[] = 'markdown'; + $this->conf->set('general.enabled_plugins', $enabledPlugins); + + $this->assertTrue(in_array('markdown', $this->conf->get('general.enabled_plugins'))); + $this->assertTrue($updater->updateMethodFormatterSetting()); + $this->assertEquals('markdown', $this->conf->get('formatter')); + $this->assertFalse(in_array('markdown', $this->conf->get('general.enabled_plugins'))); + + $this->conf = new ConfigManager($sandboxConf); + $this->assertEquals('markdown', $this->conf->get('formatter')); + $this->assertFalse(in_array('markdown', $this->conf->get('general.enabled_plugins'))); + } +} -- cgit v1.2.3 From b0428aa9b02b058b72c40b6e8dc2298d55bf692f Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Thu, 23 Jan 2020 21:13:41 +0100 Subject: Migrate cache purge function to a proper class And update dependencies and tests. Note that SESSION['tags'] has been removed a log ago --- tests/legacy/LegacyLinkDBTest.php | 1 - 1 file changed, 1 deletion(-) (limited to 'tests/legacy') diff --git a/tests/legacy/LegacyLinkDBTest.php b/tests/legacy/LegacyLinkDBTest.php index 17b2b0e6..0884ad03 100644 --- a/tests/legacy/LegacyLinkDBTest.php +++ b/tests/legacy/LegacyLinkDBTest.php @@ -11,7 +11,6 @@ use ReflectionClass; use Shaarli; use Shaarli\Bookmark\Bookmark; -require_once 'application/feed/Cache.php'; require_once 'application/Utils.php'; require_once 'tests/utils/ReferenceLinkDB.php'; -- cgit v1.2.3 From 1a8ac737e52cb25a5c346232ee398f5908cee7d7 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Mon, 6 Jul 2020 08:04:35 +0200 Subject: Process main page (linklist) through Slim controller Including a bunch of improvements on the container, and helper used across new controllers. --- tests/legacy/LegacyControllerTest.php | 99 +++++++ tests/legacy/LegacyRouterTest.php | 512 ++++++++++++++++++++++++++++++++++ 2 files changed, 611 insertions(+) create mode 100644 tests/legacy/LegacyControllerTest.php create mode 100644 tests/legacy/LegacyRouterTest.php (limited to 'tests/legacy') diff --git a/tests/legacy/LegacyControllerTest.php b/tests/legacy/LegacyControllerTest.php new file mode 100644 index 00000000..ff4520a3 --- /dev/null +++ b/tests/legacy/LegacyControllerTest.php @@ -0,0 +1,99 @@ +createContainer(); + + $this->controller = new LegacyController($this->container); + } + + /** + * @dataProvider getProcessProvider + */ + public function testProcess(string $legacyRoute, array $queryParameters, string $slimRoute, bool $isLoggedIn): void + { + $request = $this->createMock(Request::class); + $request->method('getQueryParams')->willReturn($queryParameters); + $request + ->method('getParam') + ->willReturnCallback(function (string $key) use ($queryParameters): ?string { + return $queryParameters[$key] ?? null; + }) + ; + $response = new Response(); + + $this->container->loginManager->method('isLoggedIn')->willReturn($isLoggedIn); + + $result = $this->controller->process($request, $response, $legacyRoute); + + static::assertSame('/subfolder' . $slimRoute, $result->getHeader('location')[0]); + } + + public function testProcessNotFound(): void + { + $request = $this->createMock(Request::class); + $response = new Response(); + + $this->expectException(UnknowLegacyRouteException::class); + + $this->controller->process($request, $response, 'nope'); + } + + /** + * @return array[] Parameters: + * - string legacyRoute + * - array queryParameters + * - string slimRoute + * - bool isLoggedIn + */ + public function getProcessProvider(): array + { + return [ + ['post', [], '/admin/shaare', true], + ['post', [], '/login', false], + ['post', ['title' => 'test'], '/admin/shaare?title=test', true], + ['post', ['title' => 'test'], '/login?title=test', false], + ['addlink', [], '/admin/add-shaare', true], + ['addlink', [], '/login', false], + ['login', [], '/login', true], + ['login', [], '/login', false], + ['logout', [], '/logout', true], + ['logout', [], '/logout', false], + ['picwall', [], '/picture-wall', false], + ['picwall', [], '/picture-wall', true], + ['tagcloud', [], '/tags/cloud', false], + ['tagcloud', [], '/tags/cloud', true], + ['taglist', [], '/tags/list', false], + ['taglist', [], '/tags/list', true], + ['daily', [], '/daily', false], + ['daily', [], '/daily', true], + ['daily', ['day' => '123456789', 'discard' => '1'], '/daily?day=123456789', false], + ['rss', [], '/feed/rss', false], + ['rss', [], '/feed/rss', true], + ['rss', ['search' => 'filter123', 'other' => 'param'], '/feed/rss?search=filter123&other=param', false], + ['atom', [], '/feed/atom', false], + ['atom', [], '/feed/atom', true], + ['atom', ['search' => 'filter123', 'other' => 'param'], '/feed/atom?search=filter123&other=param', false], + ['opensearch', [], '/open-search', false], + ['opensearch', [], '/open-search', true], + ['dailyrss', [], '/daily-rss', false], + ['dailyrss', [], '/daily-rss', true], + ]; + } +} diff --git a/tests/legacy/LegacyRouterTest.php b/tests/legacy/LegacyRouterTest.php new file mode 100644 index 00000000..c2019ca7 --- /dev/null +++ b/tests/legacy/LegacyRouterTest.php @@ -0,0 +1,512 @@ +assertEquals( + LegacyRouter::$PAGE_LOGIN, + LegacyRouter::findPage('do=login', array(), false) + ); + + $this->assertEquals( + LegacyRouter::$PAGE_LOGIN, + LegacyRouter::findPage('do=login', array(), 1) + ); + + $this->assertEquals( + LegacyRouter::$PAGE_LOGIN, + LegacyRouter::findPage('do=login&stuff', array(), false) + ); + } + + /** + * Test findPage: login page output. + * Invalid: page shouldn't be return. + * + * @return void + */ + public function testFindPageLoginInvalid() + { + $this->assertNotEquals( + LegacyRouter::$PAGE_LOGIN, + LegacyRouter::findPage('do=login', array(), true) + ); + + $this->assertNotEquals( + LegacyRouter::$PAGE_LOGIN, + LegacyRouter::findPage('do=other', array(), false) + ); + } + + /** + * Test findPage: picwall page output. + * Valid: page should be return. + * + * @return void + */ + public function testFindPagePicwallValid() + { + $this->assertEquals( + LegacyRouter::$PAGE_PICWALL, + LegacyRouter::findPage('do=picwall', array(), false) + ); + + $this->assertEquals( + LegacyRouter::$PAGE_PICWALL, + LegacyRouter::findPage('do=picwall', array(), true) + ); + } + + /** + * Test findPage: picwall page output. + * Invalid: page shouldn't be return. + * + * @return void + */ + public function testFindPagePicwallInvalid() + { + $this->assertEquals( + LegacyRouter::$PAGE_PICWALL, + LegacyRouter::findPage('do=picwall&stuff', array(), false) + ); + + $this->assertNotEquals( + LegacyRouter::$PAGE_PICWALL, + LegacyRouter::findPage('do=other', array(), false) + ); + } + + /** + * Test findPage: tagcloud page output. + * Valid: page should be return. + * + * @return void + */ + public function testFindPageTagcloudValid() + { + $this->assertEquals( + LegacyRouter::$PAGE_TAGCLOUD, + LegacyRouter::findPage('do=tagcloud', array(), false) + ); + + $this->assertEquals( + LegacyRouter::$PAGE_TAGCLOUD, + LegacyRouter::findPage('do=tagcloud', array(), true) + ); + + $this->assertEquals( + LegacyRouter::$PAGE_TAGCLOUD, + LegacyRouter::findPage('do=tagcloud&stuff', array(), false) + ); + } + + /** + * Test findPage: tagcloud page output. + * Invalid: page shouldn't be return. + * + * @return void + */ + public function testFindPageTagcloudInvalid() + { + $this->assertNotEquals( + LegacyRouter::$PAGE_TAGCLOUD, + LegacyRouter::findPage('do=other', array(), false) + ); + } + + /** + * Test findPage: linklist page output. + * Valid: page should be return. + * + * @return void + */ + public function testFindPageLinklistValid() + { + $this->assertEquals( + LegacyRouter::$PAGE_LINKLIST, + LegacyRouter::findPage('', array(), true) + ); + + $this->assertEquals( + LegacyRouter::$PAGE_LINKLIST, + LegacyRouter::findPage('whatever', array(), true) + ); + + $this->assertEquals( + LegacyRouter::$PAGE_LINKLIST, + LegacyRouter::findPage('whatever', array(), false) + ); + + $this->assertEquals( + LegacyRouter::$PAGE_LINKLIST, + LegacyRouter::findPage('do=tools', array(), false) + ); + } + + /** + * Test findPage: tools page output. + * Valid: page should be return. + * + * @return void + */ + public function testFindPageToolsValid() + { + $this->assertEquals( + LegacyRouter::$PAGE_TOOLS, + LegacyRouter::findPage('do=tools', array(), true) + ); + + $this->assertEquals( + LegacyRouter::$PAGE_TOOLS, + LegacyRouter::findPage('do=tools&stuff', array(), true) + ); + } + + /** + * Test findPage: tools page output. + * Invalid: page shouldn't be return. + * + * @return void + */ + public function testFindPageToolsInvalid() + { + $this->assertNotEquals( + LegacyRouter::$PAGE_TOOLS, + LegacyRouter::findPage('do=tools', array(), 1) + ); + + $this->assertNotEquals( + LegacyRouter::$PAGE_TOOLS, + LegacyRouter::findPage('do=tools', array(), false) + ); + + $this->assertNotEquals( + LegacyRouter::$PAGE_TOOLS, + LegacyRouter::findPage('do=other', array(), true) + ); + } + + /** + * Test findPage: changepasswd page output. + * Valid: page should be return. + * + * @return void + */ + public function testFindPageChangepasswdValid() + { + $this->assertEquals( + LegacyRouter::$PAGE_CHANGEPASSWORD, + LegacyRouter::findPage('do=changepasswd', array(), true) + ); + $this->assertEquals( + LegacyRouter::$PAGE_CHANGEPASSWORD, + LegacyRouter::findPage('do=changepasswd&stuff', array(), true) + ); + } + + /** + * Test findPage: changepasswd page output. + * Invalid: page shouldn't be return. + * + * @return void + */ + public function testFindPageChangepasswdInvalid() + { + $this->assertNotEquals( + LegacyRouter::$PAGE_CHANGEPASSWORD, + LegacyRouter::findPage('do=changepasswd', array(), 1) + ); + + $this->assertNotEquals( + LegacyRouter::$PAGE_CHANGEPASSWORD, + LegacyRouter::findPage('do=changepasswd', array(), false) + ); + + $this->assertNotEquals( + LegacyRouter::$PAGE_CHANGEPASSWORD, + LegacyRouter::findPage('do=other', array(), true) + ); + } + /** + * Test findPage: configure page output. + * Valid: page should be return. + * + * @return void + */ + public function testFindPageConfigureValid() + { + $this->assertEquals( + LegacyRouter::$PAGE_CONFIGURE, + LegacyRouter::findPage('do=configure', array(), true) + ); + + $this->assertEquals( + LegacyRouter::$PAGE_CONFIGURE, + LegacyRouter::findPage('do=configure&stuff', array(), true) + ); + } + + /** + * Test findPage: configure page output. + * Invalid: page shouldn't be return. + * + * @return void + */ + public function testFindPageConfigureInvalid() + { + $this->assertNotEquals( + LegacyRouter::$PAGE_CONFIGURE, + LegacyRouter::findPage('do=configure', array(), 1) + ); + + $this->assertNotEquals( + LegacyRouter::$PAGE_CONFIGURE, + LegacyRouter::findPage('do=configure', array(), false) + ); + + $this->assertNotEquals( + LegacyRouter::$PAGE_CONFIGURE, + LegacyRouter::findPage('do=other', array(), true) + ); + } + + /** + * Test findPage: changetag page output. + * Valid: page should be return. + * + * @return void + */ + public function testFindPageChangetagValid() + { + $this->assertEquals( + LegacyRouter::$PAGE_CHANGETAG, + LegacyRouter::findPage('do=changetag', array(), true) + ); + + $this->assertEquals( + LegacyRouter::$PAGE_CHANGETAG, + LegacyRouter::findPage('do=changetag&stuff', array(), true) + ); + } + + /** + * Test findPage: changetag page output. + * Invalid: page shouldn't be return. + * + * @return void + */ + public function testFindPageChangetagInvalid() + { + $this->assertNotEquals( + LegacyRouter::$PAGE_CHANGETAG, + LegacyRouter::findPage('do=changetag', array(), 1) + ); + + $this->assertNotEquals( + LegacyRouter::$PAGE_CHANGETAG, + LegacyRouter::findPage('do=changetag', array(), false) + ); + + $this->assertNotEquals( + LegacyRouter::$PAGE_CHANGETAG, + LegacyRouter::findPage('do=other', array(), true) + ); + } + + /** + * Test findPage: addlink page output. + * Valid: page should be return. + * + * @return void + */ + public function testFindPageAddlinkValid() + { + $this->assertEquals( + LegacyRouter::$PAGE_ADDLINK, + LegacyRouter::findPage('do=addlink', array(), true) + ); + + $this->assertEquals( + LegacyRouter::$PAGE_ADDLINK, + LegacyRouter::findPage('do=addlink&stuff', array(), true) + ); + } + + /** + * Test findPage: addlink page output. + * Invalid: page shouldn't be return. + * + * @return void + */ + public function testFindPageAddlinkInvalid() + { + $this->assertNotEquals( + LegacyRouter::$PAGE_ADDLINK, + LegacyRouter::findPage('do=addlink', array(), 1) + ); + + $this->assertNotEquals( + LegacyRouter::$PAGE_ADDLINK, + LegacyRouter::findPage('do=addlink', array(), false) + ); + + $this->assertNotEquals( + LegacyRouter::$PAGE_ADDLINK, + LegacyRouter::findPage('do=other', array(), true) + ); + } + + /** + * Test findPage: export page output. + * Valid: page should be return. + * + * @return void + */ + public function testFindPageExportValid() + { + $this->assertEquals( + LegacyRouter::$PAGE_EXPORT, + LegacyRouter::findPage('do=export', array(), true) + ); + + $this->assertEquals( + LegacyRouter::$PAGE_EXPORT, + LegacyRouter::findPage('do=export&stuff', array(), true) + ); + } + + /** + * Test findPage: export page output. + * Invalid: page shouldn't be return. + * + * @return void + */ + public function testFindPageExportInvalid() + { + $this->assertNotEquals( + LegacyRouter::$PAGE_EXPORT, + LegacyRouter::findPage('do=export', array(), 1) + ); + + $this->assertNotEquals( + LegacyRouter::$PAGE_EXPORT, + LegacyRouter::findPage('do=export', array(), false) + ); + + $this->assertNotEquals( + LegacyRouter::$PAGE_EXPORT, + LegacyRouter::findPage('do=other', array(), true) + ); + } + + /** + * Test findPage: import page output. + * Valid: page should be return. + * + * @return void + */ + public function testFindPageImportValid() + { + $this->assertEquals( + LegacyRouter::$PAGE_IMPORT, + LegacyRouter::findPage('do=import', array(), true) + ); + + $this->assertEquals( + LegacyRouter::$PAGE_IMPORT, + LegacyRouter::findPage('do=import&stuff', array(), true) + ); + } + + /** + * Test findPage: import page output. + * Invalid: page shouldn't be return. + * + * @return void + */ + public function testFindPageImportInvalid() + { + $this->assertNotEquals( + LegacyRouter::$PAGE_IMPORT, + LegacyRouter::findPage('do=import', array(), 1) + ); + + $this->assertNotEquals( + LegacyRouter::$PAGE_IMPORT, + LegacyRouter::findPage('do=import', array(), false) + ); + + $this->assertNotEquals( + LegacyRouter::$PAGE_IMPORT, + LegacyRouter::findPage('do=other', array(), true) + ); + } + + /** + * Test findPage: editlink page output. + * Valid: page should be return. + * + * @return void + */ + public function testFindPageEditlinkValid() + { + $this->assertEquals( + LegacyRouter::$PAGE_EDITLINK, + LegacyRouter::findPage('whatever', array('edit_link' => 1), true) + ); + + $this->assertEquals( + LegacyRouter::$PAGE_EDITLINK, + LegacyRouter::findPage('', array('edit_link' => 1), true) + ); + + + $this->assertEquals( + LegacyRouter::$PAGE_EDITLINK, + LegacyRouter::findPage('whatever', array('post' => 1), true) + ); + + $this->assertEquals( + LegacyRouter::$PAGE_EDITLINK, + LegacyRouter::findPage('whatever', array('post' => 1, 'edit_link' => 1), true) + ); + } + + /** + * Test findPage: editlink page output. + * Invalid: page shouldn't be return. + * + * @return void + */ + public function testFindPageEditlinkInvalid() + { + $this->assertNotEquals( + LegacyRouter::$PAGE_EDITLINK, + LegacyRouter::findPage('whatever', array('edit_link' => 1), false) + ); + + $this->assertNotEquals( + LegacyRouter::$PAGE_EDITLINK, + LegacyRouter::findPage('whatever', array('edit_link' => 1), 1) + ); + + $this->assertNotEquals( + LegacyRouter::$PAGE_EDITLINK, + LegacyRouter::findPage('whatever', array(), true) + ); + } +} -- cgit v1.2.3 From bedbb845eec20363b928b424143787dbe988eefe Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Thu, 13 Aug 2020 11:08:13 +0200 Subject: Move all admin controller into a dedicated group Also handle authentication check in a new middleware for the admin group. --- tests/legacy/LegacyControllerTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tests/legacy') diff --git a/tests/legacy/LegacyControllerTest.php b/tests/legacy/LegacyControllerTest.php index ff4520a3..759a5b2a 100644 --- a/tests/legacy/LegacyControllerTest.php +++ b/tests/legacy/LegacyControllerTest.php @@ -73,8 +73,8 @@ class LegacyControllerTest extends TestCase ['addlink', [], '/login', false], ['login', [], '/login', true], ['login', [], '/login', false], - ['logout', [], '/logout', true], - ['logout', [], '/logout', false], + ['logout', [], '/admin/logout', true], + ['logout', [], '/admin/logout', false], ['picwall', [], '/picture-wall', false], ['picwall', [], '/picture-wall', true], ['tagcloud', [], '/tags/cloud', false], -- cgit v1.2.3 From aca995e09cf9c210ffe45584fbe50dcedb8bdebb Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Tue, 1 Sep 2020 10:12:54 +0200 Subject: Fix support for legacy route login redirection Makes sure that the user is properly redirected to the bookmark form after login, even with legacy routes --- tests/legacy/LegacyControllerTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'tests/legacy') diff --git a/tests/legacy/LegacyControllerTest.php b/tests/legacy/LegacyControllerTest.php index 759a5b2a..460fbb3b 100644 --- a/tests/legacy/LegacyControllerTest.php +++ b/tests/legacy/LegacyControllerTest.php @@ -66,11 +66,11 @@ class LegacyControllerTest extends TestCase { return [ ['post', [], '/admin/shaare', true], - ['post', [], '/login', false], + ['post', [], '/login?returnurl=/admin/shaare', false], ['post', ['title' => 'test'], '/admin/shaare?title=test', true], - ['post', ['title' => 'test'], '/login?title=test', false], + ['post', ['title' => 'test'], '/login?returnurl=/admin/shaare?title=test', false], ['addlink', [], '/admin/add-shaare', true], - ['addlink', [], '/login', false], + ['addlink', [], '/login?returnurl=/admin/add-shaare', false], ['login', [], '/login', true], ['login', [], '/login', false], ['logout', [], '/admin/logout', true], -- cgit v1.2.3 From 9e2d47e519783a991962e1fe2c9f28d77756ae49 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Tue, 1 Sep 2020 10:40:18 +0200 Subject: Fix legacy redirection when Shaarli instance is under a subfolder --- tests/legacy/LegacyControllerTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'tests/legacy') diff --git a/tests/legacy/LegacyControllerTest.php b/tests/legacy/LegacyControllerTest.php index 460fbb3b..2bbbb1d7 100644 --- a/tests/legacy/LegacyControllerTest.php +++ b/tests/legacy/LegacyControllerTest.php @@ -66,11 +66,11 @@ class LegacyControllerTest extends TestCase { return [ ['post', [], '/admin/shaare', true], - ['post', [], '/login?returnurl=/admin/shaare', false], + ['post', [], '/login?returnurl=/subfolder/admin/shaare', false], ['post', ['title' => 'test'], '/admin/shaare?title=test', true], - ['post', ['title' => 'test'], '/login?returnurl=/admin/shaare?title=test', false], + ['post', ['title' => 'test'], '/login?returnurl=/subfolder/admin/shaare?title=test', false], ['addlink', [], '/admin/add-shaare', true], - ['addlink', [], '/login?returnurl=/admin/add-shaare', false], + ['addlink', [], '/login?returnurl=/subfolder/admin/add-shaare', false], ['login', [], '/login', true], ['login', [], '/login', false], ['logout', [], '/admin/logout', true], -- cgit v1.2.3 From 11aa4a7a29c5a6358584ce0f63c061fdb0704b18 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Tue, 1 Sep 2020 10:40:35 +0200 Subject: Support redirection of legacy route 'do=configure' --- tests/legacy/LegacyControllerTest.php | 2 ++ 1 file changed, 2 insertions(+) (limited to 'tests/legacy') diff --git a/tests/legacy/LegacyControllerTest.php b/tests/legacy/LegacyControllerTest.php index 2bbbb1d7..4e52f3e1 100644 --- a/tests/legacy/LegacyControllerTest.php +++ b/tests/legacy/LegacyControllerTest.php @@ -94,6 +94,8 @@ class LegacyControllerTest extends TestCase ['opensearch', [], '/open-search', true], ['dailyrss', [], '/daily-rss', false], ['dailyrss', [], '/daily-rss', true], + ['configure', [], '/login?returnurl=/subfolder/admin/configure', false], + ['configure', [], '/admin/configure', true], ]; } } -- cgit v1.2.3 From ce7918386a00c4a10ad8c9942c8ac28ea1fae0c2 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Thu, 3 Sep 2020 10:09:32 +0200 Subject: Improve backward compatibility for LegacyRouter LegacyRouter is no longer used for routing, only in existing plugins to match the _PAGE_ parameter. So we change a few of its values there, to match the new ones defined in TemplatePage. @see discussion in shaarli/Shaarli#1537 --- tests/legacy/LegacyRouterTest.php | 512 -------------------------------------- 1 file changed, 512 deletions(-) delete mode 100644 tests/legacy/LegacyRouterTest.php (limited to 'tests/legacy') diff --git a/tests/legacy/LegacyRouterTest.php b/tests/legacy/LegacyRouterTest.php deleted file mode 100644 index c2019ca7..00000000 --- a/tests/legacy/LegacyRouterTest.php +++ /dev/null @@ -1,512 +0,0 @@ -assertEquals( - LegacyRouter::$PAGE_LOGIN, - LegacyRouter::findPage('do=login', array(), false) - ); - - $this->assertEquals( - LegacyRouter::$PAGE_LOGIN, - LegacyRouter::findPage('do=login', array(), 1) - ); - - $this->assertEquals( - LegacyRouter::$PAGE_LOGIN, - LegacyRouter::findPage('do=login&stuff', array(), false) - ); - } - - /** - * Test findPage: login page output. - * Invalid: page shouldn't be return. - * - * @return void - */ - public function testFindPageLoginInvalid() - { - $this->assertNotEquals( - LegacyRouter::$PAGE_LOGIN, - LegacyRouter::findPage('do=login', array(), true) - ); - - $this->assertNotEquals( - LegacyRouter::$PAGE_LOGIN, - LegacyRouter::findPage('do=other', array(), false) - ); - } - - /** - * Test findPage: picwall page output. - * Valid: page should be return. - * - * @return void - */ - public function testFindPagePicwallValid() - { - $this->assertEquals( - LegacyRouter::$PAGE_PICWALL, - LegacyRouter::findPage('do=picwall', array(), false) - ); - - $this->assertEquals( - LegacyRouter::$PAGE_PICWALL, - LegacyRouter::findPage('do=picwall', array(), true) - ); - } - - /** - * Test findPage: picwall page output. - * Invalid: page shouldn't be return. - * - * @return void - */ - public function testFindPagePicwallInvalid() - { - $this->assertEquals( - LegacyRouter::$PAGE_PICWALL, - LegacyRouter::findPage('do=picwall&stuff', array(), false) - ); - - $this->assertNotEquals( - LegacyRouter::$PAGE_PICWALL, - LegacyRouter::findPage('do=other', array(), false) - ); - } - - /** - * Test findPage: tagcloud page output. - * Valid: page should be return. - * - * @return void - */ - public function testFindPageTagcloudValid() - { - $this->assertEquals( - LegacyRouter::$PAGE_TAGCLOUD, - LegacyRouter::findPage('do=tagcloud', array(), false) - ); - - $this->assertEquals( - LegacyRouter::$PAGE_TAGCLOUD, - LegacyRouter::findPage('do=tagcloud', array(), true) - ); - - $this->assertEquals( - LegacyRouter::$PAGE_TAGCLOUD, - LegacyRouter::findPage('do=tagcloud&stuff', array(), false) - ); - } - - /** - * Test findPage: tagcloud page output. - * Invalid: page shouldn't be return. - * - * @return void - */ - public function testFindPageTagcloudInvalid() - { - $this->assertNotEquals( - LegacyRouter::$PAGE_TAGCLOUD, - LegacyRouter::findPage('do=other', array(), false) - ); - } - - /** - * Test findPage: linklist page output. - * Valid: page should be return. - * - * @return void - */ - public function testFindPageLinklistValid() - { - $this->assertEquals( - LegacyRouter::$PAGE_LINKLIST, - LegacyRouter::findPage('', array(), true) - ); - - $this->assertEquals( - LegacyRouter::$PAGE_LINKLIST, - LegacyRouter::findPage('whatever', array(), true) - ); - - $this->assertEquals( - LegacyRouter::$PAGE_LINKLIST, - LegacyRouter::findPage('whatever', array(), false) - ); - - $this->assertEquals( - LegacyRouter::$PAGE_LINKLIST, - LegacyRouter::findPage('do=tools', array(), false) - ); - } - - /** - * Test findPage: tools page output. - * Valid: page should be return. - * - * @return void - */ - public function testFindPageToolsValid() - { - $this->assertEquals( - LegacyRouter::$PAGE_TOOLS, - LegacyRouter::findPage('do=tools', array(), true) - ); - - $this->assertEquals( - LegacyRouter::$PAGE_TOOLS, - LegacyRouter::findPage('do=tools&stuff', array(), true) - ); - } - - /** - * Test findPage: tools page output. - * Invalid: page shouldn't be return. - * - * @return void - */ - public function testFindPageToolsInvalid() - { - $this->assertNotEquals( - LegacyRouter::$PAGE_TOOLS, - LegacyRouter::findPage('do=tools', array(), 1) - ); - - $this->assertNotEquals( - LegacyRouter::$PAGE_TOOLS, - LegacyRouter::findPage('do=tools', array(), false) - ); - - $this->assertNotEquals( - LegacyRouter::$PAGE_TOOLS, - LegacyRouter::findPage('do=other', array(), true) - ); - } - - /** - * Test findPage: changepasswd page output. - * Valid: page should be return. - * - * @return void - */ - public function testFindPageChangepasswdValid() - { - $this->assertEquals( - LegacyRouter::$PAGE_CHANGEPASSWORD, - LegacyRouter::findPage('do=changepasswd', array(), true) - ); - $this->assertEquals( - LegacyRouter::$PAGE_CHANGEPASSWORD, - LegacyRouter::findPage('do=changepasswd&stuff', array(), true) - ); - } - - /** - * Test findPage: changepasswd page output. - * Invalid: page shouldn't be return. - * - * @return void - */ - public function testFindPageChangepasswdInvalid() - { - $this->assertNotEquals( - LegacyRouter::$PAGE_CHANGEPASSWORD, - LegacyRouter::findPage('do=changepasswd', array(), 1) - ); - - $this->assertNotEquals( - LegacyRouter::$PAGE_CHANGEPASSWORD, - LegacyRouter::findPage('do=changepasswd', array(), false) - ); - - $this->assertNotEquals( - LegacyRouter::$PAGE_CHANGEPASSWORD, - LegacyRouter::findPage('do=other', array(), true) - ); - } - /** - * Test findPage: configure page output. - * Valid: page should be return. - * - * @return void - */ - public function testFindPageConfigureValid() - { - $this->assertEquals( - LegacyRouter::$PAGE_CONFIGURE, - LegacyRouter::findPage('do=configure', array(), true) - ); - - $this->assertEquals( - LegacyRouter::$PAGE_CONFIGURE, - LegacyRouter::findPage('do=configure&stuff', array(), true) - ); - } - - /** - * Test findPage: configure page output. - * Invalid: page shouldn't be return. - * - * @return void - */ - public function testFindPageConfigureInvalid() - { - $this->assertNotEquals( - LegacyRouter::$PAGE_CONFIGURE, - LegacyRouter::findPage('do=configure', array(), 1) - ); - - $this->assertNotEquals( - LegacyRouter::$PAGE_CONFIGURE, - LegacyRouter::findPage('do=configure', array(), false) - ); - - $this->assertNotEquals( - LegacyRouter::$PAGE_CONFIGURE, - LegacyRouter::findPage('do=other', array(), true) - ); - } - - /** - * Test findPage: changetag page output. - * Valid: page should be return. - * - * @return void - */ - public function testFindPageChangetagValid() - { - $this->assertEquals( - LegacyRouter::$PAGE_CHANGETAG, - LegacyRouter::findPage('do=changetag', array(), true) - ); - - $this->assertEquals( - LegacyRouter::$PAGE_CHANGETAG, - LegacyRouter::findPage('do=changetag&stuff', array(), true) - ); - } - - /** - * Test findPage: changetag page output. - * Invalid: page shouldn't be return. - * - * @return void - */ - public function testFindPageChangetagInvalid() - { - $this->assertNotEquals( - LegacyRouter::$PAGE_CHANGETAG, - LegacyRouter::findPage('do=changetag', array(), 1) - ); - - $this->assertNotEquals( - LegacyRouter::$PAGE_CHANGETAG, - LegacyRouter::findPage('do=changetag', array(), false) - ); - - $this->assertNotEquals( - LegacyRouter::$PAGE_CHANGETAG, - LegacyRouter::findPage('do=other', array(), true) - ); - } - - /** - * Test findPage: addlink page output. - * Valid: page should be return. - * - * @return void - */ - public function testFindPageAddlinkValid() - { - $this->assertEquals( - LegacyRouter::$PAGE_ADDLINK, - LegacyRouter::findPage('do=addlink', array(), true) - ); - - $this->assertEquals( - LegacyRouter::$PAGE_ADDLINK, - LegacyRouter::findPage('do=addlink&stuff', array(), true) - ); - } - - /** - * Test findPage: addlink page output. - * Invalid: page shouldn't be return. - * - * @return void - */ - public function testFindPageAddlinkInvalid() - { - $this->assertNotEquals( - LegacyRouter::$PAGE_ADDLINK, - LegacyRouter::findPage('do=addlink', array(), 1) - ); - - $this->assertNotEquals( - LegacyRouter::$PAGE_ADDLINK, - LegacyRouter::findPage('do=addlink', array(), false) - ); - - $this->assertNotEquals( - LegacyRouter::$PAGE_ADDLINK, - LegacyRouter::findPage('do=other', array(), true) - ); - } - - /** - * Test findPage: export page output. - * Valid: page should be return. - * - * @return void - */ - public function testFindPageExportValid() - { - $this->assertEquals( - LegacyRouter::$PAGE_EXPORT, - LegacyRouter::findPage('do=export', array(), true) - ); - - $this->assertEquals( - LegacyRouter::$PAGE_EXPORT, - LegacyRouter::findPage('do=export&stuff', array(), true) - ); - } - - /** - * Test findPage: export page output. - * Invalid: page shouldn't be return. - * - * @return void - */ - public function testFindPageExportInvalid() - { - $this->assertNotEquals( - LegacyRouter::$PAGE_EXPORT, - LegacyRouter::findPage('do=export', array(), 1) - ); - - $this->assertNotEquals( - LegacyRouter::$PAGE_EXPORT, - LegacyRouter::findPage('do=export', array(), false) - ); - - $this->assertNotEquals( - LegacyRouter::$PAGE_EXPORT, - LegacyRouter::findPage('do=other', array(), true) - ); - } - - /** - * Test findPage: import page output. - * Valid: page should be return. - * - * @return void - */ - public function testFindPageImportValid() - { - $this->assertEquals( - LegacyRouter::$PAGE_IMPORT, - LegacyRouter::findPage('do=import', array(), true) - ); - - $this->assertEquals( - LegacyRouter::$PAGE_IMPORT, - LegacyRouter::findPage('do=import&stuff', array(), true) - ); - } - - /** - * Test findPage: import page output. - * Invalid: page shouldn't be return. - * - * @return void - */ - public function testFindPageImportInvalid() - { - $this->assertNotEquals( - LegacyRouter::$PAGE_IMPORT, - LegacyRouter::findPage('do=import', array(), 1) - ); - - $this->assertNotEquals( - LegacyRouter::$PAGE_IMPORT, - LegacyRouter::findPage('do=import', array(), false) - ); - - $this->assertNotEquals( - LegacyRouter::$PAGE_IMPORT, - LegacyRouter::findPage('do=other', array(), true) - ); - } - - /** - * Test findPage: editlink page output. - * Valid: page should be return. - * - * @return void - */ - public function testFindPageEditlinkValid() - { - $this->assertEquals( - LegacyRouter::$PAGE_EDITLINK, - LegacyRouter::findPage('whatever', array('edit_link' => 1), true) - ); - - $this->assertEquals( - LegacyRouter::$PAGE_EDITLINK, - LegacyRouter::findPage('', array('edit_link' => 1), true) - ); - - - $this->assertEquals( - LegacyRouter::$PAGE_EDITLINK, - LegacyRouter::findPage('whatever', array('post' => 1), true) - ); - - $this->assertEquals( - LegacyRouter::$PAGE_EDITLINK, - LegacyRouter::findPage('whatever', array('post' => 1, 'edit_link' => 1), true) - ); - } - - /** - * Test findPage: editlink page output. - * Invalid: page shouldn't be return. - * - * @return void - */ - public function testFindPageEditlinkInvalid() - { - $this->assertNotEquals( - LegacyRouter::$PAGE_EDITLINK, - LegacyRouter::findPage('whatever', array('edit_link' => 1), false) - ); - - $this->assertNotEquals( - LegacyRouter::$PAGE_EDITLINK, - LegacyRouter::findPage('whatever', array('edit_link' => 1), 1) - ); - - $this->assertNotEquals( - LegacyRouter::$PAGE_EDITLINK, - LegacyRouter::findPage('whatever', array(), true) - ); - } -} -- cgit v1.2.3 From 8f60e1206e45e67c96a7630d4ff94e72fe875f09 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sat, 26 Sep 2020 15:08:39 +0200 Subject: Comply with PHPUnit V8: setup/teardown functions must return void --- tests/legacy/LegacyLinkDBTest.php | 2 +- tests/legacy/LegacyLinkFilterTest.php | 2 +- tests/legacy/LegacyUpdaterTest.php | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) (limited to 'tests/legacy') diff --git a/tests/legacy/LegacyLinkDBTest.php b/tests/legacy/LegacyLinkDBTest.php index 0884ad03..92cf607c 100644 --- a/tests/legacy/LegacyLinkDBTest.php +++ b/tests/legacy/LegacyLinkDBTest.php @@ -52,7 +52,7 @@ class LegacyLinkDBTest extends \PHPUnit\Framework\TestCase * * Resets test data for each test */ - protected function setUp() + protected function setUp(): void { if (file_exists(self::$testDatastore)) { unlink(self::$testDatastore); diff --git a/tests/legacy/LegacyLinkFilterTest.php b/tests/legacy/LegacyLinkFilterTest.php index ba9ec529..8223cc15 100644 --- a/tests/legacy/LegacyLinkFilterTest.php +++ b/tests/legacy/LegacyLinkFilterTest.php @@ -34,7 +34,7 @@ class LegacyLinkFilterTest extends \PHPUnit\Framework\TestCase /** * Instantiate linkFilter with ReferenceLinkDB data. */ - public static function setUpBeforeClass() + public static function setUpBeforeClass(): void { self::$refDB = new ReferenceLinkDB(true); self::$refDB->write(self::$testDatastore); diff --git a/tests/legacy/LegacyUpdaterTest.php b/tests/legacy/LegacyUpdaterTest.php index 7c429811..9b693dcb 100644 --- a/tests/legacy/LegacyUpdaterTest.php +++ b/tests/legacy/LegacyUpdaterTest.php @@ -40,7 +40,7 @@ class LegacyUpdaterTest extends \PHPUnit\Framework\TestCase /** * Executed before each test. */ - public function setUp() + protected function setUp(): void { copy('tests/utils/config/configJson.json.php', self::$configFile .'.json.php'); $this->conf = new ConfigManager(self::$configFile); @@ -754,7 +754,7 @@ $GLOBALS[\'privateLinkByDefault\'] = true;'; if (isset($_SESSION['warnings'])) { unset($_SESSION['warnings']); } - + $updater = new LegacyUpdater([], [], $this->conf, true, $_SESSION); $this->assertTrue($updater->updateMethodWebThumbnailer()); $this->assertFalse($this->conf->exists('thumbnail')); -- cgit v1.2.3 From b1baca99f280570d0336b4d71ad1f9dca213a35b Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sun, 27 Sep 2020 14:07:08 +0200 Subject: Convert legacy PHPUnit @expected* to new ->expect* Converted automatically using https://github.com/ArthurHoaro/convert-legacy-phpunit-expect --- tests/legacy/LegacyLinkDBTest.php | 11 ++++++----- tests/legacy/LegacyLinkFilterTest.php | 10 ++++++---- tests/legacy/LegacyUpdaterTest.php | 10 ++++++---- 3 files changed, 18 insertions(+), 13 deletions(-) (limited to 'tests/legacy') diff --git a/tests/legacy/LegacyLinkDBTest.php b/tests/legacy/LegacyLinkDBTest.php index 92cf607c..819bc272 100644 --- a/tests/legacy/LegacyLinkDBTest.php +++ b/tests/legacy/LegacyLinkDBTest.php @@ -101,10 +101,11 @@ class LegacyLinkDBTest extends \PHPUnit\Framework\TestCase * Attempt to instantiate a LinkDB whereas the datastore is not writable * * @expectedException Shaarli\Exceptions\IOException - * @expectedExceptionMessageRegExp /Error accessing "null"/ */ public function testConstructDatastoreNotWriteable() { + $this->expectExceptionMessageRegExp('/Error accessing "null"/'); + new LegacyLinkDB('null/store.db', false, false); } @@ -420,22 +421,22 @@ class LegacyLinkDBTest extends \PHPUnit\Framework\TestCase /** * Test filterHash() with an invalid smallhash. - * - * @expectedException \Shaarli\Bookmark\Exception\BookmarkNotFoundException */ public function testFilterHashInValid1() { + $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException::class); + $request = 'blabla'; self::$publicLinkDB->filterHash($request); } /** * Test filterHash() with an empty smallhash. - * - * @expectedException \Shaarli\Bookmark\Exception\BookmarkNotFoundException */ public function testFilterHashInValid() { + $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException::class); + self::$publicLinkDB->filterHash(''); } diff --git a/tests/legacy/LegacyLinkFilterTest.php b/tests/legacy/LegacyLinkFilterTest.php index 8223cc15..9db921a9 100644 --- a/tests/legacy/LegacyLinkFilterTest.php +++ b/tests/legacy/LegacyLinkFilterTest.php @@ -198,20 +198,22 @@ class LegacyLinkFilterTest extends \PHPUnit\Framework\TestCase /** * Use an invalid date format * @expectedException Exception - * @expectedExceptionMessageRegExp /Invalid date format/ */ public function testFilterInvalidDayWithChars() { + $this->expectExceptionMessageRegExp('/Invalid date format/'); + self::$linkFilter->filter(LegacyLinkFilter::$FILTER_DAY, 'Rainy day, dream away'); } /** * Use an invalid date format * @expectedException Exception - * @expectedExceptionMessageRegExp /Invalid date format/ */ public function testFilterInvalidDayDigits() { + $this->expectExceptionMessageRegExp('/Invalid date format/'); + self::$linkFilter->filter(LegacyLinkFilter::$FILTER_DAY, '20'); } @@ -235,11 +237,11 @@ class LegacyLinkFilterTest extends \PHPUnit\Framework\TestCase /** * No link for this hash - * - * @expectedException \Shaarli\Bookmark\Exception\BookmarkNotFoundException */ public function testFilterUnknownSmallHash() { + $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException::class); + self::$linkFilter->filter(LegacyLinkFilter::$FILTER_HASH, 'Iblaah'); } diff --git a/tests/legacy/LegacyUpdaterTest.php b/tests/legacy/LegacyUpdaterTest.php index 9b693dcb..acfac530 100644 --- a/tests/legacy/LegacyUpdaterTest.php +++ b/tests/legacy/LegacyUpdaterTest.php @@ -82,10 +82,11 @@ class LegacyUpdaterTest extends \PHPUnit\Framework\TestCase * Test errors in UpdaterUtils::write_updates_file(): empty updates file. * * @expectedException Exception - * @expectedExceptionMessageRegExp /Updates file path is not set(.*)/ */ public function testWriteEmptyUpdatesFile() { + $this->expectExceptionMessageRegExp('/Updates file path is not set(.*)/'); + UpdaterUtils::write_updates_file('', array('test')); } @@ -93,10 +94,11 @@ class LegacyUpdaterTest extends \PHPUnit\Framework\TestCase * Test errors in UpdaterUtils::write_updates_file(): not writable updates file. * * @expectedException Exception - * @expectedExceptionMessageRegExp /Unable to write(.*)/ */ public function testWriteUpdatesFileNotWritable() { + $this->expectExceptionMessageRegExp('/Unable to write(.*)/'); + $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt'; touch($updatesFile); chmod($updatesFile, 0444); @@ -161,11 +163,11 @@ class LegacyUpdaterTest extends \PHPUnit\Framework\TestCase /** * Test Update failed. - * - * @expectedException \Exception */ public function testUpdateFailed() { + $this->expectException(\Exception::class); + $updates = array( 'updateMethodDummy1', 'updateMethodDummy2', -- cgit v1.2.3 From a5a9cf23acd1248585173aa32757d9720b5f2d62 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Tue, 29 Sep 2020 14:41:40 +0200 Subject: Compatibility with PHPUnit 9 --- tests/legacy/LegacyControllerTest.php | 2 +- tests/legacy/LegacyLinkDBTest.php | 12 ++++++------ tests/legacy/LegacyLinkFilterTest.php | 2 +- tests/legacy/LegacyUpdaterTest.php | 4 ++-- 4 files changed, 10 insertions(+), 10 deletions(-) (limited to 'tests/legacy') diff --git a/tests/legacy/LegacyControllerTest.php b/tests/legacy/LegacyControllerTest.php index 4e52f3e1..1a2549a3 100644 --- a/tests/legacy/LegacyControllerTest.php +++ b/tests/legacy/LegacyControllerTest.php @@ -4,8 +4,8 @@ declare(strict_types=1); namespace Shaarli\Legacy; -use PHPUnit\Framework\TestCase; use Shaarli\Front\Controller\Visitor\FrontControllerMockHelper; +use Shaarli\TestCase; use Slim\Http\Request; use Slim\Http\Response; diff --git a/tests/legacy/LegacyLinkDBTest.php b/tests/legacy/LegacyLinkDBTest.php index 819bc272..66746dfc 100644 --- a/tests/legacy/LegacyLinkDBTest.php +++ b/tests/legacy/LegacyLinkDBTest.php @@ -18,7 +18,7 @@ require_once 'tests/utils/ReferenceLinkDB.php'; /** * Unitary tests for LegacyLinkDBTest */ -class LegacyLinkDBTest extends \PHPUnit\Framework\TestCase +class LegacyLinkDBTest extends \Shaarli\TestCase { // datastore to test write operations protected static $testDatastore = 'sandbox/datastore.php'; @@ -258,7 +258,7 @@ class LegacyLinkDBTest extends \PHPUnit\Framework\TestCase $link = self::$publicLinkDB->getLinkFromUrl('http://mediagoblin.org/'); $this->assertNotEquals(false, $link); - $this->assertContains( + $this->assertContainsPolyfill( 'A free software media publishing platform', $link['description'] ); @@ -471,9 +471,9 @@ class LegacyLinkDBTest extends \PHPUnit\Framework\TestCase $res = $linkDB->renameTag('cartoon', 'Taz'); $this->assertEquals(3, count($res)); - $this->assertContains(' Taz ', $linkDB[4]['tags']); - $this->assertContains(' Taz ', $linkDB[1]['tags']); - $this->assertContains(' Taz ', $linkDB[0]['tags']); + $this->assertContainsPolyfill(' Taz ', $linkDB[4]['tags']); + $this->assertContainsPolyfill(' Taz ', $linkDB[1]['tags']); + $this->assertContainsPolyfill(' Taz ', $linkDB[0]['tags']); } /** @@ -513,7 +513,7 @@ class LegacyLinkDBTest extends \PHPUnit\Framework\TestCase $res = $linkDB->renameTag('cartoon', null); $this->assertEquals(3, count($res)); - $this->assertNotContains('cartoon', $linkDB[4]['tags']); + $this->assertNotContainsPolyfill('cartoon', $linkDB[4]['tags']); } /** diff --git a/tests/legacy/LegacyLinkFilterTest.php b/tests/legacy/LegacyLinkFilterTest.php index 9db921a9..82bc93d8 100644 --- a/tests/legacy/LegacyLinkFilterTest.php +++ b/tests/legacy/LegacyLinkFilterTest.php @@ -10,7 +10,7 @@ use Shaarli\Legacy\LegacyLinkFilter; /** * Class LegacyLinkFilterTest. */ -class LegacyLinkFilterTest extends \PHPUnit\Framework\TestCase +class LegacyLinkFilterTest extends \Shaarli\TestCase { /** * @var string Test datastore path. diff --git a/tests/legacy/LegacyUpdaterTest.php b/tests/legacy/LegacyUpdaterTest.php index acfac530..0ddcb4a6 100644 --- a/tests/legacy/LegacyUpdaterTest.php +++ b/tests/legacy/LegacyUpdaterTest.php @@ -20,7 +20,7 @@ require_once 'inc/rain.tpl.class.php'; * Class UpdaterTest. * Runs unit tests against the updater class. */ -class LegacyUpdaterTest extends \PHPUnit\Framework\TestCase +class LegacyUpdaterTest extends \Shaarli\TestCase { /** * @var string Path to test datastore. @@ -725,7 +725,7 @@ $GLOBALS[\'privateLinkByDefault\'] = true;'; $this->assertEquals(\Shaarli\Thumbnailer::MODE_ALL, $this->conf->get('thumbnails.mode')); $this->assertEquals(125, $this->conf->get('thumbnails.width')); $this->assertEquals(90, $this->conf->get('thumbnails.height')); - $this->assertContains('You have enabled or changed thumbnails', $_SESSION['warnings'][0]); + $this->assertContainsPolyfill('You have enabled or changed thumbnails', $_SESSION['warnings'][0]); } /** -- cgit v1.2.3 From f447edb73b1bcb52e86286467d3ec7b7bdc29948 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Tue, 29 Sep 2020 18:41:21 +0200 Subject: Fix missing @expectedException convertion --- tests/legacy/LegacyLinkDBTest.php | 3 +-- tests/legacy/LegacyLinkFilterTest.php | 4 ++-- tests/legacy/LegacyUpdaterTest.php | 6 ++---- 3 files changed, 5 insertions(+), 8 deletions(-) (limited to 'tests/legacy') diff --git a/tests/legacy/LegacyLinkDBTest.php b/tests/legacy/LegacyLinkDBTest.php index 66746dfc..df2cad62 100644 --- a/tests/legacy/LegacyLinkDBTest.php +++ b/tests/legacy/LegacyLinkDBTest.php @@ -99,11 +99,10 @@ class LegacyLinkDBTest extends \Shaarli\TestCase /** * Attempt to instantiate a LinkDB whereas the datastore is not writable - * - * @expectedException Shaarli\Exceptions\IOException */ public function testConstructDatastoreNotWriteable() { + $this->expectException(\Shaarli\Exceptions\IOException::class); $this->expectExceptionMessageRegExp('/Error accessing "null"/'); new LegacyLinkDB('null/store.db', false, false); diff --git a/tests/legacy/LegacyLinkFilterTest.php b/tests/legacy/LegacyLinkFilterTest.php index 82bc93d8..45d7754d 100644 --- a/tests/legacy/LegacyLinkFilterTest.php +++ b/tests/legacy/LegacyLinkFilterTest.php @@ -197,10 +197,10 @@ class LegacyLinkFilterTest extends \Shaarli\TestCase /** * Use an invalid date format - * @expectedException Exception */ public function testFilterInvalidDayWithChars() { + $this->expectException(\Exception::class); $this->expectExceptionMessageRegExp('/Invalid date format/'); self::$linkFilter->filter(LegacyLinkFilter::$FILTER_DAY, 'Rainy day, dream away'); @@ -208,10 +208,10 @@ class LegacyLinkFilterTest extends \Shaarli\TestCase /** * Use an invalid date format - * @expectedException Exception */ public function testFilterInvalidDayDigits() { + $this->expectException(\Exception::class); $this->expectExceptionMessageRegExp('/Invalid date format/'); self::$linkFilter->filter(LegacyLinkFilter::$FILTER_DAY, '20'); diff --git a/tests/legacy/LegacyUpdaterTest.php b/tests/legacy/LegacyUpdaterTest.php index 0ddcb4a6..f7391b86 100644 --- a/tests/legacy/LegacyUpdaterTest.php +++ b/tests/legacy/LegacyUpdaterTest.php @@ -80,11 +80,10 @@ class LegacyUpdaterTest extends \Shaarli\TestCase /** * Test errors in UpdaterUtils::write_updates_file(): empty updates file. - * - * @expectedException Exception */ public function testWriteEmptyUpdatesFile() { + $this->expectException(\Exception::class); $this->expectExceptionMessageRegExp('/Updates file path is not set(.*)/'); UpdaterUtils::write_updates_file('', array('test')); @@ -92,11 +91,10 @@ class LegacyUpdaterTest extends \Shaarli\TestCase /** * Test errors in UpdaterUtils::write_updates_file(): not writable updates file. - * - * @expectedException Exception */ public function testWriteUpdatesFileNotWritable() { + $this->expectException(\Exception::class); $this->expectExceptionMessageRegExp('/Unable to write(.*)/'); $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt'; -- cgit v1.2.3