View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\og\Kernel\Entity;
use Drupal\Core\Session\AccountInterface;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\KernelTests\KernelTestBase;
use Drupal\og\Og;
use Drupal\og\OgGroupAudienceHelperInterface;
use Drupal\user\Entity\User;
class SelectionHandlerTest extends KernelTestBase {
protected $selectionHandler;
public static $modules = [
'system',
'user',
'field',
'entity_reference',
'node',
'og',
];
protected $user1;
protected $user2;
protected $groupBundle;
protected $groupContentBundle;
protected $fieldDefinition;
protected $selectionPluginManager;
protected function setUp() : void {
parent::setUp();
$this
->installConfig([
'og',
]);
$this
->installEntitySchema('og_membership');
$this
->installEntitySchema('user');
$this
->installEntitySchema('node');
$this
->installSchema('system', 'sequences');
$this->groupBundle = mb_strtolower($this
->randomMachineName());
$this->groupContentBundle = mb_strtolower($this
->randomMachineName());
$this->selectionPluginManager = $this->container
->get('plugin.manager.entity_reference_selection');
NodeType::create([
'type' => $this->groupBundle,
'name' => $this
->randomString(),
])
->save();
NodeType::create([
'type' => $this->groupContentBundle,
'name' => $this
->randomString(),
])
->save();
Og::groupTypeManager()
->addGroup('node', $this->groupBundle);
$this->fieldDefinition = Og::createField(OgGroupAudienceHelperInterface::DEFAULT_FIELD, 'node', $this->groupContentBundle);
$options = [
'target_type' => $this->fieldDefinition
->getFieldStorageDefinition()
->getSetting('target_type'),
'handler' => $this->fieldDefinition
->getSetting('handler'),
'field_mode' => 'admin',
];
$this->selectionPluginManager
->getInstance($options);
$this->selectionHandler = $this->selectionPluginManager
->getSelectionHandler($this->fieldDefinition);
$this->user1 = User::create([
'name' => $this
->randomString(),
]);
$this->user1
->save();
$this->user2 = User::create([
'name' => $this
->randomString(),
]);
$this->user2
->save();
}
public function testSelectionHandler() {
$this
->assertEquals(get_class($this->selectionHandler
->getSelectionHandler()), 'Drupal\\node\\Plugin\\EntityReferenceSelection\\NodeSelection');
}
public function testSelectionHandlerResults() {
$user1_groups = $this
->createGroups(2, $this->user1);
$user2_groups = $this
->createGroups(2, $this->user2);
$this
->setCurrentAccount($this->user1);
$groups = $this->selectionHandler
->getReferenceableEntities();
$this
->assertEquals($user1_groups, array_keys($groups[$this->groupBundle]));
$this
->setCurrentAccount($this->user2);
$groups = $this->selectionHandler
->getReferenceableEntities();
$this
->assertEquals($user2_groups, array_keys($groups[$this->groupBundle]));
$options = [
'target_type' => $this->fieldDefinition
->getFieldStorageDefinition()
->getSetting('target_type'),
'handler' => $this->fieldDefinition
->getSetting('handler'),
'field_mode' => 'admin',
];
$this->selectionHandler = $this->selectionPluginManager
->getInstance($options);
$this
->setCurrentAccount($this->user1);
$groups = $this->selectionHandler
->getReferenceableEntities();
$this
->assertEquals($user2_groups, array_keys($groups[$this->groupBundle]));
$this
->setCurrentAccount($this->user2);
$groups = $this->selectionHandler
->getReferenceableEntities();
$this
->assertEquals($user1_groups, array_keys($groups[$this->groupBundle]));
}
protected function createGroups($amount, User $user) {
$groups = [];
for ($i = 0; $i <= $amount; $i++) {
$group = Node::create([
'title' => $this
->randomString(),
'uid' => $user
->id(),
'type' => $this->groupBundle,
]);
$group
->save();
$groups[] = $group
->id();
}
return $groups;
}
protected function setCurrentAccount(AccountInterface $account) {
$this->container
->get('account_switcher')
->switchTo($account);
}
}