You are here

UploadFlowTest.php in TMGMT Translator Smartling 8.3

Same filename and directory in other branches
  1. 8.4 tests/src/Kernel/UploadFlowTest.php

File

tests/src/Kernel/UploadFlowTest.php
View source
<?php

namespace Drupal\Tests\tmgmt_smartling\Kernel;

use Smartling\AuditLog\Params\CreateRecordParameters;

/**
 * Tests file upload flow.
 *
 * @group tmgmt_smartling
 */
class UploadFlowTest extends SmartlingTestBase {

  /**
   * Upload success flow with batch execution.
   */
  public function testRequestTranslationSuccessFlowExecute() {
    $translate_job = $this
      ->createJobWithItems([
      'batch_uid' => 'uid',
      'batch_execute_on_job' => 1,
    ]);
    $this->apiWrapperMock
      ->expects($this
      ->once())
      ->method('getApi')
      ->with('batch')
      ->willReturn($this->batchApiMock);
    $this->apiWrapperMock
      ->expects($this
      ->once())
      ->method('createAuditLogRecord')
      ->with($translate_job, NULL, \Drupal::currentUser(), CreateRecordParameters::ACTION_TYPE_UPLOAD);
    $this->batchApiMock
      ->expects($this
      ->once())
      ->method('uploadBatchFile')
      ->with($this
      ->callback(function ($subject) {
      return strstr($subject, '/files/tmgmt_sources/JobID1_en_de.xml') !== FALSE;
    }), 'JobID1_en_de.xml', 'xml', 'uid', $this
      ->callback(function ($subject) {
      $params = $subject
        ->exportToArray();
      return $params['authorize'] == 0 && preg_match('/^{"client":"drupal-tmgmt-connector","version":"(\\d+\\.x-\\d+\\.\\d+|\\d+\\.x-\\d+\\.x-dev|unknown)"}$/', $params['smartling.client_lib_id']) && $params['localeIdsToAuthorize'][0] == 'de' && $params['smartling.translate_paths'] == 'html/body/div/div, html/body/div/span' && $params['smartling.string_format_paths'] == 'html : html/body/div/div, @default : html/body/div/span' && $params['smartling.variants_enabled'] == TRUE && $params['smartling.source_key_paths'] == 'html/body/div/{div.sl-variant}, html/body/div/{span.sl-variant}' && $params['smartling.character_limit_paths'] == 'html/body/div/limit' && $params['smartling.placeholder_format_custom'] == '(@|%|!)[\\w-]+';
    }));
    $this->apiWrapperMock
      ->expects($this
      ->once())
      ->method('executeBatch')
      ->with('uid');
    $this->apiWrapperMock
      ->expects($this
      ->at(2))
      ->method('createFirebaseRecord')
      ->with('tmgmt_smartling', 'notifications', 10, [
      "message" => 'File uploaded. Job id: 1, file name: JobID1_en_de.xml.',
      "type" => "status",
    ]);
    $this->apiWrapperMock
      ->expects($this
      ->at(4))
      ->method('createFirebaseRecord')
      ->with('tmgmt_smartling', 'notifications', 10, [
      "message" => "Finished: content is in the job. You may need to wait a few seconds before content is authorized (if you checked 'authorize' checkbox).",
      "type" => "status",
    ]);
    $translate_job
      ->requestTranslation();
  }

  /**
   * Upload success flow without batch execution.
   */
  public function testRequestTranslationSuccessFlowDoNotExecute() {
    $translate_job = $this
      ->createJobWithItems([
      'batch_uid' => 'uid',
      'batch_execute_on_job' => 2,
    ]);
    $this->apiWrapperMock
      ->expects($this
      ->once())
      ->method('getApi')
      ->with('batch')
      ->willReturn($this->batchApiMock);
    $this->apiWrapperMock
      ->expects($this
      ->once())
      ->method('createAuditLogRecord')
      ->with($translate_job, NULL, \Drupal::currentUser(), CreateRecordParameters::ACTION_TYPE_UPLOAD);
    $this->batchApiMock
      ->expects($this
      ->once())
      ->method('uploadBatchFile')
      ->with($this
      ->callback(function ($subject) {
      return strstr($subject, '/files/tmgmt_sources/JobID1_en_de.xml') !== FALSE;
    }), 'JobID1_en_de.xml', 'xml', 'uid', $this
      ->callback(function ($subject) {
      $params = $subject
        ->exportToArray();
      return $params['authorize'] == 0 && preg_match('/^{"client":"drupal-tmgmt-connector","version":"(\\d+\\.x-\\d+\\.\\d+|\\d+\\.x-\\d+\\.x-dev|unknown)"}$/', $params['smartling.client_lib_id']) && $params['localeIdsToAuthorize'][0] == 'de' && $params['smartling.translate_paths'] == 'html/body/div/div, html/body/div/span' && $params['smartling.string_format_paths'] == 'html : html/body/div/div, @default : html/body/div/span' && $params['smartling.variants_enabled'] == TRUE && $params['smartling.source_key_paths'] == 'html/body/div/{div.sl-variant}, html/body/div/{span.sl-variant}' && $params['smartling.character_limit_paths'] == 'html/body/div/limit' && $params['smartling.placeholder_format_custom'] == '(@|%|!)[\\w-]+';
    }));
    $this->apiWrapperMock
      ->expects($this
      ->never())
      ->method('executeBatch')
      ->with('uid');
    $this->apiWrapperMock
      ->expects($this
      ->once())
      ->method('createFirebaseRecord')
      ->with('tmgmt_smartling', 'notifications', 10, [
      "message" => 'File uploaded. Job id: 1, file name: JobID1_en_de.xml.',
      "type" => "status",
    ]);
    $translate_job
      ->requestTranslation();
  }

  /**
   * Upload fail flow: no batch uid.
   */
  public function testRequestTranslationFailFlowNoBatchUid() {
    $translate_job = $this
      ->createJobWithItems([]);
    $this->apiWrapperMock
      ->expects($this
      ->once())
      ->method('createAuditLogRecord')
      ->with($translate_job, NULL, \Drupal::currentUser(), CreateRecordParameters::ACTION_TYPE_UPLOAD);
    $this->apiWrapperMock
      ->expects($this
      ->never())
      ->method('getApi');
    $this->batchApiMock
      ->expects($this
      ->never())
      ->method('uploadBatchFile');
    $this->apiWrapperMock
      ->expects($this
      ->never())
      ->method('executeBatch');
    $this->apiWrapperMock
      ->expects($this
      ->once())
      ->method('createFirebaseRecord')
      ->with('tmgmt_smartling', 'notifications', 10, [
      "message" => "File JobID1_en_de.xml (job id = 1) wasn't uploaded. Please see logs for more info.",
      "type" => "error",
    ]);
    $translate_job
      ->requestTranslation();
  }

  /**
   * Upload fail flow: error while uploading.
   */
  public function testRequestTranslationFailFlowErrorWhileUploading() {
    $exception = new \Exception("Test");
    $translate_job = $this
      ->createJobWithItems([
      'batch_uid' => 'uid',
      'batch_execute_on_job' => 1,
    ]);
    $this->apiWrapperMock
      ->expects($this
      ->once())
      ->method('getApi')
      ->with('batch')
      ->willReturn($this->batchApiMock);
    $this->apiWrapperMock
      ->expects($this
      ->once())
      ->method('createAuditLogRecord')
      ->with($translate_job, NULL, \Drupal::currentUser(), CreateRecordParameters::ACTION_TYPE_UPLOAD);
    $this->apiWrapperMock
      ->expects($this
      ->once())
      ->method('executeBatch')
      ->with('uid')
      ->will($this
      ->throwException($exception));
    $this->apiWrapperMock
      ->expects($this
      ->at(2))
      ->method('createFirebaseRecord')
      ->with('tmgmt_smartling', 'notifications', 10, [
      "message" => 'File uploaded. Job id: 1, file name: JobID1_en_de.xml.',
      "type" => "status",
    ]);
    $this->apiWrapperMock
      ->expects($this
      ->at(4))
      ->method('createFirebaseRecord')
      ->with('tmgmt_smartling', 'notifications', 10, [
      "message" => 'Error while uploading public://tmgmt_sources/JobID1_en_de.xml. Please see logs for more info.',
      "type" => "error",
    ]);
    $translate_job
      ->requestTranslation();
  }

}

Classes

Namesort descending Description
UploadFlowTest Tests file upload flow.