View source
<?php
namespace Drupal\lingotek\Tests;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\language\ConfigurableLanguageInterface;
use Drupal\language\Entity\ConfigurableLanguage;
class LingotekDashboardTest extends LingotekTestBase {
public static $modules = [
'node',
'comment',
];
public function testDashboardCanAddLanguage() {
$this
->drupalLogin($this->rootUser);
$post = [
'code' => 'it_IT',
'language' => 'Italian',
'native' => 'Italiano',
'direction' => '',
];
$request = $this
->drupalPost('/admin/lingotek/dashboard_endpoint', 'application/json', $post);
$response = json_decode($request, true);
$italian_language = ConfigurableLanguage::load('it');
$this
->assertNotNull($italian_language, 'Italian language has been added.');
$this
->assertIdentical('Italian', $italian_language
->getName());
$this
->assertIdentical(ConfigurableLanguage::DIRECTION_LTR, $italian_language
->getDirection());
$this
->assertIdentical('it', $response['xcode']);
$this
->assertIdentical('it_IT', $response['locale']);
$this
->assertIdentical(1, $response['active']);
$this
->assertIdentical(1, $response['enabled']);
$this
->assertIdentical(0, $response['source']['total']);
$this
->assertIdentical(0, $response['target']['total']);
}
public function testDashboardCanAddRTLLanguage() {
$this
->drupalLogin($this->rootUser);
$post = [
'code' => 'ar_AE',
'language' => 'Arabic',
'native' => 'العربية',
'direction' => 'RTL',
];
$request = $this
->drupalPost('/admin/lingotek/dashboard_endpoint', 'application/json', $post);
$response = json_decode($request, true);
$arabic_language = ConfigurableLanguage::load('ar');
$this
->assertNotNull($arabic_language, 'Arabic language has been added.');
$this
->assertIdentical('Arabic', $arabic_language
->getName());
$this
->assertIdentical(ConfigurableLanguage::DIRECTION_RTL, $arabic_language
->getDirection());
}
public function testDashboardCanAddArabicLanguage() {
$this
->drupalLogin($this->rootUser);
$post = [
'code' => 'ar',
'language' => 'Arabic',
'native' => 'العربية',
'direction' => 'RTL',
];
$request = $this
->drupalPost('/admin/lingotek/dashboard_endpoint', 'application/json', $post);
$response = json_decode($request, TRUE);
$arabic_language = ConfigurableLanguage::load('ar');
$this
->assertNotNull($arabic_language, 'Arabic language has been added.');
$this
->assertIdentical('Arabic', $arabic_language
->getName());
$this
->assertIdentical(ConfigurableLanguage::DIRECTION_RTL, $arabic_language
->getDirection());
}
public function testDashboardAddLanguageAndThenLocale() {
$this
->drupalLogin($this->rootUser);
$post = [
'code' => 'es_ES',
'language' => 'Spanish (Spain)',
'native' => 'Español',
'direction' => '',
];
$request = $this
->drupalPost('/admin/lingotek/dashboard_endpoint', 'application/json', $post);
$response = json_decode($request, TRUE);
$esEsLanguage = ConfigurableLanguage::load('es');
$this
->assertNotNull($esEsLanguage, 'Spanish (Spain) language has been added.');
$this
->assertIdentical('Spanish (Spain)', $esEsLanguage
->getName());
$this
->assertIdentical(ConfigurableLanguage::DIRECTION_LTR, $esEsLanguage
->getDirection());
$request = $this
->drupalGet('/admin/lingotek/dashboard_endpoint');
$response = json_decode($request, TRUE);
$returned_languages = array_keys($response['languages']);
$this
->assertIdentical([
'en_US',
'es_ES',
], $returned_languages);
$post = [
'code' => 'es_AR',
'language' => 'Spanish (Argentina)',
'native' => 'Español',
'direction' => '',
];
$request = $this
->drupalPost('/admin/lingotek/dashboard_endpoint', 'application/json', $post);
$response = json_decode($request, TRUE);
$esArLanguage = ConfigurableLanguage::load('es-ar');
$this
->assertNotNull($esArLanguage, 'Spanish (Argentina) language has been added.');
$this
->assertIdentical('Spanish (Argentina)', $esArLanguage
->getName());
$this
->assertIdentical(ConfigurableLanguage::DIRECTION_LTR, $esArLanguage
->getDirection());
$request = $this
->drupalGet('/admin/lingotek/dashboard_endpoint');
$response = json_decode($request, TRUE);
$returned_languages = array_keys($response['languages']);
$this
->assertIdentical([
'en_US',
'es_AR',
'es_ES',
], $returned_languages);
}
public function testDashboardAddLocaleAndThenLanguage() {
$this
->drupalLogin($this->rootUser);
$post = [
'code' => 'es_AR',
'language' => 'Spanish (Argentina)',
'native' => 'Español',
'direction' => '',
];
$request = $this
->drupalPost('/admin/lingotek/dashboard_endpoint', 'application/json', $post);
$response = json_decode($request, TRUE);
$esArLanguage = ConfigurableLanguage::load('es');
$this
->assertNotNull($esArLanguage, 'Spanish (Argentina) language has been added.');
$this
->assertIdentical('Spanish (Argentina)', $esArLanguage
->getName());
$this
->assertIdentical(ConfigurableLanguage::DIRECTION_LTR, $esArLanguage
->getDirection());
$request = $this
->drupalGet('/admin/lingotek/dashboard_endpoint');
$response = json_decode($request, TRUE);
$returned_languages = array_keys($response['languages']);
$this
->assertIdentical([
'en_US',
'es_AR',
], $returned_languages);
$post = [
'code' => 'es_ES',
'language' => 'Spanish (Spain)',
'native' => 'Español',
'direction' => '',
];
$request = $this
->drupalPost('/admin/lingotek/dashboard_endpoint', 'application/json', $post);
$response = json_decode($request, TRUE);
$esEsLanguage = ConfigurableLanguage::load('es-es');
$this
->assertNotNull($esEsLanguage, 'Spanish (Spain) language has been added.');
$this
->assertIdentical('Spanish (Spain)', $esEsLanguage
->getName());
$this
->assertIdentical(ConfigurableLanguage::DIRECTION_LTR, $esEsLanguage
->getDirection());
$request = $this
->drupalGet('/admin/lingotek/dashboard_endpoint');
$response = json_decode($request, TRUE);
$returned_languages = array_keys($response['languages']);
$this
->assertIdentical([
'en_US',
'es_AR',
'es_ES',
], $returned_languages);
}
public function testDisableLanguage() {
$post = [
'code' => 'es_ES',
'language' => 'Spanish (Spain)',
'native' => 'Español (España)',
'direction' => '',
];
$request = $this
->curlExec(array(
CURLOPT_URL => $this
->buildUrl('/admin/lingotek/dashboard_endpoint', []),
CURLOPT_POST => TRUE,
CURLOPT_POSTFIELDS => $this
->serializePostValues($post),
CURLOPT_HTTPHEADER => array(
'Accept: application/json',
'Content-Type: application/x-www-form-urlencoded',
),
));
$response = json_decode($request, TRUE);
$this
->verbose(var_export($response, TRUE));
$language_manager = \Drupal::service('language_manager');
$languages = $language_manager
->getLanguages();
$this
->assertIdentical(2, count($languages));
$request = $this
->curlExec(array(
CURLOPT_URL => \Drupal::url('lingotek.dashboard_endpoint', [
'code' => 'es_ES',
], [
'absolute' => TRUE,
]),
CURLOPT_HTTPGET => TRUE,
CURLOPT_CUSTOMREQUEST => NULL,
CURLOPT_NOBODY => FALSE,
CURLOPT_HTTPHEADER => array(
'Accept: application/json',
),
));
$response = json_decode($request, TRUE);
$this
->assertIdentical('GET', $response['method']);
$this
->assertIdentical('es', $response['xcode']);
$this
->assertIdentical('es_ES', $response['locale']);
$this
->assertIdentical(1, $response['active']);
$this
->assertIdentical(1, $response['enabled']);
$language = ConfigurableLanguage::load('es');
$this
->assertIdentical($language
->getThirdPartySetting('lingotek', 'disabled', NULL), FALSE, 'The Spanish language is enabled');
$request = $this
->curlExec(array(
CURLOPT_URL => \Drupal::url('lingotek.dashboard_endpoint', array(), array(
'absolute' => TRUE,
)),
CURLOPT_HTTPGET => FALSE,
CURLOPT_CUSTOMREQUEST => 'DELETE',
CURLOPT_POSTFIELDS => $this
->serializePostValues([
'code' => 'es_ES',
]),
CURLOPT_HTTPHEADER => array(
'Accept: application/json',
'Content-Type: application/x-www-form-urlencoded',
),
));
$response = json_decode($request, TRUE);
$this
->verbose(var_export($response, TRUE));
$this
->assertIdentical('DELETE', $response['method']);
$this
->assertIdentical('es', $response['language']);
$this
->assertIdentical('Language disabled: es_ES', $response['message']);
$languages = $language_manager
->getLanguages();
$this
->assertIdentical(2, count($languages), 'Spanish language is disabled, but not deleted.');
$this
->rebuildContainer();
$language = ConfigurableLanguage::load('es');
$this
->assertIdentical($language
->getThirdPartySetting('lingotek', 'disabled', NULL), TRUE, 'The Spanish language is disabled');
$request = $this
->curlExec(array(
CURLOPT_URL => \Drupal::url('lingotek.dashboard_endpoint', [
'code' => 'es_ES',
], [
'absolute' => TRUE,
]),
CURLOPT_HTTPGET => TRUE,
CURLOPT_CUSTOMREQUEST => NULL,
CURLOPT_NOBODY => FALSE,
CURLOPT_HTTPHEADER => array(
'Accept: application/json',
),
));
$response = json_decode($request, TRUE);
$this
->assertIdentical('GET', $response['method']);
$this
->assertIdentical('es', $response['xcode']);
$this
->assertIdentical('es_ES', $response['locale']);
$this
->assertIdentical(0, $response['active']);
$this
->assertIdentical(1, $response['enabled']);
$post = [
'code' => 'es_ES',
'language' => 'Spanish (Spain)',
'native' => 'Español',
'direction' => '',
];
$response = $this
->drupalPost('/admin/lingotek/dashboard_endpoint', 'application/json', $post);
$response = json_decode($response, TRUE);
$this
->verbose(var_export($response, TRUE));
$request = $this
->curlExec(array(
CURLOPT_URL => \Drupal::url('lingotek.dashboard_endpoint', [
'code' => 'es_ES',
], [
'absolute' => TRUE,
]),
CURLOPT_HTTPGET => TRUE,
CURLOPT_CUSTOMREQUEST => NULL,
CURLOPT_NOBODY => FALSE,
CURLOPT_HTTPHEADER => array(
'Accept: application/json',
),
));
$response = json_decode($request, TRUE);
$this
->assertIdentical('GET', $response['method']);
$this
->assertIdentical('es', $response['xcode']);
$this
->assertIdentical('es_ES', $response['locale']);
$this
->assertIdentical(1, $response['active']);
$this
->assertIdentical(1, $response['enabled']);
$languages = $language_manager
->getLanguages();
$this
->assertIdentical(2, count($languages), 'Spanish language is enabled again, no new languages added.');
$this
->rebuildContainer();
$language = ConfigurableLanguage::load('es');
$this
->assertIdentical($language
->getThirdPartySetting('lingotek', 'disabled', NULL), FALSE, 'The Spanish language is enabled');
}
public function testDisabledLanguageInStats() {
$post = [
'code' => 'es_ES',
'language' => 'Spanish (Spain)',
'native' => 'Español (España)',
'direction' => '',
];
$request = $this
->curlExec(array(
CURLOPT_URL => $this
->buildUrl('/admin/lingotek/dashboard_endpoint', []),
CURLOPT_POST => TRUE,
CURLOPT_POSTFIELDS => $this
->serializePostValues($post),
CURLOPT_HTTPHEADER => array(
'Accept: application/json',
'Content-Type: application/x-www-form-urlencoded',
),
));
$response = json_decode($request, TRUE);
$this
->verbose(var_export($response, TRUE));
$language_manager = \Drupal::service('language_manager');
$languages = $language_manager
->getLanguages();
$this
->assertIdentical(2, count($languages));
$request = $this
->curlExec(array(
CURLOPT_URL => \Drupal::url('lingotek.dashboard_endpoint', [], [
'absolute' => TRUE,
]),
CURLOPT_HTTPGET => TRUE,
CURLOPT_CUSTOMREQUEST => NULL,
CURLOPT_NOBODY => FALSE,
CURLOPT_HTTPHEADER => array(
'Accept: application/json',
),
));
$response = json_decode($request, TRUE);
$this
->verbose(var_export($response, TRUE));
$this
->assertIdentical('GET', $response['method']);
$this
->assertIdentical(2, $response['count']);
$this
->assertIdentical('en', $response['languages']['en_US']['xcode']);
$this
->assertIdentical(1, $response['languages']['en_US']['active']);
$this
->assertIdentical(1, $response['languages']['en_US']['enabled']);
$this
->assertIdentical('es', $response['languages']['es_ES']['xcode']);
$this
->assertIdentical(1, $response['languages']['es_ES']['active']);
$this
->assertIdentical(1, $response['languages']['es_ES']['enabled']);
$request = $this
->curlExec(array(
CURLOPT_URL => \Drupal::url('lingotek.dashboard_endpoint', array(), array(
'absolute' => TRUE,
)),
CURLOPT_HTTPGET => FALSE,
CURLOPT_CUSTOMREQUEST => 'DELETE',
CURLOPT_POSTFIELDS => $this
->serializePostValues([
'code' => 'es_ES',
]),
CURLOPT_HTTPHEADER => array(
'Accept: application/json',
'Content-Type: application/x-www-form-urlencoded',
),
));
$response = json_decode($request, TRUE);
$this
->verbose(var_export($response, TRUE));
$this
->assertIdentical('DELETE', $response['method']);
$this
->assertIdentical('es', $response['language']);
$this
->assertIdentical('Language disabled: es_ES', $response['message']);
$request = $this
->curlExec(array(
CURLOPT_URL => \Drupal::url('lingotek.dashboard_endpoint', [], [
'absolute' => TRUE,
]),
CURLOPT_HTTPGET => TRUE,
CURLOPT_CUSTOMREQUEST => NULL,
CURLOPT_NOBODY => FALSE,
CURLOPT_HTTPHEADER => array(
'Accept: application/json',
),
));
$response = json_decode($request, TRUE);
$this
->verbose(var_export($response, TRUE));
$this
->assertIdentical('GET', $response['method']);
$this
->assertIdentical(2, $response['count']);
$this
->assertIdentical('en', $response['languages']['en_US']['xcode']);
$this
->assertIdentical(1, $response['languages']['en_US']['active']);
$this
->assertIdentical(1, $response['languages']['en_US']['enabled']);
$this
->assertIdentical('es', $response['languages']['es_ES']['xcode']);
$this
->assertIdentical(0, $response['languages']['es_ES']['active']);
$this
->assertIdentical(1, $response['languages']['es_ES']['enabled']);
}
public function testTranslationsAvailable() {
ConfigurableLanguage::createFromLangcode('es')
->setThirdPartySetting('lingotek', 'locale', 'es_MX')
->save();
$status = locale_translation_get_status();
$status['drupal']['es'] = new \stdClass();
$status['drupal']['es']->type = 'current';
\Drupal::state()
->set('locale.translation_status', $status);
$this
->drupalGet('admin/lingotek');
$this
->assertNoRaw(t('Missing translations for: @languages. See the <a href=":updates">Available translation updates</a> page for more information.', array(
'@languages' => t('Spanish'),
':updates' => \Drupal::url('locale.translate_status'),
)), 'Missing translations message');
$status = locale_translation_get_status();
$status['lingotek']['es'] = new \stdClass();
$status['lingotek']['es']->type = 'local';
\Drupal::state()
->set('locale.translation_status', $status);
$this
->drupalGet('admin/lingotek');
$this
->assertNoRaw(t('Missing translations for: @languages. See the <a href=":updates">Available translation updates</a> page for more information.', array(
'@languages' => t('Spanish'),
':updates' => \Drupal::url('locale.translate_status'),
)), 'Translations message visible');
}
}