View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\og\Kernel\Entity;
use Drupal\entity_test\Entity\EntityTestStringId;
use Drupal\KernelTests\KernelTestBase;
use Drupal\og\Og;
use Drupal\og\OgGroupAudienceHelperInterface;
class ReferenceStringIdTest extends KernelTestBase {
public static $modules = [
'user',
'entity_test',
'field',
'og',
'system',
];
protected $bundles;
protected $fieldName;
protected $group;
protected function setUp() : void {
parent::setUp();
$this
->installConfig([
'og',
]);
$this
->installEntitySchema('entity_test_string_id');
$this
->installEntitySchema('og_membership');
$this
->installEntitySchema('user');
$this
->installSchema('system', 'sequences');
for ($i = 0; $i < 2; $i++) {
$bundle = EntityTestStringId::create([
'type' => mb_strtolower($this
->randomMachineName()),
'name' => $this
->randomString(),
'id' => $this
->randomMachineName(),
]);
$bundle
->save();
$this->bundles[] = $bundle
->id();
}
$group = EntityTestStringId::create([
'type' => $this->bundles[0],
'id' => $this
->randomMachineName(),
]);
$group
->save();
$this->group = $group;
Og::groupTypeManager()
->addGroup('entity_test_string_id', $this->bundles[0]);
$this->fieldName = strtolower($this
->randomMachineName());
Og::CreateField(OgGroupAudienceHelperInterface::DEFAULT_FIELD, 'entity_test_string_id', $this->bundles[1], [
'field_name' => $this->fieldName,
]);
}
public function testReferencingStringIds() {
$entity = EntityTestStringId::create([
'type' => $this->bundles[1],
'name' => $this
->randomString(),
'id' => $this
->randomMachineName(),
$this->fieldName => [
[
'target_id' => $this->group
->id(),
],
],
]);
$entity
->save();
$references = $this->container
->get('entity_type.manager')
->getStorage('entity_test_string_id')
->getQuery()
->condition($this->fieldName, $this->group
->id())
->execute();
$this
->assertEquals([
$entity
->id(),
], array_keys($references), 'The correct group is referenced.');
}
}