function EntityBundleConditionTest::testConditions

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/EntityBundleConditionTest.php \Drupal\KernelTests\Core\Entity\EntityBundleConditionTest::testConditions()
  2. 10 core/tests/Drupal/KernelTests/Core/Entity/EntityBundleConditionTest.php \Drupal\KernelTests\Core\Entity\EntityBundleConditionTest::testConditions()

Tests conditions.

File

core/tests/Drupal/KernelTests/Core/Entity/EntityBundleConditionTest.php, line 36

Class

EntityBundleConditionTest
Tests that entity bundle conditions works properly.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testConditions() : void {
    $this->createUser();
    // Get some entities of various bundles to check against.
    $page = EntityTestWithBundle::create([
        'type' => 'page',
        'name' => $this->randomMachineName(),
    ]);
    $page->save();
    $article = EntityTestWithBundle::create([
        'type' => 'article',
        'name' => $this->randomMachineName(),
    ]);
    $article->save();
    $test = EntityTestWithBundle::create([
        'type' => 'test',
        'name' => $this->randomMachineName(),
    ]);
    $test->save();
    // Grab the bundle condition and configure it to check against bundle of
    // 'article' and set the context to the page type entity.
    
    /** @var \Drupal\Core\Entity\Plugin\Condition\EntityBundle $condition */
    $condition = $this->container
        ->get('plugin.manager.condition')
        ->createInstance('entity_bundle:entity_test_with_bundle')
        ->setConfig('bundles', [
        'article' => 'article',
    ])
        ->setContextValue('entity_test_with_bundle', $page);
    $this->assertFalse($condition->execute(), 'Page type entities fail bundle checks for articles.');
    // Check for the proper summary.
    $this->assertEquals('Test entity bundle is article', $condition->summary());
    $this->assertEquals('entity_test', $condition->getPluginDefinition()['provider']);
    // Set the bundle check to page.
    $condition->setConfig('bundles', [
        'page' => 'page',
    ]);
    $this->assertTrue($condition->execute(), 'Page type entities pass bundle checks for pages');
    // Check for the proper summary.
    $this->assertEquals('Test entity bundle is page', $condition->summary());
    // Set the bundle check to page or article.
    $condition->setConfig('bundles', [
        'page' => 'page',
        'article' => 'article',
    ]);
    $this->assertTrue($condition->execute(), 'Page type entities pass bundle checks for pages or articles');
    // Check for the proper summary.
    $this->assertEquals('Test entity bundle is page or article', $condition->summary());
    // Set the context to the article entity.
    $condition->setContextValue('entity_test_with_bundle', $article);
    $this->assertTrue($condition->execute(), 'Article type entities pass bundle checks for pages or articles');
    // Set the context to the test entity.
    $condition->setContextValue('entity_test_with_bundle', $test);
    $this->assertFalse($condition->execute(), 'Test type entities pass bundle checks for pages or articles');
    // Check a greater than 2 bundles summary scenario.
    $condition->setConfig('bundles', [
        'page' => 'page',
        'article' => 'article',
        'test' => 'test',
    ]);
    $this->assertEquals('Test entity bundle is page, article or test', $condition->summary());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.