og-8.x-1.x-dev/tests/src/Kernel/GroupManagerSubscriptionTest.php

tests/src/Kernel/GroupManagerSubscriptionTest.php
<?php

declare(strict_types=1);

namespace Drupal\Tests\og\Kernel;

use Drupal\og\MembershipManagerInterface;
use Drupal\og\GroupTypeManagerInterface;
use Drupal\user\UserInterface;
use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\og\Entity\OgRole;
use Drupal\og\OgMembershipInterface;
use Drupal\user\Entity\User;

/**
 * Tests if the group manager is subscribed automatically upon group creation.
 *
 * @group og
 */
class GroupManagerSubscriptionTest extends KernelTestBase {

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'field',
    'node',
    'og',
    'og_test',
    'system',
    'user',
    'options',
  ];

  /**
   * Test group owner.
   */
  protected UserInterface $owner;

  /**
   * The group type manager.
   */
  protected GroupTypeManagerInterface $groupTypeManager;

  /**
   * The membership manager.
   */
  protected MembershipManagerInterface $membershipManager;

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

    $this->groupTypeManager = $this->container->get('og.group_type_manager');
    $this->membershipManager = $this->container->get('og.membership_manager');

    // Add membership and config schema.
    $this->installConfig(['og']);
    $this->installEntitySchema('node');
    $this->installEntitySchema('og_membership');
    $this->installEntitySchema('user');
    $this->installSchema('system', ['sequences']);

    // Create a group type.
    NodeType::create([
      'name' => $this->randomString(),
      'type' => 'group',
    ])->save();
    $this->groupTypeManager->addGroup('node', 'group');

    // Create a test group owner.
    $this->owner = User::create(['name' => $this->randomString()]);
    $this->owner->save();

    // Create a custom role that will be used to check if other modules can
    // override the membership that is created by default.
    $role = OgRole::create();
    $role
      ->setName('moderator')
      ->setLabel($this->randomString())
      ->setGroupType('node')
      ->setGroupBundle('group')
      ->save();
  }

  /**
   * Tests whether a group manager is subscribed when creating a group.
   *
   * @dataProvider groupManagerSubscriptionProvider
   */
  public function testGroupManagerSubscription(bool $group_has_owner, bool $membership_is_overridden): void {
    // Create the group, with a specific title that can be detected by the hook
    // implementation that overrides the creation of the membership.
    // @see og_test_entity_insert()
    $group = Node::create([
      'title' => $membership_is_overridden ? 'membership is overridden' : 'membership is not overridden',
      'type' => 'group',
    ]);
    // Set the group owner if the test requires it.
    if ($group_has_owner) {
      $group->setOwner($this->owner);
    }
    $group->save();

    // Check that a membership has only been created if the group had an owner
    // set.
    $membership = $this->membershipManager->getMembership($group, $this->owner->id());
    $this->assertEquals($group_has_owner, !empty($membership));

    // Check if the membership has been overridden.
    $this->assertEquals($membership_is_overridden, $this->isMembershipOverridden($membership));
  }

  /**
   * Tests 'Automatically add creators to the group' admin settings.
   *
   * @dataProvider autoAddOwnersSettingProvider
   */
  public function testAutoAddOwnersSetting(bool $auto_add_group_owner_membership): void {
    $config = $this->container->get('config.factory')->getEditable('og.settings');
    $config->set('auto_add_group_owner_membership', $auto_add_group_owner_membership);
    $config->save();
    $group = Node::create([
      'title' => 'membership is not overridden',
      'type' => 'group',
      'uid' => $this->owner->id(),
    ]);
    $group->save();
    $membership = $this->membershipManager->getMembership($group, $this->owner->id());
    // Membership should be created only when auto_add_group_owner_membership is
    // enabled.
    $this->assertEquals($auto_add_group_owner_membership, !empty($membership));
  }

  /**
   * Checks if the membership is overridden by a custom hook implementation.
   *
   * @param \Drupal\og\OgMembershipInterface $membership
   *   The OG Membership to check. If empty the membership does not exist.
   *
   * @return bool
   *   Whether or not the membership is overridden.
   */
  protected function isMembershipOverridden(?OgMembershipInterface $membership = NULL): bool {
    // If the membership doesn't exist it is not overridden.
    if (empty($membership)) {
      return FALSE;
    }

    // If the membership is overridden, it will have the 'moderator' role
    // assigned to it.
    // @see og_test_entity_insert()
    return $membership->hasRole('node-group-moderator');
  }

  /**
   * Data provider for ::testGroupManagerSubscription().
   *
   * @return array
   *   An array of test data arrays, each test data array having the following
   *   three values:
   *   - A boolean indicating whether or not the group manager will be marked as
   *     the author of the group when the entity is created.
   *   - A boolean indicating whether or not another hook_entity_insert() will
   *     fire first and will override the membership.
   */
  public static function groupManagerSubscriptionProvider(): array {
    return [
      // Test a group created by the group manager.
      [
        // Whether or not to set the group manager as author of the group
        // entity.
        TRUE,
        // Whether or not another hook_entity_insert() implementation will fire
        // first and create a subscription.
        FALSE,
      ],
      // Test a group created by an anonymous user.
      [
        FALSE,
        FALSE,
      ],
      // Test a group created by the group manager, but the subscription will be
      // already be created by another hook_entity_insert() implementation. This
      // allows us to test whether developers can override the automatic
      // creation of the membership.
      [
        TRUE,
        TRUE,
      ],
    ];
  }

  /**
   * Data provider for ::testAutoAddOwnersSetting().
   *
   * @return array[]
   *   Array of data with values for the 'auto_add_group_owner_membership'
   *   config key.
   */
  public static function autoAddOwnersSettingProvider(): array {
    return [
      [
        // Auto add owners.
        TRUE,
      ],
      [
        // Don't auto add owners.
        FALSE,
      ],
    ];
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc