You are here

class BynderApiTest in Bynder 8.3

Same name and namespace in other branches
  1. 8 tests/modules/bynder_test_module/src/BynderApiTest.php \Drupal\bynder_test_module\BynderApiTest
  2. 8.2 tests/modules/bynder_test_module/src/BynderApiTest.php \Drupal\bynder_test_module\BynderApiTest
  3. 4.0.x tests/modules/bynder_test_module/src/BynderApiTest.php \Drupal\bynder_test_module\BynderApiTest

Bynder API test service.

@package Drupal\bynder

Hierarchy

Expanded class hierarchy of BynderApiTest

1 file declares its use of BynderApiTest
UsageTest.php in tests/src/Functional/UsageTest.php

File

tests/modules/bynder_test_module/src/BynderApiTest.php, line 14

Namespace

Drupal\bynder_test_module
View source
class BynderApiTest extends BynderApi {
  const BYNDER_INTEGRATION_ID = 'a7129512-c6e3-47a3-be40-9a66503e82ed';
  public function getIntegrationId() {
    return self::BYNDER_INTEGRATION_ID;
  }

  /**
   * Returns the defined value.
   */
  public function getBrands() {
    if ($brands = $this->state
      ->get('bynder.bynder_test_brands')) {
      return $brands;
    }
    throw new RequestException('Test', new Request('test', 'test'));
  }

  /**
   * Returns value set in state.
   *
   * @param string $media_uuid
   *   The media UUID.
   *
   * @return mixed
   *   Returns what is set in the state.
   */
  public function getMediaInfo($media_uuid) {
    if ($media_info = $this->state
      ->get('bynder.bynder_test_media_info')) {
      return $media_info;
    }
    if ($media_list = $this->state
      ->get('bynder.bynder_test_media_list')) {
      foreach ($media_list['media'] as $media_info) {
        if ($media_uuid == $media_info['id']) {
          return $media_info;
        }
      }
    }
  }

  /**
   * Returns value set in state.
   *
   * @param array $query
   *   Search query.
   *
   * @return mixed
   *   Returns what is set in the state.
   *
   * @throws \Exception
   *   Connection fails.
   */
  public function getMediaList(array $query) {
    if (!($media_list = $this->state
      ->get('bynder.bynder_test_media_list'))) {
      throw new \Exception();
    }

    // Filter on keyword.
    if (!empty($query['keyword'])) {
      foreach ($media_list['media'] as $key => $media) {
        if ($query['keyword'] != $media['keyword']) {
          unset($media_list['media'][$key]);
        }
      }
    }

    // Filter on meta-property options.
    $metaproperties = array_filter($query, function ($key) {
      return strpos($key, 'property_') === 0;
    }, ARRAY_FILTER_USE_KEY);
    if ($metaproperties) {
      foreach ($metaproperties as $metaproperty => $options) {
        $options = explode(',', $options);
        foreach ($media_list['media'] as $key => $media) {
          if (empty(array_intersect($options, $media[$metaproperty]))) {
            unset($media_list['media'][$key]);
          }
        }
      }
    }

    // Filter on tags.
    if (!empty($query['tags'])) {
      foreach ($media_list['media'] as $key => $media) {
        if (!in_array($query['tags'], $media['tags'])) {
          unset($media_list['media'][$key]);
        }
      }
    }
    return $media_list;
  }

  /**
   * Returns value set in state.
   */
  public function getMetaproperties() {
    if (!$this->state
      ->get('bynder.bynder_test_metaproperties')) {
      throw new \Exception();
    }
    return $this->state
      ->get('bynder.bynder_test_metaproperties');
  }

  /**
   * Returns value set in state.
   */
  public function hasAccessToken() {
    if (!$this->state
      ->get('bynder.bynder_test_access_token')) {
      return FALSE;
    }
    return TRUE;
  }

  /**
   * Returns value set in state.
   */
  public function getTags($query = []) {
    if (!$this->state
      ->get('bynder.bynder_test_tags')) {
      throw new \Exception();
    }
    return $this->state
      ->get('bynder.bynder_test_tags');
  }

  /**
   * Returns value set in state.
   */
  public function getDerivatives() {
    if (!is_array($this->state
      ->get('bynder.bynder_test_derivatives'))) {
      throw new \Exception();
    }
    return $this->state
      ->get('bynder.bynder_test_derivatives');
  }

  /**
   * Returns value set in state.
   *
   * @param string $media_uuid
   *   The media UUID.
   *
   * @return array
   *   Returns array of expected values.
   */
  public function uploadFileAsync($media_uuid) {
    return [
      'success' => $this->state
        ->get('bynder.bynder_test_upload_success'),
      'mediaid' => $this->state
        ->get('bynder.bynder_test_upload_mediaid'),
    ];
  }

  /**
   * Simulate deleting media and returns void.
   *
   * @param string $media_uuid
   *   The media UUID.
   */
  public function deleteMedia($media_uuid) {
  }

  /**
   * Simulate upload permissions check.
   */
  public function hasUploadPermissions() {
    return 'MEDIAUPLOAD';
  }

  /**
   * Simulate cache update.
   */
  public function updateCachedData() {
  }

  /**
   * Sets values in state.
   *
   * @param string $integration_id
   *   The Drupal 8 integration id.
   * @param string $asset_id
   *   The Bynder media id.
   * @param string $timestamp
   *   Current timestamp.
   * @param string $location
   *   Url location for entity that references this asset.
   * @param string $additional
   *   Additional media info.
   */
  public function addAssetUsage($asset_id, $usage_url, $creation_date, $additional_info = NULL) {
    $values = [
      'integration_id' => $this
        ->getIntegrationId(),
      'asset_id' => $asset_id,
      'timestamp' => $creation_date,
      'location' => $usage_url,
      'additional' => $additional_info,
    ];
    $this->state
      ->set('bynder.bynder_add_usage', $values);
  }

  /**
   * Sets values in state.
   *
   * @param string $integration_id
   *   The Drupal 8 integration id.
   * @param string $asset_id
   *   The Bynder media id.
   * @param string $location
   *   Url location for entity that references this asset.
   */
  public function removeAssetUsage($asset_id, $usage_url = NULL) {
    $values = [
      'integration_id' => $this
        ->getIntegrationId(),
      'asset_id' => $asset_id,
      'location' => $usage_url,
    ];
    $this->state
      ->set('bynder.bynder_delete_usage', $values);
  }

  /**
   * {@inheritdoc}
   */
  public function getAssetUsages($asset_id) {
    return $this->state
      ->get('bynder.bynder_add_usage');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BynderApi::$bynderApi protected property Bynder Api instance.
BynderApi::$bynderConfig protected property Bynder configuration.
BynderApi::$cache protected property The cache service.
BynderApi::$configFactory protected property The configuration factory.
BynderApi::$loggerFactory protected property The logger factory.
BynderApi::$session protected property The active session.
BynderApi::$state protected property The state service.
BynderApi::$time protected property The time service.
BynderApi::AUTO_UPDATED_TAGS_QUERIES constant List of getTags queries that are automatically updated in crons.
BynderApi::CACHED_CALLS constant List of API calls that should be cache with their cache keys as values.
BynderApi::CID_DERIVATIVES constant Cache ID used to store the derivatives information.
BynderApi::CID_METAPROPERTIES constant Cache ID used to store the metaproperties information.
BynderApi::CID_TAGS constant Cache ID used to store the tags information.
BynderApi::finishOAuthTokenRetrieval public function Finishes the access token retrieval after the user has been redirected. Overrides BynderApiInterface::finishOAuthTokenRetrieval
BynderApi::getAssetBankManager public function Gets an instance of the asset bank manager to use for DAM queries. Overrides BynderApiInterface::getAssetBankManager
BynderApi::initiateOAuthTokenRetrieval public function Initiates the access token retrieval. Overrides BynderApiInterface::initiateOAuthTokenRetrieval
BynderApi::setBynderConfiguration public function Sets the Bynder configuration. Overrides BynderApiInterface::setBynderConfiguration
BynderApi::__call public function
BynderApi::__construct public function BynderApi constructor.
BynderApiTest::addAssetUsage public function Sets values in state. Overrides BynderApi::addAssetUsage
BynderApiTest::BYNDER_INTEGRATION_ID constant The Bynder integration ID. Overrides BynderApi::BYNDER_INTEGRATION_ID
BynderApiTest::deleteMedia public function Simulate deleting media and returns void.
BynderApiTest::getAssetUsages public function Retrieves all asset usage for a specific asset. Overrides BynderApi::getAssetUsages
BynderApiTest::getBrands public function Returns the defined value.
BynderApiTest::getDerivatives public function Returns value set in state.
BynderApiTest::getIntegrationId public function Overrides BynderApi::getIntegrationId
BynderApiTest::getMediaInfo public function Returns value set in state.
BynderApiTest::getMediaList public function Returns value set in state.
BynderApiTest::getMetaproperties public function Returns value set in state.
BynderApiTest::getTags public function Returns value set in state. Overrides BynderApi::getTags
BynderApiTest::hasAccessToken public function Returns value set in state. Overrides BynderApi::hasAccessToken
BynderApiTest::hasUploadPermissions public function Simulate upload permissions check. Overrides BynderApi::hasUploadPermissions
BynderApiTest::removeAssetUsage public function Sets values in state. Overrides BynderApi::removeAssetUsage
BynderApiTest::updateCachedData public function Simulate cache update. Overrides BynderApi::updateCachedData
BynderApiTest::uploadFileAsync public function Returns value set in state.