function CrudTest::testRemoteMappings

File

tests/src/Kernel/CrudTest.php, line 130

Class

CrudTest
Basic crud operations for jobs and translators

Namespace

Drupal\Tests\tmgmt\Kernel

Code

function testRemoteMappings() {
  $data_key = '5][test_source][type';
  $translator = $this
    ->createTranslator();
  $job = $this
    ->createJob();
  $job->translator = $translator
    ->id();
  $job
    ->save();
  $item1 = $job
    ->addItem('test_source', 'type', 5);
  $item2 = $job
    ->addItem('test_source', 'type', 4);
  $mapping_data = array(
    'remote_identifier_2' => 'id12',
    'remote_identifier_3' => 'id13',
    'amount' => 1043,
    'currency' => 'EUR',
  );
  $result = $item1
    ->addRemoteMapping($data_key, 'id11', $mapping_data);
  $this
    ->assertEquals(SAVED_NEW, $result);
  $job_mappings = $job
    ->getRemoteMappings();
  $item_mappings = $item1
    ->getRemoteMappings();
  $job_mapping = array_shift($job_mappings);
  $item_mapping = array_shift($item_mappings);
  $_job = $job_mapping
    ->getJob();
  $this
    ->assertEquals($job
    ->id(), $_job
    ->id());
  $_job = $item_mapping
    ->getJob();
  $this
    ->assertEquals($job
    ->id(), $_job
    ->id());
  $_item1 = $item_mapping
    ->getJobItem();
  $this
    ->assertEquals($item1
    ->id(), $_item1
    ->id());
  $remote_mappings = RemoteMapping::loadByRemoteIdentifier('id11', 'id12', 'id13');
  $remote_mapping = array_shift($remote_mappings);
  $this
    ->assertEquals($item1
    ->id(), $remote_mapping
    ->id());
  $this
    ->assertEquals($mapping_data['amount'], $remote_mapping
    ->getAmount());
  $this
    ->assertEquals($mapping_data['currency'], $remote_mapping
    ->getCurrency());
  $this
    ->assertCount(1, RemoteMapping::loadByRemoteIdentifier('id11'));
  $this
    ->assertCount(0, RemoteMapping::loadByRemoteIdentifier('id11', ''));
  $this
    ->assertCount(0, RemoteMapping::loadByRemoteIdentifier('id11', NULL, ''));
  $this
    ->assertCount(1, RemoteMapping::loadByRemoteIdentifier(NULL, NULL, 'id13'));
  $this
    ->assertEquals('id11', $remote_mapping
    ->getRemoteIdentifier1());
  $this
    ->assertEquals('id12', $remote_mapping
    ->getRemoteIdentifier2());
  $this
    ->assertEquals('id13', $remote_mapping
    ->getRemoteIdentifier3());

  // Test remote data.
  $item_mapping
    ->addRemoteData('test_data', 'test_value');
  $item_mapping
    ->save();
  $item_mapping = RemoteMapping::load($item_mapping
    ->id());
  $this
    ->assertEquals('test_value', $item_mapping
    ->getRemoteData('test_data'));

  // Add mapping to the other job item as well.
  $item2
    ->addRemoteMapping($data_key, 'id21', array(
    'remote_identifier_2' => 'id22',
    'remote_identifier_3' => 'id23',
  ));

  // Test deleting.
  // Delete item1.
  $item1
    ->delete();

  // Test if mapping for item1 has been removed as well.
  $this
    ->assertCount(0, RemoteMapping::loadByLocalData(NULL, $item1
    ->id()));

  // We still should have mapping for item2.
  $this
    ->assertCount(1, RemoteMapping::loadByLocalData(NULL, $item2
    ->id()));

  // Now delete the job and see if remaining mappings were removed as well.
  $job
    ->delete();
  $this
    ->assertCount(0, RemoteMapping::loadByLocalData(NULL, $item2
    ->id()));
}