View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\og\Kernel\Form;
use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\node\NodeInterface;
use Drupal\og\Entity\OgMembership;
use Drupal\og\Entity\OgRole;
use Drupal\og\Og;
use Drupal\og\OgRoleInterface;
use Drupal\user\Entity\Role;
use Drupal\user\Entity\User;
class GroupSubscribeFormTest extends KernelTestBase {
public static $modules = [
'system',
'user',
'field',
'node',
'og',
];
protected $user1;
protected $group1;
protected $group2;
protected $group3;
protected function setUp() : void {
parent::setUp();
$this
->installConfig([
'og',
]);
$this
->installEntitySchema('og_membership');
$this
->installEntitySchema('user');
$this
->installEntitySchema('node');
$this
->installSchema('system', 'sequences');
$bundle_names = [];
for ($i = 0; $i < 3; $i++) {
$bundle_name = mb_strtolower($this
->randomMachineName());
NodeType::create([
'type' => $bundle_name,
])
->save();
Og::groupTypeManager()
->addGroup('node', $bundle_name);
$bundle_names[] = $bundle_name;
}
$user = User::create([
'name' => $this
->randomString(),
]);
$user
->save();
$this->group1 = Node::create([
'type' => $bundle_names[0],
'title' => $this
->randomString(),
'uid' => $user
->id(),
]);
$this->group1
->save();
$this->group2 = Node::create([
'type' => $bundle_names[1],
'title' => $this
->randomString(),
'uid' => $user
->id(),
]);
$this->group2
->save();
$this->group3 = Node::create([
'type' => $bundle_names[2],
'title' => $this
->randomString(),
'uid' => $user
->id(),
'status' => NodeInterface::NOT_PUBLISHED,
]);
$this->group3
->save();
$role = OgRole::getRole('node', $bundle_names[0], OgRoleInterface::ANONYMOUS);
$role
->grantPermission('subscribe')
->save();
$role = OgRole::getRole('node', $bundle_names[1], OgRoleInterface::ANONYMOUS);
$role
->grantPermission('subscribe without approval')
->save();
$role = OgRole::getRole('node', $bundle_names[2], OgRoleInterface::ANONYMOUS);
$role
->grantPermission('subscribe without approval')
->save();
}
public function testIsStateActive() {
$user = $this
->createUser([
'access content',
]);
$form = \Drupal::entityTypeManager()
->getFormObject('og_membership', 'subscribe');
$membership_pending = OgMembership::create();
$membership_pending
->setGroup($this->group1)
->setOwner($user);
$form
->setEntity($membership_pending);
$this
->assertFalse($form
->isStateActive());
$membership_active = OgMembership::create();
$membership_active
->setGroup($this->group2)
->setOwner($user);
$form
->setEntity($membership_active);
$this
->assertTrue($form
->isStateActive());
$this
->assertTrue($this->group2
->access('view', $user));
$membership = OgMembership::create();
$membership
->setGroup($this->group3)
->setOwner($user);
$this
->assertFalse($this->group3
->access('view', $user));
$form
->setEntity($membership);
$this
->assertFalse($form
->isStateActive());
$this
->config('og.settings')
->set('deny_subscribe_without_approval', FALSE)
->save();
$this
->assertTrue($form
->isStateActive());
}
protected function createUser(array $permissions = []) {
$values = [];
if ($permissions) {
$role = Role::create([
'id' => strtolower($this
->randomMachineName(8)),
'label' => $this
->randomMachineName(8),
]);
$role
->save();
user_role_grant_permissions($role
->id(), $permissions);
$values['roles'][] = $role
->id();
}
$account = User::create($values + [
'name' => $this
->randomMachineName(),
'status' => 1,
]);
$account
->enforceIsNew();
$account
->save();
return $account;
}
}