You are here

public function LingotekDashboardTest::testDisableLanguage in Lingotek Translation 3.6.x

Same name and namespace in other branches
  1. 8.2 tests/src/Functional/LingotekDashboardTest.php \Drupal\Tests\lingotek\Functional\LingotekDashboardTest::testDisableLanguage()
  2. 4.0.x tests/src/Functional/LingotekDashboardTest.php \Drupal\Tests\lingotek\Functional\LingotekDashboardTest::testDisableLanguage()
  3. 3.0.x tests/src/Functional/LingotekDashboardTest.php \Drupal\Tests\lingotek\Functional\LingotekDashboardTest::testDisableLanguage()
  4. 3.1.x tests/src/Functional/LingotekDashboardTest.php \Drupal\Tests\lingotek\Functional\LingotekDashboardTest::testDisableLanguage()
  5. 3.2.x tests/src/Functional/LingotekDashboardTest.php \Drupal\Tests\lingotek\Functional\LingotekDashboardTest::testDisableLanguage()
  6. 3.3.x tests/src/Functional/LingotekDashboardTest.php \Drupal\Tests\lingotek\Functional\LingotekDashboardTest::testDisableLanguage()
  7. 3.4.x tests/src/Functional/LingotekDashboardTest.php \Drupal\Tests\lingotek\Functional\LingotekDashboardTest::testDisableLanguage()
  8. 3.5.x tests/src/Functional/LingotekDashboardTest.php \Drupal\Tests\lingotek\Functional\LingotekDashboardTest::testDisableLanguage()
  9. 3.7.x tests/src/Functional/LingotekDashboardTest.php \Drupal\Tests\lingotek\Functional\LingotekDashboardTest::testDisableLanguage()
  10. 3.8.x tests/src/Functional/LingotekDashboardTest.php \Drupal\Tests\lingotek\Functional\LingotekDashboardTest::testDisableLanguage()

Tests that we can disable languages in the dashboard.

File

tests/src/Functional/LingotekDashboardTest.php, line 348

Class

LingotekDashboardTest
Tests the Lingotek dashboard.

Namespace

Drupal\Tests\lingotek\Functional

Code

public function testDisableLanguage() {
  $url = Url::fromRoute('lingotek.dashboard_endpoint')
    ->setAbsolute()
    ->toString();

  // Add a language.
  $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));

  // Rebuild the container so that the new languages are picked up by services
  // that hold a list of languages.
  $this
    ->rebuildContainer();

  /** @var \Drupal\Core\Language\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->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']);

  // Rebuild the container so that the new languages are picked up by services
  // that hold a list of languages.
  $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');

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

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

  // 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');
}