You are here

public function SubscriptionsBlockTest::testBlockSubscriptions in Message Subscribe 8

Tests that a user can subscribe to all referenced entities using the block.

File

message_subscribe_ui/tests/src/Functional/SubscriptionsBlockTest.php, line 120

Class

SubscriptionsBlockTest
Tests for the advanced subscriptions block.

Namespace

Drupal\Tests\message_subscribe_ui\Functional

Code

public function testBlockSubscriptions() {
  $this
    ->drupalLogin($this->webUser);

  // Check that the block is empty when viewing another user.
  $this
    ->drupalGet($this->adminUser
    ->toUrl());
  $this
    ->assertSession()
    ->pageTextNotContains(t('Manage subscriptions'));
  $this
    ->assertSession()
    ->pageTextNotContains(t('Subscribe to @label', [
    '@label' => $this->adminUser
      ->getDisplayName(),
  ]));
  $this
    ->drupalGet($this->node
    ->toUrl());
  $this
    ->assertSession()
    ->pageTextContains(t('Manage subscriptions'));
  foreach ([
    1,
    3,
    7,
    9,
  ] as $i) {
    $this
      ->assertSession()
      ->pageTextContains(t('Subscribe to @title', [
      '@title' => $this->terms[$i]
        ->label(),
    ]));
  }

  // The subscription to the node itself should be available.
  $this
    ->assertSession()
    ->pageTextContains(t('Subscribe to @title', [
    '@title' => $this->node
      ->label(),
  ]));
  $this
    ->assertSession()
    ->pageTextNotContains(t('Subscribe to @label', [
    '@label' => $this->adminUser
      ->getDisplayName(),
  ]));

  // Subscribe to 1 and 7.
  $edit = [
    'subscriptions[taxonomy_term][' . $this->terms[1]
      ->id() . ']' => TRUE,
    'subscriptions[taxonomy_term][' . $this->terms[7]
      ->id() . ']' => TRUE,
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $flag = $this->flagService
    ->getFlagById('subscribe_term');
  foreach ([
    1,
    3,
    7,
    9,
  ] as $i) {
    $term = $this->terms[$i];
    if (in_array($i, [
      1,
      7,
    ])) {
      $this
        ->assertNotEmpty($this->flagService
        ->getEntityFlaggings($flag, $term, $this->webUser));

      // Subscriptions should be checked.
      $this
        ->assertSession()
        ->checkboxChecked(t('Subscribe to @label', [
        '@label' => $term
          ->label(),
      ]));
    }
    else {
      $this
        ->assertEmpty($this->flagService
        ->getEntityFlaggings($flag, $term, $this->webUser));

      // Subscriptions should not be unchecked.
      $this
        ->assertSession()
        ->checkboxNotChecked(t('Subscribe to @label', [
        '@label' => $term
          ->label(),
      ]));
    }
  }

  // Grant permission for user subscriptions.

  /** @var \Drupal\user\RoleInterface $role */
  $role = $this->container
    ->get('entity_type.manager')
    ->getStorage('user_role')
    ->load($this->webUser
    ->getRoles(TRUE)[0]);
  $role
    ->grantPermission('flag subscribe_user');
  $role
    ->grantPermission('unflag subscribe_user');
  $role
    ->save();
  $this
    ->drupalGet($this->node
    ->toUrl());
  $this
    ->assertSession()
    ->pageTextContains(t('Subscribe to @label', [
    '@label' => $this->adminUser
      ->getDisplayName(),
  ]));
  $this
    ->drupalGet($this->adminUser
    ->toUrl());
  $this
    ->assertSession()
    ->pageTextContains(t('Subscribe to @label', [
    '@label' => $this->adminUser
      ->getDisplayName(),
  ]));
  $edit = [
    'subscriptions[user][' . $this->adminUser
      ->id() . ']' => TRUE,
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $flag = $this->flagService
    ->getFlagById('subscribe_user');
  $this
    ->assertNotEmpty($this->flagService
    ->getEntityFlaggings($flag, $this->adminUser, $this->webUser));

  // Subscriptions should be checked.
  $this
    ->assertSession()
    ->checkboxChecked(t('Subscribe to @label', [
    '@label' => $this->adminUser
      ->getDisplayName(),
  ]));

  // Remove node and taxonomy flagging, and recheck the node page.
  $role
    ->revokePermission('flag subscribe_term');
  $role
    ->revokePermission('unflag subscribe_term');
  $role
    ->revokePermission('flag subscribe_node');
  $role
    ->revokePermission('unflag subscribe_node');
  $role
    ->save();
  $this
    ->drupalGet($this->node
    ->toUrl());
  foreach ([
    1,
    3,
    7,
    9,
  ] as $i) {
    $this
      ->assertSession()
      ->pageTextNotContains(t('Subscribe to @title', [
      '@title' => $this->terms[$i]
        ->label(),
    ]));
  }

  // The subscription to the node itself should not be available.
  $this
    ->assertSession()
    ->pageTextNotContains(t('Subscribe to @title', [
    '@title' => $this->node
      ->label(),
  ]));
  $this
    ->assertSession()
    ->pageTextContains(t('Subscribe to @label', [
    '@label' => $this->adminUser
      ->getDisplayName(),
  ]));
}