Commit 38e27bc7 authored by Tess's avatar Tess
Browse files

Implemented global flag behavior.

parent 43319348
Loading
Loading
Loading
Loading
+14 −4
Original line number Diff line number Diff line
@@ -245,21 +245,31 @@ class Flag extends ConfigEntityBase implements FlagInterface {
   * {@inheritdoc}
   */
  public function isFlagged(EntityInterface $entity, AccountInterface $account = NULL) {
    // Get the current user if one wasn't passed to the method.
    if ($account == NULL) {
      $account = \Drupal::currentUser();
    }

    $result = \Drupal::entityQuery('flagging')
      ->condition('uid', $account->id())
    // Query the flagging entities for the given flag and flaggable.
    $query = \Drupal::entityQuery('flagging')
      ->condition('fid', $this->id())
      ->condition('entity_type', $entity->getEntityTypeId())
      ->condition('entity_id', $entity->id())
      ->execute();
      ->condition('entity_id', $entity->id());

    // Select by user if the flag is not global.
    if (!$this->isGlobal()) {
      $query = $query->condition('uid', $account->id());
    }

    // Execute the query.
    $result = $query->execute();

    // If we found a result, return TRUE.
    if (!empty($result)) {
      return TRUE;
    }

    // If there's no result, the flag hasn't been used.
    return FALSE;
  }

+1 −1
Original line number Diff line number Diff line
@@ -168,7 +168,7 @@ class FlagService {
  public function getFlaggings(EntityInterface $entity = NULL, FlagInterface $flag = NULL, AccountInterface $account = NULL) {
    $query = $this->entityQueryManager->get('flagging');

    if (!empty($account)) {
    if (!empty($account) && !$flag->isGlobal()) {
      $query = $query->condition('uid', $account->id());
    }

+60 −3
Original line number Diff line number Diff line
@@ -74,7 +74,9 @@ class FlagSimpleTest extends WebTestBase {
    $this->drupalLogin($this->adminUser);

    $this->doTestFlagAdd();
    $this->doGlobalFlag();
    $this->doTestHideFlagLinkFromTeaser();
    $this->doTestUserDeletion();
  }

  /**
@@ -151,6 +153,60 @@ class FlagSimpleTest extends WebTestBase {
    $this->assertNoLink('Flag this item');
  }

  /**
   * Test global flag.
   */
  public function doGlobalFlag() {
    $node = $this->drupalCreateNode(['type' => $this->nodeType]);
    $node_id = $node->id();

    // Grant the flag permissions to the authenticated role, so that both
    // users have the same roles and share the render cache.
    $role = Role::load(DRUPAL_AUTHENTICATED_RID);
    $role->grantPermission('flag ' . $this->id);
    $role->grantPermission('unflag ' . $this->id);
    $role->save();

    // Create and login a new user.
    $user_1 = $this->drupalCreateUser();
    $user_2 = $this->drupalCreateUser();
    $this->drupalLogin($user_1);

    // Flag the node with user 1.
    $this->drupalGet('node/' . $node_id);
    $this->clickLink('Flag this item');
    $this->assertResponse(200);
    $this->assertLink('Unflag this item');

    $this->drupalLogin($user_2);
    $this->drupalGet('node/' . $node_id);
    $this->assertLink('Flag this item');

    $this->drupalLogin($this->adminUser);
    $edit = [
      'is_global' => true,
    ];
    $this->drupalPostForm('admin/structure/flags/manage/' . $this->id, $edit, t('Save Flag'));
    $this->drupalGet('admin/structure/flags/manage/' . $this->id);
    $this->assertFieldChecked('edit-is-global');

    $this->drupalLogin($user_2);
    $this->drupalGet('node/' . $node_id);
    $this->assertLink('Unflag this item');

    $this->drupalLogin($this->adminUser);
    $edit = [
      'is_global' => false,
    ];
    $this->drupalPostForm('admin/structure/flags/manage/' . $this->id, $edit, t('Save Flag'));
    $this->drupalGet('admin/structure/flags/manage/' . $this->id);
    $this->assertNoFieldChecked('edit-is-global');

    $this->drupalLogin($user_2);
    $this->drupalGet('node/' . $node_id);
    $this->assertLink('Flag this item');
  }

  /**
   * Node creation and flag link.
   */
@@ -222,7 +278,7 @@ class FlagSimpleTest extends WebTestBase {
      ->count()
      ->execute();

    $this->assertTrue(0, $count_flags_after);
    $this->assertEqual(0, $count_flags_after);
  }

  /**
@@ -283,7 +339,7 @@ class FlagSimpleTest extends WebTestBase {
      ->condition('entity_id', $node_id)
      ->count()
      ->execute();
    $this->assertTrue(1, $count_flags_before);
    $this->assertEqual(1, $count_flags_before);

    // Delete  user 1.
    $user_1->delete();
@@ -296,6 +352,7 @@ class FlagSimpleTest extends WebTestBase {
      ->condition('entity_id', $node_id)
      ->count()
      ->execute();
    $this->assertTrue(0, $count_flags_before);

    $this->assertEqual(0, $count_flags_before);
  }
}