outline-8.x-1.x-dev/tests/src/Functional/EntryTranslationUITest.php

tests/src/Functional/EntryTranslationUITest.php
<?php

namespace Drupal\Tests\outline\Functional;

use Drupal\Core\Database\Database;
use Drupal\Tests\content_translation\Functional\ContentTranslationUITestBase;
use Drupal\Core\Language\LanguageInterface;
use Drupal\outline\Entity\Outline;

/**
 * Tests the Entry Translation UI.
 *
 * @group outline
 */
class EntryTranslationUITest extends ContentTranslationUITestBase {

  /**
   * The outline used for creating entries.
   *
   * @var \Drupal\outline\OutlineInterface
   */
  protected $outline;

  /**
   * Modules to enable.
   *
   * @var array
   */
  protected static $modules = ['language', 'content_translation', 'outline'];

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'classy';

  protected function setUp(): void {
    $this->entityTypeId = 'outline_entry';
    $this->bundle = 'tags';
    parent::setUp();
  }

  /**
   * {@inheritdoc}
   */
  protected function setupBundle() {
    parent::setupBundle();

    // Create a outline.
    $this->outline = Outline::create([
      'name' => $this->bundle,
      'description' => $this->randomMachineName(),
      'oid' => $this->bundle,
      'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
      'weight' => mt_rand(0, 10),
    ]);
    $this->outline->save();
  }

  /**
   * {@inheritdoc}
   */
  protected function getTranslatorPermissions() {
    return array_merge(parent::getTranslatorPermissions(), ['administer outline']);
  }

  /**
   * {@inheritdoc}
   */
  protected function getNewEntityValues($langcode) {
    return ['name' => $this->randomMachineName()] + parent::getNewEntityValues($langcode);
  }

  /**
   * Returns an edit array containing the values to be posted.
   */
  protected function getEditValues($values, $langcode, $new = FALSE) {
    $edit = parent::getEditValues($values, $langcode, $new);

    // To be able to post values for the configurable base fields (name,
    // description) have to be suffixed with [0][value].
    foreach ($edit as $property => $value) {
      foreach (['name', 'description'] as $key) {
        if ($property == $key) {
          $edit[$key . '[0][value]'] = $value;
          unset($edit[$property]);
        }
      }
    }
    return $edit;
  }

  /**
   * {@inheritdoc}
   */
  public function testTranslationUI() {
    parent::testTranslationUI();

    // Make sure that no row was inserted for outline outlines which do
    // not have translations enabled.
    $rows = Database::getConnection()->query('SELECT tid, count(tid) AS count FROM {outline_entry_field_data} WHERE vid <> :vid GROUP BY tid', [':vid' => $this->bundle])->fetchAll();
    foreach ($rows as $row) {
      $this->assertTrue($row->count < 2, 'Entry does not have translations.');
    }
  }

  /**
   * Tests translate link on outline entry list.
   */
  public function testTranslateLinkOutlineAdminPage() {
    $this->drupalLogin($this->drupalCreateUser(array_merge(parent::getTranslatorPermissions(), ['access administration pages', 'administer outline'])));

    $values = [
      'name' => $this->randomMachineName(),
    ];
    $translatable_tid = $this->createEntity($values, $this->langcodes[0], $this->outline->id());

    // Create an untranslatable outline.
    $untranslatable_outline = Outline::create([
      'name' => 'untranslatable_outline',
      'description' => $this->randomMachineName(),
      'oid' => 'untranslatable_voc',
      'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
      'weight' => mt_rand(0, 10),
    ]);
    $untranslatable_outline->save();

    $values = [
      'name' => $this->randomMachineName(),
    ];
    $untranslatable_tid = $this->createEntity($values, $this->langcodes[0], $untranslatable_outline->id());

    // Verify translation links.
    $this->drupalGet('admin/structure/outline/manage/' . $this->outline->id() . '/overview');
    $this->assertSession()->statusCodeEquals(200);
    $this->assertLinkByHref('entry/' . $translatable_tid . '/translations', 0, 'The translations link exists for a translatable outline.');
    $this->assertLinkByHref('entry/' . $translatable_tid . '/edit', 0, 'The edit link exists for a translatable outline.');

    $this->drupalGet('admin/structure/outline/manage/' . $untranslatable_outline->id() . '/overview');
    $this->assertSession()->statusCodeEquals(200);
    $this->assertLinkByHref('entry/' . $untranslatable_tid . '/edit');
    $this->assertNoLinkByHref('entry/' . $untranslatable_tid . '/translations');
  }

  /**
   * {@inheritdoc}
   */
  protected function doTestTranslationEdit() {
    $storage = $this->container->get('entity_type.manager')
      ->getStorage($this->entityTypeId);
    $storage->resetCache([$this->entityId]);
    $entity = $storage->load($this->entityId);
    $languages = $this->container->get('language_manager')->getLanguages();

    foreach ($this->langcodes as $langcode) {
      // We only want to test the title for non-english translations.
      if ($langcode != 'en') {
        $options = ['language' => $languages[$langcode]];
        $url = $entity->toUrl('edit-form', $options);
        $this->drupalGet($url);

        $title = t('@title [%language translation]', [
          '@title' => $entity->getTranslation($langcode)->label(),
          '%language' => $languages[$langcode]->getName(),
        ]);
        $this->assertRaw($title);
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  protected function doTestPublishedStatus() {
    $storage = $this->container->get('entity_type.manager')
      ->getStorage($this->entityTypeId);
    $storage->resetCache([$this->entityId]);
    $entity = $storage->load($this->entityId);
    $languages = $this->container->get('language_manager')->getLanguages();

    $statuses = [
      TRUE,
      FALSE,
    ];

    foreach ($statuses as $index => $value) {
      // (Un)publish the entry translations and check that the translation
      // statuses are (un)published accordingly.
      foreach ($this->langcodes as $langcode) {
        $options = ['language' => $languages[$langcode]];
        $url = $entity->toUrl('edit-form', $options);
        $this->drupalPostForm($url, ['status[value]' => $value], t('Save'), $options);
      }
      $storage->resetCache([$this->entityId]);
      $entity = $storage->load($this->entityId);
      foreach ($this->langcodes as $langcode) {
        // The entry is created as unpublished thus we switch to the published
        // status first.
        $status = !$index;
        $translation = $entity->getTranslation($langcode);
        $this->assertEquals($status, $this->manager->getTranslationMetadata($translation)->isPublished(), 'The translation has been correctly unpublished.');
      }
    }
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc