You are here

class DeleteAssetsTest in Acquia Content Hub 8.2

Tests the DeleteAssets webhook event subscriber.

@group acquia_contenthub

@package Drupal\Tests\acquia_contenthub\Unit\EventSubscriber\HandleWebhook\DeleteAssets

@covers \Drupal\acquia_contenthub_subscriber\EventSubscriber\HandleWebhook\DeleteAssets

Hierarchy

Expanded class hierarchy of DeleteAssetsTest

File

tests/src/Unit/EventSubscriber/HandleWebhook/DeleteAssetsTest.php, line 28

Namespace

Drupal\Tests\acquia_contenthub\Unit\EventSubscriber\HandleWebhook
View source
class DeleteAssetsTest extends UnitTestCase {
  private const STATUS_SUCCESSFUL = 'successful';
  private const OPERATION_DELETE = 'delete';
  private const CLIENT_UUID = 'some-client-uuid';
  private const INITIATOR_UUID = 'some-initiator-uuid';
  private const EXISTING_ENTITY_UUID = 'some-existing-entity-uuid';
  private const NON_EXISTING_ENTITY_UUID = 'some-non-existing-entity-uuid';
  private const EXISTING_DISCONNECTED_ENTITY_UUID = 'some-existing-disconnected-entity-uuid';
  private const ASSET_TYPE_D8_CONTENT_ENTITY = 'drupal8_content_entity';
  private const WEBHOOK_UUID = 'webhook-uuid';

  /**
   * A Drupal entity.
   *
   * @var \Drupal\Core\Entity\Entity
   */
  private $entity;

  /**
   * The ContentHubClient Settings object.
   *
   * @var \Acquia\ContentHubClient\Settings
   */
  private $contentHubSettings;

  /**
   * The ContentHubClient.
   *
   * @var \Acquia\ContentHubClient\ContentHubClient
   */
  private $contentHubClient;

  /**
   * The ClientFactory.
   *
   * @var \Drupal\acquia_contenthub\Client\ClientFactory
   */
  private $clientFactory;

  /**
   * The Config Factory Interface.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * The HMAC Key.
   *
   * @var \Acquia\Hmac\Key
   */
  private $key;

  /**
   * The request object.
   *
   * @var \Symfony\Component\HttpFoundation\Request
   */
  private $request;

  /**
   * The SubscriberTracker.
   *
   * @var \Drupal\acquia_contenthub_subscriber\SubscriberTracker
   */
  private $tracker;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    $this->entity = $this
      ->prophesize(EntityBase::class);
    $entity = $this->entity;
    $this->contentHubSettings = $this
      ->prophesize(Settings::class);
    $this->contentHubSettings
      ->getUuid()
      ->willReturn(self::CLIENT_UUID);
    $this->contentHubSettings
      ->getWebhook("uuid")
      ->willReturn(self::WEBHOOK_UUID);
    $this->contentHubClient = $this
      ->prophesize(ContentHubClient::class);
    $this->contentHubClient
      ->getSettings()
      ->willReturn($this->contentHubSettings);
    $this->clientFactory = $this
      ->prophesize(ClientFactory::class);
    $this->clientFactory
      ->getClient()
      ->willReturn($this->contentHubClient);
    $this->configFactory = $this
      ->prophesize(ConfigFactoryInterface::class);
    $config = $this
      ->prophesize(Config::class);
    $config
      ->get(Argument::any())
      ->willReturn(TRUE);
    $this->configFactory
      ->getEditable(Argument::any())
      ->willReturn($config
      ->reveal());
    $this->key = new Key('id', 'secret');
    $this->request = $this
      ->prophesize(Request::class)
      ->reveal();
    $this->tracker = $this
      ->prophesize(SubscriberTracker::class);
    $this->tracker
      ->delete(Argument::type('string'));
    $this->tracker
      ->getEntityByRemoteIdAndHash(Argument::type('string'))
      ->will(function ($uuid) use ($entity) {
      switch (current($uuid)) {
        case self::NON_EXISTING_ENTITY_UUID:
          return NULL;
        case self::EXISTING_ENTITY_UUID:
          return $entity;
        case self::EXISTING_DISCONNECTED_ENTITY_UUID:
          return $entity;
      }
    });
    $this->tracker
      ->getStatusByUuid(Argument::type('string'))
      ->will(function ($uuid) {
      switch (current($uuid)) {
        case self::EXISTING_DISCONNECTED_ENTITY_UUID:
          return SubscriberTracker::AUTO_UPDATE_DISABLED;
        default:
          return SubscriberTracker::IMPORTED;
      }
    });
  }

  /**
   * Test an unsuccessful payload status.
   */
  public function testNonSuccessfulStatus() : void {
    $payload = [
      'status' => 'some-status',
      'crud' => self::OPERATION_DELETE,
      'initiator' => self::INITIATOR_UUID,
      'assets' => [
        [
          'type' => self::ASSET_TYPE_D8_CONTENT_ENTITY,
          'uuid' => self::EXISTING_ENTITY_UUID,
        ],
      ],
    ];
    $this
      ->triggerEvent($this
      ->createEvent($payload));
    $this
      ->assertNoOperationShouldBeDone();
  }

  /**
   * Test successful non-delete crud operations.
   */
  public function testNonDeleteCrud() : void {
    $payload = [
      'status' => self::STATUS_SUCCESSFUL,
      'crud' => 'some-funny-operation',
      'initiator' => self::INITIATOR_UUID,
      'assets' => [
        [
          'type' => self::ASSET_TYPE_D8_CONTENT_ENTITY,
          'uuid' => self::EXISTING_ENTITY_UUID,
        ],
      ],
    ];
    $this
      ->triggerEvent($this
      ->createEvent($payload));
    $this
      ->assertNoOperationShouldBeDone();
  }

  /**
   * Test a webhook that was initiated by my own site.
   */
  public function testInitiatorSameAsClient() : void {
    $payload = [
      'status' => self::STATUS_SUCCESSFUL,
      'crud' => self::OPERATION_DELETE,
      'initiator' => self::CLIENT_UUID,
      'assets' => [
        [
          'type' => self::ASSET_TYPE_D8_CONTENT_ENTITY,
          'uuid' => self::EXISTING_ENTITY_UUID,
        ],
      ],
    ];
    $this
      ->triggerEvent($this
      ->createEvent($payload));
    $this
      ->assertNoOperationShouldBeDone();
  }

  /**
   * Test empty asset payload functionality.
   */
  public function testAnEmptyAssetList() : void {
    $payload = [
      'status' => self::STATUS_SUCCESSFUL,
      'crud' => self::OPERATION_DELETE,
      'initiator' => self::INITIATOR_UUID,
      'assets' => [],
    ];
    $this
      ->triggerEvent($this
      ->createEvent($payload));
    $this
      ->assertNoOperationShouldBeDone();
  }

  /**
   * Test an unsupported asset type.
   */
  public function testUnsupportedAssetType() : void {
    $payload = [
      'status' => self::STATUS_SUCCESSFUL,
      'crud' => self::OPERATION_DELETE,
      'initiator' => self::INITIATOR_UUID,
      'assets' => [
        [
          'type' => 'some_unsupported_type',
          'uuid' => self::EXISTING_ENTITY_UUID,
        ],
      ],
    ];
    $this
      ->triggerEvent($this
      ->createEvent($payload));
    $this
      ->assertNoOperationShouldBeDone();
  }

  /**
   * Test an entity that does not exit.
   */
  public function testNonExistingEntity() : void {
    $payload = [
      'status' => self::STATUS_SUCCESSFUL,
      'crud' => self::OPERATION_DELETE,
      'initiator' => self::INITIATOR_UUID,
      'assets' => [
        [
          'type' => self::ASSET_TYPE_D8_CONTENT_ENTITY,
          'uuid' => self::NON_EXISTING_ENTITY_UUID,
        ],
      ],
    ];
    $this->tracker
      ->delete(self::NON_EXISTING_ENTITY_UUID)
      ->shouldBeCalledTimes(1);
    $this->contentHubClient
      ->deleteInterest(self::NON_EXISTING_ENTITY_UUID, self::WEBHOOK_UUID)
      ->shouldBeCalledTimes(1);
    $this->entity
      ->delete()
      ->shouldNotBeCalled();
    $this
      ->triggerEvent($this
      ->createEvent($payload));
  }

  /**
   * Test an incoming delete webhook.
   */
  public function testDeletionOfAnExistingEntity() : void {
    $payload = [
      'status' => self::STATUS_SUCCESSFUL,
      'crud' => self::OPERATION_DELETE,
      'initiator' => self::INITIATOR_UUID,
      'assets' => [
        [
          'type' => self::ASSET_TYPE_D8_CONTENT_ENTITY,
          'uuid' => self::EXISTING_ENTITY_UUID,
        ],
      ],
    ];
    $this
      ->triggerEvent($this
      ->createEvent($payload));
    $this->tracker
      ->getEntityByRemoteIdAndHash(self::EXISTING_ENTITY_UUID)
      ->shouldBeCalledTimes(1);
    $this->tracker
      ->getStatusByUuid(self::EXISTING_ENTITY_UUID)
      ->shouldBeCalledTimes(1);
    $this->entity
      ->delete()
      ->shouldBeCalledTimes(1);
  }

  /**
   * Test deletion of an existing disconnected local entity.
   */
  public function testDeletionOfAnExistingDisconnectedEntity() : void {
    $payload = [
      'status' => self::STATUS_SUCCESSFUL,
      'crud' => self::OPERATION_DELETE,
      'initiator' => self::INITIATOR_UUID,
      'assets' => [
        [
          'type' => self::ASSET_TYPE_D8_CONTENT_ENTITY,
          'uuid' => self::EXISTING_DISCONNECTED_ENTITY_UUID,
        ],
      ],
    ];
    $this->tracker
      ->delete(self::EXISTING_DISCONNECTED_ENTITY_UUID)
      ->shouldBeCalledTimes(1);
    $this
      ->triggerEvent($this
      ->createEvent($payload));
    $this->tracker
      ->getEntityByRemoteIdAndHash(self::EXISTING_DISCONNECTED_ENTITY_UUID)
      ->shouldBeCalledTimes(1);
    $this->tracker
      ->getStatusByUuid(self::EXISTING_DISCONNECTED_ENTITY_UUID)
      ->shouldBeCalledTimes(1);
    $this->entity
      ->delete()
      ->shouldNotBeCalled();
  }

  /**
   * Creates a HandleWebhookEvent.
   *
   * @param array $payload
   *   The payload.
   *
   * @return \Drupal\acquia_contenthub\Event\HandleWebhookEvent
   *   Handle webhook event.
   */
  private function createEvent(array $payload) : HandleWebhookEvent {
    return new HandleWebhookEvent($this->request, $payload, $this->key, $this->contentHubClient
      ->reveal());
  }

  /**
   * Triggers the DeleteAssets subscriber.
   *
   * @param \Drupal\acquia_contenthub\Event\HandleWebhookEvent $event
   *   Handle webhook event.
   *
   * @throws \Exception
   */
  private function triggerEvent(HandleWebhookEvent $event) : void {
    (new DeleteAssets($this->tracker
      ->reveal(), $this->configFactory
      ->reveal()))
      ->onHandleWebhook($event);
  }

  /**
   * Assert that no operation (entity lookup and deletion) would take place.
   */
  private function assertNoOperationShouldBeDone() : void {
    $this->tracker
      ->getEntityByRemoteIdAndHash()
      ->shouldNotBeCalled();
    $this->tracker
      ->delete()
      ->shouldNotBeCalled();
    $this->entity
      ->delete()
      ->shouldNotBeCalled();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DeleteAssetsTest::$clientFactory private property The ClientFactory.
DeleteAssetsTest::$configFactory protected property The Config Factory Interface.
DeleteAssetsTest::$contentHubClient private property The ContentHubClient.
DeleteAssetsTest::$contentHubSettings private property The ContentHubClient Settings object.
DeleteAssetsTest::$entity private property A Drupal entity.
DeleteAssetsTest::$key private property The HMAC Key.
DeleteAssetsTest::$request private property The request object.
DeleteAssetsTest::$tracker private property The SubscriberTracker.
DeleteAssetsTest::assertNoOperationShouldBeDone private function Assert that no operation (entity lookup and deletion) would take place.
DeleteAssetsTest::ASSET_TYPE_D8_CONTENT_ENTITY private constant
DeleteAssetsTest::CLIENT_UUID private constant
DeleteAssetsTest::createEvent private function Creates a HandleWebhookEvent.
DeleteAssetsTest::EXISTING_DISCONNECTED_ENTITY_UUID private constant
DeleteAssetsTest::EXISTING_ENTITY_UUID private constant
DeleteAssetsTest::INITIATOR_UUID private constant
DeleteAssetsTest::NON_EXISTING_ENTITY_UUID private constant
DeleteAssetsTest::OPERATION_DELETE private constant
DeleteAssetsTest::setUp public function Overrides UnitTestCase::setUp
DeleteAssetsTest::STATUS_SUCCESSFUL private constant
DeleteAssetsTest::testAnEmptyAssetList public function Test empty asset payload functionality.
DeleteAssetsTest::testDeletionOfAnExistingDisconnectedEntity public function Test deletion of an existing disconnected local entity.
DeleteAssetsTest::testDeletionOfAnExistingEntity public function Test an incoming delete webhook.
DeleteAssetsTest::testInitiatorSameAsClient public function Test a webhook that was initiated by my own site.
DeleteAssetsTest::testNonDeleteCrud public function Test successful non-delete crud operations.
DeleteAssetsTest::testNonExistingEntity public function Test an entity that does not exit.
DeleteAssetsTest::testNonSuccessfulStatus public function Test an unsuccessful payload status.
DeleteAssetsTest::testUnsupportedAssetType public function Test an unsupported asset type.
DeleteAssetsTest::triggerEvent private function Triggers the DeleteAssets subscriber.
DeleteAssetsTest::WEBHOOK_UUID private constant
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.