From: ArthurHoaro Date: Sat, 3 Oct 2020 10:59:16 +0000 (+0200) Subject: Merge pull request #1575 from ArthurHoaro/feature/php8 X-Git-Tag: v0.12.0-beta-2~7 X-Git-Url: https://git.immae.eu/?a=commitdiff_plain;h=7b18876361f2223672f3a2cac9fc0da16d12d69f;hp=-c;p=github%2Fshaarli%2FShaarli.git Merge pull request #1575 from ArthurHoaro/feature/php8 --- 7b18876361f2223672f3a2cac9fc0da16d12d69f diff --combined tests/api/ApiMiddlewareTest.php index 32031750,7386e435..86700840 --- a/tests/api/ApiMiddlewareTest.php +++ b/tests/api/ApiMiddlewareTest.php @@@ -18,7 -18,7 +18,7 @@@ use Slim\Http\Response * * @package Api */ - class ApiMiddlewareTest extends \PHPUnit\Framework\TestCase + class ApiMiddlewareTest extends \Shaarli\TestCase { /** * @var string datastore to test write operations @@@ -26,7 -26,7 +26,7 @@@ protected static $testDatastore = 'sandbox/datastore.php'; /** - * @var \ConfigManager instance + * @var ConfigManager instance */ protected $conf; @@@ -66,53 -66,6 +66,53 @@@ @unlink(self::$testDatastore); } + /** + * Invoke the middleware with a valid token + */ + public function testInvokeMiddlewareWithValidToken(): void + { + $next = function (Request $request, Response $response): Response { + return $response; + }; + $mw = new ApiMiddleware($this->container); + $env = Environment::mock([ + 'REQUEST_METHOD' => 'GET', + 'REQUEST_URI' => '/echo', + 'HTTP_AUTHORIZATION'=> 'Bearer ' . ApiUtilsTest::generateValidJwtToken('NapoleonWasALizard'), + ]); + $request = Request::createFromEnvironment($env); + $response = new Response(); + /** @var Response $response */ + $response = $mw($request, $response, $next); + + $this->assertEquals(200, $response->getStatusCode()); + } + + /** + * Invoke the middleware with a valid token + * Using specific Apache CGI redirected authorization. + */ + public function testInvokeMiddlewareWithValidTokenFromRedirectedHeader(): void + { + $next = function (Request $request, Response $response): Response { + return $response; + }; + + $token = 'Bearer ' . ApiUtilsTest::generateValidJwtToken('NapoleonWasALizard'); + $this->container->environment['REDIRECT_HTTP_AUTHORIZATION'] = $token; + $mw = new ApiMiddleware($this->container); + $env = Environment::mock([ + 'REQUEST_METHOD' => 'GET', + 'REQUEST_URI' => '/echo', + ]); + $request = Request::createFromEnvironment($env); + $response = new Response(); + /** @var Response $response */ + $response = $mw($request, $response, $next); + + $this->assertEquals(200, $response->getStatusCode()); + } + /** * Invoke the middleware with the API disabled: * should return a 401 error Unauthorized. @@@ -156,7 -109,7 +156,7 @@@ $this->assertEquals(401, $response->getStatusCode()); $body = json_decode((string) $response->getBody()); $this->assertEquals('Not authorized: API is disabled', $body->message); - $this->assertContains('ApiAuthorizationException', $body->stacktrace); + $this->assertContainsPolyfill('ApiAuthorizationException', $body->stacktrace); } /** @@@ -179,7 -132,7 +179,7 @@@ $this->assertEquals(401, $response->getStatusCode()); $body = json_decode((string) $response->getBody()); $this->assertEquals('Not authorized: JWT token not provided', $body->message); - $this->assertContains('ApiAuthorizationException', $body->stacktrace); + $this->assertContainsPolyfill('ApiAuthorizationException', $body->stacktrace); } /** @@@ -204,7 -157,7 +204,7 @@@ $this->assertEquals(401, $response->getStatusCode()); $body = json_decode((string) $response->getBody()); $this->assertEquals('Not authorized: Token secret must be set in Shaarli\'s administration', $body->message); - $this->assertContains('ApiAuthorizationException', $body->stacktrace); + $this->assertContainsPolyfill('ApiAuthorizationException', $body->stacktrace); } /** @@@ -227,7 -180,7 +227,7 @@@ $this->assertEquals(401, $response->getStatusCode()); $body = json_decode((string) $response->getBody()); $this->assertEquals('Not authorized: Invalid JWT header', $body->message); - $this->assertContains('ApiAuthorizationException', $body->stacktrace); + $this->assertContainsPolyfill('ApiAuthorizationException', $body->stacktrace); } /** @@@ -253,6 -206,6 +253,6 @@@ $this->assertEquals(401, $response->getStatusCode()); $body = json_decode((string) $response->getBody()); $this->assertEquals('Not authorized: Malformed JWT token', $body->message); - $this->assertContains('ApiAuthorizationException', $body->stacktrace); + $this->assertContainsPolyfill('ApiAuthorizationException', $body->stacktrace); } } diff --combined tests/bookmark/LinkUtilsTest.php index 0d07897b,a7087953..ef00b92f --- a/tests/bookmark/LinkUtilsTest.php +++ b/tests/bookmark/LinkUtilsTest.php @@@ -2,7 -2,7 +2,7 @@@ namespace Shaarli\Bookmark; - use PHPUnit\Framework\TestCase; + use Shaarli\TestCase; require_once 'tests/utils/CurlUtils.php'; @@@ -42,19 -42,6 +42,19 @@@ class LinkUtilsTest extends TestCas $this->assertEquals(strtolower($charset), header_extract_charset($headers)); } + /** + * Test headers_extract_charset() when the charset is found with odd quotes. + */ + public function testHeadersExtractExistentCharsetWithQuotes() + { + $charset = 'x-MacCroatian'; + $headers = 'text/html; charset="' . $charset . '"otherstuff="test"'; + $this->assertEquals(strtolower($charset), header_extract_charset($headers)); + + $headers = 'text/html; charset=\'' . $charset . '\'otherstuff="test"'; + $this->assertEquals(strtolower($charset), header_extract_charset($headers)); + } + /** * Test headers_extract_charset() when the charset is not found. */ @@@ -450,13 -437,13 +450,13 @@@ カタカナ #カタカナ」カタカナ\n'; $autolinkedDescription = hashtag_autolink($rawDescription, $index); - $this->assertContains($this->getHashtagLink('hashtag', $index), $autolinkedDescription); - $this->assertNotContains(' #hashtag', $autolinkedDescription); - $this->assertNotContains('>#nothashtag', $autolinkedDescription); - $this->assertContains($this->getHashtagLink('ашок', $index), $autolinkedDescription); - $this->assertContains($this->getHashtagLink('カタカナ', $index), $autolinkedDescription); - $this->assertContains($this->getHashtagLink('hashtag_hashtag', $index), $autolinkedDescription); - $this->assertNotContains($this->getHashtagLink('hashtag-nothashtag', $index), $autolinkedDescription); + $this->assertContainsPolyfill($this->getHashtagLink('hashtag', $index), $autolinkedDescription); + $this->assertNotContainsPolyfill(' #hashtag', $autolinkedDescription); + $this->assertNotContainsPolyfill('>#nothashtag', $autolinkedDescription); + $this->assertContainsPolyfill($this->getHashtagLink('ашок', $index), $autolinkedDescription); + $this->assertContainsPolyfill($this->getHashtagLink('カタカナ', $index), $autolinkedDescription); + $this->assertContainsPolyfill($this->getHashtagLink('hashtag_hashtag', $index), $autolinkedDescription); + $this->assertNotContainsPolyfill($this->getHashtagLink('hashtag-nothashtag', $index), $autolinkedDescription); } /** @@@ -467,9 -454,9 +467,9 @@@ $rawDescription = 'blabla #hashtag x#nothashtag'; $autolinkedDescription = hashtag_autolink($rawDescription); - $this->assertContains($this->getHashtagLink('hashtag'), $autolinkedDescription); - $this->assertNotContains(' #hashtag', $autolinkedDescription); - $this->assertNotContains('>#nothashtag', $autolinkedDescription); + $this->assertContainsPolyfill($this->getHashtagLink('hashtag'), $autolinkedDescription); + $this->assertNotContainsPolyfill(' #hashtag', $autolinkedDescription); + $this->assertNotContainsPolyfill('>#nothashtag', $autolinkedDescription); } /** diff --combined tests/front/controller/admin/ManageShaareControllerTest/SaveBookmarkTest.php index a5e2dbc5,3999b44e..f7a68226 --- a/tests/front/controller/admin/ManageShaareControllerTest/SaveBookmarkTest.php +++ b/tests/front/controller/admin/ManageShaareControllerTest/SaveBookmarkTest.php @@@ -4,7 -4,6 +4,6 @@@ declare(strict_types=1) namespace Shaarli\Front\Controller\Admin\ManageShaareControllerTest; - use PHPUnit\Framework\TestCase; use Shaarli\Bookmark\Bookmark; use Shaarli\Config\ConfigManager; use Shaarli\Front\Controller\Admin\FrontAdminControllerMockHelper; @@@ -12,6 -11,7 +11,7 @@@ use Shaarli\Front\Controller\Admin\Mana use Shaarli\Front\Exception\WrongTokenException; use Shaarli\Http\HttpAccess; use Shaarli\Security\SessionManager; + use Shaarli\TestCase; use Shaarli\Thumbnailer; use Slim\Http\Request; use Slim\Http\Response; @@@ -88,17 -88,18 +88,18 @@@ class SaveBookmarkTest extends TestCas // Make sure that PluginManager hook is triggered $this->container->pluginManager - ->expects(static::at(0)) + ->expects(static::atLeastOnce()) ->method('executeHooks') + ->withConsecutive(['save_link']) ->willReturnCallback(function (string $hook, array $data) use ($parameters, $id): array { - static::assertSame('save_link', $hook); - - static::assertSame($id, $data['id']); - static::assertSame($parameters['lf_url'], $data['url']); - static::assertSame($parameters['lf_title'], $data['title']); - static::assertSame($parameters['lf_description'], $data['description']); - static::assertSame($parameters['lf_tags'], $data['tags']); - static::assertTrue($data['private']); + if ('save_link' === $hook) { + static::assertSame($id, $data['id']); + static::assertSame($parameters['lf_url'], $data['url']); + static::assertSame($parameters['lf_title'], $data['title']); + static::assertSame($parameters['lf_description'], $data['description']); + static::assertSame($parameters['lf_tags'], $data['tags']); + static::assertTrue($data['private']); + } return $data; }) @@@ -174,17 -175,18 +175,18 @@@ // Make sure that PluginManager hook is triggered $this->container->pluginManager - ->expects(static::at(0)) + ->expects(static::atLeastOnce()) ->method('executeHooks') + ->withConsecutive(['save_link']) ->willReturnCallback(function (string $hook, array $data) use ($parameters, $id): array { - static::assertSame('save_link', $hook); - - static::assertSame($id, $data['id']); - static::assertSame($parameters['lf_url'], $data['url']); - static::assertSame($parameters['lf_title'], $data['title']); - static::assertSame($parameters['lf_description'], $data['description']); - static::assertSame($parameters['lf_tags'], $data['tags']); - static::assertTrue($data['private']); + if ('save_link' === $hook) { + static::assertSame($id, $data['id']); + static::assertSame($parameters['lf_url'], $data['url']); + static::assertSame($parameters['lf_title'], $data['title']); + static::assertSame($parameters['lf_description'], $data['description']); + static::assertSame($parameters['lf_tags'], $data['tags']); + static::assertTrue($data['private']); + } return $data; }) @@@ -238,30 -240,6 +240,30 @@@ static::assertSame(302, $result->getStatusCode()); } + /** + * Test save a bookmark - with ID #0 + */ + public function testSaveBookmarkWithIdZero(): void + { + $parameters = ['lf_id' => '0']; + + $request = $this->createMock(Request::class); + $request + ->method('getParam') + ->willReturnCallback(function (string $key) use ($parameters): ?string { + return $parameters[$key] ?? null; + }) + ; + $response = new Response(); + + $this->container->bookmarkService->expects(static::once())->method('exists')->with(0)->willReturn(true); + $this->container->bookmarkService->expects(static::once())->method('get')->with(0)->willReturn(new Bookmark()); + + $result = $this->controller->save($request, $response); + + static::assertSame(302, $result->getStatusCode()); + } + /** * Change the password with a wrong existing password */