You are here

public function ContextAllAnyTest::testContextAll in Context 8.4

Tests the context all condition against a path/route.

File

tests/src/Kernel/ContextAllAnyTest.php, line 86

Class

ContextAllAnyTest
Tests the context all condition plugin.

Namespace

Drupal\Tests\context\Kernel

Code

public function testContextAll() {
  $request = Request::create('/node/22');
  $this->requestStack
    ->push($request);

  /** @var \Drupal\context\Plugin\Condition\RequestDomain $condition */
  $condition = $this->pluginManager
    ->createInstance('context_all');
  $this->aliasManager
    ->addAlias('/node/22', '/node/22');

  // 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', 'ano*');
  $this
    ->assertTrue($condition
    ->evaluate(), 'All contexts are active');
  $this
    ->assertEquals($condition
    ->summary(), 'Return true on the basis of other active contexts: ano*', 'Context applied.');
  $condition
    ->setConfig('values', 'te*');
  $this
    ->assertTrue($condition
    ->evaluate(), 'All contexts are active');
  $this
    ->assertEquals($condition
    ->summary(), 'Return true on the basis of other active contexts: te*', 'Context applied.');

  // Test that condition is false on specific route for one context and true
  // for the other.
  $request = Request::create('/user/2');
  $request->attributes
    ->set('_route', 'entity.user.canonical');
  $request->request
    ->set('user', 2);
  $request->attributes
    ->set('_raw_variables', new ParameterBag([
    'user' => 2,
  ]));
  $request->attributes
    ->set('_route_object', new Route('/user/{user}'));
  $this->requestStack
    ->push($request);
  $condition
    ->setConfig('values', 'another_context');
  $this->aliasManager
    ->addAlias('/user/2', '/user/2');
  $this
    ->assertFalse($condition
    ->evaluate(), 'All contexts are not active');
  $condition
    ->setConfig('values', 'another_co*');
  $this
    ->assertFalse($condition
    ->evaluate(), 'All contexts are not active');
  $condition
    ->setConfig('values', "another_context\r\ntest");
  $this
    ->assertFalse($condition
    ->evaluate(), 'All contexts are not active');
  $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.');
  $condition
    ->setConfig('values', 'te*');
  $this
    ->assertTrue($condition
    ->evaluate(), 'All contexts are active');
  $this
    ->assertEquals($condition
    ->summary(), 'Return true on the basis of other active contexts: te*', 'Context applied.');

  // Test that condition is true with the tilde.
  $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.');
}