You are here

function TMGMTGoogleTestCase::testGoogle in TMGMT Translator Google 7

Tests basic API methods of the plugin.

File

./tmgmt_google.test, line 34
Test cases for the google translator module.

Class

TMGMTGoogleTestCase
Basic tests for the google translator.

Code

function testGoogle() {
  $translator = $this
    ->createTranslator();
  $translator->plugin = 'google';
  $translator
    ->save();

  /**
   * @var TMGMTGoogleTranslatorPluginController $plugin
   */
  $plugin = $translator
    ->getController();
  $this
    ->assertTrue(is_a($plugin, 'TMGMTGoogleTranslatorPluginController'), t('Plugin initialization - we expect TMGMTGoogleTranslatorPluginController type.'));

  // Override plugin params to query tmgmt_google_test mock service instead
  // of Google Translate service.
  $plugin
    ->setQParamName('_q');
  $plugin
    ->setTranslatorURL(url('tmgmt_google_test', array(
    'absolute' => TRUE,
  )));
  $job = $this
    ->createJob();
  $job->translator = $translator->name;
  $item = $job
    ->addItem('test_source', 'test', '1');
  $item->data = array(
    'wrapper' => array(
      '#text' => 'Hello world',
    ),
  );
  $item
    ->save();
  $this
    ->assertFalse($job
    ->isTranslatable(), 'Check if the translator is not
                       available at this point because we did not define the API
                       parameters.');

  // Save a wrong api key.
  $translator->settings['api_key'] = 'wrong key';
  $translator
    ->save();
  $t = $job
    ->getTranslator();
  $languages = $t
    ->getSupportedTargetLanguages('en');
  $this
    ->assertTrue(empty($languages), t('We can not get the languages using wrong api parameters.'));

  // Save a correct api key.
  $translator->settings['api_key'] = 'correct key';
  $translator
    ->save();

  // Make sure the translator returns the correct supported target languages.
  $t = $job
    ->getTranslator();
  cache_clear_all('languages:' . $t->name, 'cache_tmgmt');
  $languages = $t
    ->getSupportedTargetLanguages('en');
  $this
    ->assertTrue(isset($languages['de']));
  $this
    ->assertTrue(isset($languages['fr']));

  // As we requested source language english it should not be included.
  $this
    ->assertTrue(!isset($languages['en']));
  $this
    ->assertTrue($job
    ->isTranslatable());
  $job
    ->requestTranslation();

  // Now it should be needs review.
  foreach ($job
    ->getItems() as $item) {
    $this
      ->assertTrue($item
      ->isNeedsReview());
  }
  $items = $job
    ->getItems();
  $item = end($items);
  $data = $item
    ->getData();
  $this
    ->assertEqual('Hallo Welt', $data['wrapper']['#translation']['#text']);
}