BlockContentTranslationUITest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. namespace Drupal\Tests\block_content\Functional;
  3. use Drupal\block_content\Entity\BlockContent;
  4. use Drupal\block_content\Entity\BlockContentType;
  5. use Drupal\Core\Database\Database;
  6. use Drupal\Tests\content_translation\Functional\ContentTranslationUITestBase;
  7. /**
  8. * Tests the block content translation UI.
  9. *
  10. * @group block_content
  11. */
  12. class BlockContentTranslationUITest extends ContentTranslationUITestBase {
  13. /**
  14. * Modules to enable.
  15. *
  16. * @var array
  17. */
  18. public static $modules = [
  19. 'language',
  20. 'content_translation',
  21. 'block',
  22. 'field_ui',
  23. 'block_content',
  24. ];
  25. /**
  26. * {@inheritdoc}
  27. */
  28. protected $defaultTheme = 'classy';
  29. /**
  30. * {@inheritdoc}
  31. */
  32. protected $defaultCacheContexts = [
  33. 'languages:language_interface',
  34. 'session',
  35. 'theme',
  36. 'url.path',
  37. 'url.query_args',
  38. 'user.permissions',
  39. 'user.roles:authenticated',
  40. ];
  41. /**
  42. * {@inheritdoc}
  43. */
  44. protected function setUp() {
  45. $this->entityTypeId = 'block_content';
  46. $this->bundle = 'basic';
  47. $this->testLanguageSelector = FALSE;
  48. parent::setUp();
  49. $this->drupalPlaceBlock('page_title_block');
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. protected function setupBundle() {
  55. // Create the basic bundle since it is provided by standard.
  56. $bundle = BlockContentType::create([
  57. 'id' => $this->bundle,
  58. 'label' => $this->bundle,
  59. 'revision' => FALSE,
  60. ]);
  61. $bundle->save();
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function getTranslatorPermissions() {
  67. return array_merge(parent::getTranslatorPermissions(), [
  68. 'translate any entity',
  69. 'access administration pages',
  70. 'administer blocks',
  71. 'administer block_content fields',
  72. ]);
  73. }
  74. /**
  75. * Creates a custom block.
  76. *
  77. * @param bool|string $title
  78. * (optional) Title of block. When no value is given uses a random name.
  79. * Defaults to FALSE.
  80. * @param bool|string $bundle
  81. * (optional) Bundle name. When no value is given, defaults to
  82. * $this->bundle. Defaults to FALSE.
  83. *
  84. * @return \Drupal\block_content\Entity\BlockContent
  85. * Created custom block.
  86. */
  87. protected function createBlockContent($title = FALSE, $bundle = FALSE) {
  88. $title = $title ?: $this->randomMachineName();
  89. $bundle = $bundle ?: $this->bundle;
  90. $block_content = BlockContent::create([
  91. 'info' => $title,
  92. 'type' => $bundle,
  93. 'langcode' => 'en',
  94. ]);
  95. $block_content->save();
  96. return $block_content;
  97. }
  98. /**
  99. * {@inheritdoc}
  100. */
  101. protected function getNewEntityValues($langcode) {
  102. return ['info' => mb_strtolower($this->randomMachineName())] + parent::getNewEntityValues($langcode);
  103. }
  104. /**
  105. * Returns an edit array containing the values to be posted.
  106. */
  107. protected function getEditValues($values, $langcode, $new = FALSE) {
  108. $edit = parent::getEditValues($values, $langcode, $new);
  109. foreach ($edit as $property => $value) {
  110. if ($property == 'info') {
  111. $edit['info[0][value]'] = $value;
  112. unset($edit[$property]);
  113. }
  114. }
  115. return $edit;
  116. }
  117. /**
  118. * {@inheritdoc}
  119. */
  120. protected function doTestBasicTranslation() {
  121. parent::doTestBasicTranslation();
  122. // Ensure that a block translation can be created using the same description
  123. // as in the original language.
  124. $default_langcode = $this->langcodes[0];
  125. $values = $this->getNewEntityValues($default_langcode);
  126. $storage = \Drupal::entityTypeManager()->getStorage($this->entityTypeId);
  127. /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
  128. $entity = $storage->create(['type' => 'basic'] + $values);
  129. $entity->save();
  130. $entity->addTranslation('it', $values);
  131. try {
  132. $entity->save();
  133. }
  134. catch (\Exception $e) {
  135. $this->fail('Blocks can have translations with the same "info" value.');
  136. }
  137. // Check that the translate operation link is shown.
  138. $this->drupalGet('admin/structure/block/block-content');
  139. $this->assertLinkByHref('block/' . $entity->id() . '/translations');
  140. }
  141. /**
  142. * Test that no metadata is stored for a disabled bundle.
  143. */
  144. public function testDisabledBundle() {
  145. // Create a bundle that does not have translation enabled.
  146. $disabled_bundle = $this->randomMachineName();
  147. $bundle = BlockContentType::create([
  148. 'id' => $disabled_bundle,
  149. 'label' => $disabled_bundle,
  150. 'revision' => FALSE,
  151. ]);
  152. $bundle->save();
  153. // Create a block content for each bundle.
  154. $enabled_block_content = $this->createBlockContent();
  155. $disabled_block_content = $this->createBlockContent(FALSE, $bundle->id());
  156. // Make sure that only a single row was inserted into the block table.
  157. $rows = Database::getConnection()->query('SELECT * FROM {block_content_field_data} WHERE id = :id', [':id' => $enabled_block_content->id()])->fetchAll();
  158. $this->assertCount(1, $rows);
  159. }
  160. /**
  161. * {@inheritdoc}
  162. */
  163. protected function doTestTranslationEdit() {
  164. $storage = $this->container->get('entity_type.manager')
  165. ->getStorage($this->entityTypeId);
  166. $storage->resetCache([$this->entityId]);
  167. $entity = $storage->load($this->entityId);
  168. $languages = $this->container->get('language_manager')->getLanguages();
  169. foreach ($this->langcodes as $langcode) {
  170. // We only want to test the title for non-english translations.
  171. if ($langcode != 'en') {
  172. $options = ['language' => $languages[$langcode]];
  173. $url = $entity->toUrl('edit-form', $options);
  174. $this->drupalGet($url);
  175. $title = t('<em>Edit @type</em> @title [%language translation]', [
  176. '@type' => $entity->bundle(),
  177. '@title' => $entity->getTranslation($langcode)->label(),
  178. '%language' => $languages[$langcode]->getName(),
  179. ]);
  180. $this->assertRaw($title);
  181. }
  182. }
  183. }
  184. }