*
* @package Api
*/
- class ApiMiddlewareTest extends \PHPUnit\Framework\TestCase
+ class ApiMiddlewareTest extends \Shaarli\TestCase
{
/**
* @var string datastore to test write operations
protected static $testDatastore = 'sandbox/datastore.php';
/**
- * @var \ConfigManager instance
+ * @var ConfigManager instance
*/
protected $conf;
@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.
$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);
}
/**
$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);
}
/**
$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);
}
/**
$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);
}
/**
$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);
}
}
namespace Shaarli\Bookmark;
- use PHPUnit\Framework\TestCase;
+ use Shaarli\TestCase;
require_once 'tests/utils/CurlUtils.php';
$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.
*/
カタカナ #カタカナ」カタカナ\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);
}
/**
$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);
}
/**
namespace Shaarli\Front\Controller\Admin\ManageShaareControllerTest;
- use PHPUnit\Framework\TestCase;
use Shaarli\Bookmark\Bookmark;
use Shaarli\Config\ConfigManager;
use Shaarli\Front\Controller\Admin\FrontAdminControllerMockHelper;
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;
// 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;
})
// 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;
})
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
*/