3 namespace Drupal\Tests\block_content\Functional;
5 use Drupal\block_content\Entity\BlockContent;
6 use Drupal\block_content\Entity\BlockContentType;
7 use Drupal\Tests\content_translation\Functional\ContentTranslationUITestBase;
10 * Tests the block content translation UI.
12 * @group block_content
14 class BlockContentTranslationUITest extends ContentTranslationUITestBase {
21 public static $modules = [
23 'content_translation',
32 protected $defaultCacheContexts = [
33 'languages:language_interface',
39 'user.roles:authenticated',
45 protected function setUp() {
46 $this->entityTypeId = 'block_content';
47 $this->bundle = 'basic';
48 $this->testLanguageSelector = FALSE;
51 $this->drupalPlaceBlock('page_title_block');
57 protected function setupBundle() {
58 // Create the basic bundle since it is provided by standard.
59 $bundle = BlockContentType::create([
60 'id' => $this->bundle,
61 'label' => $this->bundle,
70 public function getTranslatorPermissions() {
71 return array_merge(parent::getTranslatorPermissions(), [
72 'translate any entity',
73 'access administration pages',
75 'administer block_content fields',
80 * Creates a custom block.
82 * @param bool|string $title
83 * (optional) Title of block. When no value is given uses a random name.
85 * @param bool|string $bundle
86 * (optional) Bundle name. When no value is given, defaults to
87 * $this->bundle. Defaults to FALSE.
89 * @return \Drupal\block_content\Entity\BlockContent
90 * Created custom block.
92 protected function createBlockContent($title = FALSE, $bundle = FALSE) {
93 $title = $title ?: $this->randomMachineName();
94 $bundle = $bundle ?: $this->bundle;
95 $block_content = BlockContent::create([
100 $block_content->save();
101 return $block_content;
107 protected function getNewEntityValues($langcode) {
108 return ['info' => mb_strtolower($this->randomMachineName())] + parent::getNewEntityValues($langcode);
112 * Returns an edit array containing the values to be posted.
114 protected function getEditValues($values, $langcode, $new = FALSE) {
115 $edit = parent::getEditValues($values, $langcode, $new);
116 foreach ($edit as $property => $value) {
117 if ($property == 'info') {
118 $edit['info[0][value]'] = $value;
119 unset($edit[$property]);
128 protected function doTestBasicTranslation() {
129 parent::doTestBasicTranslation();
131 // Ensure that a block translation can be created using the same description
132 // as in the original language.
133 $default_langcode = $this->langcodes[0];
134 $values = $this->getNewEntityValues($default_langcode);
135 $storage = \Drupal::entityManager()->getStorage($this->entityTypeId);
136 /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
137 $entity = $storage->create(['type' => 'basic'] + $values);
139 $entity->addTranslation('it', $values);
142 $message = 'Blocks can have translations with the same "info" value.';
144 $this->pass($message);
146 catch (\Exception $e) {
147 $this->fail($message);
150 // Check that the translate operation link is shown.
151 $this->drupalGet('admin/structure/block/block-content');
152 $this->assertLinkByHref('block/' . $entity->id() . '/translations');
156 * Test that no metadata is stored for a disabled bundle.
158 public function testDisabledBundle() {
159 // Create a bundle that does not have translation enabled.
160 $disabled_bundle = $this->randomMachineName();
161 $bundle = BlockContentType::create([
162 'id' => $disabled_bundle,
163 'label' => $disabled_bundle,
168 // Create a block content for each bundle.
169 $enabled_block_content = $this->createBlockContent();
170 $disabled_block_content = $this->createBlockContent(FALSE, $bundle->id());
172 // Make sure that only a single row was inserted into the block table.
173 $rows = db_query('SELECT * FROM {block_content_field_data} WHERE id = :id', [':id' => $enabled_block_content->id()])->fetchAll();
174 $this->assertEqual(1, count($rows));
180 protected function doTestTranslationEdit() {
181 $storage = $this->container->get('entity_type.manager')
182 ->getStorage($this->entityTypeId);
183 $storage->resetCache([$this->entityId]);
184 $entity = $storage->load($this->entityId);
185 $languages = $this->container->get('language_manager')->getLanguages();
187 foreach ($this->langcodes as $langcode) {
188 // We only want to test the title for non-english translations.
189 if ($langcode != 'en') {
190 $options = ['language' => $languages[$langcode]];
191 $url = $entity->urlInfo('edit-form', $options);
192 $this->drupalGet($url);
194 $title = t('<em>Edit @type</em> @title [%language translation]', [
195 '@type' => $entity->bundle(),
196 '@title' => $entity->getTranslation($langcode)->label(),
197 '%language' => $languages[$langcode]->getName(),
199 $this->assertRaw($title);