/** * Tests that getFlaggingUsers method returns the expected result. */ public function testFlagServiceGetFlaggingUsers() { // The service methods don't check access, so our user can be anybody. $accounts = array($this->createUser(), $this->createUser()); // Create a flag. $flag = Flag::create([ 'id' => strtolower($this->randomMachineName()), 'label' => $this->randomString(), 'entity_type' => 'node', 'bundles' => ['article'], 'flag_type' => 'entity:node', 'link_type' => 'reload', 'flagTypeConfig' => [], 'linkTypeConfig' => [], ]); $flag->save(); // Flag the node. $flaggable_node = Node::create([ 'type' => 'article', 'title' => $this->randomMachineName(8), ]); $flaggable_node->save(); foreach ($accounts as $account) { $this->flagService->flag($flag, $flaggable_node, $account); } $flagging_users = $this->flagService->getFlaggingUsers($flaggable_node, $flag); $this->assertTrue(is_array($flagging_users), "The method getFlaggingUsers() returns an array."); foreach ($accounts as $account) { foreach ($flagging_users as $flagging_user) { if ($flagging_user->id() == $account->id()) { break; } } $this->assertTrue($flagging_user->id() == $account->id(), "The returned array has the flagged account included."); } }
/** * Tests flaggings and counts are deleted when its user is deleted. */ public function testUserDeletion() { $auth_user = $this->createUser(); // Create a flag. $user_flag = Flag::create([ 'id' => strtolower($this->randomMachineName()), 'label' => $this->randomString(), 'entity_type' => 'user', 'flag_type' => 'entity:user', 'link_type' => 'reload', 'flagTypeConfig' => [], 'linkTypeConfig' => [], ]); $user_flag->save(); $article = Node::create([ 'type' => 'article', 'title' => $this->randomMachineName(8), ]); $article->save(); $this->flagService->flag($user_flag, $auth_user, $this->adminUser); $this->flagService->flag($this->flag, $article, $auth_user); $user_before_count = $this->flagCountService->getEntityFlagCounts($auth_user); $this->assertEqual($user_before_count[$user_flag->id()], 1, 'The user has been flagged.'); $article_count_before = $this->flagCountService->getEntityFlagCounts($article); $this->assertEqual($article_count_before[$this->flag->id()], 1, 'The article has been flagged by the user.'); $auth_user->delete(); $flaggings_after = $this->flagService->getFlaggings($user_flag); $this->assertEmpty($flaggings_after, 'The user flaggings were removed when the user was deleted.'); $flaggings_after = $this->flagService->getFlaggings($this->flag); $this->assert(empty($flaggings_after), 'The node flaggings were removed when the user was deleted'); }
/** * Creates a flag from an array. * * Sensible key values pairs will be inserted into the input array if not * provided. * * @param array $edit * The edit array to pass to Flag::create(). * * @return \Drupal\flag\FlagInterface * A new flag entity with the given criteria. */ protected function createFlagFromArray(array $edit) { $default = [ 'id' => strtolower($this->randomMachineName()), 'label' => $this->randomString(), 'entity_type' => 'node', 'bundles' => array_keys(\Drupal::service('entity_type.bundle.info')->getBundleInfo('node')), 'flag_short' => $this->randomString(), 'flag_type' => $this->getFlagType('node'), 'link_type' => 'reload', 'flagTypeConfig' => [], 'linkTypeConfig' => [], 'global' => FALSE, ]; foreach ($default as $key => $value) { if (empty($edit[$key])) { $edit[$key] = $value; } } // Create the flag programmatically. $flag = Flag::create($edit); // Save the flag. $flag->save(); // Make sure that we actually did get a flag entity. $this->assertTrue($flag instanceof Flag); return $flag; }