2020-01-23 20:06:32 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-05-22 13:20:31 +02:00
|
|
|
namespace Shaarli\Front\Controller\Visitor;
|
2020-01-23 20:06:32 +01:00
|
|
|
|
|
|
|
use Shaarli\Bookmark\BookmarkFilter;
|
2020-09-29 14:41:40 +02:00
|
|
|
use Shaarli\TestCase;
|
2020-05-22 13:20:31 +02:00
|
|
|
use Slim\Http\Request;
|
2020-05-22 11:02:56 +02:00
|
|
|
use Slim\Http\Response;
|
2020-01-23 20:06:32 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class ShaarliControllerTest
|
|
|
|
*
|
2020-06-13 11:22:14 +02:00
|
|
|
* This class is used to test default behavior of ShaarliVisitorController abstract class.
|
2020-01-23 20:06:32 +01:00
|
|
|
* It uses a dummy non abstract controller.
|
|
|
|
*/
|
2020-06-13 11:22:14 +02:00
|
|
|
class ShaarliVisitorControllerTest extends TestCase
|
2020-01-23 20:06:32 +01:00
|
|
|
{
|
2020-05-20 12:43:40 +02:00
|
|
|
use FrontControllerMockHelper;
|
2020-01-23 20:06:32 +01:00
|
|
|
|
|
|
|
/** @var LoginController */
|
|
|
|
protected $controller;
|
|
|
|
|
|
|
|
/** @var mixed[] List of variable assigned to the template */
|
|
|
|
protected $assignedValues;
|
|
|
|
|
2020-05-22 13:20:31 +02:00
|
|
|
/** @var Request */
|
|
|
|
protected $request;
|
|
|
|
|
2020-01-23 20:06:32 +01:00
|
|
|
public function setUp(): void
|
|
|
|
{
|
2020-05-20 12:43:40 +02:00
|
|
|
$this->createContainer();
|
|
|
|
|
2020-05-22 13:20:31 +02:00
|
|
|
$this->controller = new class($this->container) extends ShaarliVisitorController
|
2020-01-23 20:06:32 +01:00
|
|
|
{
|
2020-05-22 13:20:31 +02:00
|
|
|
public function assignView(string $key, $value): ShaarliVisitorController
|
2020-01-23 20:06:32 +01:00
|
|
|
{
|
|
|
|
return parent::assignView($key, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function render(string $template): string
|
|
|
|
{
|
|
|
|
return parent::render($template);
|
|
|
|
}
|
2020-05-22 11:02:56 +02:00
|
|
|
|
|
|
|
public function redirectFromReferer(
|
2020-05-22 13:20:31 +02:00
|
|
|
Request $request,
|
2020-05-22 11:02:56 +02:00
|
|
|
Response $response,
|
|
|
|
array $loopTerms = [],
|
2020-06-13 11:22:14 +02:00
|
|
|
array $clearParams = [],
|
|
|
|
string $anchor = null
|
2020-05-22 11:02:56 +02:00
|
|
|
): Response {
|
2020-06-13 11:22:14 +02:00
|
|
|
return parent::redirectFromReferer($request, $response, $loopTerms, $clearParams, $anchor);
|
2020-05-22 11:02:56 +02:00
|
|
|
}
|
2020-01-23 20:06:32 +01:00
|
|
|
};
|
|
|
|
$this->assignedValues = [];
|
2020-05-22 13:20:31 +02:00
|
|
|
|
|
|
|
$this->request = $this->createMock(Request::class);
|
2020-01-23 20:06:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAssignView(): void
|
|
|
|
{
|
2020-05-20 12:43:40 +02:00
|
|
|
$this->assignTemplateVars($this->assignedValues);
|
|
|
|
|
2020-01-23 20:06:32 +01:00
|
|
|
$self = $this->controller->assignView('variableName', 'variableValue');
|
|
|
|
|
2020-05-22 13:20:31 +02:00
|
|
|
static::assertInstanceOf(ShaarliVisitorController::class, $self);
|
2020-01-23 20:06:32 +01:00
|
|
|
static::assertSame('variableValue', $this->assignedValues['variableName']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRender(): void
|
|
|
|
{
|
2020-05-20 12:43:40 +02:00
|
|
|
$this->assignTemplateVars($this->assignedValues);
|
|
|
|
|
|
|
|
$this->container->bookmarkService
|
|
|
|
->method('count')
|
|
|
|
->willReturnCallback(function (string $visibility): int {
|
|
|
|
return $visibility === BookmarkFilter::$PRIVATE ? 5 : 10;
|
|
|
|
})
|
|
|
|
;
|
|
|
|
|
|
|
|
$this->container->pluginManager
|
|
|
|
->method('executeHooks')
|
|
|
|
->willReturnCallback(function (string $hook, array &$data, array $params): array {
|
|
|
|
return $data[$hook] = $params;
|
|
|
|
});
|
|
|
|
$this->container->pluginManager->method('getErrors')->willReturn(['error']);
|
|
|
|
|
|
|
|
$this->container->loginManager->method('isLoggedIn')->willReturn(true);
|
|
|
|
|
2020-01-23 20:06:32 +01:00
|
|
|
$render = $this->controller->render('templateName');
|
|
|
|
|
|
|
|
static::assertSame('templateName', $render);
|
|
|
|
|
2021-01-19 10:34:11 +01:00
|
|
|
static::assertSame('templateName', $this->assignedValues['_PAGE_']);
|
|
|
|
static::assertSame('templateName', $this->assignedValues['template']);
|
|
|
|
|
2020-01-23 20:06:32 +01:00
|
|
|
static::assertSame(10, $this->assignedValues['linkcount']);
|
|
|
|
static::assertSame(5, $this->assignedValues['privateLinkcount']);
|
|
|
|
static::assertSame(['error'], $this->assignedValues['plugin_errors']);
|
|
|
|
|
|
|
|
static::assertSame('templateName', $this->assignedValues['plugins_includes']['render_includes']['target']);
|
|
|
|
static::assertTrue($this->assignedValues['plugins_includes']['render_includes']['loggedin']);
|
|
|
|
static::assertSame('templateName', $this->assignedValues['plugins_header']['render_header']['target']);
|
|
|
|
static::assertTrue($this->assignedValues['plugins_header']['render_header']['loggedin']);
|
|
|
|
static::assertSame('templateName', $this->assignedValues['plugins_footer']['render_footer']['target']);
|
|
|
|
static::assertTrue($this->assignedValues['plugins_footer']['render_footer']['loggedin']);
|
|
|
|
}
|
2020-05-22 11:02:56 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test redirectFromReferer() - Default behaviour
|
|
|
|
*/
|
|
|
|
public function testRedirectFromRefererDefault(): void
|
|
|
|
{
|
2020-09-22 15:17:13 +02:00
|
|
|
$this->container->environment['HTTP_REFERER'] = 'http://shaarli/subfolder/controller?query=param&other=2';
|
2020-05-22 11:02:56 +02:00
|
|
|
|
|
|
|
$response = new Response();
|
|
|
|
|
2020-05-22 13:20:31 +02:00
|
|
|
$result = $this->controller->redirectFromReferer($this->request, $response);
|
2020-05-22 11:02:56 +02:00
|
|
|
|
|
|
|
static::assertSame(302, $result->getStatusCode());
|
|
|
|
static::assertSame(['/subfolder/controller?query=param&other=2'], $result->getHeader('location'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test redirectFromReferer() - With a loop term not matched in the referer
|
|
|
|
*/
|
|
|
|
public function testRedirectFromRefererWithUnmatchedLoopTerm(): void
|
|
|
|
{
|
2020-09-22 15:17:13 +02:00
|
|
|
$this->container->environment['HTTP_REFERER'] = 'http://shaarli/subfolder/controller?query=param&other=2';
|
2020-05-22 11:02:56 +02:00
|
|
|
|
|
|
|
$response = new Response();
|
|
|
|
|
2020-05-22 13:20:31 +02:00
|
|
|
$result = $this->controller->redirectFromReferer($this->request, $response, ['nope']);
|
2020-05-22 11:02:56 +02:00
|
|
|
|
|
|
|
static::assertSame(302, $result->getStatusCode());
|
|
|
|
static::assertSame(['/subfolder/controller?query=param&other=2'], $result->getHeader('location'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test redirectFromReferer() - With a loop term matching the referer in its path -> redirect to default
|
|
|
|
*/
|
|
|
|
public function testRedirectFromRefererWithMatchingLoopTermInPath(): void
|
|
|
|
{
|
2020-09-22 15:17:13 +02:00
|
|
|
$this->container->environment['HTTP_REFERER'] = 'http://shaarli/subfolder/controller?query=param&other=2';
|
2020-05-22 11:02:56 +02:00
|
|
|
|
|
|
|
$response = new Response();
|
|
|
|
|
2020-05-22 13:20:31 +02:00
|
|
|
$result = $this->controller->redirectFromReferer($this->request, $response, ['nope', 'controller']);
|
2020-05-22 11:02:56 +02:00
|
|
|
|
|
|
|
static::assertSame(302, $result->getStatusCode());
|
2020-06-13 11:22:14 +02:00
|
|
|
static::assertSame(['/subfolder/'], $result->getHeader('location'));
|
2020-05-22 11:02:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test redirectFromReferer() - With a loop term matching the referer in its query parameters -> redirect to default
|
|
|
|
*/
|
|
|
|
public function testRedirectFromRefererWithMatchingLoopTermInQueryParam(): void
|
|
|
|
{
|
2020-09-22 15:17:13 +02:00
|
|
|
$this->container->environment['HTTP_REFERER'] = 'http://shaarli/subfolder/controller?query=param&other=2';
|
2020-05-22 11:02:56 +02:00
|
|
|
|
|
|
|
$response = new Response();
|
|
|
|
|
2020-05-22 13:20:31 +02:00
|
|
|
$result = $this->controller->redirectFromReferer($this->request, $response, ['nope', 'other']);
|
2020-05-22 11:02:56 +02:00
|
|
|
|
|
|
|
static::assertSame(302, $result->getStatusCode());
|
2020-06-13 11:22:14 +02:00
|
|
|
static::assertSame(['/subfolder/'], $result->getHeader('location'));
|
2020-05-22 11:02:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test redirectFromReferer() - With a loop term matching the referer in its query value
|
|
|
|
* -> we do not block redirection for query parameter values.
|
|
|
|
*/
|
|
|
|
public function testRedirectFromRefererWithMatchingLoopTermInQueryValue(): void
|
|
|
|
{
|
2020-09-22 15:17:13 +02:00
|
|
|
$this->container->environment['HTTP_REFERER'] = 'http://shaarli/subfolder/controller?query=param&other=2';
|
2020-05-22 11:02:56 +02:00
|
|
|
|
|
|
|
$response = new Response();
|
|
|
|
|
2020-05-22 13:20:31 +02:00
|
|
|
$result = $this->controller->redirectFromReferer($this->request, $response, ['nope', 'param']);
|
2020-05-22 11:02:56 +02:00
|
|
|
|
|
|
|
static::assertSame(302, $result->getStatusCode());
|
|
|
|
static::assertSame(['/subfolder/controller?query=param&other=2'], $result->getHeader('location'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test redirectFromReferer() - With a loop term matching the referer in its domain name
|
|
|
|
* -> we do not block redirection for shaarli's hosts
|
|
|
|
*/
|
|
|
|
public function testRedirectFromRefererWithLoopTermInDomain(): void
|
|
|
|
{
|
2020-09-22 15:17:13 +02:00
|
|
|
$this->container->environment['HTTP_REFERER'] = 'http://shaarli/subfolder/controller?query=param&other=2';
|
2020-05-22 11:02:56 +02:00
|
|
|
|
|
|
|
$response = new Response();
|
|
|
|
|
2020-05-22 13:20:31 +02:00
|
|
|
$result = $this->controller->redirectFromReferer($this->request, $response, ['shaarli']);
|
2020-05-22 11:02:56 +02:00
|
|
|
|
|
|
|
static::assertSame(302, $result->getStatusCode());
|
|
|
|
static::assertSame(['/subfolder/controller?query=param&other=2'], $result->getHeader('location'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test redirectFromReferer() - With a loop term matching a query parameter AND clear this query param
|
|
|
|
* -> the param should be cleared before checking if it matches the redir loop terms
|
|
|
|
*/
|
|
|
|
public function testRedirectFromRefererWithMatchingClearedParam(): void
|
|
|
|
{
|
2020-09-22 15:17:13 +02:00
|
|
|
$this->container->environment['HTTP_REFERER'] = 'http://shaarli/subfolder/controller?query=param&other=2';
|
2020-05-22 11:02:56 +02:00
|
|
|
|
|
|
|
$response = new Response();
|
|
|
|
|
2020-05-22 13:20:31 +02:00
|
|
|
$result = $this->controller->redirectFromReferer($this->request, $response, ['query'], ['query']);
|
2020-05-22 11:02:56 +02:00
|
|
|
|
|
|
|
static::assertSame(302, $result->getStatusCode());
|
|
|
|
static::assertSame(['/subfolder/controller?other=2'], $result->getHeader('location'));
|
|
|
|
}
|
2020-09-22 15:17:13 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test redirectFromReferer() - From another domain -> we ignore the given referrer.
|
|
|
|
*/
|
|
|
|
public function testRedirectExternalReferer(): void
|
|
|
|
{
|
|
|
|
$this->container->environment['HTTP_REFERER'] = 'http://other.domain.tld/controller?query=param&other=2';
|
|
|
|
|
|
|
|
$response = new Response();
|
|
|
|
|
|
|
|
$result = $this->controller->redirectFromReferer($this->request, $response, ['query'], ['query']);
|
|
|
|
|
|
|
|
static::assertSame(302, $result->getStatusCode());
|
|
|
|
static::assertSame(['/subfolder/'], $result->getHeader('location'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test redirectFromReferer() - From another domain -> we ignore the given referrer.
|
|
|
|
*/
|
|
|
|
public function testRedirectExternalRefererExplicitDomainName(): void
|
|
|
|
{
|
|
|
|
$this->container->environment['SERVER_NAME'] = 'my.shaarli.tld';
|
|
|
|
$this->container->environment['HTTP_REFERER'] = 'http://your.shaarli.tld/controller?query=param&other=2';
|
|
|
|
|
|
|
|
$response = new Response();
|
|
|
|
|
|
|
|
$result = $this->controller->redirectFromReferer($this->request, $response, ['query'], ['query']);
|
|
|
|
|
|
|
|
static::assertSame(302, $result->getStatusCode());
|
|
|
|
static::assertSame(['/subfolder/'], $result->getHeader('location'));
|
|
|
|
}
|
2020-01-23 20:06:32 +01:00
|
|
|
}
|