View source
<?php
namespace Drupal\Tests\condition_query\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
class RequestParamConditionTest extends KernelTestBase {
protected $pluginManager;
protected $requestStack;
protected static $modules = [
'system',
'user',
'condition_query',
];
protected function setUp() : void {
parent::setUp();
$this
->installSchema('system', [
'sequences',
]);
$this->pluginManager = $this->container
->get('plugin.manager.condition');
$this->requestStack = new RequestStack();
$this->container
->set('request_stack', $this->requestStack);
}
public function testEvaluate(string $request_path, array $config, bool $expected) : void {
$condition = $this->pluginManager
->createInstance('request_param');
foreach ($config as $key => $value) {
$condition
->setConfig($key, $value);
}
$request = Request::create($request_path);
$this->requestStack
->push($request);
$this
->assertEquals($expected, $condition
->execute());
$this->requestStack
->pop();
}
public function provideEvaluate() : array {
return [
'wrong query parameter' => [
'request_path' => '/my/page?broken=yes',
'config' => [
'request_param' => "test=yes",
],
'expected' => FALSE,
],
'right parameter, wrong value' => [
'request_path' => '/my/page?test=no',
'config' => [
'request_param' => "test=yes",
],
'expected' => FALSE,
],
'right parameter, right value' => [
'request_path' => '/my/page?test=yes',
'config' => [
'request_param' => "test=yes",
],
'expected' => TRUE,
],
'two parameters, both wrong' => [
'request_path' => '/my/page?test=no&foo=no',
'config' => [
'request_param' => "test=yes\r\nfoo=yes",
],
'expected' => FALSE,
],
'two parameters, one wrong, one right' => [
'request_path' => '/my/page?test=no&foo=yes',
'config' => [
'request_param' => "test=yes\r\nfoo=yes",
],
'expected' => TRUE,
],
'parameter without a value, present in request' => [
'request_path' => '/my/page?empty',
'config' => [
'request_param' => "test=yes\r\nempty",
],
'expected' => TRUE,
],
'parameter without a value, missing from request' => [
'request_path' => '/my/page',
'config' => [
'request_param' => "test=yes\r\nempty",
],
'expected' => FALSE,
],
];
}
public function testSummary(array $config, string $expected) : void {
$condition = $this->pluginManager
->createInstance('request_param');
foreach ($config as $key => $value) {
$condition
->setConfig($key, $value);
}
$this
->assertEquals($expected, $condition
->summary());
}
public function provideSummary() : array {
return [
'One parameter with a value' => [
'config' => [
'request_param' => "test=yes",
],
'expected' => 'Return true on the following query parameters: test=yes',
],
'One parameter with a value, negated' => [
'config' => [
'request_param' => "test=yes",
'negate' => TRUE,
],
'expected' => 'Do not return true on the following query parameters: test=yes',
],
'Two parameters, each with a value' => [
'config' => [
'request_param' => "test=yes\r\nfoo=no",
],
'expected' => 'Return true on the following query parameters: test=yes, foo=no',
],
'Three parameters, one without a value' => [
'config' => [
'request_param' => "test=yes\r\nfoo=no\r\nempty",
],
'expected' => 'Return true on the following query parameters: test=yes, foo=no, empty',
],
'Three parameters, one without a value, negated' => [
'config' => [
'request_param' => "test=yes\r\nfoo=no\r\nempty",
'negate' => TRUE,
],
'expected' => 'Do not return true on the following query parameters: test=yes, foo=no, empty',
],
];
}
}