You are here

LingotekNodeBulkDebugTest.php in Lingotek Translation 3.8.x

File

tests/src/Functional/LingotekNodeBulkDebugTest.php
View source
<?php

namespace Drupal\Tests\lingotek\Functional;

use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\language\Entity\ContentLanguageSettings;
use Drupal\node\Entity\Node;

/**
 * Tests debugging a node using the bulk management form.
 *
 * @group lingotek
 */
class LingotekNodeBulkDebugTest extends LingotekTestBase {

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

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

    // Create Article node types.
    $this
      ->drupalCreateContentType([
      'type' => 'article',
      'name' => 'Article',
    ]);

    // 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', 'article')
      ->setLanguageAlterable(TRUE)
      ->save();
    \Drupal::service('content_translation.manager')
      ->setEnabled('node', 'article', TRUE);
    drupal_static_reset();
    \Drupal::entityTypeManager()
      ->clearCachedDefinitions();
    $this
      ->applyEntityUpdates();

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

  /**
   * Tests that a node can be translated using the links on the management page.
   */
  public function testDebugOptionsDisplay() {

    // Login as admin.
    $this
      ->drupalLogin($this->rootUser);
    $this
      ->goToContentBulkManagementForm();

    // There is no 'debug' option group.
    $this
      ->assertEmpty($this
      ->xpath('//select[@id=:id]//optgroup[@label=:label]', [
      ':id' => 'edit-operation',
      ':label' => 'debug',
    ]), 'There is no debug group.');

    // Enable the debug operations.
    $this
      ->drupalGet('admin/lingotek/settings');
    $this
      ->drupalPostForm(NULL, [], t('Enable debug operations'));

    // Back to the bulk node management page.
    $this
      ->goToContentBulkManagementForm();

    // There should be a 'debug' option group with the right operation.
    $this
      ->assertNotEmpty($this
      ->xpath('//select[@id=:id]//optgroup[@label=:label]', [
      ':id' => 'edit-operation',
      ':label' => 'debug',
    ]), 'There is a debug group.');
    $this
      ->assertNotEmpty($this
      ->xpath('//select[@id=:id]//option[@value=:value]', [
      ':id' => 'edit-operation',
      ':value' => 'debug.export',
    ]), 'There is a debug export option.');
  }
  public function testDebugExport() {

    // Login as admin.
    $this
      ->drupalLogin($this->rootUser);

    // Create a node.
    $edit = [];
    $edit['title[0][value]'] = 'Llamas are cool';
    $edit['body[0][value]'] = 'Llamas are very cool';
    $edit['langcode[0][value]'] = 'en';
    $edit['lingotek_translation_management[lingotek_translation_profile]'] = 'manual';
    $this
      ->saveAndPublishNodeForm($edit);

    // Enable the debug operations.
    $this
      ->drupalGet('admin/lingotek/settings');
    $this
      ->drupalPostForm(NULL, [], t('Enable debug operations'));
    $this
      ->goToContentBulkManagementForm();
    $key = $this
      ->getBulkSelectionKey('en', 1);
    $edit = [
      $key => TRUE,
      $this
        ->getBulkOperationFormName() => 'debug.export',
    ];
    $this
      ->drupalPostForm(NULL, $edit, $this
      ->getApplyActionsButtonLabel());
    $this
      ->assertText('Exports available');

    // Download the file.
    $this
      ->clickLink('node.article.1.json');
    $response = json_decode($this
      ->getSession()
      ->getPage()
      ->getContent(), TRUE);
    $this
      ->assertIdentical('Llamas are cool', $response['title'][0]['value']);
    $this
      ->assertIdentical('Llamas are very cool', $response['body'][0]['value']);
    $this
      ->assertIdentical('article (node): Llamas are cool', $response['_debug']['title']);
    $this
      ->assertIdentical('manual', $response['_debug']['profile']);
    $this
      ->assertIdentical('en_US', $response['_debug']['source_locale']);
  }
  public function testDebugExportError() {

    // Login as admin.
    $this
      ->drupalLogin($this->rootUser);

    // Create a node.
    $node = Node::create([
      'title' => 'Llamas are cool',
      'type' => 'article',
    ]);
    $node
      ->save();

    /** @var \Drupal\lingotek\LingotekContentTranslationServiceInterface $translation_service */
    $translation_service = \Drupal::service('lingotek.content_translation');
    $translation_service
      ->deleteMetadata($node);

    // Enable the debug operations.
    $this
      ->drupalGet('admin/lingotek/settings');
    $this
      ->drupalPostForm(NULL, [], t('Enable debug operations'));
    $this
      ->goToContentBulkManagementForm();
    $key = $this
      ->getBulkSelectionKey('en', 1);
    $edit = [
      $key => TRUE,
      $this
        ->getBulkOperationFormName() => 'debug.export',
    ];
    $this
      ->drupalPostForm(NULL, $edit, $this
      ->getApplyActionsButtonLabel());
    $this
      ->assertText('The Article Llamas are cool has no profile assigned so it was not processed.');
    $this
      ->assertNoText('Exports available');
  }

}

Classes

Namesort descending Description
LingotekNodeBulkDebugTest Tests debugging a node using the bulk management form.