From e4a3d2a9709b7b212b6059fba8bbb2aced5b4d74 Mon Sep 17 00:00:00 2001 From: Damian Lee Date: Tue, 24 Nov 2015 16:29:08 +0000 Subject: [PATCH 01/53] Add group field type, add subscribe formatter --- og.module | 28 +++ og_ui/og_ui.module | 1 + .../GroupSubscribeFormatter.php | 185 ++++++++++++++++++ src/Plugin/Field/FieldType/OgGroupItem.php | 44 +++++ 4 files changed, 258 insertions(+) create mode 100644 og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php create mode 100644 src/Plugin/Field/FieldType/OgGroupItem.php diff --git a/og.module b/og.module index 8a8cfa72d..fbdeab22e 100755 --- a/og.module +++ b/og.module @@ -7,6 +7,8 @@ use Drupal\Core\Access\AccessResult; use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Entity\EntityTypeInterface; +use Drupal\Core\Field\BaseFieldDefinition; use Drupal\Core\Session\AccountInterface; use Drupal\og\Entity\OgRoleInterface; use Drupal\og\Og; @@ -181,6 +183,32 @@ function og_entity_create_access(AccountInterface $account, array $context, $bun return $required ? AccessResult::forbiddenIf($node_access_strict) : AccessResult::neutral(); } +/** + * + * Implements hook_entity_bundle_field_info(). + */ +function og_entity_bundle_field_info(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) { + // Add a read only property to group entities as a group flag. + if (Og::isGroup($entity_type->id(), $bundle)) { + $fields = []; + + $fields['og_group'] = BaseFieldDefinition::create('og_group') + ->setLabel(t('OG Group')) + ->setComputed(TRUE) + ->setTranslatable(FALSE) + ->setDefaultValue(TRUE) + ->setReadOnly(TRUE) + ->setDisplayOptions('view', [ + 'type' => 'og_ui_group_subscribe', + 'weight' => 0, + 'label' => 'above', + ]) + ->setDisplayConfigurable('view', TRUE); + + return $fields; + } +} + /** * Implements hook_help(). */ diff --git a/og_ui/og_ui.module b/og_ui/og_ui.module index 711f96520..d0a943491 100644 --- a/og_ui/og_ui.module +++ b/og_ui/og_ui.module @@ -1,4 +1,5 @@ getEntity(); + // @todo Inject this properly. + $account = \Drupal::currentUser()->getAccount(); + + if (!Og::isGroup($entity->getEntityTypeId(), $entity->bundle())) { + return []; + } + + if (($entity instanceof EntityOwnerInterface) && ($entity->getOwnerId() == $account->id())) { + // User is the group manager. + $elements[0] = [ + '#type' => 'html_tag', + '#tag' => 'span', + '#attributes' => ['title' => t('You are the group manager'), 'class' => 'group manager'], + '#value' => t('You are the group manager'), + ]; + + return $elements; + } + + if (og_is_member($entity_type, $id, 'user', $account, [OG_STATE_ACTIVE, OG_STATE_PENDING])) { + if (og_user_access($entity_type, $id, 'unsubscribe', $account)) { + $links['title'] = $this->t('Unsubscribe from group'); + $links['href'] = "group/$entity_type/$id/unsubscribe"; + $links['class'] = 'group unsubscribe'; + } + } + else { + if (og_is_member($entity_type, $id, 'user', $account, array(OG_STATE_BLOCKED))) { + // If user is blocked, they should not be able to apply for + // membership. + return []; + } + + // Check if user can subscribe to the field. + if (empty($settings['field_name']) && $audience_field_name = og_get_best_group_audience_field('user', $account, $entity_type, $bundle)) { + $settings['field_name'] = $audience_field_name; + } + if (!$settings['field_name']) { + return []; + } + + $field_info = field_info_field($settings['field_name']); + + // Check if entity is referencable. + if ($field_info['settings']['target_type'] != $entity_type) { + // Group type doesn't match. + return []; + } + if (!empty($field_info['settings']['handler_settings']['target_bundles']) && !in_array($bundle, $field_info['settings']['handler_settings']['target_bundles'])) { + // Bundles don't match. + return []; + } + + if (!og_check_field_cardinality('user', $account, $settings['field_name'])) { + $elements[0] = array( + '#type' => 'html_tag', + '#tag' => 'span', + '#attributes' => array('title' => format_plural($field_info['cardinality'], 'You are already registered to another group', 'You are already registered to @count groups'), 'class' => 'group other'), + '#value' => format_plural($field_info['cardinality'], 'You are already registered to another group', 'You are already registered to @count groups'), + ); + + return $elements; + } + + $url = "group/$entity_type/$id/subscribe"; + if ($settings['field_name']) { + $url .= '/' . $settings['field_name']; + } + + if (og_user_access($entity_type, $id, 'subscribe without approval', $account)) { + $links['title'] = t('Subscribe to group'); + $links['class'] = 'group subscribe'; + if ($account->uid) { + $links['href'] = $url; + } + else { + $links['href'] = 'user/login'; + $links['options'] = array('query' => array('destination' => $url)); + } + } + elseif (og_user_access($entity_type, $id, 'subscribe')) { + $links['title'] = t('Request group membership'); + $links['class'] = 'group subscribe request'; + if ($account->uid) { + $links['href'] = $url; + } + else { + $links['href'] = 'user/login'; + $links['options'] = array('query' => array('destination' => $url)); + } + } + else { + $elements[0] = array( + '#type' => 'html_tag', + '#tag' => 'span', + '#attributes' => array('title' => t('This is a closed group. Only a group administrator can add you.'), 'class' => 'group closed'), + '#value' => t('This is a closed group. Only a group administrator can add you.'), + ); + + return $elements; + } + } + + if (!empty($links['title'])) { + $links += array('options' => array('attributes' => array('title' => $links['title'], 'class' => array($links['class'])))); + $elements[0] = array( + '#type' => 'link', + '#title' => $links['title'], + '#href' => $links['href'], + '#options' => $links['options'], + ); + } + + return $elements; + } + + /** + * {@inheritdoc} + */ + public function settingsForm(array $form, FormStateInterface $form_state) { + $form = parent::settingsForm($form, $form_state); + + $form['field_name'] = [ + '#title' => $this->t('Field name'), + '#description' => $this->t('Select the field that should register the user subscription.'), + '#type' => 'select', + '#options' => [0 => $this->t('Automatic (best matching)')], //+ og_get_group_audience_fields('user', 'user'), + '#default_value' => $this->getSetting('field_name'), + ]; + + return $form; + } + + + /** + * {@inheritdoc} + */ + protected function checkAccess(EntityInterface $entity) { + // Always allow an entity author's username to be read, even if the current + // user does not have permission to view the entity author's profile. + return AccessResult::allowed(); + } + +} + diff --git a/src/Plugin/Field/FieldType/OgGroupItem.php b/src/Plugin/Field/FieldType/OgGroupItem.php new file mode 100644 index 000000000..66f9f4bb8 --- /dev/null +++ b/src/Plugin/Field/FieldType/OgGroupItem.php @@ -0,0 +1,44 @@ + Date: Tue, 24 Nov 2015 16:45:45 +0000 Subject: [PATCH 02/53] Add default display to og_ui entity_bundle_info_alter instead, add settingsSummary method --- og.module | 8 +------- og_ui/og_ui.module | 14 ++++++++++++++ .../FieldFormatter/GroupSubscribeFormatter.php | 16 ++++++++++++++++ src/Plugin/Field/FieldType/OgGroupItem.php | 6 ++++++ 4 files changed, 37 insertions(+), 7 deletions(-) diff --git a/og.module b/og.module index fbdeab22e..04034c907 100755 --- a/og.module +++ b/og.module @@ -197,13 +197,7 @@ function og_entity_bundle_field_info(EntityTypeInterface $entity_type, $bundle, ->setComputed(TRUE) ->setTranslatable(FALSE) ->setDefaultValue(TRUE) - ->setReadOnly(TRUE) - ->setDisplayOptions('view', [ - 'type' => 'og_ui_group_subscribe', - 'weight' => 0, - 'label' => 'above', - ]) - ->setDisplayConfigurable('view', TRUE); + ->setReadOnly(TRUE); return $fields; } diff --git a/og_ui/og_ui.module b/og_ui/og_ui.module index d0a943491..187d3e125 100644 --- a/og_ui/og_ui.module +++ b/og_ui/og_ui.module @@ -8,6 +8,7 @@ use Drupal\Core\Config\Entity\ConfigEntityBundleBase; use Drupal\Core\Entity\BundleEntityFormBase; use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\field\Entity\FieldConfig; use Drupal\og\Og; @@ -92,3 +93,16 @@ function og_ui_entity_type_save(EntityInterface $entity) { } } } + +/** + * Implements hook_entity_bundle_field_info_alter(). + */ +function og_ui_entity_bundle_field_info_alter(&$fields, EntityTypeInterface $entity_type, $bundle) { + if (isset($fields['og_group'])) { + $fields['og_group']->setDisplayOptions('view', [ + 'type' => 'og_ui_group_subscribe', + 'weight' => 0, + 'label' => 'above', + ])->setDisplayConfigurable('view', TRUE); + } +} diff --git a/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php b/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php index 9c4ac80a0..1e90a5e19 100644 --- a/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php +++ b/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php @@ -171,6 +171,22 @@ public function settingsForm(array $form, FormStateInterface $form_state) { return $form; } + /** + * {@inheritdoc} + */ + public function settingsSummary() { + $summary = parent::settingsSummary(); + + if ($field_name = $this->getSetting('field_name')) { + $fields = og_get_group_audience_fields(); + $summary[] = $this->t('Field %label', array('%label' => $fields[$field_name])); + } + else { + $summary[] = $this->t('No field selected (best matching)'); + } + + return $summary; + } /** * {@inheritdoc} diff --git a/src/Plugin/Field/FieldType/OgGroupItem.php b/src/Plugin/Field/FieldType/OgGroupItem.php index 66f9f4bb8..a21eb6f8f 100644 --- a/src/Plugin/Field/FieldType/OgGroupItem.php +++ b/src/Plugin/Field/FieldType/OgGroupItem.php @@ -24,10 +24,16 @@ */ class OgGroupItem extends FieldItemBase { + /** + * {@inheritdoc} + */ public static function schema(FieldStorageDefinitionInterface $field_definition) { return []; } + /** + * {@inheritdoc} + */ public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) { return []; } From 6dcdbea3a3bed4bcf3f2fae6bf4849292d11af0e Mon Sep 17 00:00:00 2001 From: Damian Lee Date: Tue, 24 Nov 2015 17:49:48 +0000 Subject: [PATCH 03/53] Convert _og_get_group_audience_fields to Og:: method --- .../Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php b/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php index 1e90a5e19..26b73750c 100644 --- a/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php +++ b/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php @@ -164,7 +164,7 @@ public function settingsForm(array $form, FormStateInterface $form_state) { '#title' => $this->t('Field name'), '#description' => $this->t('Select the field that should register the user subscription.'), '#type' => 'select', - '#options' => [0 => $this->t('Automatic (best matching)')], //+ og_get_group_audience_fields('user', 'user'), + '#options' => [0 => $this->t('Automatic (best matching)')] + Og::getAllGroupAudienceFields('user', 'user'), '#default_value' => $this->getSetting('field_name'), ]; @@ -178,7 +178,7 @@ public function settingsSummary() { $summary = parent::settingsSummary(); if ($field_name = $this->getSetting('field_name')) { - $fields = og_get_group_audience_fields(); + $fields = Og::getAllGroupAudienceFields('user', 'user'); $summary[] = $this->t('Field %label', array('%label' => $fields[$field_name])); } else { From 480a9b24e1ab97efb34a686598e512873ec3e2c7 Mon Sep 17 00:00:00 2001 From: Damian Lee Date: Mon, 30 Nov 2015 09:21:13 +0000 Subject: [PATCH 04/53] Add computed boolean value for group field value --- src/OgGroupBoolean.php | 31 ++++++++++++++++++++++ src/Plugin/Field/FieldType/OgGroupItem.php | 17 ++++++------ 2 files changed, 40 insertions(+), 8 deletions(-) create mode 100644 src/OgGroupBoolean.php diff --git a/src/OgGroupBoolean.php b/src/OgGroupBoolean.php new file mode 100644 index 000000000..3e5774f17 --- /dev/null +++ b/src/OgGroupBoolean.php @@ -0,0 +1,31 @@ +definition->getSetting('group_value'); + } + + /** + * {@inheritdoc} + */ + public function getCastedValue() { + return $this->getValue(); + } + +} diff --git a/src/Plugin/Field/FieldType/OgGroupItem.php b/src/Plugin/Field/FieldType/OgGroupItem.php index a21eb6f8f..d74b39ab5 100644 --- a/src/Plugin/Field/FieldType/OgGroupItem.php +++ b/src/Plugin/Field/FieldType/OgGroupItem.php @@ -9,6 +9,7 @@ use Drupal\Core\Field\FieldItemBase; use Drupal\Core\Field\FieldStorageDefinitionInterface; +use Drupal\Core\TypedData\DataDefinition; /** * OG Group Data class to return TRUE always. @@ -35,16 +36,16 @@ public static function schema(FieldStorageDefinitionInterface $field_definition) * {@inheritdoc} */ public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) { - return []; - } + $definitions = []; + $definitions['value'] = DataDefinition::create('boolean') + ->setLabel('Group item value') + ->setReadOnly(TRUE) + ->setComputed(TRUE) + ->setClass('\Drupal\og\OgGroupBoolean') + ->setSetting('group_value', TRUE); - /** - * {@inheritdoc} - */ - public function getValue() { - return TRUE; + return $definitions; } - } From 1e3f65b43bff8730da65fbd41913483400557cd4 Mon Sep 17 00:00:00 2001 From: Damian Lee Date: Mon, 30 Nov 2015 10:33:32 +0000 Subject: [PATCH 05/53] Add helper to get option labels from Og::getAllGroupAudienceFields --- .../GroupSubscribeFormatter.php | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php b/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php index 26b73750c..0dd498e6c 100644 --- a/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php +++ b/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php @@ -164,7 +164,7 @@ public function settingsForm(array $form, FormStateInterface $form_state) { '#title' => $this->t('Field name'), '#description' => $this->t('Select the field that should register the user subscription.'), '#type' => 'select', - '#options' => [0 => $this->t('Automatic (best matching)')] + Og::getAllGroupAudienceFields('user', 'user'), + '#options' => $this->getAudienceFieldOptions(), '#default_value' => $this->getSetting('field_name'), ]; @@ -178,8 +178,8 @@ public function settingsSummary() { $summary = parent::settingsSummary(); if ($field_name = $this->getSetting('field_name')) { - $fields = Og::getAllGroupAudienceFields('user', 'user'); - $summary[] = $this->t('Field %label', array('%label' => $fields[$field_name])); + $options = $this->getAudienceFieldOptions(); + $summary[] = $this->t('Field %label', array('%label' => $options[$field_name])); } else { $summary[] = $this->t('No field selected (best matching)'); @@ -188,6 +188,22 @@ public function settingsSummary() { return $summary; } + /** + * Returns audience field options. + * + * @return array + * An array of audience field options. + */ + protected function getAudienceFieldOptions() { + $options = [0 => $this->t('Automatic (best matching)')]; + + foreach (Og::getAllGroupAudienceFields('user', 'user') as $field_name => $field_definition) { + $options[$field_name] = $field_definition->getLabel(); + } + + return $options; + } + /** * {@inheritdoc} */ From ddd6cc4cbd63f9d6ba2e2539e4bad37918c194b6 Mon Sep 17 00:00:00 2001 From: Damian Lee Date: Mon, 30 Nov 2015 10:39:48 +0000 Subject: [PATCH 06/53] Add default setting value for field_name, always use audience field options for label --- .../GroupSubscribeFormatter.php | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php b/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php index 0dd498e6c..57556f906 100644 --- a/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php +++ b/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php @@ -154,6 +154,17 @@ public function viewElements(FieldItemListInterface $items, $langcode) { return $elements; } + /** + * {@inheritdoc} + */ + public static function defaultSettings() { + $options = parent::defaultSettings(); + + $options['field_name'] = 0; + + return $options; + } + /** * {@inheritdoc} */ @@ -177,14 +188,17 @@ public function settingsForm(array $form, FormStateInterface $form_state) { public function settingsSummary() { $summary = parent::settingsSummary(); + // 0 will be the 'Automatic' option. + $selection = 0; + + // If a field name is configured, use that. if ($field_name = $this->getSetting('field_name')) { - $options = $this->getAudienceFieldOptions(); - $summary[] = $this->t('Field %label', array('%label' => $options[$field_name])); - } - else { - $summary[] = $this->t('No field selected (best matching)'); + $selection = $field_name; } + $options = $this->getAudienceFieldOptions(); + $summary[] = $this->t('Field: %label', array('%label' => $options[$selection])); + return $summary; } From 0d680c31d6d2b8f5b345ac1be14858b7001f9c7a Mon Sep 17 00:00:00 2001 From: Damian Lee Date: Mon, 30 Nov 2015 10:58:24 +0000 Subject: [PATCH 07/53] Feedback form Amitai and code tidy of GroupSubscriberFormatter::viewElements method --- og.module | 25 +++--- og_ui/og_ui.module | 4 +- .../GroupSubscribeFormatter.php | 83 ++++++++++++------- src/Plugin/Field/FieldType/OgGroupItem.php | 4 + 4 files changed, 74 insertions(+), 42 deletions(-) diff --git a/og.module b/og.module index 04034c907..d1923d341 100755 --- a/og.module +++ b/og.module @@ -186,21 +186,24 @@ function og_entity_create_access(AccountInterface $account, array $context, $bun /** * * Implements hook_entity_bundle_field_info(). + * + * Add a read only property to group entities as a group flag. */ function og_entity_bundle_field_info(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) { - // Add a read only property to group entities as a group flag. - if (Og::isGroup($entity_type->id(), $bundle)) { - $fields = []; + if (!Og::isGroup($entity_type->id(), $bundle)) { + return NULL; + } - $fields['og_group'] = BaseFieldDefinition::create('og_group') - ->setLabel(t('OG Group')) - ->setComputed(TRUE) - ->setTranslatable(FALSE) - ->setDefaultValue(TRUE) - ->setReadOnly(TRUE); + $fields = []; - return $fields; - } + $fields['og_group'] = BaseFieldDefinition::create('og_group') + ->setLabel(t('OG Group')) + ->setComputed(TRUE) + ->setTranslatable(FALSE) + ->setDefaultValue(TRUE) + ->setReadOnly(TRUE); + + return $fields; } /** diff --git a/og_ui/og_ui.module b/og_ui/og_ui.module index 187d3e125..7cac68bd2 100644 --- a/og_ui/og_ui.module +++ b/og_ui/og_ui.module @@ -96,13 +96,13 @@ function og_ui_entity_type_save(EntityInterface $entity) { /** * Implements hook_entity_bundle_field_info_alter(). + * + * Set the default field formatter of fields of type OG group. */ function og_ui_entity_bundle_field_info_alter(&$fields, EntityTypeInterface $entity_type, $bundle) { if (isset($fields['og_group'])) { $fields['og_group']->setDisplayOptions('view', [ 'type' => 'og_ui_group_subscribe', - 'weight' => 0, - 'label' => 'above', ])->setDisplayConfigurable('view', TRUE); } } diff --git a/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php b/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php index 57556f906..7a7d79d37 100644 --- a/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php +++ b/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php @@ -38,9 +38,9 @@ public function viewElements(FieldItemListInterface $items, $langcode) { $elements = []; $entity = $items->getEntity(); - // @todo Inject this properly. $account = \Drupal::currentUser()->getAccount(); + // Entity is not a group. if (!Og::isGroup($entity->getEntityTypeId(), $entity->bundle())) { return []; } @@ -50,8 +50,11 @@ public function viewElements(FieldItemListInterface $items, $langcode) { $elements[0] = [ '#type' => 'html_tag', '#tag' => 'span', - '#attributes' => ['title' => t('You are the group manager'), 'class' => 'group manager'], - '#value' => t('You are the group manager'), + '#attributes' => [ + 'title' => $this->t('You are the group manager'), + 'class' => ['group', 'manager'], + ], + '#value' => $this->t('You are the group manager'), ]; return $elements; @@ -61,7 +64,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) { if (og_user_access($entity_type, $id, 'unsubscribe', $account)) { $links['title'] = $this->t('Unsubscribe from group'); $links['href'] = "group/$entity_type/$id/unsubscribe"; - $links['class'] = 'group unsubscribe'; + $links['class'] = ['group', 'unsubscribe']; } } else { @@ -79,25 +82,32 @@ public function viewElements(FieldItemListInterface $items, $langcode) { return []; } - $field_info = field_info_field($settings['field_name']); - // Check if entity is referencable. - if ($field_info['settings']['target_type'] != $entity_type) { + if ($this->getSetting('target_type') != $entity_type) { // Group type doesn't match. return []; } - if (!empty($field_info['settings']['handler_settings']['target_bundles']) && !in_array($bundle, $field_info['settings']['handler_settings']['target_bundles'])) { + + // Check handler bundles, if any. + $handler_settings = $this->getSetting('handler_settings'); + + if (!empty($handler_settings['target_bundles']) && !in_array($bundle, $handler_settings['target_bundles'])) { // Bundles don't match. return []; } if (!og_check_field_cardinality('user', $account, $settings['field_name'])) { - $elements[0] = array( + $cardinality = $this->fieldDefinition->getFieldStorageDefinition()->getCardinality(); + + $elements[0] = [ '#type' => 'html_tag', '#tag' => 'span', - '#attributes' => array('title' => format_plural($field_info['cardinality'], 'You are already registered to another group', 'You are already registered to @count groups'), 'class' => 'group other'), - '#value' => format_plural($field_info['cardinality'], 'You are already registered to another group', 'You are already registered to @count groups'), - ); + '#attributes' => [ + 'title' => $this->formatPlural($cardinality, 'You are already registered to another group', 'You are already registered to @count groups'), + 'class' => ['group', 'other'], + ], + '#value' => $this->formatPlural($cardinality, 'You are already registered to another group', 'You are already registered to @count groups'), + ]; return $elements; } @@ -108,47 +118,64 @@ public function viewElements(FieldItemListInterface $items, $langcode) { } if (og_user_access($entity_type, $id, 'subscribe without approval', $account)) { - $links['title'] = t('Subscribe to group'); - $links['class'] = 'group subscribe'; - if ($account->uid) { + $links['title'] = $this->t('Subscribe to group'); + $links['class'] = ['group', 'subscribe']; + if ($account->isAuthenticated()) { $links['href'] = $url; } else { $links['href'] = 'user/login'; - $links['options'] = array('query' => array('destination' => $url)); + $links['options'] = [ + 'query' => [ + 'destination' => $url], + ]; } } elseif (og_user_access($entity_type, $id, 'subscribe')) { - $links['title'] = t('Request group membership'); - $links['class'] = 'group subscribe request'; - if ($account->uid) { + $links['title'] = $this->t('Request group membership'); + $links['class'] = ['group', 'subscribe', 'request']; + if ($account->isAuthenticated()) { $links['href'] = $url; } else { $links['href'] = 'user/login'; - $links['options'] = array('query' => array('destination' => $url)); + $links['options'] = [ + 'query' => [ + 'destination' => $url], + ]; } } else { - $elements[0] = array( + $elements[0] = [ '#type' => 'html_tag', '#tag' => 'span', - '#attributes' => array('title' => t('This is a closed group. Only a group administrator can add you.'), 'class' => 'group closed'), - '#value' => t('This is a closed group. Only a group administrator can add you.'), - ); + '#attributes' => [ + 'title' => $this->t('This is a closed group. Only a group administrator can add you.'), + 'class' => ['group', 'closed'], + ], + '#value' => $this->t('This is a closed group. Only a group administrator can add you.'), + ]; return $elements; } } if (!empty($links['title'])) { - $links += array('options' => array('attributes' => array('title' => $links['title'], 'class' => array($links['class'])))); - $elements[0] = array( + $links += [ + 'options' => [ + 'attributes' => [ + 'title' => $links['title'], + 'class' => [$links['class']], + ], + ], + ]; + + $elements[0] = [ '#type' => 'link', '#title' => $links['title'], '#href' => $links['href'], '#options' => $links['options'], - ); + ]; } return $elements; @@ -222,8 +249,6 @@ protected function getAudienceFieldOptions() { * {@inheritdoc} */ protected function checkAccess(EntityInterface $entity) { - // Always allow an entity author's username to be read, even if the current - // user does not have permission to view the entity author's profile. return AccessResult::allowed(); } diff --git a/src/Plugin/Field/FieldType/OgGroupItem.php b/src/Plugin/Field/FieldType/OgGroupItem.php index d74b39ab5..b92965c8f 100644 --- a/src/Plugin/Field/FieldType/OgGroupItem.php +++ b/src/Plugin/Field/FieldType/OgGroupItem.php @@ -14,6 +14,10 @@ /** * OG Group Data class to return TRUE always. * + * This field type is used simply as a placeholder for a field formatter that + * would allow non-groups members, members, and group managers to join, request + * or leave a group. + * * @FieldType( * id = "og_group", * label = @Translation("OG Group"), From c880869d5846ec3b3244cfcceb5da914289b08a2 Mon Sep 17 00:00:00 2001 From: Damian Lee Date: Mon, 30 Nov 2015 11:03:59 +0000 Subject: [PATCH 08/53] Simplify settingsSummary method --- .../Field/FieldFormatter/GroupSubscribeFormatter.php | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php b/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php index 7a7d79d37..ada40935c 100644 --- a/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php +++ b/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php @@ -214,17 +214,11 @@ public function settingsForm(array $form, FormStateInterface $form_state) { */ public function settingsSummary() { $summary = parent::settingsSummary(); + $options = $this->getAudienceFieldOptions(); - // 0 will be the 'Automatic' option. - $selection = 0; - - // If a field name is configured, use that. - if ($field_name = $this->getSetting('field_name')) { - $selection = $field_name; - } + $field_name = $this->getSetting('field_name'); - $options = $this->getAudienceFieldOptions(); - $summary[] = $this->t('Field: %label', array('%label' => $options[$selection])); + $summary[] = $this->t('Field: %label', array('%label' => $options[$field_name])); return $summary; } From 2c03acde3c00fd9c1f239e7da7869f903039356e Mon Sep 17 00:00:00 2001 From: Damian Lee Date: Mon, 30 Nov 2015 11:56:09 +0000 Subject: [PATCH 09/53] Remove usage of group_value setting --- src/OgGroupBoolean.php | 2 +- src/Plugin/Field/FieldType/OgGroupItem.php | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/OgGroupBoolean.php b/src/OgGroupBoolean.php index 3e5774f17..c232d0536 100644 --- a/src/OgGroupBoolean.php +++ b/src/OgGroupBoolean.php @@ -18,7 +18,7 @@ class OgGroupBoolean extends BooleanData { * {@inheritdoc} */ public function getValue() { - return $this->definition->getSetting('group_value'); + return TRUE; } /** diff --git a/src/Plugin/Field/FieldType/OgGroupItem.php b/src/Plugin/Field/FieldType/OgGroupItem.php index b92965c8f..b3ebcf6fc 100644 --- a/src/Plugin/Field/FieldType/OgGroupItem.php +++ b/src/Plugin/Field/FieldType/OgGroupItem.php @@ -46,8 +46,7 @@ public static function propertyDefinitions(FieldStorageDefinitionInterface $fiel ->setLabel('Group item value') ->setReadOnly(TRUE) ->setComputed(TRUE) - ->setClass('\Drupal\og\OgGroupBoolean') - ->setSetting('group_value', TRUE); + ->setClass('\Drupal\og\OgGroupBoolean'); return $definitions; } From 9caeb7e39197f95dc0ead66cc9e5a6362e6ab873 Mon Sep 17 00:00:00 2001 From: Damian Lee Date: Wed, 2 Dec 2015 09:09:37 +0000 Subject: [PATCH 10/53] Use Og::isMember --- .../Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php b/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php index ada40935c..c5ee3cee1 100644 --- a/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php +++ b/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php @@ -60,7 +60,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) { return $elements; } - if (og_is_member($entity_type, $id, 'user', $account, [OG_STATE_ACTIVE, OG_STATE_PENDING])) { + if (Og::isMember($entity, $account, [OG_STATE_ACTIVE, OG_STATE_PENDING])) { if (og_user_access($entity_type, $id, 'unsubscribe', $account)) { $links['title'] = $this->t('Unsubscribe from group'); $links['href'] = "group/$entity_type/$id/unsubscribe"; @@ -68,7 +68,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) { } } else { - if (og_is_member($entity_type, $id, 'user', $account, array(OG_STATE_BLOCKED))) { + if (Og::isMember($entity, $account, [OG_STATE_BLOCKED])) { // If user is blocked, they should not be able to apply for // membership. return []; From 3a0d662e5c7930589d3c8e766e5301d06b8d331c Mon Sep 17 00:00:00 2001 From: Damian Lee Date: Wed, 2 Dec 2015 14:23:59 +0000 Subject: [PATCH 11/53] Add usage of OgGroupAudienceHelper::checkFieldCardinality --- .../GroupSubscribeFormatter.php | 30 ++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php b/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php index c5ee3cee1..0976f5c44 100644 --- a/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php +++ b/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php @@ -15,6 +15,7 @@ use Drupal\Core\Field\FormatterBase; use Drupal\Core\Form\FormStateInterface; use Drupal\og\Og; +use Drupal\og\OgGroupAudienceHelper; use Drupal\user\EntityOwnerInterface; /** @@ -39,6 +40,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) { $entity = $items->getEntity(); $account = \Drupal::currentUser()->getAccount(); + $field_name = $this->fieldDefinition->getName(); // Entity is not a group. if (!Og::isGroup($entity->getEntityTypeId(), $entity->bundle())) { @@ -74,19 +76,19 @@ public function viewElements(FieldItemListInterface $items, $langcode) { return []; } - // Check if user can subscribe to the field. - if (empty($settings['field_name']) && $audience_field_name = og_get_best_group_audience_field('user', $account, $entity_type, $bundle)) { - $settings['field_name'] = $audience_field_name; - } - if (!$settings['field_name']) { - return []; - } - - // Check if entity is referencable. - if ($this->getSetting('target_type') != $entity_type) { - // Group type doesn't match. - return []; - } +// // Check if user can subscribe to the field. +// if (empty($settings['field_name']) && $audience_field_name = og_get_best_group_audience_field('user', $account, $entity_type, $bundle)) { +// $settings['field_name'] = $audience_field_name; +// } +// if (!$settings['field_name']) { +// return []; +// } +// +// // Check if entity is referencable. +// if ($this->getSetting('target_type') != $entity_type) { +// // Group type doesn't match. +// return []; +// } // Check handler bundles, if any. $handler_settings = $this->getSetting('handler_settings'); @@ -96,7 +98,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) { return []; } - if (!og_check_field_cardinality('user', $account, $settings['field_name'])) { + if (!OgGroupAudienceHelper::checkFieldCardinality($account, $field_name)) { $cardinality = $this->fieldDefinition->getFieldStorageDefinition()->getCardinality(); $elements[0] = [ From 1ad173e13b003004aa1f0d457da31485e30f7b50 Mon Sep 17 00:00:00 2001 From: Damian Lee Date: Wed, 2 Dec 2015 14:27:51 +0000 Subject: [PATCH 12/53] Use field_name setting --- .../GroupSubscribeFormatter.php | 34 ++++++++++--------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php b/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php index 0976f5c44..d62032f53 100644 --- a/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php +++ b/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php @@ -40,7 +40,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) { $entity = $items->getEntity(); $account = \Drupal::currentUser()->getAccount(); - $field_name = $this->fieldDefinition->getName(); + $field_name = $this->getSetting('field_name'); // Entity is not a group. if (!Og::isGroup($entity->getEntityTypeId(), $entity->bundle())) { @@ -76,19 +76,21 @@ public function viewElements(FieldItemListInterface $items, $langcode) { return []; } -// // Check if user can subscribe to the field. -// if (empty($settings['field_name']) && $audience_field_name = og_get_best_group_audience_field('user', $account, $entity_type, $bundle)) { -// $settings['field_name'] = $audience_field_name; -// } -// if (!$settings['field_name']) { -// return []; -// } -// -// // Check if entity is referencable. -// if ($this->getSetting('target_type') != $entity_type) { -// // Group type doesn't match. -// return []; -// } + // Check if user can subscribe to the field. + if (empty($field_name) && $audience_field_name = og_get_best_group_audience_field('user', $account, $entity_type, $bundle)) { + $settings['field_name'] = $audience_field_name; + } + if (!$field_name) { + return []; + } + + // @todo Not sure this is applicable now we have our own dedicated field + // type? + // Check if entity is referencable. + if ($this->getSetting('target_type') !== $entity->getEntityTypeId()) { + // Group type doesn't match. + return []; + } // Check handler bundles, if any. $handler_settings = $this->getSetting('handler_settings'); @@ -115,8 +117,8 @@ public function viewElements(FieldItemListInterface $items, $langcode) { } $url = "group/$entity_type/$id/subscribe"; - if ($settings['field_name']) { - $url .= '/' . $settings['field_name']; + if (!empty($field_name)) { + $url .= '/' . $field_name; } if (og_user_access($entity_type, $id, 'subscribe without approval', $account)) { From 13f5efc31571a7fa676fa84d14900765237df334 Mon Sep 17 00:00:00 2001 From: Damian Lee Date: Wed, 2 Dec 2015 17:29:54 +0000 Subject: [PATCH 13/53] Convert links to use url parameters --- .../GroupSubscribeFormatter.php | 75 +++++++++---------- 1 file changed, 37 insertions(+), 38 deletions(-) diff --git a/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php b/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php index d62032f53..6f034d516 100644 --- a/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php +++ b/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php @@ -14,6 +14,8 @@ use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\FormatterBase; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Routing\RedirectDestinationTrait; +use Drupal\Core\Url; use Drupal\og\Og; use Drupal\og\OgGroupAudienceHelper; use Drupal\user\EntityOwnerInterface; @@ -32,6 +34,8 @@ */ class GroupSubscribeFormatter extends FormatterBase { + use RedirectDestinationTrait; + /** * {@inheritdoc} */ @@ -64,9 +68,10 @@ public function viewElements(FieldItemListInterface $items, $langcode) { if (Og::isMember($entity, $account, [OG_STATE_ACTIVE, OG_STATE_PENDING])) { if (og_user_access($entity_type, $id, 'unsubscribe', $account)) { - $links['title'] = $this->t('Unsubscribe from group'); - $links['href'] = "group/$entity_type/$id/unsubscribe"; - $links['class'] = ['group', 'unsubscribe']; + $link['title'] = $this->t('Unsubscribe from group'); + $link['href'] = "group/$entity_type/$id/unsubscribe"; + $link['url'] = Url::fromRoute('og_ui.entity.unsubscribe', ['entity_type_id' => $entity->getEntityTypeId(), 'entity_id' => $entity->id()]); + $link['class'] = ['unsubscribe']; } } else { @@ -116,38 +121,34 @@ public function viewElements(FieldItemListInterface $items, $langcode) { return $elements; } - $url = "group/$entity_type/$id/subscribe"; - if (!empty($field_name)) { - $url .= '/' . $field_name; + // If hte user is authenticated, set up the subscribe link. + if ($account->isAuthenticated()) { + $parameters = [ + 'entity_type_id' => $entity->getEntityTypeId(), + 'entity_id' => $entity->id(), + ]; + + // Add the field name as an additional query parameter. + if (!empty($field_name)) { + $parameters['field_name'] = $field_name; + } + + $url = Url::fromRoute('og_ui.entity.subscribe', $parameters); + } + // Otherwise, link to user login and redirect back to here. + else { + $url = Url::fromRoute('user.login', [], ['query' => $this->getDestinationArray()]); } if (og_user_access($entity_type, $id, 'subscribe without approval', $account)) { - $links['title'] = $this->t('Subscribe to group'); - $links['class'] = ['group', 'subscribe']; - if ($account->isAuthenticated()) { - $links['href'] = $url; - } - else { - $links['href'] = 'user/login'; - $links['options'] = [ - 'query' => [ - 'destination' => $url], - ]; - } + $link['title'] = $this->t('Subscribe to group'); + $link['class'] = ['subscribe']; + $link['url'] = $url; } elseif (og_user_access($entity_type, $id, 'subscribe')) { - $links['title'] = $this->t('Request group membership'); - $links['class'] = ['group', 'subscribe', 'request']; - if ($account->isAuthenticated()) { - $links['href'] = $url; - } - else { - $links['href'] = 'user/login'; - $links['options'] = [ - 'query' => [ - 'destination' => $url], - ]; - } + $link['title'] = $this->t('Request group membership'); + $link['class'] = ['subscribe', 'request']; + $link['url'] = $url; } else { $elements[0] = [ @@ -164,21 +165,20 @@ public function viewElements(FieldItemListInterface $items, $langcode) { } } - if (!empty($links['title'])) { - $links += [ + if (!empty($link['title'])) { + $link += [ 'options' => [ 'attributes' => [ - 'title' => $links['title'], - 'class' => [$links['class']], + 'title' => $link['title'], + 'class' => ['group'] + $link['class'], ], ], ]; $elements[0] = [ '#type' => 'link', - '#title' => $links['title'], - '#href' => $links['href'], - '#options' => $links['options'], + '#title' => $link['title'], + '#url' => $link['url'], ]; } @@ -251,4 +251,3 @@ protected function checkAccess(EntityInterface $entity) { } } - From b0489c760d27fc2b51f2d9c76258d01c0ad95c58 Mon Sep 17 00:00:00 2001 From: Damian Lee Date: Mon, 7 Dec 2015 10:11:30 +0000 Subject: [PATCH 14/53] Use OgAccess::userAccess in group formatter --- .../Field/FieldFormatter/GroupSubscribeFormatter.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php b/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php index 6f034d516..310616ae2 100644 --- a/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php +++ b/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php @@ -10,14 +10,15 @@ use Drupal\Core\Access\AccessResult; use Drupal\Core\Entity\EntityInterface; -use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\FormatterBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Routing\RedirectDestinationTrait; use Drupal\Core\Url; use Drupal\og\Og; +use Drupal\og\OgAccess; use Drupal\og\OgGroupAudienceHelper; +use Drupal\user\Entity\User; use Drupal\user\EntityOwnerInterface; /** @@ -43,7 +44,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) { $elements = []; $entity = $items->getEntity(); - $account = \Drupal::currentUser()->getAccount(); + $account = User::load(\Drupal::currentUser()->id()); $field_name = $this->getSetting('field_name'); // Entity is not a group. @@ -67,9 +68,8 @@ public function viewElements(FieldItemListInterface $items, $langcode) { } if (Og::isMember($entity, $account, [OG_STATE_ACTIVE, OG_STATE_PENDING])) { - if (og_user_access($entity_type, $id, 'unsubscribe', $account)) { + if (OgAccess::userAccess($entity, 'unsubscribe', $account)) { $link['title'] = $this->t('Unsubscribe from group'); - $link['href'] = "group/$entity_type/$id/unsubscribe"; $link['url'] = Url::fromRoute('og_ui.entity.unsubscribe', ['entity_type_id' => $entity->getEntityTypeId(), 'entity_id' => $entity->id()]); $link['class'] = ['unsubscribe']; } @@ -140,12 +140,12 @@ public function viewElements(FieldItemListInterface $items, $langcode) { $url = Url::fromRoute('user.login', [], ['query' => $this->getDestinationArray()]); } - if (og_user_access($entity_type, $id, 'subscribe without approval', $account)) { + if (OgAccess::userAccess($entity, 'subscribe without approval', $account)) { $link['title'] = $this->t('Subscribe to group'); $link['class'] = ['subscribe']; $link['url'] = $url; } - elseif (og_user_access($entity_type, $id, 'subscribe')) { + elseif (OgAccess::userAccess($entity, 'subscribe')) { $link['title'] = $this->t('Request group membership'); $link['class'] = ['subscribe', 'request']; $link['url'] = $url; From 69a330f11c77e895cefe06e56307c43af149ed20 Mon Sep 17 00:00:00 2001 From: Damian Lee Date: Mon, 7 Dec 2015 10:57:29 +0000 Subject: [PATCH 15/53] Rename subscribe/unsubscribe routes --- .../Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php b/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php index 310616ae2..8bfd3724a 100644 --- a/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php +++ b/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php @@ -70,7 +70,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) { if (Og::isMember($entity, $account, [OG_STATE_ACTIVE, OG_STATE_PENDING])) { if (OgAccess::userAccess($entity, 'unsubscribe', $account)) { $link['title'] = $this->t('Unsubscribe from group'); - $link['url'] = Url::fromRoute('og_ui.entity.unsubscribe', ['entity_type_id' => $entity->getEntityTypeId(), 'entity_id' => $entity->id()]); + $link['url'] = Url::fromRoute('og_ui.unsubscribe', ['entity_type_id' => $entity->getEntityTypeId(), 'entity_id' => $entity->id()]); $link['class'] = ['unsubscribe']; } } @@ -133,7 +133,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) { $parameters['field_name'] = $field_name; } - $url = Url::fromRoute('og_ui.entity.subscribe', $parameters); + $url = Url::fromRoute('og_ui.subscribe', $parameters); } // Otherwise, link to user login and redirect back to here. else { From 76951bcffe68fc00e1d5c8fa236893247c5507e4 Mon Sep 17 00:00:00 2001 From: Damian Lee Date: Mon, 4 Jan 2016 10:36:29 +0000 Subject: [PATCH 16/53] Update subscribe formatter to use OgGroupAudienceHelper::getMatchingField() method --- .../FieldFormatter/GroupSubscribeFormatter.php | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php b/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php index 8bfd3724a..b5a99aeff 100644 --- a/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php +++ b/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php @@ -44,6 +44,9 @@ public function viewElements(FieldItemListInterface $items, $langcode) { $elements = []; $entity = $items->getEntity(); + $entity_type_id = $entity->getEntityTypeId(); + $bundle_id = $entity->bundle(); + $account = User::load(\Drupal::currentUser()->id()); $field_name = $this->getSetting('field_name'); @@ -70,7 +73,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) { if (Og::isMember($entity, $account, [OG_STATE_ACTIVE, OG_STATE_PENDING])) { if (OgAccess::userAccess($entity, 'unsubscribe', $account)) { $link['title'] = $this->t('Unsubscribe from group'); - $link['url'] = Url::fromRoute('og_ui.unsubscribe', ['entity_type_id' => $entity->getEntityTypeId(), 'entity_id' => $entity->id()]); + $link['url'] = Url::fromRoute('og_ui.unsubscribe', ['entity_type_id' => $entity_type_id, 'entity_id' => $entity->id()]); $link['class'] = ['unsubscribe']; } } @@ -82,15 +85,14 @@ public function viewElements(FieldItemListInterface $items, $langcode) { } // Check if user can subscribe to the field. - if (empty($field_name) && $audience_field_name = og_get_best_group_audience_field('user', $account, $entity_type, $bundle)) { - $settings['field_name'] = $audience_field_name; + if (empty($field_name) && ($audience_field_name = OgGroupAudienceHelper::getMatchingField($account, $entity_type_id, $bundle_id))) { + $field_name = $audience_field_name; } - if (!$field_name) { + + if (empty($field_name)) { return []; } - // @todo Not sure this is applicable now we have our own dedicated field - // type? // Check if entity is referencable. if ($this->getSetting('target_type') !== $entity->getEntityTypeId()) { // Group type doesn't match. @@ -100,7 +102,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) { // Check handler bundles, if any. $handler_settings = $this->getSetting('handler_settings'); - if (!empty($handler_settings['target_bundles']) && !in_array($bundle, $handler_settings['target_bundles'])) { + if (!empty($handler_settings['target_bundles']) && !in_array($bundle_id, $handler_settings['target_bundles'])) { // Bundles don't match. return []; } From 28e491864672d50579e94bfed947a81fd028f25b Mon Sep 17 00:00:00 2001 From: Amitai Burstein Date: Thu, 4 Aug 2016 21:16:32 +0300 Subject: [PATCH 17/53] Cleanup old code --- .../GroupSubscribeFormatter.php | 147 +++--------------- 1 file changed, 20 insertions(+), 127 deletions(-) diff --git a/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php b/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php index b5a99aeff..16be1f64a 100644 --- a/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php +++ b/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php @@ -17,6 +17,7 @@ use Drupal\Core\Url; use Drupal\og\Og; use Drupal\og\OgAccess; +use Drupal\og\OgAccessInterface; use Drupal\og\OgGroupAudienceHelper; use Drupal\user\Entity\User; use Drupal\user\EntityOwnerInterface; @@ -43,19 +44,17 @@ class GroupSubscribeFormatter extends FormatterBase { public function viewElements(FieldItemListInterface $items, $langcode) { $elements = []; - $entity = $items->getEntity(); - $entity_type_id = $entity->getEntityTypeId(); - $bundle_id = $entity->bundle(); + $group = $items->getEntity(); + $entity_type_id = $group->getEntityTypeId(); - $account = User::load(\Drupal::currentUser()->id()); - $field_name = $this->getSetting('field_name'); + $user = User::load(\Drupal::currentUser()->id()); // Entity is not a group. - if (!Og::isGroup($entity->getEntityTypeId(), $entity->bundle())) { + if (!Og::isGroup($group->getEntityTypeId(), $group->bundle())) { return []; } - if (($entity instanceof EntityOwnerInterface) && ($entity->getOwnerId() == $account->id())) { + if (($group instanceof EntityOwnerInterface) && ($group->getOwnerId() == $user->id())) { // User is the group manager. $elements[0] = [ '#type' => 'html_tag', @@ -70,84 +69,43 @@ public function viewElements(FieldItemListInterface $items, $langcode) { return $elements; } - if (Og::isMember($entity, $account, [OG_STATE_ACTIVE, OG_STATE_PENDING])) { - if (OgAccess::userAccess($entity, 'unsubscribe', $account)) { + /** @var OgAccessInterface $og_access */ + $og_access = \Drupal::service('og.access'); + + if (Og::isMember($group, $user, [OG_STATE_ACTIVE, OG_STATE_PENDING])) { + if ($og_access->userAccess($group, 'unsubscribe', $user)) { $link['title'] = $this->t('Unsubscribe from group'); - $link['url'] = Url::fromRoute('og_ui.unsubscribe', ['entity_type_id' => $entity_type_id, 'entity_id' => $entity->id()]); + $link['url'] = Url::fromRoute('og.unsubscribe', ['entity_type_id' => $entity_type_id, 'entity_id' => $group->id()]); $link['class'] = ['unsubscribe']; } } else { - if (Og::isMember($entity, $account, [OG_STATE_BLOCKED])) { + if (Og::isMemberBlocked($group, $user)) { // If user is blocked, they should not be able to apply for // membership. return []; } - // Check if user can subscribe to the field. - if (empty($field_name) && ($audience_field_name = OgGroupAudienceHelper::getMatchingField($account, $entity_type_id, $bundle_id))) { - $field_name = $audience_field_name; - } - - if (empty($field_name)) { - return []; - } - - // Check if entity is referencable. - if ($this->getSetting('target_type') !== $entity->getEntityTypeId()) { - // Group type doesn't match. - return []; - } - - // Check handler bundles, if any. - $handler_settings = $this->getSetting('handler_settings'); - - if (!empty($handler_settings['target_bundles']) && !in_array($bundle_id, $handler_settings['target_bundles'])) { - // Bundles don't match. - return []; - } - - if (!OgGroupAudienceHelper::checkFieldCardinality($account, $field_name)) { - $cardinality = $this->fieldDefinition->getFieldStorageDefinition()->getCardinality(); - - $elements[0] = [ - '#type' => 'html_tag', - '#tag' => 'span', - '#attributes' => [ - 'title' => $this->formatPlural($cardinality, 'You are already registered to another group', 'You are already registered to @count groups'), - 'class' => ['group', 'other'], - ], - '#value' => $this->formatPlural($cardinality, 'You are already registered to another group', 'You are already registered to @count groups'), - ]; - - return $elements; - } - // If hte user is authenticated, set up the subscribe link. - if ($account->isAuthenticated()) { + if ($user->isAuthenticated()) { $parameters = [ - 'entity_type_id' => $entity->getEntityTypeId(), - 'entity_id' => $entity->id(), + 'entity_type_id' => $group->getEntityTypeId(), + 'entity_id' => $group->id(), ]; - // Add the field name as an additional query parameter. - if (!empty($field_name)) { - $parameters['field_name'] = $field_name; - } - - $url = Url::fromRoute('og_ui.subscribe', $parameters); + $url = Url::fromRoute('og.subscribe', $parameters); } - // Otherwise, link to user login and redirect back to here. else { + // User is anonymous, link to user login and redirect back to here. $url = Url::fromRoute('user.login', [], ['query' => $this->getDestinationArray()]); } - if (OgAccess::userAccess($entity, 'subscribe without approval', $account)) { + if ($og_access->userAccess($group, 'subscribe without approval', $user)) { $link['title'] = $this->t('Subscribe to group'); $link['class'] = ['subscribe']; $link['url'] = $url; } - elseif (OgAccess::userAccess($entity, 'subscribe')) { + elseif ($og_access->userAccess($group, 'subscribe')) { $link['title'] = $this->t('Request group membership'); $link['class'] = ['subscribe', 'request']; $link['url'] = $url; @@ -187,69 +145,4 @@ public function viewElements(FieldItemListInterface $items, $langcode) { return $elements; } - /** - * {@inheritdoc} - */ - public static function defaultSettings() { - $options = parent::defaultSettings(); - - $options['field_name'] = 0; - - return $options; - } - - /** - * {@inheritdoc} - */ - public function settingsForm(array $form, FormStateInterface $form_state) { - $form = parent::settingsForm($form, $form_state); - - $form['field_name'] = [ - '#title' => $this->t('Field name'), - '#description' => $this->t('Select the field that should register the user subscription.'), - '#type' => 'select', - '#options' => $this->getAudienceFieldOptions(), - '#default_value' => $this->getSetting('field_name'), - ]; - - return $form; - } - - /** - * {@inheritdoc} - */ - public function settingsSummary() { - $summary = parent::settingsSummary(); - $options = $this->getAudienceFieldOptions(); - - $field_name = $this->getSetting('field_name'); - - $summary[] = $this->t('Field: %label', array('%label' => $options[$field_name])); - - return $summary; - } - - /** - * Returns audience field options. - * - * @return array - * An array of audience field options. - */ - protected function getAudienceFieldOptions() { - $options = [0 => $this->t('Automatic (best matching)')]; - - foreach (Og::getAllGroupAudienceFields('user', 'user') as $field_name => $field_definition) { - $options[$field_name] = $field_definition->getLabel(); - } - - return $options; - } - - /** - * {@inheritdoc} - */ - protected function checkAccess(EntityInterface $entity) { - return AccessResult::allowed(); - } - } From 6cef12262fc7be41bc24deb319d86c550a285ed9 Mon Sep 17 00:00:00 2001 From: Amitai Burstein Date: Thu, 4 Aug 2016 22:56:25 +0300 Subject: [PATCH 18/53] Prevent caching of links --- .../GroupSubscribeFormatter.php | 25 ++++++++++++++----- src/Og.php | 3 +-- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php b/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php index 16be1f64a..b5e98cf85 100644 --- a/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php +++ b/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php @@ -19,6 +19,7 @@ use Drupal\og\OgAccess; use Drupal\og\OgAccessInterface; use Drupal\og\OgGroupAudienceHelper; +use Drupal\og\OgMembershipInterface; use Drupal\user\Entity\User; use Drupal\user\EntityOwnerInterface; @@ -28,7 +29,7 @@ * @FieldFormatter( * id = "og_ui_group_subscribe", * label = @Translation("OG Group subscribe"), - * description = @Translation("Display OG Group subscribe links."), + * description = @Translation("Display OG Group subscribe and un-subscribe links."), * field_types = { * "og_group" * } @@ -47,13 +48,12 @@ public function viewElements(FieldItemListInterface $items, $langcode) { $group = $items->getEntity(); $entity_type_id = $group->getEntityTypeId(); - $user = User::load(\Drupal::currentUser()->id()); - - // Entity is not a group. - if (!Og::isGroup($group->getEntityTypeId(), $group->bundle())) { + if (!Og::isGroup($entity_type_id, $group->bundle())) { + // Entity is not a group. return []; } + $user = User::load(\Drupal::currentUser()->id()); if (($group instanceof EntityOwnerInterface) && ($group->getOwnerId() == $user->id())) { // User is the group manager. $elements[0] = [ @@ -66,13 +66,15 @@ public function viewElements(FieldItemListInterface $items, $langcode) { '#value' => $this->t('You are the group manager'), ]; + $this->addCacheToElement($elements); return $elements; } /** @var OgAccessInterface $og_access */ $og_access = \Drupal::service('og.access'); - if (Og::isMember($group, $user, [OG_STATE_ACTIVE, OG_STATE_PENDING])) { + + if (Og::isMember($group, $user, [OgMembershipInterface::STATE_ACTIVE, OgMembershipInterface::STATE_PENDING])) { if ($og_access->userAccess($group, 'unsubscribe', $user)) { $link['title'] = $this->t('Unsubscribe from group'); $link['url'] = Url::fromRoute('og.unsubscribe', ['entity_type_id' => $entity_type_id, 'entity_id' => $group->id()]); @@ -142,7 +144,18 @@ public function viewElements(FieldItemListInterface $items, $langcode) { ]; } + + $this->addCacheToElement($elements); return $elements; } + /** + * Adds the correct cache context to the render array. + * + * @param [] $elements + * A renderable array. + */ + protected function addCacheToElement(&$elements) { + $elements['#cache']['max-age'] = 0; + } } diff --git a/src/Og.php b/src/Og.php index f7aacfde1..b0109408a 100644 --- a/src/Og.php +++ b/src/Og.php @@ -490,8 +490,7 @@ public static function getGroupContentIds(EntityInterface $entity, array $entity * Defaults to active memberships. * * @return bool - * TRUE if the entity (e.g. the user or node) belongs to a group with - * a certain state. + * TRUE if the user belongs to a group with a certain state. */ public static function isMember(EntityInterface $group, AccountInterface $user, $states = [OgMembershipInterface::STATE_ACTIVE]) { $group_ids = static::getUserGroupIds($user, $states); From b566311535ec4f1afb077f2e412166309c5678d2 Mon Sep 17 00:00:00 2001 From: Amitai Burstein Date: Thu, 4 Aug 2016 22:57:30 +0300 Subject: [PATCH 19/53] Sniffer fixes --- .../FieldFormatter/GroupSubscribeFormatter.php | 15 +-------------- src/OgGroupBoolean.php | 5 ----- src/Plugin/Field/FieldType/OgGroupItem.php | 5 ----- 3 files changed, 1 insertion(+), 24 deletions(-) diff --git a/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php b/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php index b5e98cf85..763232491 100644 --- a/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php +++ b/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php @@ -1,24 +1,12 @@ userAccess($group, 'unsubscribe', $user)) { $link['title'] = $this->t('Unsubscribe from group'); @@ -144,7 +131,6 @@ public function viewElements(FieldItemListInterface $items, $langcode) { ]; } - $this->addCacheToElement($elements); return $elements; } @@ -158,4 +144,5 @@ public function viewElements(FieldItemListInterface $items, $langcode) { protected function addCacheToElement(&$elements) { $elements['#cache']['max-age'] = 0; } + } diff --git a/src/OgGroupBoolean.php b/src/OgGroupBoolean.php index c232d0536..511f88bf8 100644 --- a/src/OgGroupBoolean.php +++ b/src/OgGroupBoolean.php @@ -1,10 +1,5 @@ Date: Thu, 4 Aug 2016 23:05:13 +0300 Subject: [PATCH 20/53] Move formatter to core --- og.module | 19 ++++++++++++++++++- og_ui/og_ui.module | 13 ------------- .../GroupSubscribeFormatter.php | 2 +- 3 files changed, 19 insertions(+), 15 deletions(-) rename {og_ui/src => src}/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php (98%) diff --git a/og.module b/og.module index ba78a6487..023f4257d 100755 --- a/og.module +++ b/og.module @@ -179,11 +179,11 @@ function og_entity_create_access(AccountInterface $account, array $context, $bun */ function og_entity_bundle_field_info(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) { if (!Og::isGroup($entity_type->id(), $bundle)) { + // Not a group type. return NULL; } $fields = []; - $fields['og_group'] = BaseFieldDefinition::create('og_group') ->setLabel(t('OG Group')) ->setComputed(TRUE) @@ -194,6 +194,23 @@ function og_entity_bundle_field_info(EntityTypeInterface $entity_type, $bundle, return $fields; } +/** + * Implements hook_entity_bundle_field_info_alter(). + * + * Set the default field formatter of fields of type OG group. + */ +function og_entity_bundle_field_info_alter(&$fields, EntityTypeInterface $entity_type, $bundle) { + if (!isset($fields['og_group'])) { + // No OG group fields. + return; + } + + $fields['og_group']->setDisplayOptions('view', [ + 'type' => 'og_ui_group_subscribe', + ])->setDisplayConfigurable('view', TRUE); +} + + /** * Implements hook_field_formatter_info_alter(). * diff --git a/og_ui/og_ui.module b/og_ui/og_ui.module index 9d743f4e8..2cfc51870 100644 --- a/og_ui/og_ui.module +++ b/og_ui/og_ui.module @@ -97,16 +97,3 @@ function og_ui_entity_type_save(EntityInterface $entity) { } } } - -/** - * Implements hook_entity_bundle_field_info_alter(). - * - * Set the default field formatter of fields of type OG group. - */ -function og_ui_entity_bundle_field_info_alter(&$fields, EntityTypeInterface $entity_type, $bundle) { - if (isset($fields['og_group'])) { - $fields['og_group']->setDisplayOptions('view', [ - 'type' => 'og_ui_group_subscribe', - ])->setDisplayConfigurable('view', TRUE); - } -} diff --git a/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php b/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php similarity index 98% rename from og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php rename to src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php index 763232491..0dc5dbb60 100644 --- a/og_ui/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php +++ b/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php @@ -1,6 +1,6 @@ Date: Thu, 4 Aug 2016 23:06:07 +0300 Subject: [PATCH 21/53] Replace field ID --- og.module | 2 +- src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php | 2 +- src/Plugin/Field/FieldType/OgGroupItem.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/og.module b/og.module index 023f4257d..ed4f094f2 100755 --- a/og.module +++ b/og.module @@ -206,7 +206,7 @@ function og_entity_bundle_field_info_alter(&$fields, EntityTypeInterface $entity } $fields['og_group']->setDisplayOptions('view', [ - 'type' => 'og_ui_group_subscribe', + 'type' => 'og_group_subscribe', ])->setDisplayConfigurable('view', TRUE); } diff --git a/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php b/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php index 0dc5dbb60..fad7a8a0b 100644 --- a/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php +++ b/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php @@ -15,7 +15,7 @@ * Plugin implementation for the OG subscribe formatter. * * @FieldFormatter( - * id = "og_ui_group_subscribe", + * id = "og_group_subscribe", * label = @Translation("OG Group subscribe"), * description = @Translation("Display OG Group subscribe and un-subscribe links."), * field_types = { diff --git a/src/Plugin/Field/FieldType/OgGroupItem.php b/src/Plugin/Field/FieldType/OgGroupItem.php index 36627ef76..ee35a0b3a 100644 --- a/src/Plugin/Field/FieldType/OgGroupItem.php +++ b/src/Plugin/Field/FieldType/OgGroupItem.php @@ -19,7 +19,7 @@ * description = @Translation("OG Group"), * category = @Translation("OG"), * no_ui = TRUE, - * default_formatter = "og_ui_group_subscribe", + * default_formatter = "og_group_subscribe", * ) */ class OgGroupItem extends FieldItemBase { From f399bb0b87bd967d2760729df7647de939e4e1c8 Mon Sep 17 00:00:00 2001 From: Amitai Burstein Date: Thu, 4 Aug 2016 23:07:26 +0300 Subject: [PATCH 22/53] Improve the field description --- src/Plugin/Field/FieldType/OgGroupItem.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Plugin/Field/FieldType/OgGroupItem.php b/src/Plugin/Field/FieldType/OgGroupItem.php index ee35a0b3a..beb588185 100644 --- a/src/Plugin/Field/FieldType/OgGroupItem.php +++ b/src/Plugin/Field/FieldType/OgGroupItem.php @@ -16,7 +16,7 @@ * @FieldType( * id = "og_group", * label = @Translation("OG Group"), - * description = @Translation("OG Group"), + * description = @Translation("allow non-groups members, members, and group managers to join, request or leave a group"), * category = @Translation("OG"), * no_ui = TRUE, * default_formatter = "og_group_subscribe", From f831620a0a9d964435abe6feca48abcdb6d4d1ce Mon Sep 17 00:00:00 2001 From: Amitai Burstein Date: Thu, 4 Aug 2016 23:37:34 +0300 Subject: [PATCH 23/53] Starting adding unit tests --- .../src/Unit/GroupSubscribeFormatterTest.php | 175 ++++++++++++++++++ 1 file changed, 175 insertions(+) create mode 100644 tests/src/Unit/GroupSubscribeFormatterTest.php diff --git a/tests/src/Unit/GroupSubscribeFormatterTest.php b/tests/src/Unit/GroupSubscribeFormatterTest.php new file mode 100644 index 000000000..e8c066e99 --- /dev/null +++ b/tests/src/Unit/GroupSubscribeFormatterTest.php @@ -0,0 +1,175 @@ +entityTypeId = $this->randomMachineName(); + $this->bundle = $this->randomMachineName(); + + $this->group = $this->prophesize(EntityInterface::class); + + $this->fieldItemList = $this->prophesize(FieldItemListInterface::class); + $this + ->fieldItemList + ->getEntity() + ->willReturn($this->group); + + $this + ->group + ->getEntityTypeId() + ->willReturn($this->entityTypeId); + + $this + ->group + ->bundle() + ->willReturn($this->bundle); + + $this->groupManager = $this->prophesize(GroupManager::class); + $this + ->groupManager + ->isGroup($this->entityTypeId, $this->bundle) + ->willReturn(FALSE); + + $this->fieldDefinitionInterface = $this->prophesize(FieldDefinitionInterface::class); + + $container = new ContainerBuilder(); + $container->set('og.group.manager', $this->groupManager->reveal()); + \Drupal::setContainer($container); + +// $this->entityStorage = $this->prophesize(EntityStorageInterface::class); +// $this->entityManager = $this->prophesize(EntityManagerInterface::class); +// +// $this->entityManager->getStorage('og_membership') +// ->willReturn($this->entityStorage->reveal()); +// +// $this->entityManager->getEntityTypeFromClass('Drupal\og\Entity\OgMembership') +// ->willReturn('og_membership'); +// +// // Create a mocked Og Membership entity. +// $membership_entity = $this->prophesize(OgMembershipInterface::class); +// +// $this->entityStorage +// ->create(Argument::type('array')) +// ->willReturn($membership_entity->reveal()); +// +// // Create a mocked test group. +// $this->group = $this->prophesize(EntityInterface::class); +// +// // Create a mocked test user. +// $this->user = $this->prophesize(AccountInterface::class); +// +// $membership_entity +// ->setUser($this->user) +// ->willReturn($membership_entity->reveal()); +// +// $membership_entity +// ->setGroup($this->group) +// ->willReturn($membership_entity->reveal()); +// +// $container = new ContainerBuilder(); +// $container->set('entity.manager', $this->entityManager->reveal()); +// \Drupal::setContainer($container); + } + + /** + * Tests creating membership for an un-saved group. + * + * @covers ::viewElements + */ + public function testNonGroup() { + // $plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings) + $formatter = new GroupSubscribeFormatter('', [], $this->fieldDefinitionInterface->reveal(), [], '', '', []); + $result = $formatter->viewElements($this->fieldItemList->reveal(), $this->randomMachineName()); + } + +} From b7b783f852bdf80e7e214e3cceb41389b99bf2fb Mon Sep 17 00:00:00 2001 From: Amitai Burstein Date: Thu, 4 Aug 2016 23:42:36 +0300 Subject: [PATCH 24/53] Fix test --- tests/src/Unit/GroupSubscribeFormatterTest.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tests/src/Unit/GroupSubscribeFormatterTest.php b/tests/src/Unit/GroupSubscribeFormatterTest.php index e8c066e99..d16064b11 100644 --- a/tests/src/Unit/GroupSubscribeFormatterTest.php +++ b/tests/src/Unit/GroupSubscribeFormatterTest.php @@ -162,14 +162,18 @@ public function setUp() { } /** - * Tests creating membership for an un-saved group. + * Tests formatter on a non-group. + * + * This verifies an edge case, where the formatter was somehow added to a + * non-group entity. * * @covers ::viewElements */ public function testNonGroup() { // $plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings) - $formatter = new GroupSubscribeFormatter('', [], $this->fieldDefinitionInterface->reveal(), [], '', '', []); - $result = $formatter->viewElements($this->fieldItemList->reveal(), $this->randomMachineName()); + $formatter = new GroupSubscribeFormatter('', [], $this->fieldDefinitionInterface->reveal(), [], '', [], []); + $elements = $formatter->viewElements($this->fieldItemList->reveal(), $this->randomMachineName()); + $this->assertArrayEquals([], $elements); } } From 1b251206d67417fbd1051961442b38a8dac534ca Mon Sep 17 00:00:00 2001 From: Amitai Burstein Date: Thu, 4 Aug 2016 23:58:23 +0300 Subject: [PATCH 25/53] extend tests [skip ci] --- .../src/Unit/GroupSubscribeFormatterTest.php | 62 ++++++++++++++++--- 1 file changed, 53 insertions(+), 9 deletions(-) diff --git a/tests/src/Unit/GroupSubscribeFormatterTest.php b/tests/src/Unit/GroupSubscribeFormatterTest.php index d16064b11..ee88b9719 100644 --- a/tests/src/Unit/GroupSubscribeFormatterTest.php +++ b/tests/src/Unit/GroupSubscribeFormatterTest.php @@ -9,11 +9,13 @@ use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Session\AccountInterface; +use Drupal\Core\Session\AccountProxyInterface; use Drupal\og\GroupManager; use Drupal\og\Og; use Drupal\og\OgMembershipInterface; use Drupal\og\Plugin\Field\FieldFormatter\GroupSubscribeFormatter; use Drupal\Tests\UnitTestCase; +use Drupal\user\EntityOwnerInterface; use Prophecy\Argument; /** @@ -87,18 +89,32 @@ class GroupSubscribeFormatterTest extends UnitTestCase { */ protected $groupManager; + /** + * The field definition. + * + * @var \Drupal\Core\Field\FieldDefinitionInterface + */ + protected $fieldDefinitionInterface; + /** * {@inheritdoc} */ public function setUp() { parent::setUp(); + $this->accountProxy = $this->prophesize(AccountProxyInterface::class); + $this->entityId = rand(10, 50); + $this->entityManager = $this->prophesize(EntityManagerInterface::class); + $this->entityStorage = $this->prophesize(EntityStorageInterface::class); $this->entityTypeId = $this->randomMachineName(); $this->bundle = $this->randomMachineName(); - + $this->fieldDefinitionInterface = $this->prophesize(FieldDefinitionInterface::class); + $this->fieldItemList = $this->prophesize(FieldItemListInterface::class); $this->group = $this->prophesize(EntityInterface::class); + $this->groupManager = $this->prophesize(GroupManager::class); + $this->user = $this->prophesize(AccountInterface::class); + $this->userId = rand(10, 50); - $this->fieldItemList = $this->prophesize(FieldItemListInterface::class); $this ->fieldItemList ->getEntity() @@ -114,16 +130,10 @@ public function setUp() { ->bundle() ->willReturn($this->bundle); - $this->groupManager = $this->prophesize(GroupManager::class); - $this - ->groupManager - ->isGroup($this->entityTypeId, $this->bundle) - ->willReturn(FALSE); - - $this->fieldDefinitionInterface = $this->prophesize(FieldDefinitionInterface::class); $container = new ContainerBuilder(); $container->set('og.group.manager', $this->groupManager->reveal()); + $container->set('current_user', $this->accountProxy->reveal()); \Drupal::setContainer($container); // $this->entityStorage = $this->prophesize(EntityStorageInterface::class); @@ -170,10 +180,44 @@ public function setUp() { * @covers ::viewElements */ public function testNonGroup() { + $this->groupManager->isGroup($this->entityTypeId, $this->bundle)->willReturn(FALSE); + // $plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings) $formatter = new GroupSubscribeFormatter('', [], $this->fieldDefinitionInterface->reveal(), [], '', [], []); $elements = $formatter->viewElements($this->fieldItemList->reveal(), $this->randomMachineName()); $this->assertArrayEquals([], $elements); } + public function testGroupOwner() { + $this->groupManager->isGroup($this->entityTypeId, $this->bundle)->willReturn(TRUE); + $this->entityManager->getStorage('user') + ->willReturn($this->entityStorage->reveal()); + + $this + ->accountProxy + ->id() + ->willReturn($this->entityId); + + $this->entityManager->getEntityTypeFromClass('Drupal\user\Entity\User') + ->willReturn('user'); + + $this->entityStorage + ->load($this->entityId) + ->willReturn($this->user->reveal()); + + $this + ->user + ->id() + ->willReturn($this->userId); + + $this + ->group + ->getOwnerId() + ->willReturn($this->userId); + + $formatter = new GroupSubscribeFormatter('', [], $this->fieldDefinitionInterface->reveal(), [], '', [], []); + $elements = $formatter->viewElements($this->fieldItemList->reveal(), $this->randomMachineName()); + $this->assertArrayEquals([], $elements); + } + } From 8aa5a409f497a951ecbd712ba0306bae1500687c Mon Sep 17 00:00:00 2001 From: Amitai Burstein Date: Fri, 5 Aug 2016 22:09:04 +0300 Subject: [PATCH 26/53] Fix group owner test --- .../GroupSubscribeFormatter.php | 23 ++----- .../src/Unit/GroupSubscribeFormatterTest.php | 64 +++---------------- 2 files changed, 15 insertions(+), 72 deletions(-) diff --git a/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php b/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php index fad7a8a0b..a5b508c1a 100644 --- a/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php +++ b/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php @@ -33,14 +33,14 @@ class GroupSubscribeFormatter extends FormatterBase { public function viewElements(FieldItemListInterface $items, $langcode) { $elements = []; + // We cannot use the field cache, as the formatter changes according to the + // user. Furthermore, this is not an expensive check, so we remove the cache + // entirely. + $elements['#cache']['max-age'] = 0; + $group = $items->getEntity(); $entity_type_id = $group->getEntityTypeId(); - if (!Og::isGroup($entity_type_id, $group->bundle())) { - // Entity is not a group. - return []; - } - $user = User::load(\Drupal::currentUser()->id()); if (($group instanceof EntityOwnerInterface) && ($group->getOwnerId() == $user->id())) { // User is the group manager. @@ -54,7 +54,6 @@ public function viewElements(FieldItemListInterface $items, $langcode) { '#value' => $this->t('You are the group manager'), ]; - $this->addCacheToElement($elements); return $elements; } @@ -131,18 +130,6 @@ public function viewElements(FieldItemListInterface $items, $langcode) { ]; } - $this->addCacheToElement($elements); return $elements; } - - /** - * Adds the correct cache context to the render array. - * - * @param [] $elements - * A renderable array. - */ - protected function addCacheToElement(&$elements) { - $elements['#cache']['max-age'] = 0; - } - } diff --git a/tests/src/Unit/GroupSubscribeFormatterTest.php b/tests/src/Unit/GroupSubscribeFormatterTest.php index ee88b9719..77c8d7354 100644 --- a/tests/src/Unit/GroupSubscribeFormatterTest.php +++ b/tests/src/Unit/GroupSubscribeFormatterTest.php @@ -10,6 +10,7 @@ use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Session\AccountProxyInterface; +use Drupal\entity_test\Entity\EntityTest; use Drupal\og\GroupManager; use Drupal\og\Og; use Drupal\og\OgMembershipInterface; @@ -110,7 +111,8 @@ public function setUp() { $this->bundle = $this->randomMachineName(); $this->fieldDefinitionInterface = $this->prophesize(FieldDefinitionInterface::class); $this->fieldItemList = $this->prophesize(FieldItemListInterface::class); - $this->group = $this->prophesize(EntityInterface::class); + // We use EntityTest as it implements EntityOwnerInterface. + $this->group = $this->prophesize(EntityTest::class); $this->groupManager = $this->prophesize(GroupManager::class); $this->user = $this->prophesize(AccountInterface::class); $this->userId = rand(10, 50); @@ -132,60 +134,12 @@ public function setUp() { $container = new ContainerBuilder(); - $container->set('og.group.manager', $this->groupManager->reveal()); $container->set('current_user', $this->accountProxy->reveal()); - \Drupal::setContainer($container); - -// $this->entityStorage = $this->prophesize(EntityStorageInterface::class); -// $this->entityManager = $this->prophesize(EntityManagerInterface::class); -// -// $this->entityManager->getStorage('og_membership') -// ->willReturn($this->entityStorage->reveal()); -// -// $this->entityManager->getEntityTypeFromClass('Drupal\og\Entity\OgMembership') -// ->willReturn('og_membership'); -// -// // Create a mocked Og Membership entity. -// $membership_entity = $this->prophesize(OgMembershipInterface::class); -// -// $this->entityStorage -// ->create(Argument::type('array')) -// ->willReturn($membership_entity->reveal()); -// -// // Create a mocked test group. -// $this->group = $this->prophesize(EntityInterface::class); -// -// // Create a mocked test user. -// $this->user = $this->prophesize(AccountInterface::class); -// -// $membership_entity -// ->setUser($this->user) -// ->willReturn($membership_entity->reveal()); -// -// $membership_entity -// ->setGroup($this->group) -// ->willReturn($membership_entity->reveal()); -// -// $container = new ContainerBuilder(); -// $container->set('entity.manager', $this->entityManager->reveal()); -// \Drupal::setContainer($container); - } - - /** - * Tests formatter on a non-group. - * - * This verifies an edge case, where the formatter was somehow added to a - * non-group entity. - * - * @covers ::viewElements - */ - public function testNonGroup() { - $this->groupManager->isGroup($this->entityTypeId, $this->bundle)->willReturn(FALSE); + $container->set('entity.manager', $this->entityManager->reveal()); + $container->set('og.group.manager', $this->groupManager->reveal()); + $container->set('string_translation', $this->getStringTranslationStub()); - // $plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings) - $formatter = new GroupSubscribeFormatter('', [], $this->fieldDefinitionInterface->reveal(), [], '', [], []); - $elements = $formatter->viewElements($this->fieldItemList->reveal(), $this->randomMachineName()); - $this->assertArrayEquals([], $elements); + \Drupal::setContainer($container); } public function testGroupOwner() { @@ -217,7 +171,9 @@ public function testGroupOwner() { $formatter = new GroupSubscribeFormatter('', [], $this->fieldDefinitionInterface->reveal(), [], '', [], []); $elements = $formatter->viewElements($this->fieldItemList->reveal(), $this->randomMachineName()); - $this->assertArrayEquals([], $elements); + + $this->assertEquals('You are the group manager', $elements[0]['#value']); + } } From ef60105d8936e378b61694fc624b0d02783b0d26 Mon Sep 17 00:00:00 2001 From: Amitai Burstein Date: Fri, 5 Aug 2016 22:17:06 +0300 Subject: [PATCH 27/53] Sniffer fixes --- .../Field/FieldFormatter/GroupSubscribeFormatter.php | 1 + tests/src/Unit/GroupSubscribeFormatterTest.php | 9 +++------ 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php b/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php index a5b508c1a..fc3e243e1 100644 --- a/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php +++ b/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php @@ -132,4 +132,5 @@ public function viewElements(FieldItemListInterface $items, $langcode) { return $elements; } + } diff --git a/tests/src/Unit/GroupSubscribeFormatterTest.php b/tests/src/Unit/GroupSubscribeFormatterTest.php index 77c8d7354..0499761b6 100644 --- a/tests/src/Unit/GroupSubscribeFormatterTest.php +++ b/tests/src/Unit/GroupSubscribeFormatterTest.php @@ -3,7 +3,6 @@ namespace Drupal\Tests\og\Unit; use Drupal\Core\DependencyInjection\ContainerBuilder; -use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Field\FieldDefinitionInterface; @@ -12,12 +11,8 @@ use Drupal\Core\Session\AccountProxyInterface; use Drupal\entity_test\Entity\EntityTest; use Drupal\og\GroupManager; -use Drupal\og\Og; -use Drupal\og\OgMembershipInterface; use Drupal\og\Plugin\Field\FieldFormatter\GroupSubscribeFormatter; use Drupal\Tests\UnitTestCase; -use Drupal\user\EntityOwnerInterface; -use Prophecy\Argument; /** * Tests the OG group formatter. @@ -132,7 +127,6 @@ public function setUp() { ->bundle() ->willReturn($this->bundle); - $container = new ContainerBuilder(); $container->set('current_user', $this->accountProxy->reveal()); $container->set('entity.manager', $this->entityManager->reveal()); @@ -142,6 +136,9 @@ public function setUp() { \Drupal::setContainer($container); } + /** + * Tests the formatter for a group owner. + */ public function testGroupOwner() { $this->groupManager->isGroup($this->entityTypeId, $this->bundle)->willReturn(TRUE); $this->entityManager->getStorage('user') From 0848737537418972117dbc0951dbfc965a0037de Mon Sep 17 00:00:00 2001 From: Amitai Burstein Date: Fri, 5 Aug 2016 23:04:35 +0300 Subject: [PATCH 28/53] Expand tests --- .../GroupSubscribeFormatter.php | 2 +- .../src/Unit/GroupSubscribeFormatterTest.php | 43 +++++++++++-------- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php b/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php index fc3e243e1..9aa56e79b 100644 --- a/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php +++ b/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php @@ -57,7 +57,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) { return $elements; } - /** @var OgAccessInterface $og_access */ + /** @var \Drupal\og\OgAccessInterface $og_access */ $og_access = \Drupal::service('og.access'); if (Og::isMember($group, $user, [OgMembershipInterface::STATE_ACTIVE, OgMembershipInterface::STATE_PENDING])) { diff --git a/tests/src/Unit/GroupSubscribeFormatterTest.php b/tests/src/Unit/GroupSubscribeFormatterTest.php index 0499761b6..524be2879 100644 --- a/tests/src/Unit/GroupSubscribeFormatterTest.php +++ b/tests/src/Unit/GroupSubscribeFormatterTest.php @@ -127,19 +127,6 @@ public function setUp() { ->bundle() ->willReturn($this->bundle); - $container = new ContainerBuilder(); - $container->set('current_user', $this->accountProxy->reveal()); - $container->set('entity.manager', $this->entityManager->reveal()); - $container->set('og.group.manager', $this->groupManager->reveal()); - $container->set('string_translation', $this->getStringTranslationStub()); - - \Drupal::setContainer($container); - } - - /** - * Tests the formatter for a group owner. - */ - public function testGroupOwner() { $this->groupManager->isGroup($this->entityTypeId, $this->bundle)->willReturn(TRUE); $this->entityManager->getStorage('user') ->willReturn($this->entityStorage->reveal()); @@ -161,16 +148,38 @@ public function testGroupOwner() { ->id() ->willReturn($this->userId); - $this - ->group - ->getOwnerId() - ->willReturn($this->userId); + $container = new ContainerBuilder(); + $container->set('current_user', $this->accountProxy->reveal()); + $container->set('entity.manager', $this->entityManager->reveal()); + $container->set('og.group.manager', $this->groupManager->reveal()); + $container->set('string_translation', $this->getStringTranslationStub()); + + \Drupal::setContainer($container); + } + + /** + * Tests the formatter for a group owner. + */ + public function testGroupOwner() { + // Return the same ID as the user. + $this->group->getOwnerId()->willReturn($this->userId); $formatter = new GroupSubscribeFormatter('', [], $this->fieldDefinitionInterface->reveal(), [], '', [], []); $elements = $formatter->viewElements($this->fieldItemList->reveal(), $this->randomMachineName()); $this->assertEquals('You are the group manager', $elements[0]['#value']); + } + + /** + * Tests the formatter for an "active" group member. + */ + public function testGroupMemberActive() { + $this->group->getOwnerId()->willReturn(rand(100, 200)); + $formatter = new GroupSubscribeFormatter('', [], $this->fieldDefinitionInterface->reveal(), [], '', [], []); + $elements = $formatter->viewElements($this->fieldItemList->reveal(), $this->randomMachineName()); + + $this->assertEquals('You are the group manager', $elements[0]['#value']); } } From b44d90da4fd6b2b5f30840285c720e4d7fdfd415 Mon Sep 17 00:00:00 2001 From: Amitai Burstein Date: Wed, 17 Aug 2016 14:36:21 +0300 Subject: [PATCH 29/53] Add more mocking --- .../src/Unit/GroupSubscribeFormatterTest.php | 32 ++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/tests/src/Unit/GroupSubscribeFormatterTest.php b/tests/src/Unit/GroupSubscribeFormatterTest.php index 524be2879..0bfb9b34e 100644 --- a/tests/src/Unit/GroupSubscribeFormatterTest.php +++ b/tests/src/Unit/GroupSubscribeFormatterTest.php @@ -3,16 +3,19 @@ namespace Drupal\Tests\og\Unit; use Drupal\Core\DependencyInjection\ContainerBuilder; +use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Session\AccountProxyInterface; -use Drupal\entity_test\Entity\EntityTest; use Drupal\og\GroupManager; +use Drupal\og\MembershipManagerInterface; +use Drupal\og\OgAccessInterface; use Drupal\og\Plugin\Field\FieldFormatter\GroupSubscribeFormatter; use Drupal\Tests\UnitTestCase; +use Drupal\user\EntityOwnerInterface; /** * Tests the OG group formatter. @@ -88,10 +91,24 @@ class GroupSubscribeFormatterTest extends UnitTestCase { /** * The field definition. * - * @var \Drupal\Core\Field\FieldDefinitionInterface + * @var \Drupal\Core\Field\FieldDefinitionInterface|\Prophecy\Prophecy\ObjectProphecy */ protected $fieldDefinitionInterface; + /** + * The OG access service. + * + * @var \Drupal\og\OgAccessInterface|\Prophecy\Prophecy\ObjectProphecy + */ + protected $ogAccess; + + /** + * The membership manager service. + * + * @var \Drupal\og\MembershipManagerInterface|\Prophecy\Prophecy\ObjectProphecy + */ + protected $membershipManager; + /** * {@inheritdoc} */ @@ -106,9 +123,10 @@ public function setUp() { $this->bundle = $this->randomMachineName(); $this->fieldDefinitionInterface = $this->prophesize(FieldDefinitionInterface::class); $this->fieldItemList = $this->prophesize(FieldItemListInterface::class); - // We use EntityTest as it implements EntityOwnerInterface. - $this->group = $this->prophesize(EntityTest::class); + $this->group = $this->prophesize(EntityInterface::class); $this->groupManager = $this->prophesize(GroupManager::class); + $this->membershipManager = $this->prophesize(MembershipManagerInterface::class); + $this->ogAccess = $this->prophesize(OgAccessInterface::class); $this->user = $this->prophesize(AccountInterface::class); $this->userId = rand(10, 50); @@ -117,6 +135,10 @@ public function setUp() { ->getEntity() ->willReturn($this->group); + $this + ->group + ->willImplement(EntityOwnerInterface::class); + $this ->group ->getEntityTypeId() @@ -151,7 +173,9 @@ public function setUp() { $container = new ContainerBuilder(); $container->set('current_user', $this->accountProxy->reveal()); $container->set('entity.manager', $this->entityManager->reveal()); + $container->set('og.access', $this->ogAccess->reveal()); $container->set('og.group.manager', $this->groupManager->reveal()); + $container->set('og.membership_manager', $this->membershipManager->reveal()); $container->set('string_translation', $this->getStringTranslationStub()); \Drupal::setContainer($container); From e18798549e90a4e0a716a882b431382fac31adc9 Mon Sep 17 00:00:00 2001 From: Amitai Burstein Date: Wed, 17 Aug 2016 14:39:09 +0300 Subject: [PATCH 30/53] Fix typo --- src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php | 2 +- tests/src/Unit/GroupSubscribeFormatterTest.php | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php b/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php index 9aa56e79b..59e787af4 100644 --- a/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php +++ b/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php @@ -74,7 +74,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) { return []; } - // If hte user is authenticated, set up the subscribe link. + // If the user is authenticated, set up the subscribe link. if ($user->isAuthenticated()) { $parameters = [ 'entity_type_id' => $group->getEntityTypeId(), diff --git a/tests/src/Unit/GroupSubscribeFormatterTest.php b/tests/src/Unit/GroupSubscribeFormatterTest.php index 0bfb9b34e..e09215d58 100644 --- a/tests/src/Unit/GroupSubscribeFormatterTest.php +++ b/tests/src/Unit/GroupSubscribeFormatterTest.php @@ -165,6 +165,11 @@ public function setUp() { ->load($this->entityId) ->willReturn($this->user->reveal()); + $this + ->user + ->isAuthenticated() + ->willReturn(TRUE); + $this ->user ->id() From f3a04aa6e736808fe54ea3b7dab12403ee42173d Mon Sep 17 00:00:00 2001 From: Amitai Burstein Date: Wed, 17 Aug 2016 14:55:51 +0300 Subject: [PATCH 31/53] Fix logic and add tests --- .../GroupSubscribeFormatter.php | 5 +- .../src/Unit/GroupSubscribeFormatterTest.php | 53 +++++++++++++++++-- 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php b/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php index 59e787af4..05d1de27b 100644 --- a/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php +++ b/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php @@ -88,12 +88,13 @@ public function viewElements(FieldItemListInterface $items, $langcode) { $url = Url::fromRoute('user.login', [], ['query' => $this->getDestinationArray()]); } - if ($og_access->userAccess($group, 'subscribe without approval', $user)) { + /** @var \Drupal\Core\Access\AccessResult $access */ + if (($access = $og_access->userAccess($group, 'subscribe without approval', $user)) && $access->isAllowed()) { $link['title'] = $this->t('Subscribe to group'); $link['class'] = ['subscribe']; $link['url'] = $url; } - elseif ($og_access->userAccess($group, 'subscribe')) { + elseif (($access = $og_access->userAccess($group, 'subscribe')) && $access->isAllowed()) { $link['title'] = $this->t('Request group membership'); $link['class'] = ['subscribe', 'request']; $link['url'] = $url; diff --git a/tests/src/Unit/GroupSubscribeFormatterTest.php b/tests/src/Unit/GroupSubscribeFormatterTest.php index e09215d58..4196e6f9c 100644 --- a/tests/src/Unit/GroupSubscribeFormatterTest.php +++ b/tests/src/Unit/GroupSubscribeFormatterTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\og\Unit; +use Drupal\Core\Access\AccessResultInterface; use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityManagerInterface; @@ -109,18 +110,47 @@ class GroupSubscribeFormatterTest extends UnitTestCase { */ protected $membershipManager; + /** + * The account proxy service. + * + * @var \Drupal\Core\Session\AccountProxyInterface|\Prophecy\Prophecy\ObjectProphecy + */ + protected $accountProxy; + + /** + * A random group ID. + * + * @var int + */ + protected $entityId; + + /** + * A random user ID. + * + * @var int + */ + protected $userId; + + /** + * An access result object. + * + * @var \Drupal\Core\Access\AccessResult|\Prophecy\Prophecy\ObjectProphecy + */ + protected $accessResult; + /** * {@inheritdoc} */ public function setUp() { parent::setUp(); + $this->accessResult = $this->prophesize(AccessResultInterface::class); $this->accountProxy = $this->prophesize(AccountProxyInterface::class); + $this->bundle = $this->randomMachineName(); $this->entityId = rand(10, 50); $this->entityManager = $this->prophesize(EntityManagerInterface::class); $this->entityStorage = $this->prophesize(EntityStorageInterface::class); $this->entityTypeId = $this->randomMachineName(); - $this->bundle = $this->randomMachineName(); $this->fieldDefinitionInterface = $this->prophesize(FieldDefinitionInterface::class); $this->fieldItemList = $this->prophesize(FieldItemListInterface::class); $this->group = $this->prophesize(EntityInterface::class); @@ -149,6 +179,11 @@ public function setUp() { ->bundle() ->willReturn($this->bundle); + $this + ->group + ->id() + ->willReturn($this->entityId); + $this->groupManager->isGroup($this->entityTypeId, $this->bundle)->willReturn(TRUE); $this->entityManager->getStorage('user') ->willReturn($this->entityStorage->reveal()); @@ -156,13 +191,13 @@ public function setUp() { $this ->accountProxy ->id() - ->willReturn($this->entityId); + ->willReturn($this->userId); $this->entityManager->getEntityTypeFromClass('Drupal\user\Entity\User') ->willReturn('user'); $this->entityStorage - ->load($this->entityId) + ->load($this->userId) ->willReturn($this->user->reveal()); $this @@ -205,10 +240,20 @@ public function testGroupOwner() { public function testGroupMemberActive() { $this->group->getOwnerId()->willReturn(rand(100, 200)); + $this + ->ogAccess + ->userAccess($this->group->reveal(), 'subscribe without approval', $this->user->reveal()) + ->willReturn($this->accessResult->reveal()); + + $this + ->accessResult + ->isAllowed() + ->willReturn(TRUE); + $formatter = new GroupSubscribeFormatter('', [], $this->fieldDefinitionInterface->reveal(), [], '', [], []); $elements = $formatter->viewElements($this->fieldItemList->reveal(), $this->randomMachineName()); - $this->assertEquals('You are the group manager', $elements[0]['#value']); + $this->assertEquals('Subscribe to group', $elements[0]['#title']); } } From f3d1763d5505db4ea13748b3058fb6cad661c678 Mon Sep 17 00:00:00 2001 From: Amitai Burstein Date: Wed, 17 Aug 2016 21:23:16 +0300 Subject: [PATCH 32/53] Fix pending subscribe --- .../GroupSubscribeFormatter.php | 2 +- .../src/Unit/GroupSubscribeFormatterTest.php | 45 +++++++++++++++++-- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php b/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php index 05d1de27b..a6dbf3296 100644 --- a/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php +++ b/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php @@ -94,7 +94,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) { $link['class'] = ['subscribe']; $link['url'] = $url; } - elseif (($access = $og_access->userAccess($group, 'subscribe')) && $access->isAllowed()) { + elseif (($access = $og_access->userAccess($group, 'subscribe', $user)) && $access->isAllowed()) { $link['title'] = $this->t('Request group membership'); $link['class'] = ['subscribe', 'request']; $link['url'] = $url; diff --git a/tests/src/Unit/GroupSubscribeFormatterTest.php b/tests/src/Unit/GroupSubscribeFormatterTest.php index 4196e6f9c..5570a5f88 100644 --- a/tests/src/Unit/GroupSubscribeFormatterTest.php +++ b/tests/src/Unit/GroupSubscribeFormatterTest.php @@ -240,20 +240,57 @@ public function testGroupOwner() { public function testGroupMemberActive() { $this->group->getOwnerId()->willReturn(rand(100, 200)); + $this->ogAccess->userAccess($this->group->reveal(), 'subscribe without approval', $this->user->reveal())->willReturn($this->accessResult->reveal()); + $this->accessResult->isAllowed()->willReturn(TRUE); + + $formatter = new GroupSubscribeFormatter('', [], $this->fieldDefinitionInterface->reveal(), [], '', [], []); + $elements = $formatter->viewElements($this->fieldItemList->reveal(), $this->randomMachineName()); + + $this->assertEquals('Subscribe to group', $elements[0]['#title']); + } + + + /** + * Tests the formatter for subscribe without approval. + */ + public function testSubscribeWithoutApproval() { + $this->group->getOwnerId()->willReturn(rand(100, 200)); + + $this->ogAccess->userAccess($this->group->reveal(), 'subscribe without approval', $this->user->reveal())->willReturn($this->accessResult->reveal()); + $this->accessResult->isAllowed()->willReturn(TRUE); + + $formatter = new GroupSubscribeFormatter('', [], $this->fieldDefinitionInterface->reveal(), [], '', [], []); + $elements = $formatter->viewElements($this->fieldItemList->reveal(), $this->randomMachineName()); + + $this->assertEquals('Subscribe to group', $elements[0]['#title']); + } + + /** + * Tests the formatter for subscribe with approval. + */ + public function testSubscribeWithApproval() { + $this->group->getOwnerId()->willReturn(rand(100, 200)); + $this ->ogAccess ->userAccess($this->group->reveal(), 'subscribe without approval', $this->user->reveal()) ->willReturn($this->accessResult->reveal()); + $this->accessResult->isAllowed()->willReturn(FALSE); + + /** @var \Drupal\Core\Access\AccessResult $access_result */ + $access_result = $this->prophesize(AccessResultInterface::class); $this - ->accessResult - ->isAllowed() - ->willReturn(TRUE); + ->ogAccess + ->userAccess($this->group->reveal(), 'subscribe', $this->user->reveal()) + ->willReturn($access_result->reveal()); + + $access_result->isAllowed()->willReturn(TRUE); $formatter = new GroupSubscribeFormatter('', [], $this->fieldDefinitionInterface->reveal(), [], '', [], []); $elements = $formatter->viewElements($this->fieldItemList->reveal(), $this->randomMachineName()); - $this->assertEquals('Subscribe to group', $elements[0]['#title']); + $this->assertEquals('Request group membership', $elements[0]['#title']); } } From e871312be26223bf420cb56dee9746ec2465b111 Mon Sep 17 00:00:00 2001 From: Amitai Burstein Date: Wed, 17 Aug 2016 21:26:52 +0300 Subject: [PATCH 33/53] Tests the formatter for no subscribe permission. --- .../src/Unit/GroupSubscribeFormatterTest.php | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/tests/src/Unit/GroupSubscribeFormatterTest.php b/tests/src/Unit/GroupSubscribeFormatterTest.php index 5570a5f88..41fbeebd8 100644 --- a/tests/src/Unit/GroupSubscribeFormatterTest.php +++ b/tests/src/Unit/GroupSubscribeFormatterTest.php @@ -253,7 +253,7 @@ public function testGroupMemberActive() { /** * Tests the formatter for subscribe without approval. */ - public function testSubscribeWithoutApproval() { + public function testSubscribeWithoutApprovalPermission() { $this->group->getOwnerId()->willReturn(rand(100, 200)); $this->ogAccess->userAccess($this->group->reveal(), 'subscribe without approval', $this->user->reveal())->willReturn($this->accessResult->reveal()); @@ -268,7 +268,7 @@ public function testSubscribeWithoutApproval() { /** * Tests the formatter for subscribe with approval. */ - public function testSubscribeWithApproval() { + public function testSubscribeWithApprovalPermission() { $this->group->getOwnerId()->willReturn(rand(100, 200)); $this @@ -293,4 +293,25 @@ public function testSubscribeWithApproval() { $this->assertEquals('Request group membership', $elements[0]['#title']); } + /** + * Tests the formatter for no subscribe permission. + */ + public function testNoSubscribePermission() { + $this->group->getOwnerId()->willReturn(rand(100, 200)); + + foreach (['subscribe without approval', 'subscribe'] as $perm) { + $this + ->ogAccess + ->userAccess($this->group->reveal(), $perm, $this->user->reveal()) + ->willReturn($this->accessResult->reveal()); + } + + $this->accessResult->isAllowed()->willReturn(FALSE); + + $formatter = new GroupSubscribeFormatter('', [], $this->fieldDefinitionInterface->reveal(), [], '', [], []); + $elements = $formatter->viewElements($this->fieldItemList->reveal(), $this->randomMachineName()); + + $this->assertTrue(strpos($elements[0]['#value'], 'This is a closed group.') === 0); + } + } From c873f58a6ccf06a25ace38f1f87bdc52b2265ca9 Mon Sep 17 00:00:00 2001 From: Amitai Burstein Date: Wed, 17 Aug 2016 21:41:11 +0300 Subject: [PATCH 34/53] Test blocked member --- .../FieldFormatter/GroupSubscribeFormatter.php | 12 ++++++------ tests/src/Functional/GroupSubscribeTest.php | 2 +- tests/src/Unit/GroupSubscribeFormatterTest.php | 18 ++++++++++++++++++ 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php b/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php index a6dbf3296..ee4c157a5 100644 --- a/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php +++ b/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php @@ -60,6 +60,12 @@ public function viewElements(FieldItemListInterface $items, $langcode) { /** @var \Drupal\og\OgAccessInterface $og_access */ $og_access = \Drupal::service('og.access'); + if (Og::isMemberBlocked($group, $user)) { + // If user is blocked, they should not be able to apply for + // membership. + return $elements; + } + if (Og::isMember($group, $user, [OgMembershipInterface::STATE_ACTIVE, OgMembershipInterface::STATE_PENDING])) { if ($og_access->userAccess($group, 'unsubscribe', $user)) { $link['title'] = $this->t('Unsubscribe from group'); @@ -68,12 +74,6 @@ public function viewElements(FieldItemListInterface $items, $langcode) { } } else { - if (Og::isMemberBlocked($group, $user)) { - // If user is blocked, they should not be able to apply for - // membership. - return []; - } - // If the user is authenticated, set up the subscribe link. if ($user->isAuthenticated()) { $parameters = [ diff --git a/tests/src/Functional/GroupSubscribeTest.php b/tests/src/Functional/GroupSubscribeTest.php index e498ab896..088638f71 100644 --- a/tests/src/Functional/GroupSubscribeTest.php +++ b/tests/src/Functional/GroupSubscribeTest.php @@ -97,7 +97,7 @@ protected function setUp() { $this->nonGroupBundle = Unicode::strtolower($this->randomMachineName()); $this->membershipTypeBundle = Unicode::strtolower($this->randomMachineName()); - // Define the entities as groups. + // Define the bundles as groups. Og::groupManager()->addGroup('node', $this->groupBundle1); Og::groupManager()->addGroup('node', $this->groupBundle2); diff --git a/tests/src/Unit/GroupSubscribeFormatterTest.php b/tests/src/Unit/GroupSubscribeFormatterTest.php index 41fbeebd8..0e59e7b59 100644 --- a/tests/src/Unit/GroupSubscribeFormatterTest.php +++ b/tests/src/Unit/GroupSubscribeFormatterTest.php @@ -14,6 +14,7 @@ use Drupal\og\GroupManager; use Drupal\og\MembershipManagerInterface; use Drupal\og\OgAccessInterface; +use Drupal\og\OgMembershipInterface; use Drupal\og\Plugin\Field\FieldFormatter\GroupSubscribeFormatter; use Drupal\Tests\UnitTestCase; use Drupal\user\EntityOwnerInterface; @@ -314,4 +315,21 @@ public function testNoSubscribePermission() { $this->assertTrue(strpos($elements[0]['#value'], 'This is a closed group.') === 0); } + /** + * Tests the formatter for a blocked member. + */ + public function testBlockedMember() { + $this->group->getOwnerId()->willReturn(rand(100, 200)); + + $this + ->membershipManager + ->isMember($this->group->reveal(), $this->user->reveal(), [OgMembershipInterface::STATE_BLOCKED]) + ->willReturn(TRUE); + + $formatter = new GroupSubscribeFormatter('', [], $this->fieldDefinitionInterface->reveal(), [], '', [], []); + $elements = $formatter->viewElements($this->fieldItemList->reveal(), $this->randomMachineName()); + + $this->assertTrue(empty($elements[0])); + } + } From 2d0b5b6dfde3a95b2a18a5ca2a97295fc132ad63 Mon Sep 17 00:00:00 2001 From: Amitai Burstein Date: Wed, 17 Aug 2016 21:48:47 +0300 Subject: [PATCH 35/53] Remove unsubscribe permission --- src/EventSubscriber/OgEventSubscriber.php | 7 ------ .../GroupSubscribeFormatter.php | 8 +++---- tests/src/Kernel/PermissionEventTest.php | 1 - .../src/Unit/GroupSubscribeFormatterTest.php | 24 +++++++++++++++++++ 4 files changed, 27 insertions(+), 13 deletions(-) diff --git a/src/EventSubscriber/OgEventSubscriber.php b/src/EventSubscriber/OgEventSubscriber.php index 2614e3aac..607850633 100644 --- a/src/EventSubscriber/OgEventSubscriber.php +++ b/src/EventSubscriber/OgEventSubscriber.php @@ -110,13 +110,6 @@ public function provideDefaultOgPermissions(PermissionEventInterface $event) { 'roles' => [OgRoleInterface::ANONYMOUS], 'default roles' => [], ]), - new GroupPermission([ - 'name' => 'unsubscribe', - 'title' => t('Unsubscribe from group'), - 'description' => t('Allow members to unsubscribe themselves from a group, removing their membership.'), - 'roles' => [OgRoleInterface::AUTHENTICATED], - 'default roles' => [OgRoleInterface::AUTHENTICATED], - ]), new GroupPermission([ 'name' => 'approve and deny subscription', 'title' => t('Approve and deny subscription'), diff --git a/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php b/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php index ee4c157a5..6cd9fb4ba 100644 --- a/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php +++ b/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php @@ -67,11 +67,9 @@ public function viewElements(FieldItemListInterface $items, $langcode) { } if (Og::isMember($group, $user, [OgMembershipInterface::STATE_ACTIVE, OgMembershipInterface::STATE_PENDING])) { - if ($og_access->userAccess($group, 'unsubscribe', $user)) { - $link['title'] = $this->t('Unsubscribe from group'); - $link['url'] = Url::fromRoute('og.unsubscribe', ['entity_type_id' => $entity_type_id, 'entity_id' => $group->id()]); - $link['class'] = ['unsubscribe']; - } + $link['title'] = $this->t('Unsubscribe from group'); + $link['url'] = Url::fromRoute('og.unsubscribe', ['entity_type_id' => $entity_type_id, 'entity_id' => $group->id()]); + $link['class'] = ['unsubscribe']; } else { // If the user is authenticated, set up the subscribe link. diff --git a/tests/src/Kernel/PermissionEventTest.php b/tests/src/Kernel/PermissionEventTest.php index b200ed11e..6d08b1095 100644 --- a/tests/src/Kernel/PermissionEventTest.php +++ b/tests/src/Kernel/PermissionEventTest.php @@ -135,7 +135,6 @@ public function permissionEventDataProvider() { 'manage roles', 'subscribe without approval', 'subscribe', - 'unsubscribe', 'update group', ]; // Test permissions that should only be available for the test group that diff --git a/tests/src/Unit/GroupSubscribeFormatterTest.php b/tests/src/Unit/GroupSubscribeFormatterTest.php index 0e59e7b59..11a1e7d17 100644 --- a/tests/src/Unit/GroupSubscribeFormatterTest.php +++ b/tests/src/Unit/GroupSubscribeFormatterTest.php @@ -332,4 +332,28 @@ public function testBlockedMember() { $this->assertTrue(empty($elements[0])); } + /** + * Tests the formatter for an active or pending member. + */ + public function testMember() { + $this->group->getOwnerId()->willReturn(rand(100, 200)); + + $this + ->membershipManager + ->isMember($this->group->reveal(), $this->user->reveal(), [OgMembershipInterface::STATE_BLOCKED]) + ->willReturn(FALSE); + + $this + ->membershipManager + ->isMember($this->group->reveal(), $this->user->reveal(), [OgMembershipInterface::STATE_ACTIVE, OgMembershipInterface::STATE_PENDING]) + ->willReturn(TRUE); + + $formatter = new GroupSubscribeFormatter('', [], $this->fieldDefinitionInterface->reveal(), [], '', [], []); + $elements = $formatter->viewElements($this->fieldItemList->reveal(), $this->randomMachineName()); + + + + $this->assertEquals('Unsubscribe from group', $elements[0]['#title']); + } + } From f68b8f77595c5f130cfcd13223a49a88fd3351d1 Mon Sep 17 00:00:00 2001 From: Amitai Burstein Date: Wed, 17 Aug 2016 21:54:31 +0300 Subject: [PATCH 36/53] Add GroupSubscribeFormatterTest.php --- .../GroupSubscribeFormatterTest.php | 130 ++++++++++++++++++ .../src/Unit/GroupSubscribeFormatterTest.php | 2 - 2 files changed, 130 insertions(+), 2 deletions(-) create mode 100644 tests/src/Functional/GroupSubscribeFormatterTest.php diff --git a/tests/src/Functional/GroupSubscribeFormatterTest.php b/tests/src/Functional/GroupSubscribeFormatterTest.php new file mode 100644 index 000000000..1809d0469 --- /dev/null +++ b/tests/src/Functional/GroupSubscribeFormatterTest.php @@ -0,0 +1,130 @@ +groupBundle1 = Unicode::strtolower($this->randomMachineName()); + $this->groupBundle2 = Unicode::strtolower($this->randomMachineName()); + + // Define the bundles as groups. + Og::groupManager()->addGroup('node', $this->groupBundle1); + Og::groupManager()->addGroup('node', $this->groupBundle2); + + // Create node author user. + $user = $this->createUser(); + + // Create groups. + $this->group1 = Node::create([ + 'type' => $this->groupBundle1, + 'title' => $this->randomString(), + 'uid' => $user->id(), + ]); + $this->group1->save(); + + $this->group2 = Node::create([ + 'type' => $this->groupBundle2, + 'title' => $this->randomString(), + 'uid' => $user->id(), + ]); + $this->group2->save(); + + /** @var \Drupal\og\Entity\OgRole $role */ + $role = Og::getRole('node', $this->groupBundle1, OgRoleInterface::ANONYMOUS); + $role + ->grantPermission('subscribe without approval') + ->save(); + + + $this->user1 = $this->drupalCreateUser(); + $this->user2 = $this->drupalCreateUser(); + } + + /** + * Tests the formatter changes by user and membership. + */ + public function testFormatter() { + $this->drupalLogin($this->user1); + + // Subscribe to group. + $this->drupalGet('node/' . $this->group1->id()); + $this->clickLink('Subscribe to group'); + $this->click('#edit-submit'); + + $this->drupalGet('node/' . $this->group1->id()); + $this->assertSession()->linkExists('Unsubscribe from group'); + + // Validate another user sees the correct formatter link. + $this->drupalLogin($this->user2); + $this->drupalGet('node/' . $this->group1->id()); + $this->clickLink('Subscribe to group'); + + } + +} diff --git a/tests/src/Unit/GroupSubscribeFormatterTest.php b/tests/src/Unit/GroupSubscribeFormatterTest.php index 11a1e7d17..511dddb56 100644 --- a/tests/src/Unit/GroupSubscribeFormatterTest.php +++ b/tests/src/Unit/GroupSubscribeFormatterTest.php @@ -351,8 +351,6 @@ public function testMember() { $formatter = new GroupSubscribeFormatter('', [], $this->fieldDefinitionInterface->reveal(), [], '', [], []); $elements = $formatter->viewElements($this->fieldItemList->reveal(), $this->randomMachineName()); - - $this->assertEquals('Unsubscribe from group', $elements[0]['#title']); } From 8030e43e73b68b7e8b5a7e0b14503b2ef9121878 Mon Sep 17 00:00:00 2001 From: Amitai Burstein Date: Wed, 17 Aug 2016 21:58:12 +0300 Subject: [PATCH 37/53] Sniffer fixes --- tests/src/Functional/GroupSubscribeFormatterTest.php | 1 - tests/src/Unit/GroupSubscribeFormatterTest.php | 1 - 2 files changed, 2 deletions(-) diff --git a/tests/src/Functional/GroupSubscribeFormatterTest.php b/tests/src/Functional/GroupSubscribeFormatterTest.php index 1809d0469..e7ada0988 100644 --- a/tests/src/Functional/GroupSubscribeFormatterTest.php +++ b/tests/src/Functional/GroupSubscribeFormatterTest.php @@ -101,7 +101,6 @@ protected function setUp() { ->grantPermission('subscribe without approval') ->save(); - $this->user1 = $this->drupalCreateUser(); $this->user2 = $this->drupalCreateUser(); } diff --git a/tests/src/Unit/GroupSubscribeFormatterTest.php b/tests/src/Unit/GroupSubscribeFormatterTest.php index 511dddb56..764bad9d1 100644 --- a/tests/src/Unit/GroupSubscribeFormatterTest.php +++ b/tests/src/Unit/GroupSubscribeFormatterTest.php @@ -250,7 +250,6 @@ public function testGroupMemberActive() { $this->assertEquals('Subscribe to group', $elements[0]['#title']); } - /** * Tests the formatter for subscribe without approval. */ From 8339ab7bbddc72f6f51942686adc2dac4361f352 Mon Sep 17 00:00:00 2001 From: Amitai Burstein Date: Wed, 17 Aug 2016 22:03:48 +0300 Subject: [PATCH 38/53] Cleanup test --- .../GroupSubscribeFormatterTest.php | 50 +++++-------------- 1 file changed, 13 insertions(+), 37 deletions(-) diff --git a/tests/src/Functional/GroupSubscribeFormatterTest.php b/tests/src/Functional/GroupSubscribeFormatterTest.php index e7ada0988..e2ea96b53 100644 --- a/tests/src/Functional/GroupSubscribeFormatterTest.php +++ b/tests/src/Functional/GroupSubscribeFormatterTest.php @@ -25,28 +25,14 @@ class GroupSubscribeFormatterTest extends BrowserTestBase { * * @var \Drupal\node\NodeInterface */ - protected $group1; - - /** - * Test entity group. - * - * @var \Drupal\node\NodeInterface - */ - protected $group2; - - /** - * A group bundle name. - * - * @var string - */ - protected $groupBundle1; + protected $group; /** * A group bundle name. * * @var string */ - protected $groupBundle2; + protected $groupBundle; /** * A non-author user. @@ -55,13 +41,12 @@ class GroupSubscribeFormatterTest extends BrowserTestBase { */ protected $user1; - /** - * The node author (and group manager) user. + * A non-author user. * * @var \Drupal\user\UserInterface */ - protected $authorUser; + protected $user2; /** * {@inheritdoc} @@ -70,33 +55,24 @@ protected function setUp() { parent::setUp(); // Create bundles. - $this->groupBundle1 = Unicode::strtolower($this->randomMachineName()); - $this->groupBundle2 = Unicode::strtolower($this->randomMachineName()); + $this->groupBundle = Unicode::strtolower($this->randomMachineName()); // Define the bundles as groups. - Og::groupManager()->addGroup('node', $this->groupBundle1); - Og::groupManager()->addGroup('node', $this->groupBundle2); + Og::groupManager()->addGroup('node', $this->groupBundle); // Create node author user. $user = $this->createUser(); // Create groups. - $this->group1 = Node::create([ - 'type' => $this->groupBundle1, - 'title' => $this->randomString(), - 'uid' => $user->id(), - ]); - $this->group1->save(); - - $this->group2 = Node::create([ - 'type' => $this->groupBundle2, + $this->group = Node::create([ + 'type' => $this->groupBundle, 'title' => $this->randomString(), 'uid' => $user->id(), ]); - $this->group2->save(); + $this->group->save(); /** @var \Drupal\og\Entity\OgRole $role */ - $role = Og::getRole('node', $this->groupBundle1, OgRoleInterface::ANONYMOUS); + $role = Og::getRole('node', $this->groupBundle, OgRoleInterface::ANONYMOUS); $role ->grantPermission('subscribe without approval') ->save(); @@ -112,16 +88,16 @@ public function testFormatter() { $this->drupalLogin($this->user1); // Subscribe to group. - $this->drupalGet('node/' . $this->group1->id()); + $this->drupalGet('node/' . $this->group->id()); $this->clickLink('Subscribe to group'); $this->click('#edit-submit'); - $this->drupalGet('node/' . $this->group1->id()); + $this->drupalGet('node/' . $this->group->id()); $this->assertSession()->linkExists('Unsubscribe from group'); // Validate another user sees the correct formatter link. $this->drupalLogin($this->user2); - $this->drupalGet('node/' . $this->group1->id()); + $this->drupalGet('node/' . $this->group->id()); $this->clickLink('Subscribe to group'); } From b73545be50f6a9a367280b0a99d783d48752a8a9 Mon Sep 17 00:00:00 2001 From: Amitai Burstein Date: Wed, 17 Aug 2016 22:22:32 +0300 Subject: [PATCH 39/53] Add manadatory weight property --- og.module | 1 + 1 file changed, 1 insertion(+) diff --git a/og.module b/og.module index ed4f094f2..e038060bb 100755 --- a/og.module +++ b/og.module @@ -206,6 +206,7 @@ function og_entity_bundle_field_info_alter(&$fields, EntityTypeInterface $entity } $fields['og_group']->setDisplayOptions('view', [ + 'weight' => 0, 'type' => 'og_group_subscribe', ])->setDisplayConfigurable('view', TRUE); } From 443728360ef443839eca2a07663a3f3de8cd4c2c Mon Sep 17 00:00:00 2001 From: Amitai Burstein Date: Wed, 17 Aug 2016 22:29:17 +0300 Subject: [PATCH 40/53] Fix tests --- tests/src/Functional/GroupSubscribeFormatterTest.php | 7 ++++++- tests/src/Functional/GroupSubscribeTest.php | 3 +++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/src/Functional/GroupSubscribeFormatterTest.php b/tests/src/Functional/GroupSubscribeFormatterTest.php index e2ea96b53..ca7fa069a 100644 --- a/tests/src/Functional/GroupSubscribeFormatterTest.php +++ b/tests/src/Functional/GroupSubscribeFormatterTest.php @@ -4,6 +4,7 @@ use Drupal\Component\Utility\Unicode; use Drupal\node\Entity\Node; +use Drupal\node\Entity\NodeType; use Drupal\og\Og; use Drupal\og\OgRoleInterface; use Drupal\Tests\BrowserTestBase; @@ -54,9 +55,13 @@ class GroupSubscribeFormatterTest extends BrowserTestBase { protected function setUp() { parent::setUp(); - // Create bundles. + // Create bundle. $this->groupBundle = Unicode::strtolower($this->randomMachineName()); + // Create a node type. + $node_type = NodeType::create(['type' => $this->groupBundle, 'name' => $this->groupBundle]); + $node_type->save(); + // Define the bundles as groups. Og::groupManager()->addGroup('node', $this->groupBundle); diff --git a/tests/src/Functional/GroupSubscribeTest.php b/tests/src/Functional/GroupSubscribeTest.php index 088638f71..efe4101fb 100644 --- a/tests/src/Functional/GroupSubscribeTest.php +++ b/tests/src/Functional/GroupSubscribeTest.php @@ -97,6 +97,9 @@ protected function setUp() { $this->nonGroupBundle = Unicode::strtolower($this->randomMachineName()); $this->membershipTypeBundle = Unicode::strtolower($this->randomMachineName()); + // We don't need to NodeType::create() the bundles, as we don't call the + // node view. + // Define the bundles as groups. Og::groupManager()->addGroup('node', $this->groupBundle1); Og::groupManager()->addGroup('node', $this->groupBundle2); From 51715784521262d35ddfcb747c4cfea43aaaf2c6 Mon Sep 17 00:00:00 2001 From: Amitai Burstein Date: Wed, 17 Aug 2016 22:30:33 +0300 Subject: [PATCH 41/53] Improve comment location --- tests/src/Functional/GroupSubscribeTest.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/src/Functional/GroupSubscribeTest.php b/tests/src/Functional/GroupSubscribeTest.php index efe4101fb..4ffa07607 100644 --- a/tests/src/Functional/GroupSubscribeTest.php +++ b/tests/src/Functional/GroupSubscribeTest.php @@ -92,14 +92,13 @@ protected function setUp() { parent::setUp(); // Create bundles. + // We don't need to later call NodeType::create() on the bundles, as we + // don't call the node view. $this->groupBundle1 = Unicode::strtolower($this->randomMachineName()); $this->groupBundle2 = Unicode::strtolower($this->randomMachineName()); $this->nonGroupBundle = Unicode::strtolower($this->randomMachineName()); $this->membershipTypeBundle = Unicode::strtolower($this->randomMachineName()); - // We don't need to NodeType::create() the bundles, as we don't call the - // node view. - // Define the bundles as groups. Og::groupManager()->addGroup('node', $this->groupBundle1); Og::groupManager()->addGroup('node', $this->groupBundle2); From 56c17a0d4f90f93fce3f7ffa9e07698343b415de Mon Sep 17 00:00:00 2001 From: Amitai Burstein Date: Wed, 17 Aug 2016 22:46:20 +0300 Subject: [PATCH 42/53] Remove obsolete permission --- og.routing.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/og.routing.yml b/og.routing.yml index 32b05da1b..847a01741 100644 --- a/og.routing.yml +++ b/og.routing.yml @@ -16,8 +16,6 @@ og.subscribe: membership_type: type: entity:og_membership_type - - og.unsubscribe: path: 'group/{entity_type_id}/{entity_id}/unsubscribe' defaults: @@ -25,4 +23,3 @@ og.unsubscribe: _title: 'Leave Group' requirements: _user_is_logged_in: 'TRUE' - _og_user_access_group: 'unsubscribe' From e38d3f9160763b6e6312c339457cd2c2b9dfe3e1 Mon Sep 17 00:00:00 2001 From: Sander Van Dooren Date: Fri, 19 Aug 2016 13:46:54 +0200 Subject: [PATCH 43/53] Upcast the entity ID in the unsubscribe route. --- og.routing.yml | 10 +++++-- src/Controller/SubscriptionController.php | 29 +++++-------------- .../GroupSubscribeFormatter.php | 4 +-- 3 files changed, 17 insertions(+), 26 deletions(-) diff --git a/og.routing.yml b/og.routing.yml index 847a01741..3dbeddfe1 100644 --- a/og.routing.yml +++ b/og.routing.yml @@ -1,7 +1,7 @@ # Routes for Organic groups. og.subscribe: - path: 'group/{entity_type_id}/{entity_id}/subscribe/{membership_type}' + path: 'group/{entity_type_id}/{group}/subscribe/{membership_type}' defaults: _controller: '\Drupal\og\Controller\SubscriptionController::subscribe' _title: 'Join Group' @@ -13,13 +13,19 @@ og.subscribe: _access: 'TRUE' options: parameters: + group: + type: entity:{entity_type_id} membership_type: type: entity:og_membership_type og.unsubscribe: - path: 'group/{entity_type_id}/{entity_id}/unsubscribe' + path: 'group/{entity_type_id}/{group}/unsubscribe' defaults: _controller: '\Drupal\og\Controller\SubscriptionController::unsubscribe' _title: 'Leave Group' requirements: _user_is_logged_in: 'TRUE' + options: + parameters: + group: + type: entity:{entity_type_id} diff --git a/src/Controller/SubscriptionController.php b/src/Controller/SubscriptionController.php index 022fc8331..baff12e32 100644 --- a/src/Controller/SubscriptionController.php +++ b/src/Controller/SubscriptionController.php @@ -2,15 +2,15 @@ namespace Drupal\og\Controller; -use Drupal\Component\Plugin\Exception\PluginNotFoundException; use Drupal\Core\Controller\ControllerBase; +use Drupal\Core\Entity\ContentEntityInterface; +use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Url; use Drupal\og\OgMembershipTypeInterface; use Drupal\user\Entity\User; use Drupal\user\EntityOwnerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\RedirectResponse; -use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; use Drupal\og\Og; use Drupal\og\OgAccess; @@ -47,11 +47,9 @@ public static function create(ContainerInterface $container) { /** * Subscribe a user to group. * - * @param \Symfony\Component\HttpFoundation\Request $request - * The request object. * @param string $entity_type_id * The entity type of the group entity. - * @param string|int $entity_id + * @param \Drupal\Core\Entity\EntityInterface $group * The entity ID of the group entity. * @param \Drupal\og\OgMembershipTypeInterface $membership_type * The membership type to be used for creating the membership. @@ -60,16 +58,8 @@ public static function create(ContainerInterface $container) { * Redirect user or show access denied if they are not allowed to subscribe, * otherwise provide a subscribe confirmation form. */ - public function subscribe(Request $request, $entity_type_id, $entity_id, OgMembershipTypeInterface $membership_type) { - try { - $entity_storage = $this->entityTypeManager()->getStorage($entity_type_id); - } - catch (PluginNotFoundException $e) { - // Not a valid entity type. - throw new AccessDeniedHttpException(); - } - - if (!$group = $entity_storage->load($entity_id)) { + public function subscribe($entity_type_id, EntityInterface $group, OgMembershipTypeInterface $membership_type) { + if (!$group instanceof ContentEntityInterface) { // Not a valid entity. throw new AccessDeniedHttpException(); } @@ -142,19 +132,14 @@ public function subscribe(Request $request, $entity_type_id, $entity_id, OgMembe /** * Unsubscribe a user from group. * - * @param string $entity_type_id - * The entity type of the group entity. - * @param string|int $entity_id + * @param \Drupal\core\Entity\EntityInterface $group * The entity ID of the group entity. * * @return mixed * Redirect user or show access denied if they are not allowed to subscribe, * otherwise provide an un-subscribe confirmation form. */ - public function unsubscribe($entity_type_id, $entity_id) { - $entity_storage = $this->entityTypeManager()->getStorage($entity_type_id); - $group = $entity_storage->load($entity_id); - + public function unsubscribe(EntityInterface $group) { $user = $this->currentUser(); if (!$membership = Og::getMembership($group, $user)) { diff --git a/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php b/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php index 6cd9fb4ba..d337d670d 100644 --- a/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php +++ b/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php @@ -68,7 +68,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) { if (Og::isMember($group, $user, [OgMembershipInterface::STATE_ACTIVE, OgMembershipInterface::STATE_PENDING])) { $link['title'] = $this->t('Unsubscribe from group'); - $link['url'] = Url::fromRoute('og.unsubscribe', ['entity_type_id' => $entity_type_id, 'entity_id' => $group->id()]); + $link['url'] = Url::fromRoute('og.unsubscribe', ['entity_type_id' => $entity_type_id, 'group' => $group->id()]); $link['class'] = ['unsubscribe']; } else { @@ -76,7 +76,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) { if ($user->isAuthenticated()) { $parameters = [ 'entity_type_id' => $group->getEntityTypeId(), - 'entity_id' => $group->id(), + 'group' => $group->id(), ]; $url = Url::fromRoute('og.subscribe', $parameters); From 08c161a642c4e2037d732b869193304198af2b7b Mon Sep 17 00:00:00 2001 From: Pieter Frenssen Date: Fri, 19 Aug 2016 15:35:07 +0200 Subject: [PATCH 44/53] One does not simply hijack @amitaibu's PR without failing the tests. --- tests/src/Functional/GroupSubscribeTest.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/src/Functional/GroupSubscribeTest.php b/tests/src/Functional/GroupSubscribeTest.php index 4ffa07607..06af27416 100644 --- a/tests/src/Functional/GroupSubscribeTest.php +++ b/tests/src/Functional/GroupSubscribeTest.php @@ -212,14 +212,17 @@ public function testSubscribeAccess() { [ 'entity_type_id' => Unicode::strtolower($this->randomMachineName()), 'entity_id' => 1, - 'code' => 403, + // @todo This currently returns a 500 error due to a bug in core. Change + // this to a 403 or 404 when the bug is fixed. + // @see https://www.drupal.org/node/2786897 + 'code' => 500, ], // A non existing entity ID. [ 'entity_type_id' => 'node', 'entity_id' => rand(1000, 2000), - 'code' => 403, + 'code' => 404, ], ]; From 4db893ab82b5809bb58ba3bc2b265ff8685bee7a Mon Sep 17 00:00:00 2001 From: Amitai Burstein Date: Sat, 20 Aug 2016 19:24:04 +0300 Subject: [PATCH 45/53] Rename class to OgGroupAlwaysTrue --- src/{OgGroupBoolean.php => OgGroupAlwaysTrue.php} | 2 +- src/Plugin/Field/FieldType/OgGroupItem.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename src/{OgGroupBoolean.php => OgGroupAlwaysTrue.php} (87%) diff --git a/src/OgGroupBoolean.php b/src/OgGroupAlwaysTrue.php similarity index 87% rename from src/OgGroupBoolean.php rename to src/OgGroupAlwaysTrue.php index 511f88bf8..26b099b81 100644 --- a/src/OgGroupBoolean.php +++ b/src/OgGroupAlwaysTrue.php @@ -7,7 +7,7 @@ /** * Overridden boolean data type to hardcode TRUE. */ -class OgGroupBoolean extends BooleanData { +class OgGroupAlwaysTrue extends BooleanData { /** * {@inheritdoc} diff --git a/src/Plugin/Field/FieldType/OgGroupItem.php b/src/Plugin/Field/FieldType/OgGroupItem.php index beb588185..a311cd838 100644 --- a/src/Plugin/Field/FieldType/OgGroupItem.php +++ b/src/Plugin/Field/FieldType/OgGroupItem.php @@ -41,7 +41,7 @@ public static function propertyDefinitions(FieldStorageDefinitionInterface $fiel ->setLabel('Group item value') ->setReadOnly(TRUE) ->setComputed(TRUE) - ->setClass('\Drupal\og\OgGroupBoolean'); + ->setClass('\Drupal\og\OgGroupAlwaysTrue'); return $definitions; } From 7302fd97ee707d1817f03976a4e0ddeabc20c893 Mon Sep 17 00:00:00 2001 From: Amitai Burstein Date: Sat, 20 Aug 2016 19:30:43 +0300 Subject: [PATCH 46/53] Inject services --- .../GroupSubscribeFormatter.php | 64 +++++++++++++++++-- 1 file changed, 57 insertions(+), 7 deletions(-) diff --git a/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php b/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php index 6cd9fb4ba..1f072c613 100644 --- a/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php +++ b/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php @@ -4,12 +4,16 @@ use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\FormatterBase; +use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Routing\RedirectDestinationTrait; +use Drupal\Core\Session\AccountInterface; use Drupal\Core\Url; use Drupal\og\Og; +use Drupal\og\OgAccessInterface; use Drupal\og\OgMembershipInterface; use Drupal\user\Entity\User; use Drupal\user\EntityOwnerInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Plugin implementation for the OG subscribe formatter. @@ -23,10 +27,59 @@ * } * ) */ -class GroupSubscribeFormatter extends FormatterBase { +class GroupSubscribeFormatter extends FormatterBase implements ContainerFactoryPluginInterface { use RedirectDestinationTrait; + /** + * The current user. + * + * @var \Drupal\Core\Session\AccountInterface + */ + protected $currentUser; + + /** + * The OG access service. + * + * @var \Drupal\og\OgAccessInterface + */ + protected $ogAccess; + + /** + * Constructs a new GroupSubscribeFormatter object. + * + * @param array $configuration + * A configuration array containing information about the plugin instance. + * @param string $plugin_id + * The plugin_id for the plugin instance. + * @param mixed $plugin_definition + * The plugin implementation definition. + * @param \Drupal\Core\Session\AccountInterface $current_user + * The current user. + * @param \Drupal\og\OgAccessInterface $og_access + * The OG access service. + */ + public function __construct(array $configuration, $plugin_id, $plugin_definition, AccountInterface $current_user, OgAccessInterface $og_access) { + parent::__construct($configuration, $plugin_id, $plugin_definition); + + $this->currentUser = $current_user; + $this->ogAccess = $og_access; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('current_user'), + $container->get('og.access') + ); + } + + /** * {@inheritdoc} */ @@ -41,7 +94,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) { $group = $items->getEntity(); $entity_type_id = $group->getEntityTypeId(); - $user = User::load(\Drupal::currentUser()->id()); + $user = User::load($this->currentUser->id()); if (($group instanceof EntityOwnerInterface) && ($group->getOwnerId() == $user->id())) { // User is the group manager. $elements[0] = [ @@ -57,9 +110,6 @@ public function viewElements(FieldItemListInterface $items, $langcode) { return $elements; } - /** @var \Drupal\og\OgAccessInterface $og_access */ - $og_access = \Drupal::service('og.access'); - if (Og::isMemberBlocked($group, $user)) { // If user is blocked, they should not be able to apply for // membership. @@ -87,12 +137,12 @@ public function viewElements(FieldItemListInterface $items, $langcode) { } /** @var \Drupal\Core\Access\AccessResult $access */ - if (($access = $og_access->userAccess($group, 'subscribe without approval', $user)) && $access->isAllowed()) { + if (($access = $this->ogAccess->userAccess($group, 'subscribe without approval', $user)) && $access->isAllowed()) { $link['title'] = $this->t('Subscribe to group'); $link['class'] = ['subscribe']; $link['url'] = $url; } - elseif (($access = $og_access->userAccess($group, 'subscribe', $user)) && $access->isAllowed()) { + elseif (($access = $this->ogAccess->userAccess($group, 'subscribe', $user)) && $access->isAllowed()) { $link['title'] = $this->t('Request group membership'); $link['class'] = ['subscribe', 'request']; $link['url'] = $url; From 57e2f8d928ae2f04d9393e9db71f25e40b2b2621 Mon Sep 17 00:00:00 2001 From: Amitai Burstein Date: Sat, 20 Aug 2016 19:44:22 +0300 Subject: [PATCH 47/53] Sniffer fixes --- src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php b/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php index 62fb21b2c..124c5ed20 100644 --- a/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php +++ b/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php @@ -79,7 +79,6 @@ public static function create(ContainerInterface $container, array $configuratio ); } - /** * {@inheritdoc} */ From 48f6e8edfe031e207a495f51cc240390d8476986 Mon Sep 17 00:00:00 2001 From: Amitai Burstein Date: Sat, 20 Aug 2016 19:58:24 +0300 Subject: [PATCH 48/53] Lock travis version --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 7b1d24754..bbec6148e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -45,7 +45,7 @@ before_script: - composer global require squizlabs/php_codesniffer # Coder. - - composer global require drupal/coder:dev-8.x-2.x + - composer global require drupal/coder:8.2.8 - ln -s ~/.composer/vendor/drupal/coder/coder_sniffer/Drupal ~/.composer/vendor/squizlabs/php_codesniffer/CodeSniffer/Standards/ - phpenv rehash From d5f89b02daa0d4ab34bf3ad53eead475ce283dab Mon Sep 17 00:00:00 2001 From: Amitai Burstein Date: Sat, 20 Aug 2016 20:53:28 +0300 Subject: [PATCH 49/53] Fix constructor --- .../GroupSubscribeFormatter.php | 29 +++++++++++++++---- 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php b/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php index 124c5ed20..88863ee2e 100644 --- a/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php +++ b/src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php @@ -2,6 +2,8 @@ namespace Drupal\og\Plugin\Field\FieldFormatter; +use Drupal\Core\Entity\EntityManagerInterface; +use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\FormatterBase; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; @@ -48,19 +50,29 @@ class GroupSubscribeFormatter extends FormatterBase implements ContainerFactoryP /** * Constructs a new GroupSubscribeFormatter object. * - * @param array $configuration - * A configuration array containing information about the plugin instance. * @param string $plugin_id - * The plugin_id for the plugin instance. + * The plugin_id for the formatter. * @param mixed $plugin_definition * The plugin implementation definition. + * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition + * The definition of the field to which the formatter is associated. + * @param array $settings + * The formatter settings. + * @param string $label + * The formatter label display setting. + * @param string $view_mode + * The view mode. + * @param array $third_party_settings + * Any third party settings settings. + * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager + * The entity manager. * @param \Drupal\Core\Session\AccountInterface $current_user * The current user. * @param \Drupal\og\OgAccessInterface $og_access * The OG access service. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, AccountInterface $current_user, OgAccessInterface $og_access) { - parent::__construct($configuration, $plugin_id, $plugin_definition); + public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, EntityManagerInterface $entity_manager, AccountInterface $current_user, OgAccessInterface $og_access) { + parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings, $entity_manager); $this->currentUser = $current_user; $this->ogAccess = $og_access; @@ -71,9 +83,14 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { return new static( - $configuration, $plugin_id, $plugin_definition, + $configuration['field_definition'], + $configuration['settings'], + $configuration['label'], + $configuration['view_mode'], + $configuration['third_party_settings'], + $container->get('entity.manager'), $container->get('current_user'), $container->get('og.access') ); From 0414f78b95b6c4d1f1837782a23a9342b610a8bc Mon Sep 17 00:00:00 2001 From: Amitai Burstein Date: Sat, 20 Aug 2016 23:09:46 +0300 Subject: [PATCH 50/53] Adapt unit tests --- .../src/Unit/GroupSubscribeFormatterTest.php | 53 ++++++++++--------- 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/tests/src/Unit/GroupSubscribeFormatterTest.php b/tests/src/Unit/GroupSubscribeFormatterTest.php index 764bad9d1..ce0b27d87 100644 --- a/tests/src/Unit/GroupSubscribeFormatterTest.php +++ b/tests/src/Unit/GroupSubscribeFormatterTest.php @@ -212,10 +212,7 @@ public function setUp() { ->willReturn($this->userId); $container = new ContainerBuilder(); - $container->set('current_user', $this->accountProxy->reveal()); $container->set('entity.manager', $this->entityManager->reveal()); - $container->set('og.access', $this->ogAccess->reveal()); - $container->set('og.group.manager', $this->groupManager->reveal()); $container->set('og.membership_manager', $this->membershipManager->reveal()); $container->set('string_translation', $this->getStringTranslationStub()); @@ -229,9 +226,7 @@ public function testGroupOwner() { // Return the same ID as the user. $this->group->getOwnerId()->willReturn($this->userId); - $formatter = new GroupSubscribeFormatter('', [], $this->fieldDefinitionInterface->reveal(), [], '', [], []); - $elements = $formatter->viewElements($this->fieldItemList->reveal(), $this->randomMachineName()); - + $elements = $this->getElements(); $this->assertEquals('You are the group manager', $elements[0]['#value']); } @@ -244,9 +239,7 @@ public function testGroupMemberActive() { $this->ogAccess->userAccess($this->group->reveal(), 'subscribe without approval', $this->user->reveal())->willReturn($this->accessResult->reveal()); $this->accessResult->isAllowed()->willReturn(TRUE); - $formatter = new GroupSubscribeFormatter('', [], $this->fieldDefinitionInterface->reveal(), [], '', [], []); - $elements = $formatter->viewElements($this->fieldItemList->reveal(), $this->randomMachineName()); - + $elements = $this->getElements(); $this->assertEquals('Subscribe to group', $elements[0]['#title']); } @@ -259,9 +252,7 @@ public function testSubscribeWithoutApprovalPermission() { $this->ogAccess->userAccess($this->group->reveal(), 'subscribe without approval', $this->user->reveal())->willReturn($this->accessResult->reveal()); $this->accessResult->isAllowed()->willReturn(TRUE); - $formatter = new GroupSubscribeFormatter('', [], $this->fieldDefinitionInterface->reveal(), [], '', [], []); - $elements = $formatter->viewElements($this->fieldItemList->reveal(), $this->randomMachineName()); - + $elements = $this->getElements(); $this->assertEquals('Subscribe to group', $elements[0]['#title']); } @@ -287,9 +278,7 @@ public function testSubscribeWithApprovalPermission() { $access_result->isAllowed()->willReturn(TRUE); - $formatter = new GroupSubscribeFormatter('', [], $this->fieldDefinitionInterface->reveal(), [], '', [], []); - $elements = $formatter->viewElements($this->fieldItemList->reveal(), $this->randomMachineName()); - + $elements = $this->getElements(); $this->assertEquals('Request group membership', $elements[0]['#title']); } @@ -308,9 +297,7 @@ public function testNoSubscribePermission() { $this->accessResult->isAllowed()->willReturn(FALSE); - $formatter = new GroupSubscribeFormatter('', [], $this->fieldDefinitionInterface->reveal(), [], '', [], []); - $elements = $formatter->viewElements($this->fieldItemList->reveal(), $this->randomMachineName()); - + $elements = $this->getElements(); $this->assertTrue(strpos($elements[0]['#value'], 'This is a closed group.') === 0); } @@ -325,9 +312,7 @@ public function testBlockedMember() { ->isMember($this->group->reveal(), $this->user->reveal(), [OgMembershipInterface::STATE_BLOCKED]) ->willReturn(TRUE); - $formatter = new GroupSubscribeFormatter('', [], $this->fieldDefinitionInterface->reveal(), [], '', [], []); - $elements = $formatter->viewElements($this->fieldItemList->reveal(), $this->randomMachineName()); - + $elements = $this->getElements(); $this->assertTrue(empty($elements[0])); } @@ -347,10 +332,30 @@ public function testMember() { ->isMember($this->group->reveal(), $this->user->reveal(), [OgMembershipInterface::STATE_ACTIVE, OgMembershipInterface::STATE_PENDING]) ->willReturn(TRUE); - $formatter = new GroupSubscribeFormatter('', [], $this->fieldDefinitionInterface->reveal(), [], '', [], []); - $elements = $formatter->viewElements($this->fieldItemList->reveal(), $this->randomMachineName()); - + $elements = $this->getElements(); $this->assertEquals('Unsubscribe from group', $elements[0]['#title']); } + /** + * Helper method; Return the renderable elements from the formatter. + * + * @return array + * The renderable array. + */ + protected function getElements() { + $formatter = new GroupSubscribeFormatter( + '', + [], + $this->fieldDefinitionInterface->reveal(), + [], + '', + '', + [], + $this->entityManager->reveal(), + $this->accountProxy->reveal(), + $this->ogAccess->reveal() + ); + return $formatter->viewElements($this->fieldItemList->reveal(), $this->randomMachineName()); + } + } From 0bd2a8bf20e995f7d0bbbff29bd7e2dd236ff1cc Mon Sep 17 00:00:00 2001 From: Amitai Burstein Date: Thu, 25 Aug 2016 00:32:22 +0300 Subject: [PATCH 51/53] convert GroupManager to GroupTypeManager --- tests/src/Unit/GroupSubscribeFormatterTest.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/src/Unit/GroupSubscribeFormatterTest.php b/tests/src/Unit/GroupSubscribeFormatterTest.php index ce0b27d87..86698122d 100644 --- a/tests/src/Unit/GroupSubscribeFormatterTest.php +++ b/tests/src/Unit/GroupSubscribeFormatterTest.php @@ -11,7 +11,7 @@ use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Session\AccountProxyInterface; -use Drupal\og\GroupManager; +use Drupal\og\GroupTypeManager; use Drupal\og\MembershipManagerInterface; use Drupal\og\OgAccessInterface; use Drupal\og\OgMembershipInterface; @@ -86,9 +86,9 @@ class GroupSubscribeFormatterTest extends UnitTestCase { /** * The group manager. * - * @var \Drupal\og\GroupManager|\Prophecy\Prophecy\ObjectProphecy + * @var \Drupal\og\GroupTypeManager|\Prophecy\Prophecy\ObjectProphecy */ - protected $groupManager; + protected $groupTypeManager; /** * The field definition. @@ -155,7 +155,7 @@ public function setUp() { $this->fieldDefinitionInterface = $this->prophesize(FieldDefinitionInterface::class); $this->fieldItemList = $this->prophesize(FieldItemListInterface::class); $this->group = $this->prophesize(EntityInterface::class); - $this->groupManager = $this->prophesize(GroupManager::class); + $this->groupTypeManager = $this->prophesize(GroupTypeManager::class); $this->membershipManager = $this->prophesize(MembershipManagerInterface::class); $this->ogAccess = $this->prophesize(OgAccessInterface::class); $this->user = $this->prophesize(AccountInterface::class); @@ -185,7 +185,7 @@ public function setUp() { ->id() ->willReturn($this->entityId); - $this->groupManager->isGroup($this->entityTypeId, $this->bundle)->willReturn(TRUE); + $this->groupTypeManager->isGroup($this->entityTypeId, $this->bundle)->willReturn(TRUE); $this->entityManager->getStorage('user') ->willReturn($this->entityStorage->reveal()); From 19d486f73d5de8252be89d1faae6c15f0f72ac71 Mon Sep 17 00:00:00 2001 From: Amitai Burstein Date: Thu, 25 Aug 2016 00:34:20 +0300 Subject: [PATCH 52/53] Comments fixes --- src/GroupTypeManager.php | 2 +- src/OgAccess.php | 2 -- src/OgRoleManager.php | 2 +- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/GroupTypeManager.php b/src/GroupTypeManager.php index 44d280b3c..aa8d5a212 100644 --- a/src/GroupTypeManager.php +++ b/src/GroupTypeManager.php @@ -123,7 +123,7 @@ class GroupTypeManager { protected $ogRoleManager; /** - * Constructs an GroupManager object. + * Constructs a GroupTypeManager object. * * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory * The config factory. diff --git a/src/OgAccess.php b/src/OgAccess.php index ea1246e56..4b342cfdf 100644 --- a/src/OgAccess.php +++ b/src/OgAccess.php @@ -67,8 +67,6 @@ class OgAccess implements OgAccessInterface { * The group manager. * * @var \Drupal\og\GroupTypeManager - * - * @todo This should be GroupManagerInterface. */ protected $groupTypeManager; diff --git a/src/OgRoleManager.php b/src/OgRoleManager.php index d2fdc64b8..9b1edf8f8 100644 --- a/src/OgRoleManager.php +++ b/src/OgRoleManager.php @@ -34,7 +34,7 @@ class OgRoleManager implements OgRoleManagerInterface { protected $permissionManager; /** - * Constructs an GroupManager object. + * Constructs an OgRoleManager object. * * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager * The entity type manager. From 9d2eaabd028301ad1db717036088e8d9be2ea164 Mon Sep 17 00:00:00 2001 From: Amitai Burstein Date: Thu, 25 Aug 2016 10:26:49 +0300 Subject: [PATCH 53/53] Call correct method --- tests/src/Functional/GroupSubscribeFormatterTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/src/Functional/GroupSubscribeFormatterTest.php b/tests/src/Functional/GroupSubscribeFormatterTest.php index ca7fa069a..e1c2cbcc8 100644 --- a/tests/src/Functional/GroupSubscribeFormatterTest.php +++ b/tests/src/Functional/GroupSubscribeFormatterTest.php @@ -63,7 +63,7 @@ protected function setUp() { $node_type->save(); // Define the bundles as groups. - Og::groupManager()->addGroup('node', $this->groupBundle); + Og::groupTypeManager()->addGroup('node', $this->groupBundle); // Create node author user. $user = $this->createUser();