You are here

public function LingotekDashboardTest::testDisabledLanguageInStats in Lingotek Translation 8

Tests that disabled language appear as disabled in stats.

File

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

Class

LingotekDashboardTest
Tests the Lingotek dashboard.

Namespace

Drupal\lingotek\Tests

Code

public function testDisabledLanguageInStats() {

  // 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 stats.
  $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']);

  // Disable Spanish.
  $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']);

  // Check the stats.
  $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']);
}