SubgroupTest.php in Subgroup (Graph) 1.0.x
File
tests/src/Kernel/SubgroupTest.php
View source
<?php
namespace Drupal\Tests\ggroup\Kernel;
use Drupal\group\Entity\GroupContent;
use Drupal\Tests\group\Kernel\GroupKernelTestBase;
class SubgroupTest extends GroupKernelTestBase {
public static $modules = [
'ggroup',
'ggroup_test_config',
];
protected $groupType;
protected $subGroupType;
protected function setUp() {
parent::setUp();
$this
->installConfig([
'ggroup_test_config',
]);
$this
->installSchema('ggroup', 'group_graph');
$this->entityTypeManager = $this->container
->get('entity_type.manager');
$this->groupType = $this->entityTypeManager
->getStorage('group_type')
->load('default');
$this->subGroupType = $this->entityTypeManager
->getStorage('group_type')
->load('subgroup');
}
public function testCreateSubgroup() {
list($group, $subGroup) = $this
->addGroup();
$this
->assertNotEmpty($group
->getContentByEntityId('subgroup:' . $this->subGroupType
->id(), $subGroup
->id()), 'Subgroup is group content');
}
public function testDeleteSubgroupFromGroupContent() {
list($group, $sub_group) = $this
->addGroup();
foreach (GroupContent::loadByEntity($sub_group) as $group_content) {
$group_content
->delete();
$this
->assertEquals(\Drupal::service('ggroup.group_hierarchy_manager')
->groupHasSubgroup($group, $sub_group), FALSE, 'Subgroup is removed');
}
}
public function testDeleteSubgroup() {
list($group, $sub_group) = $this
->addGroup();
$sub_group
->delete();
$this
->assertEquals(\Drupal::service('ggroup.group_hierarchy_manager')
->groupHasSubgroup($group, $sub_group), FALSE, 'Subgroup is removed');
}
private function addGroup() {
$group = $this
->createGroupByType($this->groupType
->id(), [
'uid' => $this
->getCurrentUser()
->id(),
]);
$sub_group = $this
->createGroupByType($this->subGroupType
->id(), [
'uid' => $this
->getCurrentUser()
->id(),
]);
$group
->addContent($sub_group, 'subgroup:' . $this->subGroupType
->id());
return [
$group,
$sub_group,
];
}
private function createGroupByType($type, array $values = []) {
$group = $this->entityTypeManager
->getStorage('group')
->create($values + [
'type' => $type,
'label' => $this
->randomMachineName(),
]);
$group
->enforceIsNew();
$group
->save();
return $group;
}
}