You are here

LingotekContentTranslationDocumentUploadHookTest.php in Lingotek Translation 8

File

src/Tests/LingotekContentTranslationDocumentUploadHookTest.php
View source
<?php

namespace Drupal\lingotek\Tests;

use Drupal\Core\Url;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\language\Entity\ContentLanguageSettings;
use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface;

/**
 * Tests the hooks a node.
 *
 * @group lingotek
 */
class LingotekContentTranslationDocumentUploadHookTest extends LingotekTestBase {

  /**
   * Modules to install.
   *
   * @var array
   */
  public static $modules = [
    'block',
    'node',
    'image',
  ];

  /**
   * @var NodeInterface
   */
  protected $node;
  protected function setUp() {
    parent::setUp();

    // Place the actions and title block.
    $this
      ->drupalPlaceBlock('local_tasks_block');
    $this
      ->drupalPlaceBlock('page_title_block');

    // Create node types.
    $this
      ->drupalCreateContentType(array(
      'type' => 'animal',
      'name' => 'Animal',
    ));
    $this
      ->createImageField('field_image', 'animal');

    // Add a language.
    ConfigurableLanguage::createFromLangcode('es')
      ->setThirdPartySetting('lingotek', 'locale', 'es_MX')
      ->save();

    // Enable translation for the current entity type and ensure the change is
    // picked up.
    ContentLanguageSettings::loadByEntityTypeBundle('node', 'animal')
      ->setLanguageAlterable(TRUE)
      ->save();
    \Drupal::service('content_translation.manager')
      ->setEnabled('node', 'animal', TRUE);
    drupal_static_reset();
    \Drupal::entityManager()
      ->clearCachedDefinitions();
    \Drupal::service('entity.definition_update_manager')
      ->applyUpdates();

    // Rebuild the container so that the new languages are picked up by services
    // that hold a list of languages.
    $this
      ->rebuildContainer();
    $edit = [
      'node[animal][enabled]' => 1,
      'node[animal][profiles]' => 'automatic',
      'node[animal][fields][title]' => 1,
      'node[animal][fields][body]' => 1,
      'node[animal][fields][field_image]' => 1,
      'node[animal][fields][field_image:properties][alt]' => 'alt',
    ];
    $this
      ->drupalPostForm('admin/lingotek/settings', $edit, 'Save', [], [], 'lingoteksettings-tab-content-form');
  }

  /**
   * Tests that a node can be translated.
   */
  public function testNodeTranslation() {

    // Login as admin.
    $this
      ->drupalLogin($this->rootUser);
    $test_image = current($this
      ->drupalGetTestFiles('image'));

    // Create a node.
    $edit = array();
    $edit['title[0][value]'] = 'Llamas are cool';
    $edit['body[0][value]'] = 'Llamas are very cool';
    $edit['langcode[0][value]'] = 'en';
    $edit['files[field_image_0]'] = drupal_realpath($test_image->uri);
    $this
      ->drupalPostForm('node/add/animal', $edit, t('Preview'));
    unset($edit['files[field_image_0]']);
    $edit['field_image[0][alt]'] = 'Llamas are cool';
    $this
      ->drupalPostForm(NULL, $edit, t('Save and publish'));
    $this->node = Node::load(1);

    // Check that the configured fields have been uploaded, but also the one
    // added via the hook.
    $data = json_decode(\Drupal::state()
      ->get('lingotek.uploaded_content', '[]'), true);
    $this
      ->assertUploadedDataFieldCount($data, 4);
    $this
      ->assertTrue(isset($data['title'][0]['value']));
    $this
      ->assertEqual(1, count($data['body'][0]));
    $this
      ->assertTrue(isset($data['body'][0]['value']));
    $this
      ->assertEqual(1, count($data['field_image'][0]));
    $this
      ->assertTrue(isset($data['field_image'][0]['alt']));
    $this
      ->assertTrue(isset($data['animal_date']));
    $this
      ->assertEqual('2016-05-01', $data['animal_date']);

    // Assert he locale used was correct.
    $this
      ->assertIdentical('en_US', \Drupal::state()
      ->get('lingotek.uploaded_locale'));

    // Check that the profile used was the right one.
    $used_profile = \Drupal::state()
      ->get('lingotek.used_profile');
    $this
      ->assertIdentical('automatic', $used_profile, 'The automatic profile was used.');

    // Check that the url used was the right one.
    $uploaded_url = \Drupal::state()
      ->get('lingotek.uploaded_url');
    $this
      ->assertIdentical(\Drupal::request()
      ->getBasePath() . '/animal/2016/llamas-are-cool', $uploaded_url, 'The altered url was used.');
  }

}

Classes

Namesort descending Description
LingotekContentTranslationDocumentUploadHookTest Tests the hooks a node.