OgStandardReferenceItemTest.php in Organic groups 8
File
tests/src/Kernel/Entity/OgStandardReferenceItemTest.php
View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\og\Kernel\Entity;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\KernelTests\KernelTestBase;
use Drupal\og\Og;
use Drupal\og\OgGroupAudienceHelperInterface;
class OgStandardReferenceItemTest extends KernelTestBase {
public static $modules = [
'user',
'entity_test',
'field',
'og',
'system',
];
protected $bundles;
protected $fieldName;
protected $groups;
protected function setUp() : void {
parent::setUp();
$this
->installConfig([
'og',
]);
$this
->installEntitySchema('entity_test');
$this
->installEntitySchema('og_membership');
$this
->installEntitySchema('user');
$this
->installSchema('system', 'sequences');
for ($i = 0; $i <= 2; $i++) {
$bundle = EntityTest::create([
'type' => mb_strtolower($this
->randomMachineName()),
'name' => $this
->randomString(),
]);
$bundle
->save();
$this->bundles[] = $bundle
->id();
}
for ($i = 0; $i < 2; $i++) {
$bundle = $this->bundles[$i];
Og::groupTypeManager()
->addGroup('entity_test', $bundle);
$group = EntityTest::create([
'type' => $bundle,
]);
$group
->save();
$this->groups[] = $group;
}
$this->fieldName = strtolower($this
->randomMachineName());
Og::CreateField(OgGroupAudienceHelperInterface::DEFAULT_FIELD, 'entity_test', $this->bundles[2], [
'field_name' => $this->fieldName,
]);
}
public function testStandardReference() {
$groups_query = function ($gid) {
return $this->container
->get('entity_type.manager')
->getStorage('entity_test')
->getQuery()
->condition($this->fieldName, $gid)
->execute();
};
$entity = EntityTest::create([
'type' => $this->bundles[2],
'name' => $this
->randomString(),
]);
$entity
->save();
$this
->assertEmpty($groups_query($this->groups[0]
->id()));
$entity = EntityTest::create([
'type' => $this->bundles[2],
'name' => $this
->randomString(),
$this->fieldName => [
[
'target_id' => $this->groups[1]
->id(),
],
],
]);
$entity
->save();
$this
->assertEmpty($groups_query($this->groups[0]
->id()));
$this
->assertEquals(array_keys($groups_query($this->groups[1]
->id())), [
$entity
->id(),
]);
}
}