You are here

public function LingotekDashboardTest::testDisableLanguage in Lingotek Translation 8

Tests that we can disable languages in the dashboard.

File

src/Tests/LingotekDashboardTest.php, line 218
Contains \Drupal\lingotek\Tests\LingotekDashboardTest.

Class

LingotekDashboardTest
Tests the Lingotek dashboard.

Namespace

Drupal\lingotek\Tests

Code

public function testDisableLanguage() {

  // Add a language.
  $post = [
    'code' => 'es_ES',
    'language' => 'Spanish (Spain)',
    'native' => 'Español (España)',
    'direction' => '',
  ];

  // We use curlExec in this test because drupalGet and drupalPost are not
  // reliable after doing DELETE requests, as the curl connection is reused
  // but not properly cleared. See https://www.drupal.org/node/2868666.
  $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));

  /** @var LanguageManagerInterface $language_manager */
  $language_manager = \Drupal::service('language_manager');
  $languages = $language_manager
    ->getLanguages();
  $this
    ->assertIdentical(2, count($languages));

  // Check the properties of the language.
  $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.');

  // Rebuild the container so that the new languages are picked up by services
  // that hold a list of languages.
  $this
    ->rebuildContainer();
  $language = ConfigurableLanguage::load('es');
  $this
    ->assertIdentical($language
    ->getThirdPartySetting('lingotek', 'disabled', NULL), TRUE, 'The Spanish language is disabled');

  // Check the properties of the language.
  $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));

  // Check the properties of the language.
  $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.');

  // Rebuild the container so that the new languages are picked up by services
  // that hold a list of languages.
  $this
    ->rebuildContainer();
  $language = ConfigurableLanguage::load('es');
  $this
    ->assertIdentical($language
    ->getThirdPartySetting('lingotek', 'disabled', NULL), FALSE, 'The Spanish language is enabled');
}