You are here

public function AuthenticationOAuthTest::testImport in Entity Share 8.3

Test that correct entities are created with different authentications.

File

modules/entity_share_client/tests/src/Functional/AuthenticationOAuthTest.php, line 186

Class

AuthenticationOAuthTest
Functional test class for import with "OAuth" authorization.

Namespace

Drupal\Tests\entity_share_client\Functional

Code

public function testImport() {

  // 1. Test content creation as administrative
  // user: both published and unpublished nodes should be created.
  // In this run we are also testing the access to private physical files.
  // First, assert that files didn't exist before import.
  foreach (static::$filesData as $file_data) {
    $this
      ->assertFalse(file_exists($file_data['uri']), 'The physical file ' . $file_data['filename'] . ' has been deleted.');
  }

  // Pull channel and test that all nodes and file entities are there.
  $this
    ->pullChannel('node_es_test_en');
  $this
    ->checkCreatedEntities();

  // Some stronger assertions for the uploaded private file.
  foreach (static::$filesData as $file_definition) {
    $this
      ->assertTrue(file_exists($file_definition['uri']), 'The physical file ' . $file_definition['filename'] . ' has been pulled and recreated.');
    $this
      ->assertEquals(file_get_contents($file_definition['uri']), $file_definition['file_content'], 'The content of physical file ' . $file_definition['filename'] . ' is correct.');
  }

  // 2. Test as a non-administrative user who can't access unpublished nodes.
  // Change the remote so that is uses the channel user's credentials.
  $plugin = $this
    ->createAuthenticationPlugin($this->channelUser, $this->remote);
  $this->remote
    ->mergePluginConfig($plugin);
  $this->remote
    ->save();

  // Delete all "client" entities created after the first import.
  $this
    ->resetImportedContent();

  // Also clean up all uploaded files.
  foreach (static::$filesData as $file_data) {
    $this->fileSystem
      ->delete($file_data['uri']);
  }

  // There is no need to test the physical files anymore, so we will remove
  // them from the entity array.
  unset($this->entitiesData['file']);
  unset($this->entitiesData['node']['en']['es_test_node_import_published']['field_es_test_file']);

  // Since the remote ID remains the same, we need to reset some of
  // remote manager's cached values.
  $this
    ->resetRemoteCaches();

  // Prepare the "server" content again.
  $this
    ->prepareContent();

  // Get channel info so that individual channels can be pulled next.
  $channel_infos = $this->remoteManager
    ->getChannelsInfos($this->remote);

  // Re-import data from JSON:API.
  $this
    ->reimportChannel($channel_infos);

  // Assertions.
  $entity_storage = $this->entityTypeManager
    ->getStorage('node');
  $published = $entity_storage
    ->loadByProperties([
    'uuid' => 'es_test_node_import_published',
  ]);
  $this
    ->assertEquals(count($published), 1, 'The published node was imported.');
  $not_published = $entity_storage
    ->loadByProperties([
    'uuid' => 'es_test_node_import_not_published',
  ]);
  $this
    ->assertEquals(count($not_published), 0, 'The unpublished node was not imported.');

  // 3. Test as non-administrative user, but with credentials stored using
  // Key module.
  $this
    ->setupAuthorizationPluginWithKey($this->channelUser);
  $this
    ->resetImportedContent();
  $this
    ->resetRemoteCaches();
  $this
    ->prepareContent();
  $this
    ->reimportChannel($channel_infos);

  // Assertions.
  $entity_storage = $this->entityTypeManager
    ->getStorage('node');
  $published = $entity_storage
    ->loadByProperties([
    'uuid' => 'es_test_node_import_published',
  ]);
  $this
    ->assertEquals(count($published), 1, 'The published node was imported.');
  $not_published = $entity_storage
    ->loadByProperties([
    'uuid' => 'es_test_node_import_not_published',
  ]);
  $this
    ->assertEquals(count($not_published), 0, 'The unpublished node was not imported.');
}