View source
<?php
namespace Drupal\Tests\lingotek\Functional;
use Drupal\Core\Url;
use Drupal\language\Entity\ConfigurableLanguage;
class LingotekDashboardTest extends LingotekTestBase {
public static $modules = [
'block',
'node',
'comment',
];
public function testDashboardCanAddLanguage() {
$url = Url::fromRoute('lingotek.dashboard_endpoint')
->setAbsolute()
->toString();
$post = [
'code' => 'it_IT',
'language' => 'Italian',
'native' => 'Italiano',
'direction' => '',
];
$request = $this->client
->post($url, [
'body' => http_build_query($post),
'cookies' => $this->cookies,
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/x-www-form-urlencoded',
],
'http_errors' => FALSE,
]);
$response = json_decode($request
->getBody(), TRUE);
$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']);
$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());
}
public function testDashboardCanAddRTLLanguage() {
$url = Url::fromRoute('lingotek.dashboard_endpoint')
->setAbsolute()
->toString();
$post = [
'code' => 'ar_AE',
'language' => 'Arabic',
'native' => 'العربية',
'direction' => 'RTL',
];
$request = $this->client
->post($url, [
'body' => http_build_query($post),
'cookies' => $this->cookies,
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/x-www-form-urlencoded',
],
'http_errors' => FALSE,
]);
$response = json_decode($request
->getBody(), TRUE);
$this
->assertIdentical('ar', $response['xcode']);
$this
->assertIdentical('ar_AE', $response['locale']);
$this
->assertIdentical(1, $response['active']);
$this
->assertIdentical(1, $response['enabled']);
$this
->assertIdentical(0, $response['source']['total']);
$this
->assertIdentical(0, $response['target']['total']);
$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() {
$url = Url::fromRoute('lingotek.dashboard_endpoint')
->setAbsolute()
->toString();
$post = [
'code' => 'ar',
'language' => 'Arabic',
'native' => 'العربية',
'direction' => 'RTL',
];
$request = $this->client
->post($url, [
'body' => http_build_query($post),
'cookies' => $this->cookies,
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/x-www-form-urlencoded',
],
'http_errors' => FALSE,
]);
$response = json_decode($request
->getBody(), TRUE);
$this
->assertIdentical('ar', $response['xcode']);
$this
->assertIdentical('ar', $response['locale']);
$this
->assertIdentical(1, $response['active']);
$this
->assertIdentical(1, $response['enabled']);
$this
->assertIdentical(0, $response['source']['total']);
$this
->assertIdentical(0, $response['target']['total']);
$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() {
$url = Url::fromRoute('lingotek.dashboard_endpoint')
->setAbsolute()
->toString();
$post = [
'code' => 'es_ES',
'language' => 'Spanish (Spain)',
'native' => 'Español',
'direction' => '',
];
$request = $this->client
->post($url, [
'body' => http_build_query($post),
'cookies' => $this->cookies,
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/x-www-form-urlencoded',
],
'http_errors' => FALSE,
]);
$response = json_decode($request
->getBody(), TRUE);
$this
->assertIdentical('es', $response['xcode']);
$this
->assertIdentical('es_ES', $response['locale']);
$this
->assertIdentical(1, $response['active']);
$this
->assertIdentical(1, $response['enabled']);
$this
->assertIdentical(0, $response['source']['total']);
$this
->assertIdentical(0, $response['target']['total']);
$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->client
->get($url, [
'cookies' => $this->cookies,
'headers' => [
'Accept' => 'application/json',
],
'http_errors' => FALSE,
]);
$response = json_decode($request
->getBody(), 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->client
->post($url, [
'body' => http_build_query($post),
'cookies' => $this->cookies,
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/x-www-form-urlencoded',
],
'http_errors' => FALSE,
]);
$response = json_decode($request
->getBody(), TRUE);
$this
->assertIdentical('es-ar', $response['xcode']);
$this
->assertIdentical('es_AR', $response['locale']);
$this
->assertIdentical(1, $response['active']);
$this
->assertIdentical(1, $response['enabled']);
$this
->assertIdentical(0, $response['source']['total']);
$this
->assertIdentical(0, $response['target']['total']);
$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->client
->get($url, [
'cookies' => $this->cookies,
'headers' => [
'Accept' => 'application/json',
],
'http_errors' => FALSE,
]);
$response = json_decode($request
->getBody(), TRUE);
$returned_languages = array_keys($response['languages']);
$this
->assertIdentical([
'en_US',
'es_AR',
'es_ES',
], $returned_languages);
}
public function testDashboardAddLocaleAndThenLanguage() {
$url = Url::fromRoute('lingotek.dashboard_endpoint')
->setAbsolute()
->toString();
$post = [
'code' => 'es_AR',
'language' => 'Spanish (Argentina)',
'native' => 'Español',
'direction' => '',
];
$request = $this->client
->post($url, [
'body' => http_build_query($post),
'cookies' => $this->cookies,
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/x-www-form-urlencoded',
],
'http_errors' => FALSE,
]);
$response = json_decode($request
->getBody(), TRUE);
$this
->assertIdentical('es', $response['xcode']);
$this
->assertIdentical('es_AR', $response['locale']);
$this
->assertIdentical(1, $response['active']);
$this
->assertIdentical(1, $response['enabled']);
$this
->assertIdentical(0, $response['source']['total']);
$this
->assertIdentical(0, $response['target']['total']);
$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->client
->get($url, [
'cookies' => $this->cookies,
'headers' => [
'Accept' => 'application/json',
],
'http_errors' => FALSE,
]);
$response = json_decode($request
->getBody(), 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->client
->post($url, [
'body' => http_build_query($post),
'cookies' => $this->cookies,
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/x-www-form-urlencoded',
],
'http_errors' => FALSE,
]);
$response = json_decode($request
->getBody(), TRUE);
$this
->assertIdentical('es-es', $response['xcode']);
$this
->assertIdentical('es_ES', $response['locale']);
$this
->assertIdentical(1, $response['active']);
$this
->assertIdentical(1, $response['enabled']);
$this
->assertIdentical(0, $response['source']['total']);
$this
->assertIdentical(0, $response['target']['total']);
$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->client
->get($url, [
'cookies' => $this->cookies,
'headers' => [
'Accept' => 'application/json',
],
'http_errors' => FALSE,
]);
$response = json_decode($request
->getBody(), TRUE);
$returned_languages = array_keys($response['languages']);
$this
->assertIdentical([
'en_US',
'es_AR',
'es_ES',
], $returned_languages);
}
public function testDisableLanguage() {
$url = Url::fromRoute('lingotek.dashboard_endpoint')
->setAbsolute()
->toString();
$post = [
'code' => 'es_ES',
'language' => 'Spanish (Spain)',
'native' => 'Español (España)',
'direction' => '',
];
$request = $this->client
->post($url, [
'body' => http_build_query($post),
'cookies' => $this->cookies,
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/x-www-form-urlencoded',
],
'http_errors' => FALSE,
]);
$response = json_decode($request
->getBody(), TRUE);
$this
->verbose(var_export($response, TRUE));
$this
->rebuildContainer();
$language_manager = \Drupal::service('language_manager');
$languages = $language_manager
->getLanguages();
$this
->assertIdentical(2, count($languages));
$request = $this->client
->get(Url::fromRoute('lingotek.dashboard_endpoint', [
'code' => 'es_ES',
])
->setAbsolute()
->toString(), [
'cookies' => $this->cookies,
'headers' => [
'Accept' => 'application/json',
],
'http_errors' => FALSE,
]);
$response = json_decode($request
->getBody(), 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->client
->delete($url, [
'body' => http_build_query([
'code' => 'es_ES',
]),
'cookies' => $this->cookies,
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/x-www-form-urlencoded',
],
'http_errors' => FALSE,
]);
$response = json_decode($request
->getBody(), TRUE);
$this
->assertIdentical('DELETE', $response['method']);
$this
->assertIdentical('es', $response['language']);
$this
->assertIdentical('Language disabled: es_ES', $response['message']);
$this
->rebuildContainer();
$languages = $language_manager
->getLanguages();
$this
->assertIdentical(2, count($languages), 'Spanish language is disabled, but not deleted.');
$language = ConfigurableLanguage::load('es');
$this
->assertIdentical($language
->getThirdPartySetting('lingotek', 'disabled', NULL), TRUE, 'The Spanish language is disabled');
$request = $this->client
->get(Url::fromRoute('lingotek.dashboard_endpoint', [
'code' => 'es_ES',
])
->setAbsolute()
->toString(), [
'cookies' => $this->cookies,
'headers' => [
'Accept' => 'application/json',
],
'http_errors' => FALSE,
]);
$response = json_decode($request
->getBody(), 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' => '',
];
$request = $this->client
->post($url, [
'body' => http_build_query($post),
'cookies' => $this->cookies,
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/x-www-form-urlencoded',
],
'http_errors' => FALSE,
]);
$response = json_decode($request
->getBody(), TRUE);
$this
->assertIdentical('POST', $response['method']);
$this
->assertIdentical('es', $response['xcode']);
$this
->assertIdentical('es_ES', $response['locale']);
$this
->assertIdentical(1, $response['active']);
$this
->assertIdentical(1, $response['enabled']);
$request = $this->client
->get(Url::fromRoute('lingotek.dashboard_endpoint', [
'code' => 'es_ES',
])
->setAbsolute()
->toString(), [
'cookies' => $this->cookies,
'headers' => [
'Accept' => 'application/json',
],
'http_errors' => FALSE,
]);
$response = json_decode($request
->getBody(), 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() {
$url = Url::fromRoute('lingotek.dashboard_endpoint')
->setAbsolute()
->toString();
$post = [
'code' => 'es_ES',
'language' => 'Spanish (Spain)',
'native' => 'Español (España)',
'direction' => '',
];
$request = $this->client
->post($url, [
'body' => http_build_query($post),
'cookies' => $this->cookies,
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/x-www-form-urlencoded',
],
'http_errors' => FALSE,
]);
$response = json_decode($request
->getBody(), TRUE);
$this
->assertIdentical('POST', $response['method']);
$this
->assertIdentical('es', $response['xcode']);
$this
->assertIdentical('es_ES', $response['locale']);
$this
->assertIdentical(1, $response['active']);
$this
->assertIdentical(1, $response['enabled']);
$this
->rebuildContainer();
$language_manager = \Drupal::service('language_manager');
$languages = $language_manager
->getLanguages();
$this
->assertIdentical(2, count($languages));
$request = $this->client
->get($url, [
'cookies' => $this->cookies,
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/x-www-form-urlencoded',
],
'http_errors' => FALSE,
]);
$response = json_decode($request
->getBody(), 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->client
->delete($url, [
'body' => http_build_query([
'code' => 'es_ES',
]),
'cookies' => $this->cookies,
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/x-www-form-urlencoded',
],
'http_errors' => FALSE,
]);
$response = json_decode($request
->getBody(), TRUE);
$this
->assertIdentical('DELETE', $response['method']);
$this
->assertIdentical('es', $response['language']);
$this
->assertIdentical('Language disabled: es_ES', $response['message']);
$this
->rebuildContainer();
$request = $this->client
->get($url, [
'cookies' => $this->cookies,
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/x-www-form-urlencoded',
],
'http_errors' => FALSE,
]);
$response = json_decode($request
->getBody(), 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();
$this
->drupalGet('admin/lingotek');
$this
->assertRaw(t('Missing translations for: @languages. See the <a href=":updates">Available translation updates</a> page for more information.', [
'@languages' => t('Spanish'),
':updates' => Url::fromRoute('locale.translate_status')
->toString(),
]), 'Missing translations message');
$status = locale_translation_get_status();
$status['drupal']['es'] = new \stdClass();
$status['drupal']['es']->type = 'current';
\Drupal::keyValue('locale.translation_status')
->set('drupal', $status['drupal']);
$this
->drupalGet('admin/lingotek');
$this
->assertNoRaw(t('Missing translations for: @languages. See the <a href=":updates">Available translation updates</a> page for more information.', [
'@languages' => t('Spanish'),
':updates' => Url::fromRoute('locale.translate_status')
->toString(),
]), 'No missing translations message with current translations');
$status = locale_translation_get_status();
$status['lingotek']['es'] = new \stdClass();
$status['lingotek']['es']->type = 'local';
\Drupal::keyValue('locale.translation_status')
->set('lingotek', $status['lingotek']);
$this
->drupalGet('admin/lingotek');
$this
->assertNoRaw(t('Missing translations for: @languages. See the <a href=":updates">Available translation updates</a> page for more information.', [
'@languages' => t('Spanish'),
':updates' => Url::fromRoute('locale.translate_status')
->toString(),
]), 'No missing translations message with local translations');
}
public function testDashboardEndpointUrlIsRelative() {
$basepath = \Drupal::request()
->getBasePath();
$this
->drupalGet('/admin/lingotek');
$drupalSettings = $this
->getDrupalSettings();
$this
->assertEquals($basepath . '/admin/lingotek/dashboard_endpoint', $drupalSettings['lingotek']['cms_data']['endpoint_url']);
}
}