You are here

SmartlingTestBase.php in TMGMT Translator Smartling 8.2

File

src/Tests/SmartlingTestBase.php
View source
<?php

namespace Drupal\tmgmt_smartling\Tests;

use Drupal;
use Drupal\node\Entity\Node;
use Drupal\tmgmt\Entity\Job;
use Drupal\tmgmt\JobInterface;
use Drupal\tmgmt\Tests\TMGMTTestBase;
use Smartling\Exceptions\SmartlingApiException;

/**
 * Basic tests for the Smartling translator.
 */
abstract class SmartlingTestBase extends TMGMTTestBase {

  /**
   * Name of file that contains settings for test Smartling project.
   *
   * @var string
   */
  const SETTINGS_FILE_NAME = 'tmgmt_smartling.simpletest.settings.php';

  /**
   * Smartling test project settings.
   *
   * @var array
   */
  protected $smartlingPluginProviderSettings = [];

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = [
    'tmgmt',
    'tmgmt_demo',
    'tmgmt_smartling',
    'tmgmt_smartling_context_debug',
    'tmgmt_smartling_test',
    'dblog',
  ];

  /**
   * @var int
   */
  protected $testNodeId = 3;

  /**
   * @var string
   */
  protected $testNodeTitle;

  /**
   * @var string
   */
  protected $testNodeBody;

  /**
   * @var string
   */
  protected $targetLanguage = 'fr';

  /**
   * @var string
   */
  protected $sourceLanguage = 'en';

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();
    $settings_file_path = __DIR__ . '/../../' . self::SETTINGS_FILE_NAME;

    // Include settings from external file.
    if (file_exists($settings_file_path) && empty($this->smartlingPluginProviderSettings)) {
      require_once $settings_file_path;

      // Fetch needed data.
      $this->smartlingPluginProviderSettings = $settings;
      $test_node = Node::load($this->testNodeId);
      $this->testNodeTitle = $test_node
        ->get('title')->value;
      $this->testNodeBody = trim(strip_tags($test_node
        ->get('body')->value));
    }

    // Additional permission: access to "Recent log messages" page and
    // access for manual context sending.
    $this
      ->loginAsAdmin([
      'access site reports',
      'send context smartling',
    ]);
  }

  /**
   * Invokes private/protected method.
   *
   * @param $object
   * @param $methodName
   * @param array $parameters
   *
   * @return mixed
   */
  protected function invokeMethod($object, $methodName, array $parameters = []) {
    $reflection = new \ReflectionClass(get_class($object));
    $method = $reflection
      ->getMethod($methodName);
    $method
      ->setAccessible(true);
    return $method
      ->invokeArgs($object, $parameters);
  }

  /**
   * Removes test file from Smartling dashboard.
   *
   * @param $fileName
   */
  protected function deleteTestFile($fileName) {
    try {
      $api_factory = Drupal::service('tmgmt_smartling.smartling_api_factory');
      $smartlingApi = $api_factory::create([
        'user_id' => $this->smartlingPluginProviderSettings['settings[user_id]'],
        'project_id' => $this->smartlingPluginProviderSettings['settings[project_id]'],
        'token_secret' => $this->smartlingPluginProviderSettings['settings[token_secret]'],
      ], 'file');
      $smartlingApi
        ->deleteFile($fileName);
    } catch (SmartlingApiException $e) {

      // File not found.
    }
  }

  /**
   * Sets up Smartling provider settings and returns translator plugin.
   *
   * @param array $providerSettings
   *
   * @return \Drupal\tmgmt\TranslatorInterface
   */
  protected function setUpSmartlingProviderSettings(array $providerSettings) {
    $translator = $this
      ->createTranslator([
      'plugin' => 'smartling',
      'auto_accept' => $providerSettings['auto_accept'],
      'settings' => [
        'project_id' => $providerSettings['settings[project_id]'],
        'user_id' => $providerSettings['settings[user_id]'],
        'token_secret' => $providerSettings['settings[token_secret]'],
        'contextUsername' => $providerSettings['settings[contextUsername]'],
        'context_silent_user_switching' => $providerSettings['settings[context_silent_user_switching]'],
        'retrieval_type' => $providerSettings['settings[retrieval_type]'],
        'auto_authorize_locales' => $providerSettings['settings[auto_authorize_locales]'],
        'callback_url_use' => $providerSettings['settings[callback_url_use]'],
        'callback_url_host' => $providerSettings['settings[callback_url_host]'],
        'scheme' => $providerSettings['settings[scheme]'],
        'custom_regexp_placeholder' => $providerSettings['settings[custom_regexp_placeholder]'],
        'export_format' => $providerSettings['settings[export_format]'],
      ],
    ]);
    return $translator;
  }

  /**
   * Requests translation for a given node.
   *
   * @param $nid
   * @param $language
   * @param $translator
   *
   * @return \Drupal\tmgmt\JobInterface
   */
  protected function requestTranslationForNode($nid, $language, $translator) {
    $job = $this
      ->createJob($this->sourceLanguage, $language, 1, [
      'label' => 'Job for ' . $nid,
      'job_type' => Job::TYPE_NORMAL,
    ]);
    $job->translator = $translator;
    $job
      ->addItem('content', 'node', $nid);
    $job
      ->setState(JobInterface::STATE_ACTIVE);
    $job
      ->requestTranslation();
    return $job;
  }

  /**
   * Checks if generated file exists and correct.
   *
   * @param $fileName
   * @param $nodeTitle
   */
  protected function checkGeneratedFile($fileName, $nodeTitle) {
    $url = \Drupal::service('stream_wrapper_manager')
      ->getViaUri(file_default_scheme() . "://tmgmt_sources/{$fileName}")
      ->getExternalUrl();
    $this
      ->drupalGet($url);
    $this
      ->assertResponse(200);
    $this
      ->assertRaw($nodeTitle);
  }

  /**
   * Checks if download was successful.
   *
   * @param $jobId
   * @param $fileName
   * @throws \Exception
   */
  protected function downloadAndCheckTranslatedFile($jobId, $fileName) {
    $this
      ->drupalPostForm("admin/tmgmt/jobs/{$jobId}", [], t('Download'));
    $this
      ->drupalGet('admin/reports/dblog');
    $this
      ->assertResponse(200);

    // TODO: don't know why assertLink and assertRaw doesn't work with quoted
    // strings.
    $this
      ->assertRaw('Translation for');
    $this
      ->assertRaw($fileName);
    $this
      ->assertRaw('was successfully downloaded and imported.');
  }

}

Classes

Namesort descending Description
SmartlingTestBase Basic tests for the Smartling translator.