You are here

public function ActiveEntityTest::testGetFromEntity in Configuration selector 8

Same name and namespace in other branches
  1. 8.2 tests/src/Kernel/ActiveEntityTest.php \Drupal\Tests\config_selector\Kernel\ActiveEntityTest::testGetFromEntity()

@covers ::getFromEntity

File

tests/src/Kernel/ActiveEntityTest.php, line 85

Class

ActiveEntityTest
Tests the Active service.

Namespace

Drupal\Tests\config_selector\Kernel

Code

public function testGetFromEntity() {

  /** @var \Drupal\config_selector\ActiveEntity $service */
  $service = $this->container
    ->get('config_selector.active');

  /** @var \Drupal\Core\Config\Entity\ConfigEntityInterface $entity */
  $entity = $this->testEntityStorage
    ->create([
    'id' => 'test_1',
  ]);
  $entity
    ->setThirdPartySetting('config_selector', 'feature', 'test');
  $entity
    ->save();
  $this
    ->assertSame('test_1', $service
    ->getFromEntity($entity)
    ->id());

  // Disable the entity because it is the only one we will return the same ID.
  $entity
    ->setStatus(FALSE)
    ->save();
  $this
    ->assertSame('test_1', $service
    ->getFromEntity($entity)
    ->id());

  // Create another entity.
  $entity2 = $this->testEntityStorage
    ->create([
    'id' => 'test_2',
  ]);
  $entity2
    ->save();

  // test_2 is not in feature so should return itself.
  $this
    ->assertSame('test_1', $service
    ->getFromEntity($entity)
    ->id());
  $this
    ->assertSame('test_2', $service
    ->getFromEntity($entity2)
    ->id());

  // Move test_2 into it's own feature.
  $entity2
    ->setThirdPartySetting('config_selector', 'feature', 'test_two');

  // Still get test_1 for the test feature because test_2 is in another
  // feature.
  $this
    ->assertSame('test_1', $service
    ->getFromEntity($entity)
    ->id());
  $this
    ->assertSame('test_2', $service
    ->getFromEntity($entity2)
    ->id());

  // Move entity 2 to the same feature.
  $entity2
    ->setThirdPartySetting('config_selector', 'feature', 'test')
    ->save();
  $this
    ->assertSame('test_2', $service
    ->getFromEntity($entity)
    ->id());
  $this
    ->assertSame('test_2', $service
    ->getFromEntity($entity2)
    ->id());
}