View source
<?php
namespace Drupal\Tests\block_content\Functional;
use Drupal\block_content\Entity\BlockContent;
use Drupal\block_content\Entity\BlockContentType;
use Drupal\Core\Database\Database;
use Drupal\Tests\content_translation\Functional\ContentTranslationUITestBase;
class BlockContentTranslationUITest extends ContentTranslationUITestBase {
public static $modules = [
'language',
'content_translation',
'block',
'field_ui',
'block_content',
];
protected $defaultTheme = 'classy';
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 = BlockContentType::create([
'id' => $this->bundle,
'label' => $this->bundle,
'revision' => FALSE,
]);
$bundle
->save();
}
public function getTranslatorPermissions() {
return array_merge(parent::getTranslatorPermissions(), [
'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 = BlockContent::create([
'info' => $title,
'type' => $bundle,
'langcode' => 'en',
]);
$block_content
->save();
return $block_content;
}
protected function getNewEntityValues($langcode) {
return [
'info' => mb_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::entityTypeManager()
->getStorage($this->entityTypeId);
$entity = $storage
->create([
'type' => 'basic',
] + $values);
$entity
->save();
$entity
->addTranslation('it', $values);
try {
$entity
->save();
} catch (\Exception $e) {
$this
->fail('Blocks can have translations with the same "info" value.');
}
$this
->drupalGet('admin/structure/block/block-content');
$this
->assertLinkByHref('block/' . $entity
->id() . '/translations');
}
public function testDisabledBundle() {
$disabled_bundle = $this
->randomMachineName();
$bundle = BlockContentType::create([
'id' => $disabled_bundle,
'label' => $disabled_bundle,
'revision' => FALSE,
]);
$bundle
->save();
$enabled_block_content = $this
->createBlockContent();
$disabled_block_content = $this
->createBlockContent(FALSE, $bundle
->id());
$rows = Database::getConnection()
->query('SELECT * FROM {block_content_field_data} WHERE id = :id', [
':id' => $enabled_block_content
->id(),
])
->fetchAll();
$this
->assertCount(1, $rows);
}
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) {
if ($langcode != 'en') {
$options = [
'language' => $languages[$langcode],
];
$url = $entity
->toUrl('edit-form', $options);
$this
->drupalGet($url);
$title = t('<em>Edit @type</em> @title [%language translation]', [
'@type' => $entity
->bundle(),
'@title' => $entity
->getTranslation($langcode)
->label(),
'%language' => $languages[$langcode]
->getName(),
]);
$this
->assertRaw($title);
}
}
}
}