View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\og\Functional;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\node\NodeInterface;
use Drupal\og\Entity\OgMembershipType;
use Drupal\og\Entity\OgRole;
use Drupal\og\Og;
use Drupal\og\OgMembershipInterface;
use Drupal\og\OgRoleInterface;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\og\Traits\OgMembershipCreationTrait;
class GroupSubscribeTest extends BrowserTestBase {
use OgMembershipCreationTrait;
public static $modules = [
'node',
'og',
];
protected $defaultTheme = 'stark';
protected $group1;
protected $group2;
protected $group3;
protected $group4;
protected $group5;
protected $groupBundle1;
protected $groupBundle2;
protected $groupBundle3;
protected $membershipTypeBundle;
protected $nonGroupBundle;
protected $normalUser;
protected function setUp() : void {
parent::setUp();
$this->groupBundle1 = mb_strtolower($this
->randomMachineName());
NodeType::create([
'type' => $this->groupBundle1,
])
->save();
$this->groupBundle2 = mb_strtolower($this
->randomMachineName());
NodeType::create([
'type' => $this->groupBundle2,
])
->save();
$this->groupBundle3 = mb_strtolower($this
->randomMachineName());
NodeType::create([
'type' => $this->groupBundle3,
])
->save();
$this->nonGroupBundle = mb_strtolower($this
->randomMachineName());
NodeType::create([
'type' => $this->nonGroupBundle,
])
->save();
$this->membershipTypeBundle = mb_strtolower($this
->randomMachineName());
NodeType::create([
'type' => $this->membershipTypeBundle,
])
->save();
Og::groupTypeManager()
->addGroup('node', $this->groupBundle1);
Og::groupTypeManager()
->addGroup('node', $this->groupBundle2);
Og::groupTypeManager()
->addGroup('node', $this->groupBundle3);
$user = $this
->createUser();
$this->group1 = Node::create([
'type' => $this->groupBundle1,
'title' => $this
->randomString(),
'uid' => $user
->id(),
]);
$this->group1
->save();
$this->group2 = Node::create([
'type' => $this->groupBundle2,
'title' => $this
->randomString(),
'uid' => $user
->id(),
]);
$this->group2
->save();
$this->group3 = Node::create([
'type' => $this->groupBundle1,
'title' => $this
->randomString(),
'uid' => $user
->id(),
'status' => NodeInterface::NOT_PUBLISHED,
]);
$this->group3
->save();
$this->group4 = Node::create([
'type' => $this->nonGroupBundle,
'title' => $this
->randomString(),
'uid' => $user
->id(),
]);
$this->group4
->save();
$this->group5 = Node::create([
'type' => $this->groupBundle3,
'title' => $this
->randomString(),
'uid' => $user
->id(),
]);
$this->group5
->save();
OgRole::getRole('node', $this->groupBundle1, OgRoleInterface::ANONYMOUS)
->grantPermission('subscribe without approval')
->save();
OgRole::getRole('node', $this->groupBundle3, OgRoleInterface::ANONYMOUS)
->revokePermission('subscribe')
->save();
$membership_type = OgMembershipType::create([
'type' => $this->membershipTypeBundle,
'name' => $this
->randomString(),
]);
$membership_type
->save();
$this->normalUser = $this
->drupalCreateUser();
}
public function testSubscribeAccess() {
$this
->drupalLogin($this->normalUser);
$scenarios = [
[
'entity' => $this->group1,
'membership_type' => '',
'code' => 200,
'skip_approval' => TRUE,
'private' => FALSE,
],
[
'entity' => $this->group1,
'membership_type' => OgMembershipInterface::TYPE_DEFAULT,
'code' => 200,
'skip_approval' => TRUE,
'private' => FALSE,
],
[
'entity' => $this->group1,
'membership_type' => mb_strtolower($this
->randomMachineName()),
'code' => 404,
],
[
'entity' => $this->group2,
'code' => 200,
'skip_approval' => FALSE,
'private' => FALSE,
],
[
'entity' => $this->group3,
'code' => 200,
'skip_approval' => FALSE,
'private' => TRUE,
],
[
'entity' => $this->group4,
'code' => 403,
],
[
'entity' => $this->group5,
'code' => 403,
],
[
'entity_type_id' => mb_strtolower($this
->randomMachineName()),
'entity_id' => 1,
'code' => version_compare(\Drupal::VERSION, '9.1.4', '>=') ? 404 : 500,
],
[
'entity_type_id' => 'node',
'entity_id' => rand(1000, 2000),
'code' => 404,
],
];
foreach ($scenarios as $scenario) {
if (!empty($scenario['entity'])) {
$entity = $scenario['entity'];
$entity_type_id = $entity
->getEntityTypeId();
$entity_id = $entity
->id();
}
else {
$entity_type_id = $scenario['entity_type_id'];
$entity_id = $scenario['entity_id'];
}
$path = "group/{$entity_type_id}/{$entity_id}/subscribe";
if (!empty($scenario['membership_type'])) {
$path .= '/' . $scenario['membership_type'];
}
$this
->drupalGet($path);
$this
->assertSession()
->statusCodeEquals($scenario['code']);
if ($scenario['code'] != 200) {
continue;
}
if ($scenario['skip_approval']) {
$this
->assertSession()
->elementNotExists('xpath', '//*[@id="edit-og-membership-request-0-value"]');
}
else {
$this
->assertSession()
->elementExists('xpath', '//*[@id="edit-og-membership-request-0-value"]');
}
if ($scenario['private']) {
$this
->assertSession()
->responseNotContains($entity
->label());
}
else {
$this
->assertSession()
->pageTextContains($entity
->label());
}
}
}
public function testUnSubscribeAccess() {
$this
->createOgMembership($this->group1, $this->normalUser);
$this
->drupalLogin($this->normalUser);
$scenarios = [
$this->group1
->id() => 200,
$this->group2
->id() => 403,
];
foreach ($scenarios as $entity_id => $code) {
$path = "group/node/{$entity_id}/unsubscribe";
$this
->drupalGet($path);
$this
->assertSession()
->statusCodeEquals($code);
}
}
}