Commit 3e3063fc authored by Nikolay Lobachev's avatar Nikolay Lobachev Committed by Kristiaan Van den Eynde
Browse files

Issue #3311119 by kristiaanvandeneynde, Artusamak, LOBsTerr: Group roles can not be edited

parent 7d21fccc
Loading
Loading
Loading
Loading
+27 −23
Original line number Diff line number Diff line
@@ -22,7 +22,6 @@ class GroupRoleForm extends EntityForm {

    assert($this->entity instanceof GroupRoleInterface);
    $group_role = $this->entity;
    $group_role_id = '';

    $form['label'] = [
      '#title' => $this->t('Name'),
@@ -33,28 +32,28 @@ class GroupRoleForm extends EntityForm {
      '#size' => 30,
    ];

    // UI-generated group roles have the group type prefixed to their ID.
    if ($this->operation === 'add') {
      // Since group role IDs are prefixed by the group type's ID followed by a
      // period, we need to save some space for that.
      $subtract = strlen($group_role->getGroupTypeId()) + 1;

    // Since machine names with periods in it are technically not allowed, we
    // strip the group type ID prefix when editing a group role.
    if ($group_role->id()) {
      [, $group_role_id] = explode('-', $group_role->id(), 2);
    }

      $form['id'] = [
        '#type' => 'machine_name',
      '#default_value' => $group_role_id,
        '#description' => $this->t('A unique machine-readable name for this group role. It must only contain lowercase letters, numbers, and underscores.'),
        '#maxlength' => EntityTypeInterface::ID_MAX_LENGTH - $subtract,
        '#field_prefix' => $group_role->getGroupTypeId() . '-',
        '#machine_name' => [
          'exists' => [$this, 'exists'],
          'source' => ['label'],
        ],
      '#description' => $this->t('A unique machine-readable name for this group role. It must only contain lowercase letters, numbers, and underscores.'),
      '#disabled' => !$group_role->isNew(),
      '#field_prefix' => $group_role->getGroupTypeId() . '-',
      ];
    }
    else {
      $form['id'] = [
        '#type' => 'value',
        '#value' => $group_role->id(),
      ];
    }

    $form['weight'] = [
      '#type' => 'value',
@@ -146,10 +145,15 @@ class GroupRoleForm extends EntityForm {
   */
  public function save(array $form, FormStateInterface $form_state) {
    assert($this->entity instanceof GroupRoleInterface);

    $group_role = $this->entity;
    $group_role->set('id', $group_role->getGroupTypeId() . '-' . $group_role->id());
    $group_role->set('label', trim($group_role->label()));

    // UI-generated group roles have the group type prefixed to their ID.
    if ($this->operation === 'add') {
      $group_role->set('id', $group_role->getGroupTypeId() . '-' . $group_role->id());
    }

    // Make sure the global_role property is NULL rather than FALSE.
    if ($group_role->getScope() === PermissionScopeInterface::INDIVIDUAL_ID) {
      $group_role->set('global_role', NULL);
+134 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\group\Functional;

use Drupal\Component\Render\FormattableMarkup;
use Drupal\group\Plugin\Validation\Constraint\GroupRoleScope;

/**
 * Tests the behavior of the group role form.
 *
 * @group group
 */
class GroupRoleFormTest extends GroupBrowserTestBase {

  /**
   * Group type.
   *
   * @var \Drupal\group\Entity\GroupTypeInterface
   */
  protected $groupType;

  /**
   * Group role storage.
   *
   * @var \Drupal\group\Entity\Storage\GroupRoleStorageInterface
   */
  protected $groupRoleStorage;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    $this->groupRoleStorage = $this->entityTypeManager->getStorage('group_role');

    $this->groupType = $this->createGroupType([
      'id' => 'gt',
      'label' => 'community',
      'creator_membership' => FALSE,
    ]);
  }

  /**
   * Gets the global (site) permissions for the group creator.
   *
   * @return string[]
   *   The permissions.
   */
  protected function getGlobalPermissions() {
    return [
      'administer group',
    ] + parent::getGlobalPermissions();
  }

  /**
   * Test UI role creation via code.
   */
  public function testUiRoleCreation() {
    $this->drupalGet("/admin/group/types/manage/{$this->groupType->id()}/roles/add");
    $scope = 'outsider';
    $role_name = 'Outsider authenticated';
    $role_id = "{$this->groupType->id()}-outsider_authenticated";
    $submit_button = 'Save group role';
    $edit = [
      'Name' => $role_name,
      'id' => 'outsider_authenticated',
      'scope' => $scope,
      'Global role' => 'authenticated',
    ];
    $this->submitForm($edit, $submit_button);

    $this->assertSession()->pageTextContains("The group role {$role_name} has been added.");

    // Check that a newly created role has a correct id
    // (group_type_id-role_name)
    $group_role = $this->groupRoleStorage->load($role_id);
    $this->assertIsObject($group_role);

    // We want to be sure that we can edit role.
    $this->drupalGet("/admin/group/types/manage/{$this->groupType->id()}/roles/$role_id");
    $this->assertSession()->statusCodeEquals(200);
    $this->submitForm([], $submit_button);
    $this->assertSession()->pageTextContains("The group role {$role_name} has been updated.");

    // Try to create a role with the same scope.
    $this->drupalGet("/admin/group/types/manage/{$this->groupType->id()}/roles/add");
    $edit = [
      'Name' => 'new outsider authenticated',
      'id' => 'new_outsider_authenticated',
      'scope' => $scope,
      'Global role' => 'authenticated',
    ];
    $this->submitForm($edit, $submit_button);

    // We should see constraint message.
    $constraint = new GroupRoleScope();
    $this->assertSession()->pageTextContains(strip_tags(new FormattableMarkup($constraint->duplicateScopePairMessage, [
      '%group_type' => $this->groupType->label(),
      '@scope' => $scope,
      '%role' => 'Authenticated user',
    ])));

  }

  /**
   * Test role creation via code.
   */
  public function testCodeRoleCreation() {
    $role_id = 'myrole';
    $role_name = 'My role';
    $group_role = $this->groupRoleStorage->create([
      'id' => $role_id,
      'label' => $role_name,
      'scope' => 'outsider',
      'global_role' => 'anonymous',
      'group_type' => $this->groupType->id(),
    ]);
    $group_role->save();

    // Load role to be sure, we don't have problems with entity API.
    $group_role = $this->groupRoleStorage->load($role_id);
    $this->assertIsObject($group_role);

    $this->drupalGet("/admin/group/types/manage/{$this->groupType->id()}/roles/{$role_id}");
    $this->assertSession()->statusCodeEquals(200);

    $this->submitForm([], 'Save group role');
    $this->assertSession()->statusCodeEquals(200);
    $this->assertSession()->pageTextContains("The group role {$role_name} has been updated.");

  }

}