You are here

protected function OgDeleteOrphansTest::setUp in Organic groups 8

Overrides KernelTestBase::setUp

File

tests/src/Kernel/OgDeleteOrphansTest.php, line 57

Class

OgDeleteOrphansTest
Tests deletion of orphaned group content and memberships.

Namespace

Drupal\Tests\og\Kernel

Code

protected function setUp() : void {
  parent::setUp();

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

  /** @var \Drupal\og\OgDeleteOrphansPluginManager $plugin_manager */
  $plugin_manager = \Drupal::service('plugin.manager.og.delete_orphans');
  $this->ogDeleteOrphansPluginManager = $plugin_manager;

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

  // Create a group content entity type.
  $group_content_bundle = mb_strtolower($this
    ->randomMachineName());
  NodeType::create([
    'type' => $group_content_bundle,
    'name' => $this
      ->randomString(),
  ])
    ->save();
  Og::createField(OgGroupAudienceHelperInterface::DEFAULT_FIELD, 'node', $group_content_bundle);

  // Create group admin user.
  $group_admin = User::create([
    'name' => $this
      ->randomString(),
  ]);
  $group_admin
    ->save();

  // Create a group.
  $this->group = Node::create([
    'title' => $this
      ->randomString(),
    'type' => $group_bundle,
    'uid' => $group_admin
      ->id(),
  ]);
  $this->group
    ->save();

  // Create a group content item.
  $this->groupContent = Node::create([
    'title' => $this
      ->randomString(),
    'type' => $group_content_bundle,
    OgGroupAudienceHelperInterface::DEFAULT_FIELD => [
      [
        'target_id' => $this->group
          ->id(),
      ],
    ],
  ]);
  $this->groupContent
    ->save();
}