public function ContextAllAnyTest::testContextAny in Context 8.4
Tests the context any condition against a path/route.
File
- tests/
src/ Kernel/ ContextAllAnyTest.php, line 154
Class
- ContextAllAnyTest
- Tests the context all condition plugin.
Namespace
Drupal\Tests\context\KernelCode
public function testContextAny() {
$request = Request::create('/node/2');
$this->requestStack
->push($request);
/** @var \Drupal\context\Plugin\Condition\RequestDomain $condition */
$condition = $this->pluginManager
->createInstance('context');
$this->aliasManager
->addAlias('/node/2', '/node/2');
// Test if condition is true for one context.
$condition
->setConfig('values', 'another_context');
$this
->assertTrue($condition
->evaluate(), 'All contexts are active');
$this
->assertEquals($condition
->summary(), 'Return true on the basis of other active contexts: another_context', 'Context applied.');
$condition
->setConfig('values', 'test');
$this
->assertTrue($condition
->evaluate(), 'All contexts are active');
$this
->assertEquals($condition
->summary(), 'Return true on the basis of other active contexts: test', 'Context applied.');
// Test if condition is true for two contexts.
$condition
->setConfig('values', "another_context\r\ntest");
$this
->assertTrue($condition
->evaluate(), 'All contexts are active');
$this
->assertEquals($condition
->summary(), 'Return true on the basis of other active contexts: another_context, test', 'Context applied.');
// Test if condition is true for one context with an asterisks.
$condition
->setConfig('values', 'anoth*');
$this
->assertTrue($condition
->evaluate(), 'All contexts are active');
$this
->assertEquals($condition
->summary(), 'Return true on the basis of other active contexts: anoth*', 'Context applied.');
$condition
->setConfig('values', 'tes*');
$this
->assertTrue($condition
->evaluate(), 'All contexts are active');
$this
->assertEquals($condition
->summary(), 'Return true on the basis of other active contexts: tes*', 'Context applied.');
// Test that condition is false on specific route for one context and true
// for the other.
$request = Request::create('/user/3');
$request->attributes
->set('_route', 'entity.user.canonical');
$request->request
->set('user', 3);
$request->attributes
->set('_raw_variables', new ParameterBag([
'user' => 3,
]));
$request->attributes
->set('_route_object', new Route('/user/{user}'));
$this->requestStack
->push($request);
$condition
->setConfig('values', 'another_context');
$this->aliasManager
->addAlias('/user/3', '/user/3');
$this
->assertFalse($condition
->evaluate(), 'Any context is not active');
$condition
->setConfig('values', 'another_co*');
$this
->assertFalse($condition
->evaluate(), 'Any context is not active');
$condition
->setConfig('values', "another_context\r\ntest");
$this
->assertTrue($condition
->evaluate(), 'Any context is active');
$this
->assertEquals($condition
->summary(), 'Return true on the basis of other active contexts: another_context, test', 'Context applied.');
$condition
->setConfig('values', 'test');
$this
->assertTrue($condition
->evaluate(), 'Any context is active');
$this
->assertEquals($condition
->summary(), 'Return true on the basis of other active contexts: test', 'Context applied.');
$condition
->setConfig('values', 'tes*');
$this
->assertTrue($condition
->evaluate(), 'Any context is active');
$this
->assertEquals($condition
->summary(), 'Return true on the basis of other active contexts: tes*', 'Context applied.');
// Test that condition is false with the tilde.
$condition
->setConfig('values', "another_context\r\n~test");
$this
->assertFalse($condition
->evaluate(), 'Any context is not active');
}