You are here

public function OptionalDependencyCalculationTest::testDependencyCalculation in Acquia Content Hub 8.2

Tests there are no dependencies calculated when depcalc flag is false.

Throws

\Drupal\Core\Entity\EntityStorageException

File

tests/src/Kernel/OptionalDependencyCalculationTest.php, line 72

Class

OptionalDependencyCalculationTest
Tests Optional Dependency Calculation for given entity.

Namespace

Drupal\Tests\acquia_contenthub\Kernel

Code

public function testDependencyCalculation() : void {
  $bundle = 'dummy_content_type';
  $contentType = $this
    ->createContentType([
    'type' => $bundle,
    'name' => 'Dummy content type',
  ]);
  $contentType
    ->save();
  $node = $this
    ->createNode([
    'type' => $bundle,
  ]);
  $node
    ->save();
  $queue = \Drupal::queue('acquia_contenthub_publish_export');

  // Check whether any queue item inserted on this node save
  // has default calculate dependency flag.
  // We are not going to process this item,
  // we are just going to examine it.
  $queue_item = $queue
    ->claimItem();

  // This assertion holds that default CH workflow is not affected.
  $this
    ->assertFalse(property_exists($queue_item->data, 'calculate_dependencies'));

  // Delete the queue item for next assertion.
  $queue
    ->deleteQueue();

  // Create new queue item.
  $event = new ContentHubEntityEligibilityEvent($node, 'insert');

  // This will be done by Lift Publisher event subscriber
  // and is not a Content Hub use case.
  $event
    ->setCalculateDependencies(FALSE);
  $item = new \stdClass();
  $item->type = $node
    ->getEntityTypeId();
  $item->uuid = $node
    ->uuid();
  if ($event
    ->getCalculateDependencies() === FALSE) {
    $item->calculate_dependencies = FALSE;
  }
  $queue
    ->createItem($item);
  $queue_item = $queue
    ->claimItem();
  $this
    ->assertFalse($queue_item->data->calculate_dependencies);
  $entities = [];

  // Get CDF without dependencies.
  $cdf_objects = $this->commonActionService
    ->getEntityCdf($node, $entities, TRUE, FALSE);
  $this
    ->assertEqual(1, count($cdf_objects), 'There is only 1 CDF object.');

  /** @var \Acquia\ContentHubClient\CDF\CDFObject $cdf */
  $cdf = reset($cdf_objects);

  // Only cdf object is for given node.
  $this
    ->assertEqual($node
    ->uuid(), $cdf
    ->getUuid());
  $this
    ->assertEmpty($cdf
    ->getDependencies());
  $this
    ->assertEmpty($cdf
    ->getModuleDependencies());

  // Get the CDF with dependencies.
  $entities = [];
  $cdf_objects = $this->commonActionService
    ->getEntityCdf($node, $entities);
  $this
    ->assertGreaterThan(1, count($cdf_objects));
  foreach ($cdf_objects as $cdf) {

    // Only assert for the main node as we don't know
    // if other entities in CDF will have the dependencies or not.
    if ($node
      ->uuid() === $cdf
      ->getUuid()) {
      $this
        ->assertNotEmpty($cdf
        ->getDependencies());
    }

    // All the entities in CDF will have at least one module dependency.
    $this
      ->assertNotEmpty($cdf
      ->getModuleDependencies());
  }
}