View source
<?php
namespace Drupal\block_content\Tests;
use Drupal\Component\Utility\Unicode;
use Drupal\content_translation\Tests\ContentTranslationUITestBase;
class BlockContentTranslationUITest extends ContentTranslationUITestBase {
public static $modules = array(
'language',
'content_translation',
'block',
'field_ui',
'block_content',
);
protected $defaultCacheContexts = [
'languages:language_interface',
'session',
'theme',
'url.path',
'url.query_args',
'user.permissions',
'user.roles:authenticated',
];
protected function setUp() {
$this->entityTypeId = 'block_content';
$this->bundle = 'basic';
$this->testLanguageSelector = FALSE;
parent::setUp();
$this
->drupalPlaceBlock('page_title_block');
}
protected function setupBundle() {
$bundle = entity_create('block_content_type', array(
'id' => $this->bundle,
'label' => $this->bundle,
'revision' => FALSE,
));
$bundle
->save();
}
public function getTranslatorPermissions() {
return array_merge(parent::getTranslatorPermissions(), array(
'translate any entity',
'access administration pages',
'administer blocks',
'administer block_content fields',
));
}
protected function createBlockContent($title = FALSE, $bundle = FALSE) {
$title = $title ?: $this
->randomMachineName();
$bundle = $bundle ?: $this->bundle;
$block_content = entity_create('block_content', array(
'info' => $title,
'type' => $bundle,
'langcode' => 'en',
));
$block_content
->save();
return $block_content;
}
protected function getNewEntityValues($langcode) {
return array(
'info' => Unicode::strtolower($this
->randomMachineName()),
) + parent::getNewEntityValues($langcode);
}
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;
}
protected function doTestBasicTranslation() {
parent::doTestBasicTranslation();
$default_langcode = $this->langcodes[0];
$values = $this
->getNewEntityValues($default_langcode);
$storage = \Drupal::entityManager()
->getStorage($this->entityTypeId);
$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);
}
$this
->drupalGet('admin/structure/block/block-content');
$this
->assertLinkByHref('block/' . $entity
->id() . '/translations');
}
public function testDisabledBundle() {
$disabled_bundle = $this
->randomMachineName();
$bundle = entity_create('block_content_type', array(
'id' => $disabled_bundle,
'label' => $disabled_bundle,
'revision' => FALSE,
));
$bundle
->save();
$enabled_block_content = $this
->createBlockContent();
$disabled_block_content = $this
->createBlockContent(FALSE, $bundle
->id());
$rows = db_query('SELECT * FROM {block_content_field_data} WHERE id = :id', array(
':id' => $enabled_block_content
->id(),
))
->fetchAll();
$this
->assertEqual(1, count($rows));
}
protected function doTestTranslationEdit() {
$entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
$languages = $this->container
->get('language_manager')
->getLanguages();
foreach ($this->langcodes as $langcode) {
if ($langcode != 'en') {
$options = array(
'language' => $languages[$langcode],
);
$url = $entity
->urlInfo('edit-form', $options);
$this
->drupalGet($url);
$title = t('<em>Edit @type</em> @title [%language translation]', array(
'@type' => $entity
->bundle(),
'@title' => $entity
->getTranslation($langcode)
->label(),
'%language' => $languages[$langcode]
->getName(),
));
$this
->assertRaw($title);
}
}
}
}