You are here

public function ActiveEntityTest::testGet 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::testGet()

@covers ::get

File

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

Class

ActiveEntityTest
Tests the Active service.

Namespace

Drupal\Tests\config_selector\Kernel

Code

public function testGet() {

  /** @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
    ->get('config_test', 'test')
    ->id());

  // Disable the entity meaning getIdFromFeature() cannot return an ID.
  $entity
    ->setStatus(FALSE)
    ->save();
  $this
    ->assertNull($service
    ->get('config_test', 'test'));

  // Create another entity in a different feature.
  $entity2 = $this->testEntityStorage
    ->create([
    'id' => 'test_2',
  ]);
  $entity2
    ->setThirdPartySetting('config_selector', 'feature', 'test_two');
  $entity2
    ->save();
  $this
    ->assertNull($service
    ->get('config_test', 'test'));
  $this
    ->assertSame('test_2', $service
    ->get('config_test', 'test_two')
    ->id());

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

  // Test priority sorting when there is more than one active.
  $entity
    ->setStatus(TRUE)
    ->setThirdPartySetting('config_selector', 'priority', 1)
    ->save();
  $entity2
    ->setThirdPartySetting('config_selector', 'priority', 0)
    ->save();
  $this
    ->assertSame('test_1', $service
    ->get('config_test', 'test')
    ->id());
  $entity2
    ->setThirdPartySetting('config_selector', 'priority', 2)
    ->save();
  $this
    ->assertSame('test_2', $service
    ->get('config_test', 'test')
    ->id());
}