View source
<?php
namespace Drupal\Tests\tmgmt_smartling\Functional;
use Drupal\Tests\Traits\Core\CronRunTrait;
class ContextTest extends SmartlingTestBase {
use CronRunTrait;
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.");
}
}
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.");
}
}
public function testSmartlingContextDebuggerShowContext() {
if (!empty($this->smartlingPluginProviderSettings)) {
global $base_url;
$this
->drupalPostForm('admin/tmgmt/smartling-context-debug', [
'do_direct_output' => TRUE,
'url' => $base_url . '/node/' . $this->testNodeId,
], t('Test context'));
$this
->assertText($this->testNodeTitle);
$this
->assertText($this->testNodeBody);
$this
->drupalGet('admin/reports/dblog');
}
else {
$this
->fail("Smartling settings file for simpletests not found.");
}
}
public function testSmartlingContextDebuggerSendContext() {
if (!empty($this->smartlingPluginProviderSettings)) {
global $base_url;
$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.");
}
}
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.");
}
}
public function testSilentSwitchingOn() {
if (!empty($this->smartlingPluginProviderSettings)) {
$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.");
}
}
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(file_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.");
}
}
}