You are here

protected function GroupSubscribeFormTest::setUp in Organic groups 8

Overrides KernelTestBase::setUp

File

tests/src/Kernel/Form/GroupSubscribeFormTest.php, line 68

Class

GroupSubscribeFormTest
Tests access to the create entity form through the user interface.

Namespace

Drupal\Tests\og\Kernel\Form

Code

protected function setUp() : void {
  parent::setUp();
  $this
    ->installConfig([
    'og',
  ]);
  $this
    ->installEntitySchema('og_membership');
  $this
    ->installEntitySchema('user');
  $this
    ->installEntitySchema('node');
  $this
    ->installSchema('system', 'sequences');

  // Create 3 test bundles and declare them as groups.
  $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;
  }

  // Create node author user.
  $user = User::create([
    'name' => $this
      ->randomString(),
  ]);
  $user
    ->save();

  // Create groups.
  $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();

  // Create an unpublished node, so users won't have access to it.
  $this->group3 = Node::create([
    'type' => $bundle_names[2],
    'title' => $this
      ->randomString(),
    'uid' => $user
      ->id(),
    'status' => NodeInterface::NOT_PUBLISHED,
  ]);
  $this->group3
    ->save();

  // Change the permissions of group to "subscribe".

  /** @var \Drupal\og\Entity\OgRole $role */
  $role = OgRole::getRole('node', $bundle_names[0], OgRoleInterface::ANONYMOUS);
  $role
    ->grantPermission('subscribe')
    ->save();

  // Change the permissions of group to allow "subscribe without approval".
  $role = OgRole::getRole('node', $bundle_names[1], OgRoleInterface::ANONYMOUS);
  $role
    ->grantPermission('subscribe without approval')
    ->save();

  // Change the permissions of group to allow "subscribe without approval" on
  // the unpublished node.
  $role = OgRole::getRole('node', $bundle_names[2], OgRoleInterface::ANONYMOUS);
  $role
    ->grantPermission('subscribe without approval')
    ->save();
}