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;
abstract class SmartlingTestBase extends TMGMTTestBase {
const SETTINGS_FILE_NAME = 'tmgmt_smartling.simpletest.settings.php';
protected $smartlingPluginProviderSettings = [];
public static $modules = [
'tmgmt',
'tmgmt_demo',
'tmgmt_smartling',
'tmgmt_smartling_context_debug',
'tmgmt_smartling_test',
'dblog',
];
protected $testNodeId = 3;
protected $testNodeTitle;
protected $testNodeBody;
protected $targetLanguage = 'fr';
protected $sourceLanguage = 'en';
public function setUp() {
parent::setUp();
$settings_file_path = __DIR__ . '/../../' . self::SETTINGS_FILE_NAME;
if (file_exists($settings_file_path) && empty($this->smartlingPluginProviderSettings)) {
require_once $settings_file_path;
$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));
}
$this
->loginAsAdmin([
'access site reports',
'send context smartling',
]);
}
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);
}
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) {
}
}
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;
}
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;
}
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);
}
protected function downloadAndCheckTranslatedFile($jobId, $fileName) {
$this
->drupalPostForm("admin/tmgmt/jobs/{$jobId}", [], t('Download'));
$this
->drupalGet('admin/reports/dblog');
$this
->assertResponse(200);
$this
->assertRaw('Translation for');
$this
->assertRaw($fileName);
$this
->assertRaw('was successfully downloaded and imported.');
}
}