View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\og\Unit;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Config\Config;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\RouteBuilderInterface;
use Drupal\Tests\UnitTestCase;
use Drupal\og\Entity\OgRole;
use Drupal\og\Event\GroupCreationEvent;
use Drupal\og\Event\GroupCreationEventInterface;
use Drupal\og\Event\PermissionEventInterface;
use Drupal\og\GroupTypeManager;
use Drupal\og\GroupTypeManagerInterface;
use Drupal\og\OgGroupAudienceHelperInterface;
use Drupal\og\OgRoleManagerInterface;
use Drupal\og\PermissionManagerInterface;
use Prophecy\Argument;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class GroupTypeManagerTest extends UnitTestCase {
protected $config;
protected $configFactory;
protected $entityTypeManager;
protected $entityStorage;
protected $ogRole;
protected $entityTypeBundleInfo;
protected $eventDispatcher;
protected $permissionEvent;
protected $cache;
protected $permissionManager;
protected $ogRoleManager;
protected $routeBuilder;
protected $groupAudienceHelper;
protected function setUp() : void {
$this->config = $this
->prophesize(Config::class);
$this->configFactory = $this
->prophesize(ConfigFactoryInterface::class);
$this->entityTypeBundleInfo = $this
->prophesize(EntityTypeBundleInfoInterface::class);
$this->entityTypeManager = $this
->prophesize(EntityTypeManagerInterface::class);
$this->entityStorage = $this
->prophesize(EntityStorageInterface::class);
$this->eventDispatcher = $this
->prophesize(EventDispatcherInterface::class);
$this->ogRole = $this
->prophesize(OgRole::class);
$this->ogRoleManager = $this
->prophesize(OgRoleManagerInterface::class);
$this->permissionEvent = $this
->prophesize(PermissionEventInterface::class);
$this->permissionManager = $this
->prophesize(PermissionManagerInterface::class);
$this->cache = $this
->prophesize(CacheBackendInterface::class);
$this->routeBuilder = $this
->prophesize(RouteBuilderInterface::class);
$this->groupAudienceHelper = $this
->prophesize(OgGroupAudienceHelperInterface::class);
}
public function testInstance() {
$group_manager = $this
->createGroupManager();
$this
->assertInstanceOf(GroupTypeManagerInterface::class, $group_manager);
}
public function testIsGroup($entity_type_id, $bundle_id, $expected_result) {
$groups = [
'test_entity' => [
'a',
'b',
],
];
$this
->expectGroupMapRetrieval($groups);
$manager = $this
->createGroupManager();
$this
->assertSame($expected_result, $manager
->isGroup($entity_type_id, $bundle_id));
}
public function providerTestIsGroup() {
return [
[
'test_entity',
'a',
TRUE,
],
[
'test_entity',
'b',
TRUE,
],
[
'test_entity',
'c',
FALSE,
],
[
'test_entity_non_existent',
'a',
FALSE,
],
[
'test_entity_non_existent',
'c',
FALSE,
],
];
}
public function testGetGroupBundleIdsByEntityType() {
$groups = [
'test_entity' => [
'a',
'b',
],
];
$this
->expectGroupMapRetrieval($groups);
$manager = $this
->createGroupManager();
$this
->assertSame($groups['test_entity'], $manager
->getGroupBundleIdsByEntityType('test_entity'));
$this
->assertSame([], $manager
->getGroupBundleIdsByEntityType('test_entity_non_existent'));
}
public function testAddGroupExisting() {
$groups_before = [
'test_entity' => [
'a',
'b',
],
];
$this
->expectGroupMapRetrieval($groups_before);
$groups_after = [
'test_entity' => [
'a',
'b',
'c',
],
];
$this->config
->get('groups')
->willReturn($groups_after)
->shouldBeCalled();
$manager = $this
->createGroupManager();
$this
->expectException(\InvalidArgumentException::class);
$manager
->addGroup('test_entity', 'c');
$this
->assertSame([
'a',
'b',
'c',
], $manager
->getGroupBundleIdsByEntityType('test_entity'));
$this
->assertTrue($manager
->isGroup('test_entity', 'c'));
}
public function testAddGroupNew() {
$this->configFactory
->getEditable('og.settings')
->willReturn($this->config
->reveal())
->shouldBeCalled();
$groups_before = [];
$this
->expectGroupMapRetrieval($groups_before);
$groups_after = [
'test_entity_new' => [
'a',
],
];
$config_prophecy = $this->config;
$this->config
->set('groups', $groups_after)
->will(function () use ($groups_after, $config_prophecy) {
$config_prophecy
->get('groups')
->willReturn($groups_after)
->shouldBeCalled();
})
->shouldBeCalled();
$this->config
->save()
->shouldBeCalled();
$manager = $this
->createGroupManager();
$this->ogRoleManager
->createPerBundleRoles('test_entity_new', 'a');
$this->eventDispatcher
->dispatch(GroupCreationEventInterface::EVENT_NAME, Argument::type(GroupCreationEvent::class))
->shouldBeCalled();
$manager
->addGroup('test_entity_new', 'a');
$this
->assertSame([
'a',
], $manager
->getGroupBundleIdsByEntityType('test_entity_new'));
$this
->assertTrue($manager
->isGroup('test_entity_new', 'a'));
}
public function testRemoveGroup() {
$this->configFactory
->getEditable('og.settings')
->willReturn($this->config
->reveal())
->shouldBeCalled();
$groups_before = [
'test_entity' => [
'a',
'b',
],
];
$this
->expectGroupMapRetrieval($groups_before);
$groups_after = [
'test_entity' => [
'a',
],
];
$this->config
->set('groups', $groups_after)
->shouldBeCalled();
$this->config
->save()
->shouldBeCalled();
$this->config
->get('groups')
->willReturn($groups_after)
->shouldBeCalled();
$manager = $this
->createGroupManager();
$manager
->removeGroup('test_entity', 'b');
$this
->assertSame([
'a',
], $manager
->getGroupBundleIdsByEntityType('test_entity'));
$this
->assertFalse($manager
->isGroup('test_entity', 'b'));
$this
->assertTrue($manager
->isGroup('test_entity', 'a'));
}
protected function createGroupManager() {
return new GroupTypeManager($this->configFactory
->reveal(), $this->entityTypeManager
->reveal(), $this->entityTypeBundleInfo
->reveal(), $this->eventDispatcher
->reveal(), $this->cache
->reveal(), $this->permissionManager
->reveal(), $this->ogRoleManager
->reveal(), $this->routeBuilder
->reveal(), $this->groupAudienceHelper
->reveal());
}
protected function expectGroupMapRetrieval(array $groups = []) {
$this->configFactory
->get('og.settings')
->willReturn($this->config
->reveal())
->shouldBeCalled();
$this->config
->get('groups')
->willReturn($groups)
->shouldBeCalled();
}
}