From b93cfeba7b5ddb8b20d805017404e73eafd68c95 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Thu, 3 Sep 2020 14:52:34 +0200 Subject: [PATCH] Fix subfolder configuration in unit tests --- .../controller/visitor/DailyController.php | 2 +- tests/feed/FeedBuilderTest.php | 13 +++---- .../controller/admin/ExportControllerTest.php | 2 +- .../controller/admin/ToolsControllerTest.php | 6 ++-- .../visitor/DailyControllerTest.php | 12 +++---- .../visitor/FrontControllerMockHelper.php | 3 +- .../visitor/InstallControllerTest.php | 35 ++++++++++++++++++- .../visitor/OpenSearchControllerTest.php | 2 +- 8 files changed, 55 insertions(+), 20 deletions(-) diff --git a/application/front/controller/visitor/DailyController.php b/application/front/controller/visitor/DailyController.php index 54a4778f..07617cf1 100644 --- a/application/front/controller/visitor/DailyController.php +++ b/application/front/controller/visitor/DailyController.php @@ -132,7 +132,7 @@ class DailyController extends ShaarliVisitorController 'date' => $dayDatetime, 'date_rss' => $dayDatetime->format(DateTime::RSS), 'date_human' => format_date($dayDatetime, false, true), - 'absolute_url' => $indexUrl . '/daily?day=' . $day, + 'absolute_url' => $indexUrl . 'daily?day=' . $day, 'links' => [], ]; diff --git a/tests/feed/FeedBuilderTest.php b/tests/feed/FeedBuilderTest.php index fe37d5f2..5dfe73aa 100644 --- a/tests/feed/FeedBuilderTest.php +++ b/tests/feed/FeedBuilderTest.php @@ -3,6 +3,7 @@ namespace Shaarli\Feed; use DateTime; +use PHPUnit\Framework\TestCase; use ReferenceLinkDB; use Shaarli\Bookmark\Bookmark; use Shaarli\Bookmark\BookmarkFileService; @@ -16,7 +17,7 @@ use Shaarli\History; * * Unit tests for FeedBuilder. */ -class FeedBuilderTest extends \PHPUnit\Framework\TestCase +class FeedBuilderTest extends TestCase { /** * @var string locale Basque (Spain). @@ -44,7 +45,7 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase /** * Called before every test method. */ - public static function setUpBeforeClass() + public static function setUpBeforeClass(): void { $conf = new ConfigManager('tests/utils/config/configJson'); $conf->set('resource.datastore', self::$testDatastore); @@ -60,7 +61,7 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase 'SERVER_NAME' => 'host.tld', 'SERVER_PORT' => '80', 'SCRIPT_NAME' => '/index.php', - 'REQUEST_URI' => '/index.php?do=feed', + 'REQUEST_URI' => '/feed/atom', ); } @@ -81,7 +82,7 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase $this->assertEquals(self::$RSS_LANGUAGE, $data['language']); $this->assertRegExp('/Wed, 03 Aug 2016 09:30:33 \+\d{4}/', $data['last_update']); $this->assertEquals(true, $data['show_dates']); - $this->assertEquals('http://host.tld/index.php?do=feed', $data['self_link']); + $this->assertEquals('http://host.tld/feed/atom', $data['self_link']); $this->assertEquals('http://host.tld/', $data['index_url']); $this->assertFalse($data['usepermalinks']); $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); @@ -253,7 +254,7 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase 'SERVER_NAME' => 'host.tld', 'SERVER_PORT' => '8080', 'SCRIPT_NAME' => '/~user/shaarli/index.php', - 'REQUEST_URI' => '/~user/shaarli/index.php?do=feed', + 'REQUEST_URI' => '/~user/shaarli/feed/atom', ); $feedBuilder = new FeedBuilder( self::$bookmarkService, @@ -265,7 +266,7 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase $data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, null); $this->assertEquals( - 'http://host.tld:8080/~user/shaarli/index.php?do=feed', + 'http://host.tld:8080/~user/shaarli/feed/atom', $data['self_link'] ); diff --git a/tests/front/controller/admin/ExportControllerTest.php b/tests/front/controller/admin/ExportControllerTest.php index 50d9e378..12d26f4a 100644 --- a/tests/front/controller/admin/ExportControllerTest.php +++ b/tests/front/controller/admin/ExportControllerTest.php @@ -84,7 +84,7 @@ class ExportControllerTest extends TestCase static::assertInstanceOf(BookmarkRawFormatter::class, $formatter); static::assertSame($parameters['selection'], $selection); static::assertTrue($prependNoteUrl); - static::assertSame('http://shaarli', $indexUrl); + static::assertSame('http://shaarli/subfolder/', $indexUrl); return $bookmarks; } diff --git a/tests/front/controller/admin/ToolsControllerTest.php b/tests/front/controller/admin/ToolsControllerTest.php index fc756f0f..39144d2f 100644 --- a/tests/front/controller/admin/ToolsControllerTest.php +++ b/tests/front/controller/admin/ToolsControllerTest.php @@ -8,7 +8,7 @@ use PHPUnit\Framework\TestCase; use Slim\Http\Request; use Slim\Http\Response; -class ToolsControllerTestControllerTest extends TestCase +class ToolsControllerTest extends TestCase { use FrontAdminControllerMockHelper; @@ -41,7 +41,7 @@ class ToolsControllerTestControllerTest extends TestCase static::assertSame(200, $result->getStatusCode()); static::assertSame('tools', (string) $result->getBody()); - static::assertSame('https://shaarli', $assignedVariables['pageabsaddr']); + static::assertSame('https://shaarli/', $assignedVariables['pageabsaddr']); static::assertTrue($assignedVariables['sslenabled']); } @@ -63,7 +63,7 @@ class ToolsControllerTestControllerTest extends TestCase static::assertSame(200, $result->getStatusCode()); static::assertSame('tools', (string) $result->getBody()); - static::assertSame('http://shaarli', $assignedVariables['pageabsaddr']); + static::assertSame('http://shaarli/', $assignedVariables['pageabsaddr']); static::assertFalse($assignedVariables['sslenabled']); } } diff --git a/tests/front/controller/visitor/DailyControllerTest.php b/tests/front/controller/visitor/DailyControllerTest.php index b802c62c..cb5b96f3 100644 --- a/tests/front/controller/visitor/DailyControllerTest.php +++ b/tests/front/controller/visitor/DailyControllerTest.php @@ -392,8 +392,8 @@ class DailyControllerTest extends TestCase static::assertStringContainsString('application/rss', $result->getHeader('Content-Type')[0]); static::assertSame('dailyrss', (string) $result->getBody()); static::assertSame('Shaarli', $assignedVariables['title']); - static::assertSame('http://shaarli', $assignedVariables['index_url']); - static::assertSame('http://shaarli/daily-rss', $assignedVariables['page_url']); + static::assertSame('http://shaarli/subfolder/', $assignedVariables['index_url']); + static::assertSame('http://shaarli/subfolder/daily-rss', $assignedVariables['page_url']); static::assertFalse($assignedVariables['hide_timestamps']); static::assertCount(2, $assignedVariables['days']); @@ -402,7 +402,7 @@ class DailyControllerTest extends TestCase static::assertEquals($dates[0], $day['date']); static::assertSame($dates[0]->format(\DateTime::RSS), $day['date_rss']); static::assertSame(format_date($dates[0], false), $day['date_human']); - static::assertSame('http://shaarli/daily?day='. $dates[0]->format('Ymd'), $day['absolute_url']); + static::assertSame('http://shaarli/subfolder/daily?day='. $dates[0]->format('Ymd'), $day['absolute_url']); static::assertCount(1, $day['links']); static::assertSame(1, $day['links'][0]['id']); static::assertSame('http://domain.tld/1', $day['links'][0]['url']); @@ -413,7 +413,7 @@ class DailyControllerTest extends TestCase static::assertEquals($dates[1], $day['date']); static::assertSame($dates[1]->format(\DateTime::RSS), $day['date_rss']); static::assertSame(format_date($dates[1], false), $day['date_human']); - static::assertSame('http://shaarli/daily?day='. $dates[1]->format('Ymd'), $day['absolute_url']); + static::assertSame('http://shaarli/subfolder/daily?day='. $dates[1]->format('Ymd'), $day['absolute_url']); static::assertCount(2, $day['links']); static::assertSame(2, $day['links'][0]['id']); @@ -468,8 +468,8 @@ class DailyControllerTest extends TestCase static::assertStringContainsString('application/rss', $result->getHeader('Content-Type')[0]); static::assertSame('dailyrss', (string) $result->getBody()); static::assertSame('Shaarli', $assignedVariables['title']); - static::assertSame('http://shaarli', $assignedVariables['index_url']); - static::assertSame('http://shaarli/daily-rss', $assignedVariables['page_url']); + static::assertSame('http://shaarli/subfolder/', $assignedVariables['index_url']); + static::assertSame('http://shaarli/subfolder/daily-rss', $assignedVariables['page_url']); static::assertFalse($assignedVariables['hide_timestamps']); static::assertCount(0, $assignedVariables['days']); } diff --git a/tests/front/controller/visitor/FrontControllerMockHelper.php b/tests/front/controller/visitor/FrontControllerMockHelper.php index e0bd4ecf..fe6ac9b0 100644 --- a/tests/front/controller/visitor/FrontControllerMockHelper.php +++ b/tests/front/controller/visitor/FrontControllerMockHelper.php @@ -79,8 +79,9 @@ trait FrontControllerMockHelper $this->container->environment = [ 'SERVER_NAME' => 'shaarli', 'SERVER_PORT' => '80', - 'REQUEST_URI' => '/daily-rss', + 'REQUEST_URI' => '/subfolder/daily-rss', 'REMOTE_ADDR' => '1.2.3.4', + 'SCRIPT_NAME' => '/subfolder/index.php', ]; $this->container->basePath = '/subfolder'; diff --git a/tests/front/controller/visitor/InstallControllerTest.php b/tests/front/controller/visitor/InstallControllerTest.php index 3b855365..994d3f33 100644 --- a/tests/front/controller/visitor/InstallControllerTest.php +++ b/tests/front/controller/visitor/InstallControllerTest.php @@ -257,6 +257,39 @@ class InstallControllerTest extends TestCase static::assertSame('/subfolder/login', $result->getHeader('location')[0]); static::assertSame('UTC', $confSettings['general.timezone']); - static::assertSame('Shared bookmarks on http://shaarli', $confSettings['general.title']); + static::assertSame('Shared bookmarks on http://shaarli/subfolder/', $confSettings['general.title']); + } + + /** + * Same test as testSaveInstallDefaultValues() but for an instance install in root directory. + */ + public function testSaveInstallDefaultValuesWithoutSubfolder(): void + { + $confSettings = []; + + $this->container->environment = [ + 'SERVER_NAME' => 'shaarli', + 'SERVER_PORT' => '80', + 'REQUEST_URI' => '/install', + 'REMOTE_ADDR' => '1.2.3.4', + 'SCRIPT_NAME' => '/index.php', + ]; + + $this->container->basePath = ''; + + $request = $this->createMock(Request::class); + $response = new Response(); + + $this->container->conf->method('set')->willReturnCallback(function (string $key, $value) use (&$confSettings) { + $confSettings[$key] = $value; + }); + + $result = $this->controller->save($request, $response); + + static::assertSame(302, $result->getStatusCode()); + static::assertSame('/login', $result->getHeader('location')[0]); + + static::assertSame('UTC', $confSettings['general.timezone']); + static::assertSame('Shared bookmarks on http://shaarli/', $confSettings['general.title']); } } diff --git a/tests/front/controller/visitor/OpenSearchControllerTest.php b/tests/front/controller/visitor/OpenSearchControllerTest.php index 5f9f5b12..9609a377 100644 --- a/tests/front/controller/visitor/OpenSearchControllerTest.php +++ b/tests/front/controller/visitor/OpenSearchControllerTest.php @@ -39,6 +39,6 @@ class OpenSearchControllerTest extends TestCase $result->getHeader('Content-Type')[0] ); static::assertSame('opensearch', (string) $result->getBody()); - static::assertSame('http://shaarli', $assignedVariables['serverurl']); + static::assertSame('http://shaarli/subfolder/', $assignedVariables['serverurl']); } } -- 2.41.0