You are here

public function LingotekUnitTest::testUpdateDocumentBC in Lingotek Translation 3.6.x

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

@covers ::updateDocument

File

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

Class

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

Namespace

Drupal\Tests\lingotek\Unit

Code

public function testUpdateDocumentBC() {
  $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([]));
  $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',
    ],
  ]));

  // Simplest update.
  $this->api
    ->expects($this
    ->at(0))
    ->method('patchDocument')
    ->with('my_doc_id', [
    'format' => 'JSON',
    'content' => '"content"',
    'fprm_subfilter_id' => '0e79f34d-f27b-4a0c-880e-cd9181a5d265',
    'fprm_id' => '4f91482b-5aa1-4a4a-a43f-712af7b39625',
    '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(1))
    ->method('patchDocument')
    ->with('my_doc_id', [
    'format' => 'JSON',
    'content' => '"content"',
    'external_url' => 'http://example.com/node/1',
    'fprm_subfilter_id' => '0e79f34d-f27b-4a0c-880e-cd9181a5d265',
    'fprm_id' => '4f91482b-5aa1-4a4a-a43f-712af7b39625',
    'external_application_id' => 'e39e24c7-6c69-4126-946d-cf8fbff38ef0',
  ])
    ->will($this
    ->returnValue($response));

  // If there is a title, it should be included.
  $this->api
    ->expects($this
    ->at(2))
    ->method('patchDocument')
    ->with('my_doc_id', [
    'format' => 'JSON',
    'content' => '"content"',
    'title' => 'title',
    'fprm_subfilter_id' => '0e79f34d-f27b-4a0c-880e-cd9181a5d265',
    'fprm_id' => '4f91482b-5aa1-4a4a-a43f-712af7b39625',
    'external_application_id' => 'e39e24c7-6c69-4126-946d-cf8fbff38ef0',
  ])
    ->will($this
    ->returnValue($response));

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

  // The content is an array.
  $this->api
    ->expects($this
    ->at(4))
    ->method('patchDocument')
    ->with('my_doc_id', [
    'format' => 'JSON',
    'content' => '{"content":"wedgiePlatypus"}',
    'fprm_subfilter_id' => '0e79f34d-f27b-4a0c-880e-cd9181a5d265',
    'fprm_id' => '4f91482b-5aa1-4a4a-a43f-712af7b39625',
    'external_application_id' => 'e39e24c7-6c69-4126-946d-cf8fbff38ef0',
  ])
    ->will($this
    ->returnValue($response));

  // The call includes a job_id and a profile.
  $this->api
    ->expects($this
    ->at(5))
    ->method('patchDocument')
    ->with('my_doc_id', [
    'format' => 'JSON',
    'content' => '"content"',
    'title' => 'title',
    'fprm_subfilter_id' => '0e79f34d-f27b-4a0c-880e-cd9181a5d265',
    'fprm_id' => '4f91482b-5aa1-4a4a-a43f-712af7b39625',
    'external_application_id' => 'e39e24c7-6c69-4126-946d-cf8fbff38ef0',
    'job_id' => 'my_job_id',
    'project_id' => 'test_project',
  ])
    ->will($this
    ->returnValue($response));
  $this->api
    ->expects($this
    ->at(6))
    ->method('patchDocument')
    ->with('my_doc_id', [
    'job_id' => 'my_job_id',
    'project_id' => 'test_project',
  ])
    ->will($this
    ->returnValue($response));
  $this->api
    ->expects($this
    ->at(7))
    ->method('patchDocument')
    ->with('my_doc_id', [
    'format' => 'JSON',
    'content' => '"content"',
    'title' => 'title',
    'fprm_subfilter_id' => '0e79f34d-f27b-4a0c-880e-cd9181a5d265',
    'fprm_id' => '4f91482b-5aa1-4a4a-a43f-712af7b39625',
    'external_application_id' => 'e39e24c7-6c69-4126-946d-cf8fbff38ef0',
    'project_id' => 'test_project',
    'translation_locale_code' => [
      'es_ES',
      'ca_ES',
      'it_IT',
    ],
    'translation_workflow_id' => [
      'es_workflow',
      'ca_workflow',
      'default_workflow',
    ],
  ])
    ->will($this
    ->returnValue($response));
  $this->api
    ->expects($this
    ->at(8))
    ->method('patchDocument')
    ->with('my_doc_id', [
    'format' => 'JSON',
    'content' => '"content"',
    'title' => 'title',
    'fprm_subfilter_id' => '0e79f34d-f27b-4a0c-880e-cd9181a5d265',
    'fprm_id' => '4f91482b-5aa1-4a4a-a43f-712af7b39625',
    'external_application_id' => 'e39e24c7-6c69-4126-946d-cf8fbff38ef0',
    'project_id' => 'test_project',
    'translation_locale_code' => [
      'es_ES',
      'ca_ES',
      'it_IT',
    ],
    'translation_workflow_id' => [
      'es_workflow',
      'test_workflow',
      'default_workflow',
    ],
  ])
    ->will($this
    ->returnValue($response));
  $this->api
    ->expects($this
    ->at(9))
    ->method('patchDocument')
    ->with('my_doc_id', [
    'format' => 'JSON',
    'content' => '"content"',
    'title' => 'title',
    'fprm_subfilter_id' => '0e79f34d-f27b-4a0c-880e-cd9181a5d265',
    'fprm_id' => '4f91482b-5aa1-4a4a-a43f-712af7b39625',
    'external_application_id' => 'e39e24c7-6c69-4126-946d-cf8fbff38ef0',
    'project_id' => 'test_project',
  ])
    ->will($this
    ->returnValue($response));

  // Simplest update.
  $this->lingotek
    ->updateDocument('my_doc_id', 'content');

  // If there is an url, it should be included.
  $this->lingotek
    ->updateDocument('my_doc_id', 'content', 'http://example.com/node/1');

  // If there is a title, it should be included.
  $this->lingotek
    ->updateDocument('my_doc_id', 'content', NULL, 'title');

  // If there is an url and a title, they should be included.
  $this->lingotek
    ->updateDocument('my_doc_id', 'content', 'http://example.com/node/1', 'title');

  // The content is an array.
  $this->lingotek
    ->updateDocument('my_doc_id', [
    'content' => 'wedgiePlatypus',
  ]);

  // We upload with a profile and a job ID.
  $profile = new LingotekProfile([
    'id' => 'profile0',
    'project' => 'test_project',
    'vault' => 'test_vault',
  ], 'lingotek_profile');
  $this->lingotek
    ->updateDocument('my_doc_id', 'content', NULL, 'title', $profile, 'my_job_id');

  // Only update Job ID.
  $this->lingotek
    ->updateDocument('my_doc_id', NULL, NULL, NULL, $profile, 'my_job_id');
  $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' => 'profile0',
    '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');

  // If profile contains target specific settings in proper order
  $this->lingotek
    ->updateDocument('my_doc_id', 'content', NULL, 'title', $profile, NULL, 'en_US');
  $profile = new LingotekProfile([
    'id' => 'profile0',
    '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,
          'vault' => 'ca_vault',
        ],
      ],
      'it' => [
        'overrides' => 'custom',
        'custom' => [
          'auto_request' => TRUE,
          'workflow' => 'default',
          'vault' => 'it_vault',
        ],
      ],
    ],
  ], 'lingotek_profile');

  // If amount of translation_workflow_ids doesn't match amount of translation_locale_codes, use project workflow
  $this->lingotek
    ->updateDocument('my_doc_id', 'content', NULL, 'title', $profile, NULL, 'en_US');
  $profile = new LingotekProfile([
    'id' => 'profile0',
    'project' => 'test_project',
    'vault' => 'test_vault',
    'workflow' => 'test_workflow',
  ], 'lingotek_profile');
  $this->lingotek
    ->updateDocument('my_doc_id', 'content', NULL, 'title', $profile, NULL, 'en_US');
}