You are here

public function LingotekUnitTest::testUploadDocument in Lingotek Translation 3.5.x

Same name and namespace in other branches
  1. 8 tests/src/Unit/LingotekUnitTest.php \Drupal\Tests\lingotek\Unit\LingotekUnitTest::testUploadDocument()
  2. 8.2 tests/src/Unit/LingotekUnitTest.php \Drupal\Tests\lingotek\Unit\LingotekUnitTest::testUploadDocument()
  3. 4.0.x tests/src/Unit/LingotekUnitTest.php \Drupal\Tests\lingotek\Unit\LingotekUnitTest::testUploadDocument()
  4. 3.0.x tests/src/Unit/LingotekUnitTest.php \Drupal\Tests\lingotek\Unit\LingotekUnitTest::testUploadDocument()
  5. 3.1.x tests/src/Unit/LingotekUnitTest.php \Drupal\Tests\lingotek\Unit\LingotekUnitTest::testUploadDocument()
  6. 3.2.x tests/src/Unit/LingotekUnitTest.php \Drupal\Tests\lingotek\Unit\LingotekUnitTest::testUploadDocument()
  7. 3.3.x tests/src/Unit/LingotekUnitTest.php \Drupal\Tests\lingotek\Unit\LingotekUnitTest::testUploadDocument()
  8. 3.4.x tests/src/Unit/LingotekUnitTest.php \Drupal\Tests\lingotek\Unit\LingotekUnitTest::testUploadDocument()
  9. 3.6.x tests/src/Unit/LingotekUnitTest.php \Drupal\Tests\lingotek\Unit\LingotekUnitTest::testUploadDocument()
  10. 3.7.x tests/src/Unit/LingotekUnitTest.php \Drupal\Tests\lingotek\Unit\LingotekUnitTest::testUploadDocument()
  11. 3.8.x tests/src/Unit/LingotekUnitTest.php \Drupal\Tests\lingotek\Unit\LingotekUnitTest::testUploadDocument()

@covers ::uploadDocument

File

tests/src/Unit/LingotekUnitTest.php, line 424

Class

LingotekUnitTest
@coversDefaultClass \Drupal\lingotek\Lingotek @group lingotek @preserveGlobalState disabled

Namespace

Drupal\Tests\lingotek\Unit

Code

public function testUploadDocument() {
  $response = $this
    ->getMockBuilder(ResponseInterface::class)
    ->disableOriginalConstructor()
    ->getMock();
  $response
    ->expects($this
    ->any())
    ->method('getStatusCode')
    ->willReturn(Response::HTTP_ACCEPTED);
  $response
    ->expects($this
    ->any())
    ->method('getBody')
    ->willReturn(json_encode([
    'properties' => [
      'id' => 'my-document-id',
    ],
  ]));
  $this->lingotekFilterManager
    ->expects($this
    ->any())
    ->method('getFilterId')
    ->willReturn('4f91482b-5aa1-4a4a-a43f-712af7b39625');
  $this->lingotekFilterManager
    ->expects($this
    ->any())
    ->method('getSubfilterId')
    ->willReturn('0e79f34d-f27b-4a0c-880e-cd9181a5d265');
  $this->config
    ->expects($this
    ->any())
    ->method('get')
    ->will($this
    ->returnValueMap([
    [
      'default.project',
      'default_project',
    ],
    [
      'default.vault',
      'default_vault',
    ],
    [
      'default.workflow',
      'default_workflow',
    ],
  ]));

  // Vault id has the original value.
  $this->api
    ->expects($this
    ->at(0))
    ->method('addDocument')
    ->with([
    'title' => 'title',
    'content' => '"content"',
    'locale_code' => 'es',
    'format' => 'JSON',
    'project_id' => 'my_test_project',
    'fprm_subfilter_id' => '0e79f34d-f27b-4a0c-880e-cd9181a5d265',
    'fprm_id' => '4f91482b-5aa1-4a4a-a43f-712af7b39625',
    'vault_id' => 'my_test_vault',
    'external_application_id' => 'e39e24c7-6c69-4126-946d-cf8fbff38ef0',
  ])
    ->will($this
    ->returnValue($response));

  // Vault id has changed.
  $this->api
    ->expects($this
    ->at(1))
    ->method('addDocument')
    ->with([
    'title' => 'title',
    'content' => '"content"',
    'locale_code' => 'es',
    'format' => 'JSON',
    'project_id' => 'another_test_project',
    'fprm_subfilter_id' => '0e79f34d-f27b-4a0c-880e-cd9181a5d265',
    'fprm_id' => '4f91482b-5aa1-4a4a-a43f-712af7b39625',
    'vault_id' => 'another_test_vault',
    'external_application_id' => 'e39e24c7-6c69-4126-946d-cf8fbff38ef0',
  ])
    ->will($this
    ->returnValue($response));

  // If there is a profile with default vault, it must be replaced.
  $this->api
    ->expects($this
    ->at(2))
    ->method('addDocument')
    ->with([
    'title' => 'title',
    'content' => '"content"',
    'locale_code' => 'es',
    'format' => 'JSON',
    'project_id' => 'default_project',
    'fprm_subfilter_id' => '0e79f34d-f27b-4a0c-880e-cd9181a5d265',
    'fprm_id' => '4f91482b-5aa1-4a4a-a43f-712af7b39625',
    'vault_id' => 'default_vault',
    'external_application_id' => 'e39e24c7-6c69-4126-946d-cf8fbff38ef0',
  ])
    ->will($this
    ->returnValue($response));

  // If there is no profile, vault should not be included.
  $this->api
    ->expects($this
    ->at(3))
    ->method('addDocument')
    ->with([
    'title' => 'title',
    'content' => '"content"',
    'locale_code' => 'es',
    'fprm_subfilter_id' => '0e79f34d-f27b-4a0c-880e-cd9181a5d265',
    'fprm_id' => '4f91482b-5aa1-4a4a-a43f-712af7b39625',
    'format' => 'JSON',
    'project_id' => 'default_project',
    'external_application_id' => 'e39e24c7-6c69-4126-946d-cf8fbff38ef0',
  ])
    ->will($this
    ->returnValue($response));

  // If there is an url, it should be included.
  $this->api
    ->expects($this
    ->at(4))
    ->method('addDocument')
    ->with([
    'title' => 'title',
    'content' => '"content"',
    'locale_code' => 'es',
    'fprm_subfilter_id' => '0e79f34d-f27b-4a0c-880e-cd9181a5d265',
    'fprm_id' => '4f91482b-5aa1-4a4a-a43f-712af7b39625',
    'format' => 'JSON',
    'project_id' => 'default_project',
    'external_url' => 'http://example.com/node/1',
    'external_application_id' => 'e39e24c7-6c69-4126-946d-cf8fbff38ef0',
  ])
    ->will($this
    ->returnValue($response));

  // If there is a profile using the project default workflow template vault,
  // vault should not be specified.
  $this->api
    ->expects($this
    ->at(5))
    ->method('addDocument')
    ->with([
    'title' => 'title',
    'content' => '"content"',
    'locale_code' => 'es',
    'fprm_subfilter_id' => '0e79f34d-f27b-4a0c-880e-cd9181a5d265',
    'fprm_id' => '4f91482b-5aa1-4a4a-a43f-712af7b39625',
    'format' => 'JSON',
    'project_id' => 'default_project',
    'external_application_id' => 'e39e24c7-6c69-4126-946d-cf8fbff38ef0',
  ])
    ->will($this
    ->returnValue($response));

  // We upload with array of content.
  $this->api
    ->expects($this
    ->at(6))
    ->method('addDocument')
    ->with([
    'title' => 'title',
    'content' => '{"content":"wedgiePlatypus"}',
    'locale_code' => 'es',
    'format' => 'JSON',
    'project_id' => 'test_project',
    'fprm_subfilter_id' => '0e79f34d-f27b-4a0c-880e-cd9181a5d265',
    'fprm_id' => '4f91482b-5aa1-4a4a-a43f-712af7b39625',
    'vault_id' => 'test_vault',
    'external_application_id' => 'e39e24c7-6c69-4126-946d-cf8fbff38ef0',
  ])
    ->will($this
    ->returnValue($response));

  // We upload with a job ID.
  $this->api
    ->expects($this
    ->at(7))
    ->method('addDocument')
    ->with([
    'title' => 'title',
    'content' => '{"content":"wedgiePlatypus"}',
    'locale_code' => 'es',
    'format' => 'JSON',
    'project_id' => 'test_project',
    'fprm_subfilter_id' => '0e79f34d-f27b-4a0c-880e-cd9181a5d265',
    'fprm_id' => '4f91482b-5aa1-4a4a-a43f-712af7b39625',
    'vault_id' => 'test_vault',
    'job_id' => 'my_job_id',
    'external_application_id' => 'e39e24c7-6c69-4126-946d-cf8fbff38ef0',
  ])
    ->will($this
    ->returnValue($response));

  // Default workflow
  $this->api
    ->expects($this
    ->at(8))
    ->method('addDocument')
    ->with([
    'title' => 'title',
    'content' => '"content"',
    'locale_code' => 'es',
    'format' => 'JSON',
    'project_id' => 'default_project',
    'fprm_subfilter_id' => '0e79f34d-f27b-4a0c-880e-cd9181a5d265',
    'fprm_id' => '4f91482b-5aa1-4a4a-a43f-712af7b39625',
    'vault_id' => 'default_vault',
    'external_application_id' => 'e39e24c7-6c69-4126-946d-cf8fbff38ef0',
  ])
    ->will($this
    ->returnValue($response));

  // Project default workflow should not include translation_workflow_id
  $this->api
    ->expects($this
    ->at(9))
    ->method('addDocument')
    ->with([
    'title' => 'title',
    'content' => '"content"',
    'locale_code' => 'es',
    'format' => 'JSON',
    'project_id' => 'default_project',
    'fprm_subfilter_id' => '0e79f34d-f27b-4a0c-880e-cd9181a5d265',
    'fprm_id' => '4f91482b-5aa1-4a4a-a43f-712af7b39625',
    'vault_id' => 'default_vault',
    'external_application_id' => 'e39e24c7-6c69-4126-946d-cf8fbff38ef0',
  ])
    ->will($this
    ->returnValue($response));
  $this->api
    ->expects($this
    ->at(10))
    ->method('addDocument')
    ->with([
    'title' => 'title',
    'content' => '{"content":"wedgiePlatypus"}',
    'locale_code' => 'en_US',
    'format' => 'JSON',
    'project_id' => 'test_project',
    'fprm_subfilter_id' => '0e79f34d-f27b-4a0c-880e-cd9181a5d265',
    'fprm_id' => '4f91482b-5aa1-4a4a-a43f-712af7b39625',
    'vault_id' => 'test_vault',
    'job_id' => 'my_job_id',
    'external_application_id' => 'e39e24c7-6c69-4126-946d-cf8fbff38ef0',
    'translation_locale_code' => [
      'es_ES',
      'ca_ES',
      'it_IT',
    ],
    'translation_workflow_id' => [
      'es_workflow',
      'ca_workflow',
      'default_workflow',
    ],
    'translation_vault_id' => [
      'default_vault',
      'ca_vault',
      'it_vault',
    ],
  ])
    ->will($this
    ->returnValue($response));
  $this->api
    ->expects($this
    ->at(11))
    ->method('addDocument')
    ->with([
    'title' => 'title',
    'content' => '{"content":"wedgiePlatypus"}',
    'locale_code' => 'en_US',
    'format' => 'JSON',
    'project_id' => 'test_project',
    'fprm_subfilter_id' => '0e79f34d-f27b-4a0c-880e-cd9181a5d265',
    'fprm_id' => '4f91482b-5aa1-4a4a-a43f-712af7b39625',
    'vault_id' => 'test_vault',
    'job_id' => 'my_job_id',
    'external_application_id' => 'e39e24c7-6c69-4126-946d-cf8fbff38ef0',
    'translation_locale_code' => [
      'es_ES',
      'it_IT',
    ],
    'translation_workflow_id' => [
      'es_workflow',
      'default_workflow',
    ],
    'translation_vault_id' => [
      'default_vault',
      'it_vault',
    ],
  ])
    ->will($this
    ->returnValue($response));

  // We upload with a profile that has a vault and a project.
  $profile = new LingotekProfile([
    'id' => 'profile1',
    'project' => 'my_test_project',
    'vault' => 'my_test_vault',
  ], 'lingotek_profile');
  $doc_id = $this->lingotek
    ->uploadDocument('title', 'content', 'es', NULL, $profile);
  $this
    ->assertEquals('my-document-id', $doc_id);

  // We upload with a profile that has another vault and another project.
  $profile = new LingotekProfile([
    'id' => 'profile2',
    'project' => 'another_test_project',
    'vault' => 'another_test_vault',
  ], 'lingotek_profile');
  $doc_id = $this->lingotek
    ->uploadDocument('title', 'content', 'es', NULL, $profile);
  $this
    ->assertEquals('my-document-id', $doc_id);

  // We upload with a profile that has marked to use the default vault and project,
  // so must be replaced.
  $profile = new LingotekProfile([
    'id' => 'profile2',
    'project' => 'default',
    'vault' => 'default',
    'filter' => 'drupal_default',
  ], 'lingotek_profile');
  $doc_id = $this->lingotek
    ->uploadDocument('title', 'content', 'es', NULL, $profile);
  $this
    ->assertEquals('my-document-id', $doc_id);

  // We upload without a profile.
  $doc_id = $this->lingotek
    ->uploadDocument('title', 'content', 'es', NULL, NULL);
  $this
    ->assertEquals('my-document-id', $doc_id);

  // We upload without a profile, but with url.
  $doc_id = $this->lingotek
    ->uploadDocument('title', 'content', 'es', 'http://example.com/node/1', NULL);
  $this
    ->assertEquals('my-document-id', $doc_id);

  // We upload with a profile that has marked to use the project default
  // workflow template vault, so must be omitted.
  $profile = new LingotekProfile([
    'id' => 'profile2',
    'project' => 'default',
    'vault' => 'project_default',
    'filter' => '0e79f34d-f27b-4a0c-880e-cd9181a5d265',
  ], 'lingotek_profile');
  $doc_id = $this->lingotek
    ->uploadDocument('title', 'content', 'es', NULL, $profile);
  $this
    ->assertEquals('my-document-id', $doc_id);

  // We upload with content as an array.
  $profile = new LingotekProfile([
    'id' => 'profile0',
    'project' => 'test_project',
    'vault' => 'test_vault',
  ], 'lingotek_profile');
  $doc_id = $this->lingotek
    ->uploadDocument('title', [
    'content' => 'wedgiePlatypus',
  ], 'es', NULL, $profile);
  $this
    ->assertEquals('my-document-id', $doc_id);

  // We upload with a job ID.
  $doc_id = $this->lingotek
    ->uploadDocument('title', [
    'content' => 'wedgiePlatypus',
  ], 'es', NULL, $profile, 'my_job_id');
  $this
    ->assertEquals('my-document-id', $doc_id);

  // We upload with the default workflow
  $profile = new LingotekProfile([
    'id' => 'profile1',
    'project' => 'default',
    'vault' => 'default',
    'workflow' => 'default',
  ], 'lingotek_profile');
  $doc_id = $this->lingotek
    ->uploadDocument('title', 'content', 'es', NULL, $profile);
  $this
    ->assertEquals('my-document-id', $doc_id);

  // We upload with the project default workflow
  $profile = new LingotekProfile([
    'id' => 'profile1',
    'project' => 'default',
    'vault' => 'default',
    'workflow' => 'project_default',
  ], 'lingotek_profile');
  $doc_id = $this->lingotek
    ->uploadDocument('title', 'content', 'es', NULL, $profile);
  $this
    ->assertEquals('my-document-id', $doc_id);

  // We upload with a profile that specifies auto request of targets on upload.
  $english = $this
    ->createMock(ConfigurableLanguageInterface::class);
  $english
    ->expects($this
    ->any())
    ->method('getId')
    ->willReturn('en');
  $spanish = $this
    ->createMock(ConfigurableLanguageInterface::class);
  $spanish
    ->expects($this
    ->any())
    ->method('getId')
    ->willReturn('es');
  $catalan = $this
    ->createMock(ConfigurableLanguageInterface::class);
  $catalan
    ->expects($this
    ->any())
    ->method('getId')
    ->willReturn('ca');
  $italian = $this
    ->createMock(ConfigurableLanguageInterface::class);
  $italian
    ->expects($this
    ->any())
    ->method('getId')
    ->willReturn('it');
  $this->lingotekConfiguration
    ->expects($this
    ->any())
    ->method('getEnabledLanguages')
    ->willReturn([
    'en' => $english,
    'es' => $spanish,
    'ca' => $catalan,
    'it' => $italian,
  ]);
  $this->languageLocaleMapper
    ->expects($this
    ->any())
    ->method('getLocaleForLangcode')
    ->willReturnMap([
    [
      'es',
      'es_ES',
    ],
    [
      'ca',
      'ca_ES',
    ],
    [
      'en',
      'en_US',
    ],
    [
      'it',
      'it_IT',
    ],
  ]);
  $profile = new LingotekProfile([
    'id' => 'profile_with_requests',
    'project' => 'test_project',
    'vault' => 'test_vault',
    'workflow' => 'test_workflow',
    'language_overrides' => [
      'es' => [
        'overrides' => 'custom',
        'custom' => [
          'auto_request' => TRUE,
          'workflow' => 'es_workflow',
          'vault' => 'default',
        ],
      ],
      'ca' => [
        'overrides' => 'custom',
        'custom' => [
          'auto_request' => TRUE,
          'workflow' => 'ca_workflow',
          'vault' => 'ca_vault',
        ],
      ],
      'it' => [
        'overrides' => 'custom',
        'custom' => [
          'auto_request' => TRUE,
          'workflow' => 'default',
          'vault' => 'it_vault',
        ],
      ],
    ],
  ], 'lingotek_profile');
  $profile
    ->setAutomaticUpload(TRUE);
  $profile
    ->setAutomaticRequest(TRUE);
  $doc_id = $this->lingotek
    ->uploadDocument('title', [
    'content' => 'wedgiePlatypus',
  ], 'en_US', NULL, $profile, 'my_job_id');
  $this
    ->assertEquals('my-document-id', $doc_id);
  $profile = new LingotekProfile([
    'id' => 'profile_with_disabled_targets',
    'project' => 'test_project',
    'vault' => 'test_vault',
    'workflow' => 'test_workflow',
    'language_overrides' => [
      'es' => [
        'overrides' => 'custom',
        'custom' => [
          'auto_request' => TRUE,
          'workflow' => 'es_workflow',
          'vault' => 'default',
        ],
      ],
      'ca' => [
        'overrides' => 'disabled',
        'custom' => [
          'auto_request' => TRUE,
          'workflow' => 'ca_workflow',
          'vault' => 'ca_vault',
        ],
      ],
      'it' => [
        'overrides' => 'custom',
        'custom' => [
          'auto_request' => TRUE,
          'workflow' => 'default',
          'vault' => 'it_vault',
        ],
      ],
    ],
  ], 'lingotek_profile');
  $profile
    ->setAutomaticUpload(TRUE);
  $profile
    ->setAutomaticRequest(TRUE);
  $doc_id = $this->lingotek
    ->uploadDocument('title', [
    'content' => 'wedgiePlatypus',
  ], 'en_US', NULL, $profile, 'my_job_id');
  $this
    ->assertEquals('my-document-id', $doc_id);
}