You are here

public function OgDeleteOrphansTest::testDeleteOrphans in Organic groups 8

Tests that orphaned group content is deleted when the group is deleted.

@dataProvider ogDeleteOrphansPluginProvider

Parameters

string $plugin_id: The machine name of the plugin under test.

bool $run_cron: Whether or not cron jobs should be run as part of the test.

bool $run_shutdown_functions: Whether or not shutdown functions should be run as part of the test.

bool $asynchronous: Whether or not the actual deletion of the orphans happens in an asynchronous operation (e.g. pressing the button that launches the batch process).

string $queue_id: The ID of the queue that is used by the plugin under test.

File

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

Class

OgDeleteOrphansTest
Tests deletion of orphaned group content and memberships.

Namespace

Drupal\Tests\og\Kernel

Code

public function testDeleteOrphans($plugin_id, $run_cron, $run_shutdown_functions, $asynchronous, $queue_id) {

  // Turn on deletion of orphans in the configuration and configure the chosen
  // plugin.
  $this
    ->config('og.settings')
    ->set('delete_orphans', TRUE)
    ->set('delete_orphans_plugin_id', $plugin_id)
    ->save();

  // Check that the queue is initially empty.
  $this
    ->assertQueueCount($queue_id, 0);

  // Check that the group owner has initially been subscribed to the group.
  $this
    ->assertUserMembershipCount(1);

  // Delete the group.
  $this->group
    ->delete();

  // Check that 2 orphans are queued for asynchronous processing: 1 group
  // content item and 1 user membership.
  if ($asynchronous) {
    $this
      ->assertQueueCount($queue_id, 2);
  }

  // Run cron jobs if needed.
  if ($run_cron) {
    $this->container
      ->get('cron')
      ->run();
  }

  // Run shutdown functions if needed.
  if ($run_shutdown_functions) {
    _drupal_shutdown_function();
  }

  // Simulate the initiation of the queue process by an asynchronous operation
  // (such as pressing the button that starts a batch operation).
  if ($asynchronous) {
    $this
      ->process($queue_id, $plugin_id);
  }

  // Verify the group content is deleted.
  $this
    ->assertNull(Node::load($this->groupContent
    ->id()), 'The orphaned node is deleted.');

  // Verify that the user membership is now deleted.
  $this
    ->assertUserMembershipCount(0);

  // Check that the queue is now empty.
  $this
    ->assertQueueCount($queue_id, 0);
}