BlockContentTranslationUITest.php 4.7 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\block_content\Tests\BlockContentTranslationUITest.
6 7
 */

8
namespace Drupal\block_content\Tests;
9

10
use Drupal\Component\Utility\Unicode;
11
use Drupal\content_translation\Tests\ContentTranslationUITest;
12 13

/**
14 15 16
 * Tests the node translation UI.
 *
 * @group block_content
17
 */
18
class BlockContentTranslationUITest extends ContentTranslationUITest {
19 20 21 22 23 24 25 26

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array(
    'language',
27
    'content_translation',
28 29
    'block',
    'field_ui',
30
    'block_content'
31 32 33 34 35
  );

  /**
   * Overrides \Drupal\simpletest\WebTestBase::setUp().
   */
36
  protected function setUp() {
37
    $this->entityTypeId = 'block_content';
38 39 40 41 42
    $this->bundle = 'basic';
    $this->testLanguageSelector = FALSE;
    parent::setUp();
  }

43 44 45 46 47 48 49 50 51 52 53 54 55
  /**
   * {@inheritdoc}
   */
  protected function setupBundle() {
    // Create the basic bundle since it is provided by standard.
    $bundle = entity_create('block_content_type', array(
      'id' => $this->bundle,
      'label' => $this->bundle,
      'revision' => FALSE
    ));
    $bundle->save();
  }

56
  /**
57
   * Overrides \Drupal\content_translation\Tests\ContentTranslationUITest::getTranslatorPermission().
58 59 60 61 62 63
   */
  public function getTranslatorPermissions() {
    return array_merge(parent::getTranslatorPermissions(), array(
      'translate any entity',
      'access administration pages',
      'administer blocks',
64
      'administer block_content fields'
65 66 67 68 69 70 71 72 73 74 75 76 77
    ));
  }

  /**
   * Creates a custom block.
   *
   * @param string $title
   *   (optional) Title of block. When no value is given uses a random name.
   *   Defaults to FALSE.
   * @param string $bundle
   *   (optional) Bundle name. When no value is given, defaults to
   *   $this->bundle. Defaults to FALSE.
   *
78
   * @return \Drupal\block_content\Entity\BlockContent
79 80
   *   Created custom block.
   */
81
  protected function createBlockContent($title = FALSE, $bundle = FALSE) {
82
    $title = ($title ? : $this->randomMachineName());
83
    $bundle = ($bundle ? : $this->bundle);
84
    $block_content = entity_create('block_content', array(
85 86 87 88
      'info' => $title,
      'type' => $bundle,
      'langcode' => 'en'
    ));
89 90
    $block_content->save();
    return $block_content;
91 92 93
  }

  /**
94
   * Overrides \Drupal\content_translation\Tests\ContentTranslationUITest::getNewEntityValues().
95 96
   */
  protected function getNewEntityValues($langcode) {
97
    return array('info' => Unicode::strtolower($this->randomMachineName())) + parent::getNewEntityValues($langcode);
98 99
  }

100 101 102 103 104 105 106 107 108 109 110 111 112 113
  /**
   * Returns an edit array containing the values to be posted.
   */
  protected function getEditValues($values, $langcode, $new = FALSE) {
    $edit = parent::getEditValues($values, $langcode, $new);
    foreach ($edit as $property => $value) {
      if ($property == 'info') {
        $edit['info[0][value]'] = $value;
        unset($edit[$property]);
      }
    }
    return $edit;
  }

114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
  /**
   * {@inheritdoc}
   */
  protected function doTestBasicTranslation() {
    parent::doTestBasicTranslation();

    // Ensure that a block translation can be created using the same description
    // as in the original language.
    $default_langcode = $this->langcodes[0];
    $values = $this->getNewEntityValues($default_langcode);
    $storage = \Drupal::entityManager()->getStorage($this->entityTypeId);
    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    $entity = $storage->create(array('type' => 'basic') + $values);
    $entity->save();
    $entity->addTranslation('it', $values);

    try {
      $message = 'Blocks can have translations with the same "info" value.';
      $entity->save();
      $this->pass($message);
    }
    catch (\Exception $e) {
      $this->fail($message);
    }
138 139 140 141

    // Check that the translate operation link is shown.
    $this->drupalGet('admin/structure/block/block-content');
    $this->assertLinkByHref('block/' . $entity->id() . '/translations');
142 143
  }

144 145 146 147 148
  /**
   * Test that no metadata is stored for a disabled bundle.
   */
  public function testDisabledBundle() {
    // Create a bundle that does not have translation enabled.
149
    $disabled_bundle = $this->randomMachineName();
150
    $bundle = entity_create('block_content_type', array(
151 152 153 154 155 156 157
      'id' => $disabled_bundle,
      'label' => $disabled_bundle,
      'revision' => FALSE
    ));
    $bundle->save();

    // Create a node for each bundle.
158 159
    $enabled_block_content = $this->createBlockContent();
    $disabled_block_content = $this->createBlockContent(FALSE, $bundle->id());
160

161 162
    // Make sure that only a single row was inserted into the block table.
    $rows = db_query('SELECT * FROM {block_content_field_data} WHERE id = :id', array(':id' => $enabled_block_content->id()))->fetchAll();
163 164 165 166
    $this->assertEqual(1, count($rows));
  }

}