diff --git a/src/Tests/FlagTestBase.php b/src/Tests/FlagTestBase.php index 3c2144a..275272e 100644 --- a/src/Tests/FlagTestBase.php +++ b/src/Tests/FlagTestBase.php @@ -6,18 +6,20 @@ namespace Drupal\flag\Tests; +use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\flag\FlagInterface; use Drupal\simpletest\WebTestBase; use Drupal\flag\Entity\Flag; use Drupal\user\RoleInterface; use Drupal\user\Entity\Role; - /** * Provides common methods for Flag tests. */ abstract class FlagTestBase extends WebTestBase { + use StringTranslationTrait; + /** * Set to TRUE to strict check all configuration saved. * @@ -40,7 +42,7 @@ abstract class FlagTestBase extends WebTestBase { 'node', 'field_ui', 'text', - 'block' + 'block', ); /** @@ -50,16 +52,16 @@ abstract class FlagTestBase extends WebTestBase { * using the admin UI. The flag's ID, label, flag and unflag text will be * random strings. * - * @param null $entity_type - * (optional) The entity type of the flag to create. If NULL, assumes 'node'. + * @param string|null $entity_type + * (optional) The entity type of the flag to create. If NULL, assumes 'node'. * @param array $subtypes - * (optional) An array of entity subtypes to which the flag applies. If NULL, - * all subtypes are assumed. - * @param null $link_type - * (optional) The ID of the link type to use. If NULL, assumes 'reload'. + * (optional) An array of entity subtypes to which the flag applies. If NULL, + * all subtypes are assumed. + * @param string|null $link_type + * (optional) The ID of the link type to use. If NULL, assumes 'reload'. * * @return \Drupal\flag\FlagInterface - * A new flag entity with the given criteria. + * A new flag entity with the given criteria. */ protected function createFlag($entity_type = NULL, $subtypes = [], $link_type = NULL) { @@ -107,23 +109,23 @@ abstract class FlagTestBase extends WebTestBase { * If you do not provide any bundles in $edit, all bundles for $entity_type * is assumed. * - * @param string $entity_type - * (optional) A string containing the flaggable entity type. + * @param string|null $entity_type + * (optional) A string containing the flaggable entity type. * @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 $link_type - * (optional) A string containing the link type ID. Is omitted, assumes - * 'reload'. + * (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 null|\Drupal\flag\FlagInterface - * The created flag entity. + * The created flag entity. */ protected function createFlagForm($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), - ], t('Continue')); + ], $this->t('Continue')); // Set the link type. $this->drupalPostAjaxForm(NULL, ['link_type' => $link_type], 'link_type'); @@ -139,7 +141,7 @@ abstract class FlagTestBase extends WebTestBase { // Merge the default values with the edit array. $final_edit = array_merge($default_edit, $edit); - // Check if any of the bundles have been set + // Check if any of the bundles have been set. $bundles = $this->getBundleIds($entity_type); $has_specified_bundle = FALSE; foreach ($bundles as $bundle_id) { @@ -157,7 +159,7 @@ abstract class FlagTestBase extends WebTestBase { } // Submit the flag details form. - $this->drupalPostForm(NULL, $final_edit, t('Create Flag')); + $this->drupalPostForm(NULL, $final_edit, $this->t('Create Flag')); // Load the new flag we created. $flag = Flag::load($final_edit['id']); @@ -173,29 +175,29 @@ abstract class FlagTestBase extends WebTestBase { * Grants flag and unflag permission to the given flag. * * @param \Drupal\flag\FlagInterface $flag - * The flag on which to grant permissions. + * The flag on which to grant permissions. * @param array|string $role_id - * (optional) The ID of the role to grant permissions. If omitted, the - * authenticated role is assumed. - * @param bool $canFlag - * (optional) TRUE to grant the role flagging permission, FALSE to not grant - * flagging permission to the role. If omitted, TRUE is assumed. - * @param bool $canUnflag - * Optional TRUE to grant the role unflagging permission, FALSE to not grant - * unflagging permission to the role. If omitted, TRUE is assumed. + * (optional) The ID of the role to grant permissions. If omitted, the + * authenticated role is assumed. + * @param bool $can_flag + * (optional) TRUE to grant the role flagging permission, FALSE to not grant + * flagging permission to the role. If omitted, TRUE is assumed. + * @param bool $can_unflag + * Optional TRUE to grant the role unflagging permission, FALSE to not grant + * unflagging permission to the role. If omitted, TRUE is assumed. */ protected function grantFlagPermissions(FlagInterface $flag, $role_id = RoleInterface::AUTHENTICATED_ID, - $canFlag = TRUE, $canUnflag = TRUE) { + $can_flag = TRUE, $can_unflag = TRUE) { // Grant the flag permissions to the authenticated role, so that both // users have the same roles and share the render cache. $role = Role::load($role_id); - if ($canFlag) { + if ($can_flag) { $role->grantPermission('flag ' . $flag->id()); } - if ($canUnflag) { + if ($can_unflag) { $role->grantPermission('unflag ' . $flag->id()); } @@ -205,16 +207,17 @@ abstract class FlagTestBase extends WebTestBase { /** * Get a flag type plugin ID for the given entity. * - * @param $entity_type - * The entity type of the flag type plugin to get. + * @param string $entity_type + * The entity type of the flag type plugin to get. + * * @return string - * A string containing the flag type ID. + * A string containing the flag type ID. */ protected function getFlagType($entity_type) { $all_flag_types = \Drupal::service('plugin.manager.flag.flagtype')->getDefinitions(); // Search and return the flag type ID that matches our entity. - foreach ($all_flag_types as $plugin_id => $plugin_def) { + foreach ($all_flag_types as $plugin_id => $plugin_def) { if ($plugin_def['entity_type'] == $entity_type) { return $plugin_id; } @@ -227,10 +230,10 @@ abstract class FlagTestBase extends WebTestBase { * Get the bundle IDs for the given entity type. * * @param string $entity_type - * A string containing the entity type. + * A string containing the entity type. * * @return array - * An array of bundle IDs. + * An array of bundle IDs. */ protected function getBundleIds($entity_type) { $types = entity_get_bundles($entity_type);