You are here

public function MenuTreeParametersTest::testAddCondition in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/Menu/MenuTreeParametersTest.php \Drupal\Tests\Core\Menu\MenuTreeParametersTest::testAddCondition()

Tests addCondition().

@covers ::addCondition

File

core/tests/Drupal/Tests/Core/Menu/MenuTreeParametersTest.php, line 84

Class

MenuTreeParametersTest
Tests the menu link tree parameters value object.

Namespace

Drupal\Tests\Core\Menu

Code

public function testAddCondition() {
  $parameters = new MenuTreeParameters();

  // Verify default value.
  $this
    ->assertEquals([], $parameters->conditions);

  // Add a condition.
  $parameters
    ->addCondition('expanded', 1);
  $this
    ->assertEquals([
    'expanded' => 1,
  ], $parameters->conditions);

  // Add another condition.
  $parameters
    ->addCondition('has_children', 0);
  $this
    ->assertEquals([
    'expanded' => 1,
    'has_children' => 0,
  ], $parameters->conditions);

  // Add a condition with an operator.
  $parameters
    ->addCondition('provider', [
    'module1',
    'module2',
  ], 'IN');
  $this
    ->assertEquals([
    'expanded' => 1,
    'has_children' => 0,
    'provider' => [
      [
        'module1',
        'module2',
      ],
      'IN',
    ],
  ], $parameters->conditions);

  // Add another condition with an operator.
  $parameters
    ->addCondition('id', 1337, '<');
  $this
    ->assertEquals([
    'expanded' => 1,
    'has_children' => 0,
    'provider' => [
      [
        'module1',
        'module2',
      ],
      'IN',
    ],
    'id' => [
      1337,
      '<',
    ],
  ], $parameters->conditions);

  // It's impossible to add two conditions on the same field; in that case,
  // the old condition will be overwritten.
  $parameters
    ->addCondition('provider', 'other_module');
  $this
    ->assertEquals([
    'expanded' => 1,
    'has_children' => 0,
    'provider' => 'other_module',
    'id' => [
      1337,
      '<',
    ],
  ], $parameters->conditions);
}