You are here

function CrudTest::testRemoteMappings in Translation Management Tool 8

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
    ->assertEqual($result, SAVED_NEW);
  $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
    ->assertEqual($job
    ->id(), $_job
    ->id());
  $_job = $item_mapping
    ->getJob();
  $this
    ->assertEqual($job
    ->id(), $_job
    ->id());
  $_item1 = $item_mapping
    ->getJobItem();
  $this
    ->assertEqual($item1
    ->id(), $_item1
    ->id());
  $remote_mappings = RemoteMapping::loadByRemoteIdentifier('id11', 'id12', 'id13');
  $remote_mapping = array_shift($remote_mappings);
  $this
    ->assertEqual($remote_mapping
    ->id(), $item1
    ->id());
  $this
    ->assertEqual($remote_mapping
    ->getAmount(), $mapping_data['amount']);
  $this
    ->assertEqual($remote_mapping
    ->getCurrency(), $mapping_data['currency']);
  $this
    ->assertEqual(count(RemoteMapping::loadByRemoteIdentifier('id11')), 1);
  $this
    ->assertEqual(count(RemoteMapping::loadByRemoteIdentifier('id11', '')), 0);
  $this
    ->assertEqual(count(RemoteMapping::loadByRemoteIdentifier('id11', NULL, '')), 0);
  $this
    ->assertEqual(count(RemoteMapping::loadByRemoteIdentifier(NULL, NULL, 'id13')), 1);
  $this
    ->assertEqual($remote_mapping
    ->getRemoteIdentifier1(), 'id11');
  $this
    ->assertEqual($remote_mapping
    ->getRemoteIdentifier2(), 'id12');
  $this
    ->assertEqual($remote_mapping
    ->getRemoteIdentifier3(), 'id13');

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

  // 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
    ->assertEqual(count(RemoteMapping::loadByLocalData(NULL, $item1
    ->id())), 0);

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

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