/** * {@inheritdoc} */ public static function bundleFieldDefinitions(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) { /** @var Flag $flag */ if ($flag = Flag::load($bundle)) { $fields['flagged_entity'] = clone $base_field_definitions['flagged_entity']; $fields['flagged_entity']->setSetting('target_type', $flag->getFlaggableEntityTypeId()); return $fields; } return parent::bundleFieldDefinitions($entity_type, $bundle, $base_field_definitions); }
/** * 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 entity using the admin UI. * * If you do not provide any bundles in $edit, all bundles for $entity_type * are assumed. * * @param string|null $entity_type * (optional) A string containing the flaggable entity type, by default * 'node'. * @param array $edit * (optional) An array of form field names and values. If omitted, random * strings will be used for the flag ID, label, short and long text. * @param string|null $link_type * (optional) A string containing the link type ID. Is omitted, assumes * 'reload'. * * @return \Drupal\flag\FlagInterface|null * The created flag entity. */ protected function createFlagWithForm($entity_type = 'node', $edit = [], $link_type = 'reload') { // Submit the flag add page. $this->drupalPostForm('admin/structure/flags/add', [ 'flag_entity_type' => $this->getFlagType($entity_type), ], $this->t('Continue')); // Set the link type. $this->drupalPostAjaxForm(NULL, ['link_type' => $link_type], 'link_type'); // Create an array of defaults. $default_edit = [ 'id' => strtolower($this->randomMachineName()), 'label' => $this->randomString(), 'flag_short' => $this->randomString(16), 'flag_long' => $this->randomString(16), ]; // Merge the default values with the edit array. $final_edit = array_merge($default_edit, $edit); // Submit the flag details form. $this->drupalPostForm(NULL, $final_edit, $this->t('Create Flag')); // Load the new flag we created. $flag = Flag::load($final_edit['id']); // Make sure that we actually did get a flag entity. $this->assertTrue($flag instanceof Flag); // Return the flag. return $flag; }