You are here

ContextTest.php in TMGMT Translator Smartling 8.4

Same filename and directory in other branches
  1. 8.3 tests/src/Functional/ContextTest.php

File

tests/src/Functional/ContextTest.php
View source
<?php

namespace Drupal\Tests\tmgmt_smartling\Functional;

use Drupal\Tests\Traits\Core\CronRunTrait;

/**
 * Context tests.
 *
 * @group tmgmt_smartling
 */
class ContextTest extends SmartlingTestBase {
  use CronRunTrait;

  /**
   * Test manual context sending.
   */
  public function testManualContextSending() {
    if (!empty($this->smartlingPluginProviderSettings)) {
      $translator = $this
        ->setUpSmartlingProviderSettings($this->smartlingPluginProviderSettings);
      $job = $this
        ->requestTranslationForNode($this->testNodeId, $this->targetLanguage, $translator);
      $fileName = $job
        ->getTranslatorPlugin()
        ->getFileName($job);
      $this
        ->drupalPostForm('admin/tmgmt/job_items', [
        'action' => 'tmgmt_smartling_send_context',
        'tmgmt_job_item_bulk_form[0]' => 'WyJ1bmQiLCIxIl0=',
      ], t('Apply to selected items'), [
        'query' => [
          'state' => 'All',
          'source_language' => 'All',
          'target_language' => 'All',
        ],
      ]);
      $this
        ->drupalPostForm(NULL, [], t('Send Context to Smartling'));
      $this
        ->drupalGet('admin/reports/dblog');
      $this
        ->assertRaw('We are about to switch user');
      $this
        ->assertRaw('User was successfully switched');
      $this
        ->assertRaw(t('Context upload for file @filename completed successfully.', [
        '@filename' => $fileName,
      ]));
    }
    else {
      $this
        ->fail("Smartling settings file for simpletests not found.");
    }
  }

  /**
   * Test context sending by cron.
   */
  public function testContextSendingByCron() {
    if (!empty($this->smartlingPluginProviderSettings)) {
      $translator = $this
        ->setUpSmartlingProviderSettings($this->smartlingPluginProviderSettings);
      $job = $this
        ->requestTranslationForNode($this->testNodeId, $this->targetLanguage, $translator);
      $fileName = $job
        ->getTranslatorPlugin()
        ->getFileName($job);
      $this
        ->cronRun();
      $this
        ->drupalGet('admin/reports/dblog');
      $this
        ->assertRaw('We are about to switch user');
      $this
        ->assertRaw('User was successfully switched');
      $this
        ->assertRaw(t('Context upload for file @filename completed successfully.', [
        '@filename' => $fileName,
      ]));
    }
    else {
      $this
        ->fail("Smartling settings file for simpletests not found.");
    }
  }

  /**
   * Test Smartling context debugger: show context.
   */
  public function testSmartlingContextDebuggerShowContext() {
    if (!empty($this->smartlingPluginProviderSettings)) {
      global $base_url;
      $this
        ->drupalPostForm('admin/tmgmt/smartling-context-debug', [
        'do_direct_output' => TRUE,
        'url' => 'https://www.smartling.com',
      ], t('Test context'));
      $this
        ->assertText("Move the World with Words");
      $this
        ->drupalGet('admin/reports/dblog');
    }
    else {
      $this
        ->fail("Smartling settings file for simpletests not found.");
    }
  }

  /**
   * Test Smartling context debugger: no available configs.
   */
  public function testSmartlingContextDebuggerFormNoAvailableConfigs() {
    if (!empty($this->smartlingPluginProviderSettings)) {
      $smartling_provider_configs = \Drupal::getContainer()
        ->get('tmgmt_smartling.smartling_config_manager')
        ->getAvailableConfigs();
      foreach ($smartling_provider_configs as $smartling_provider_config) {
        \Drupal::configFactory()
          ->getEditable($smartling_provider_config
          ->getName())
          ->delete();
      }
      $this
        ->drupalGet('admin/tmgmt/smartling-context-debug');
      $this
        ->assertText('No Smartling configs found. Please create Smartling config first');
    }
    else {
      $this
        ->fail("Smartling settings file for simpletests not found.");
    }
  }

  /**
   * Test Smartling context debugger: send context.
   */
  public function testSmartlingContextDebuggerSendContext() {
    if (!empty($this->smartlingPluginProviderSettings)) {
      global $base_url;

      // Workaround: TmgmtSmartlingContextDebugForm::submitForm() uses global
      // config not provider config:
      // \Drupal::config('tmgmt.translator.smartling')->get('settings');
      $this
        ->drupalPostForm('admin/tmgmt/translators/manage/smartling', $this->smartlingPluginProviderSettings, t('Save'));
      $translator = $this
        ->setUpSmartlingProviderSettings($this->smartlingPluginProviderSettings);
      $job = $this
        ->requestTranslationForNode($this->testNodeId, $this->targetLanguage, $translator);
      $fileName = $job
        ->getTranslatorPlugin()
        ->getFileName($job);
      $this
        ->checkGeneratedFile($fileName, $this->testNodeTitle);
      $this
        ->drupalPostForm('admin/tmgmt/smartling-context-debug', [
        'do_direct_output' => FALSE,
        'filename' => $fileName,
        'url' => $base_url . '/node/' . $this->testNodeId,
      ], t('Test context'));
      $this
        ->assertNoText($this->testNodeTitle);
      $this
        ->assertNoText($this->testNodeBody);
      $this
        ->assertText('Smartling response');
      $this
        ->drupalGet('admin/reports/dblog');
      $this
        ->assertRaw(t('Context upload for file @filename completed successfully.', [
        '@filename' => $fileName,
      ]));
    }
    else {
      $this
        ->fail("Smartling settings file for simpletests not found.");
    }
  }

  /**
   * Test context uploading with turned off "Silent switching mode" feature.
   */
  public function testSilentSwitchingOff() {
    if (!empty($this->smartlingPluginProviderSettings)) {
      $translator = $this
        ->setUpSmartlingProviderSettings($this->smartlingPluginProviderSettings);
      $job = $this
        ->requestTranslationForNode($this->testNodeId, $this->targetLanguage, $translator);
      $fileName = $job
        ->getTranslatorPlugin()
        ->getFileName($job);
      $this
        ->cronRun();
      $this
        ->drupalGet('admin/reports/dblog');
      $this
        ->assertRaw('We are about to switch user');
      $this
        ->assertRaw(t('User @name has logged out.', [
        '@name' => 'Anonymous',
      ]));
      $this
        ->assertRaw(t('User @name has logged in.', [
        '@name' => $this->smartlingPluginProviderSettings['settings[contextUsername]'],
      ]));
      $this
        ->assertRaw('User was successfully switched');
      $this
        ->assertRaw(t('Context upload for file @filename completed successfully.', [
        '@filename' => $fileName,
      ]));
    }
    else {
      $this
        ->fail("Smartling settings file for simpletests not found.");
    }
  }

  /**
   * Test context uploading with turned on "Silent switching mode" feature.
   */
  public function testSilentSwitchingOn() {
    if (!empty($this->smartlingPluginProviderSettings)) {

      // Set up silent user switching mode.
      $plugin_settings = $this->smartlingPluginProviderSettings;
      $plugin_settings['settings[context_silent_user_switching]'] = TRUE;
      $translator = $this
        ->setUpSmartlingProviderSettings($plugin_settings);
      $job = $this
        ->requestTranslationForNode($this->testNodeId, $this->targetLanguage, $translator);
      $fileName = $job
        ->getTranslatorPlugin()
        ->getFileName($job);
      $this
        ->cronRun();
      $this
        ->drupalGet('admin/reports/dblog');
      $this
        ->assertRaw('We are about to switch user');
      $this
        ->assertNoRaw(t('User @name has logged out.', [
        '@name' => 'Anonymous',
      ]));
      $this
        ->assertNoRaw(t('User @name has logged in.', [
        '@name' => $this->smartlingPluginProviderSettings['settings[contextUsername]'],
      ]));
      $this
        ->assertRaw('User was successfully switched');
      $this
        ->assertRaw(t('Context upload for file @filename completed successfully.', [
        '@filename' => $fileName,
      ]));
    }
    else {
      $this
        ->fail("Smartling settings file for simpletests not found.");
    }
  }

  /**
   * Test override context css class.
   *
   * We must not set "sl-override-context" class for context because it causes
   * huge load on context service.
   */
  public function testOverrideContextOption() {
    if (!empty($this->smartlingPluginProviderSettings)) {
      $translator = $this
        ->setUpSmartlingProviderSettings($this->smartlingPluginProviderSettings);
      $job = $this
        ->requestTranslationForNode($this->testNodeId, $this->targetLanguage, $translator);
      $fileName = $job
        ->getTranslatorPlugin()
        ->getFileName($job);
      $this
        ->drupalPostForm('admin/tmgmt/job_items', [
        'action' => 'tmgmt_smartling_send_context',
        'tmgmt_job_item_bulk_form[0]' => 'WyJ1bmQiLCIxIl0=',
      ], t('Apply to selected items'), [
        'query' => [
          'state' => 'All',
          'source_language' => 'All',
          'target_language' => 'All',
        ],
      ]);
      $this
        ->drupalPostForm(NULL, [], t('Send Context to Smartling'));
      $fileName = str_replace('.', '_', $fileName);
      $file_path = \Drupal::getContainer()
        ->get('file_system')
        ->realpath(\Drupal::config('system.file')
        ->get('default_scheme') . "://tmgmt_smartling_context/{$fileName}.html");
      $content = file_get_contents($file_path);
      $this
        ->assertTrue(strpos($content, 'class="sl-override-context"') === FALSE);
    }
    else {
      $this
        ->fail("Smartling settings file for simpletests not found.");
    }
  }

}

Classes

Namesort descending Description
ContextTest Context tests.