View source
<?php
namespace Drupal\Tests\config_selector\Kernel;
use Drupal\KernelTests\KernelTestBase;
class ActiveEntityTest extends KernelTestBase {
protected static $modules = [
'system',
'config_test',
'config_selector',
'config_selector_test_one',
];
protected $testEntityStorage;
public function setUp() {
parent::setUp();
$this->testEntityStorage = $this->container
->get('entity_type.manager')
->getStorage('config_test');
}
public function testGet() {
$service = $this->container
->get('config_selector.active');
$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());
$entity
->setStatus(FALSE)
->save();
$this
->assertNull($service
->get('config_test', 'test'));
$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());
$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'));
$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());
}
public function testGetFromEntity() {
$service = $this->container
->get('config_selector.active');
$entity = $this->testEntityStorage
->create([
'id' => 'test_1',
]);
$entity
->setThirdPartySetting('config_selector', 'feature', 'test');
$entity
->save();
$this
->assertSame('test_1', $service
->getFromEntity($entity)
->id());
$entity
->setStatus(FALSE)
->save();
$this
->assertSame('test_1', $service
->getFromEntity($entity)
->id());
$entity2 = $this->testEntityStorage
->create([
'id' => 'test_2',
]);
$entity2
->save();
$this
->assertSame('test_1', $service
->getFromEntity($entity)
->id());
$this
->assertSame('test_2', $service
->getFromEntity($entity2)
->id());
$entity2
->setThirdPartySetting('config_selector', 'feature', 'test_two');
$this
->assertSame('test_1', $service
->getFromEntity($entity)
->id());
$this
->assertSame('test_2', $service
->getFromEntity($entity2)
->id());
$entity2
->setThirdPartySetting('config_selector', 'feature', 'test')
->save();
$this
->assertSame('test_2', $service
->getFromEntity($entity)
->id());
$this
->assertSame('test_2', $service
->getFromEntity($entity2)
->id());
}
}