You are here

function EntityTranslationHookTestCase::testAPI in Entity Translation 7

Test whether hooks are properly fired when using the API.

File

tests/entity_translation.test, line 542
Tests for Entity translation module.

Class

EntityTranslationHookTestCase
Test CRUD hook invocation.

Code

function testAPI() {

  // Create Basic page in English.
  $node = $this
    ->createPage($this
    ->randomName(), $this
    ->randomName(), 'en');
  $handler = entity_translation_get_handler('node', $node);

  // Create a translation in Italian.
  $translation = array(
    'source' => 'en',
    'language' => 'it',
  );
  $handler
    ->setTranslation($translation);
  $handler
    ->saveTranslations();
  $node = node_load($node->nid, NULL, TRUE);
  $handler = entity_translation_get_handler('node', $node);
  $translations = $handler
    ->getTranslations();
  $this
    ->assertTrue(!empty($translations->data['it']), t('An Italian translation has been created'));
  $info = $this
    ->getHookInfo();
  $this
    ->assertTrue(!empty($info['insert']), t('Insert hook has been properly fired.'));

  // Check that the update hook is properly fired.
  $translation['status'] = 1;
  $handler
    ->setTranslation($translation);
  $handler
    ->saveTranslations();
  $info = $this
    ->getHookInfo();
  $this
    ->assertTrue(!empty($info['update']), t('Update hook has been properly fired.'));

  // Create a Spanish translation and update it before saving it.
  $translation = array(
    'source' => 'it',
    'language' => 'es',
  );
  $handler
    ->setTranslation($translation);
  $translation['status'] = 1;
  $handler
    ->setTranslation($translation);
  $handler
    ->saveTranslations();
  $node = node_load($node->nid, NULL, TRUE);
  $handler = entity_translation_get_handler('node', $node);
  $translations = $handler
    ->getTranslations();
  $this
    ->assertTrue(!empty($translations->data['es']), t('A Spanish translation has been created'));
  $info = $this
    ->getHookInfo();
  $this
    ->assertTrue(!empty($info['insert']), t('Insert hook has been properly fired.'));

  // Delete a translation after updating it without saving.
  $translation['status'] = 0;
  $handler
    ->setTranslation($translation);
  $handler
    ->removeTranslation('es');
  $handler
    ->saveTranslations();
  $info = $this
    ->getHookInfo();
  $this
    ->assertTrue(empty($info['update']), t('Update hook has not been fired.'));
  $info = $this
    ->getHookInfo('delete');
  $this
    ->assertTrue(!empty($info['es']), t('Delete hook has been properly fired.'));
}