3 namespace Drupal\block_content\Tests;
5 use Drupal\block_content\Entity\BlockContent;
6 use Drupal\block_content\Entity\BlockContentType;
7 use Drupal\Component\Utility\Unicode;
8 use Drupal\content_translation\Tests\ContentTranslationUITestBase;
11 * Tests the block content translation UI.
13 * @group block_content
15 class BlockContentTranslationUITest extends ContentTranslationUITestBase {
22 public static $modules = [
24 'content_translation',
33 protected $defaultCacheContexts = [
34 'languages:language_interface',
40 'user.roles:authenticated',
46 protected function setUp() {
47 $this->entityTypeId = 'block_content';
48 $this->bundle = 'basic';
49 $this->testLanguageSelector = FALSE;
52 $this->drupalPlaceBlock('page_title_block');
58 protected function setupBundle() {
59 // Create the basic bundle since it is provided by standard.
60 $bundle = BlockContentType::create([
61 'id' => $this->bundle,
62 'label' => $this->bundle,
71 public function getTranslatorPermissions() {
72 return array_merge(parent::getTranslatorPermissions(), [
73 'translate any entity',
74 'access administration pages',
76 'administer block_content fields'
81 * Creates a custom block.
83 * @param bool|string $title
84 * (optional) Title of block. When no value is given uses a random name.
86 * @param bool|string $bundle
87 * (optional) Bundle name. When no value is given, defaults to
88 * $this->bundle. Defaults to FALSE.
90 * @return \Drupal\block_content\Entity\BlockContent
91 * Created custom block.
93 protected function createBlockContent($title = FALSE, $bundle = FALSE) {
94 $title = $title ?: $this->randomMachineName();
95 $bundle = $bundle ?: $this->bundle;
96 $block_content = BlockContent::create([
101 $block_content->save();
102 return $block_content;
108 protected function getNewEntityValues($langcode) {
109 return ['info' => Unicode::strtolower($this->randomMachineName())] + parent::getNewEntityValues($langcode);
113 * Returns an edit array containing the values to be posted.
115 protected function getEditValues($values, $langcode, $new = FALSE) {
116 $edit = parent::getEditValues($values, $langcode, $new);
117 foreach ($edit as $property => $value) {
118 if ($property == 'info') {
119 $edit['info[0][value]'] = $value;
120 unset($edit[$property]);
129 protected function doTestBasicTranslation() {
130 parent::doTestBasicTranslation();
132 // Ensure that a block translation can be created using the same description
133 // as in the original language.
134 $default_langcode = $this->langcodes[0];
135 $values = $this->getNewEntityValues($default_langcode);
136 $storage = \Drupal::entityManager()->getStorage($this->entityTypeId);
137 /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
138 $entity = $storage->create(['type' => 'basic'] + $values);
140 $entity->addTranslation('it', $values);
143 $message = 'Blocks can have translations with the same "info" value.';
145 $this->pass($message);
147 catch (\Exception $e) {
148 $this->fail($message);
151 // Check that the translate operation link is shown.
152 $this->drupalGet('admin/structure/block/block-content');
153 $this->assertLinkByHref('block/' . $entity->id() . '/translations');
157 * Test that no metadata is stored for a disabled bundle.
159 public function testDisabledBundle() {
160 // Create a bundle that does not have translation enabled.
161 $disabled_bundle = $this->randomMachineName();
162 $bundle = BlockContentType::create([
163 'id' => $disabled_bundle,
164 'label' => $disabled_bundle,
169 // Create a block content for each bundle.
170 $enabled_block_content = $this->createBlockContent();
171 $disabled_block_content = $this->createBlockContent(FALSE, $bundle->id());
173 // Make sure that only a single row was inserted into the block table.
174 $rows = db_query('SELECT * FROM {block_content_field_data} WHERE id = :id', [':id' => $enabled_block_content->id()])->fetchAll();
175 $this->assertEqual(1, count($rows));
181 protected function doTestTranslationEdit() {
182 $storage = $this->container->get('entity_type.manager')
183 ->getStorage($this->entityTypeId);
184 $storage->resetCache([$this->entityId]);
185 $entity = $storage->load($this->entityId);
186 $languages = $this->container->get('language_manager')->getLanguages();
188 foreach ($this->langcodes as $langcode) {
189 // We only want to test the title for non-english translations.
190 if ($langcode != 'en') {
191 $options = ['language' => $languages[$langcode]];
192 $url = $entity->urlInfo('edit-form', $options);
193 $this->drupalGet($url);
195 $title = t('<em>Edit @type</em> @title [%language translation]', [
196 '@type' => $entity->bundle(),
197 '@title' => $entity->getTranslation($langcode)->label(),
198 '%language' => $languages[$langcode]->getName(),
200 $this->assertRaw($title);