You are here

public function GroupSubscribeTest::testSubscribeAccess in Organic groups 8

Tests access to subscribe page.

File

tests/src/Functional/GroupSubscribeTest.php, line 208

Class

GroupSubscribeTest
Tests subscribe and un-subscribe to groups.

Namespace

Drupal\Tests\og\Functional

Code

public function testSubscribeAccess() {
  $this
    ->drupalLogin($this->normalUser);

  // We don't use a provider function, as it makes the test run much slower.
  $scenarios = [
    // Group with active membership.
    [
      'entity' => $this->group1,
      // Don't set the membership type. "Default" will be used.
      'membership_type' => '',
      'code' => 200,
      'skip_approval' => TRUE,
      'private' => FALSE,
    ],
    [
      'entity' => $this->group1,
      // Explicitly set the membership type.
      'membership_type' => OgMembershipInterface::TYPE_DEFAULT,
      'code' => 200,
      'skip_approval' => TRUE,
      'private' => FALSE,
    ],
    [
      'entity' => $this->group1,
      // Set invalid membership type.
      'membership_type' => mb_strtolower($this
        ->randomMachineName()),
      'code' => 404,
    ],
    // Group with pending membership.
    [
      'entity' => $this->group2,
      'code' => 200,
      'skip_approval' => FALSE,
      'private' => FALSE,
    ],
    // Entity is un-accessible to the user, but we still allow to subscribe to
    // it. Since it's "private" the default membership will be pending,
    // even though the permission is "subscribe without approval".
    [
      'entity' => $this->group3,
      'code' => 200,
      'skip_approval' => FALSE,
      'private' => TRUE,
    ],
    // A non-group entity.
    [
      'entity' => $this->group4,
      'code' => 403,
    ],
    // A group which doesn't allow new subscriptions.
    [
      'entity' => $this->group5,
      'code' => 403,
    ],
    // A non existing entity type.
    [
      'entity_type_id' => mb_strtolower($this
        ->randomMachineName()),
      'entity_id' => 1,
      // @todo This currently returns a 500 error due to a bug in core. Change
      //   this to a 403 or 404 when the bug is fixed.
      // @see https://www.drupal.org/node/2786897
      'code' => version_compare(\Drupal::VERSION, '9.1.4', '>=') ? 404 : 500,
    ],
    // A non existing entity ID.
    [
      'entity_type_id' => 'node',
      'entity_id' => rand(1000, 2000),
      'code' => 404,
    ],
  ];
  foreach ($scenarios as $scenario) {

    /** @var \Drupal\Core\Entity\EntityInterface $entity */
    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'])) {

      // Add the membership type.
      $path .= '/' . $scenario['membership_type'];
    }
    $this
      ->drupalGet($path);
    $this
      ->assertSession()
      ->statusCodeEquals($scenario['code']);
    if ($scenario['code'] != 200) {
      continue;
    }

    // Assert request membership field.
    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"]');
    }

    // Assert title appears only for accessible groups.
    if ($scenario['private']) {

      // Group's title shouldn't appear anywhere.
      $this
        ->assertSession()
        ->responseNotContains($entity
        ->label());
    }
    else {
      $this
        ->assertSession()
        ->pageTextContains($entity
        ->label());
    }
  }
}