|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\Tests\layout_builder\Functional; |
| 4 | + |
| 5 | +use Drupal\block_content\Entity\BlockContentType; |
| 6 | +use Drupal\language\Entity\ConfigurableLanguage; |
| 7 | +use Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay; |
| 8 | +use Drupal\Tests\BrowserTestBase; |
| 9 | + |
| 10 | +/** |
| 11 | + * Tests Layout Builder functionality with multiple languages installed. |
| 12 | + * |
| 13 | + * @group layout_builder |
| 14 | + */ |
| 15 | +class LayoutBuilderMultilingualTest extends BrowserTestBase { |
| 16 | + |
| 17 | + /** |
| 18 | + * {@inheritdoc} |
| 19 | + */ |
| 20 | + protected static $modules = [ |
| 21 | + 'layout_builder', |
| 22 | + 'node', |
| 23 | + 'block_content', |
| 24 | + 'content_translation', |
| 25 | + 'locale', |
| 26 | + ]; |
| 27 | + |
| 28 | + /** |
| 29 | + * {@inheritdoc} |
| 30 | + */ |
| 31 | + protected function setUp() { |
| 32 | + parent::setUp(); |
| 33 | + |
| 34 | + // @todo The Layout Builder UI relies on local tasks; fix in |
| 35 | + // https://www.drupal.org/project/drupal/issues/2917777. |
| 36 | + $this->drupalPlaceBlock('local_tasks_block'); |
| 37 | + |
| 38 | + // There must be more than one block type available to trigger |
| 39 | + // \Drupal\layout_builder\Controller\ChooseBlockController::inlineBlockList(). |
| 40 | + BlockContentType::create([ |
| 41 | + 'id' => 'first_type', |
| 42 | + 'label' => 'First type', |
| 43 | + ])->save(); |
| 44 | + BlockContentType::create([ |
| 45 | + 'id' => 'second_type', |
| 46 | + 'label' => 'Second type', |
| 47 | + ])->save(); |
| 48 | + |
| 49 | + // Create a translatable content type with layout overrides enabled. |
| 50 | + $this->createContentType([ |
| 51 | + 'type' => 'bundle_with_section_field', |
| 52 | + ]); |
| 53 | + $this->container->get('content_translation.manager')->setEnabled('node', 'bundle_with_section_field', TRUE); |
| 54 | + LayoutBuilderEntityViewDisplay::load('node.bundle_with_section_field.default') |
| 55 | + ->enableLayoutBuilder() |
| 56 | + ->setOverridable() |
| 57 | + ->save(); |
| 58 | + |
| 59 | + // Create a second language. |
| 60 | + ConfigurableLanguage::createFromLangcode('es')->save(); |
| 61 | + |
| 62 | + // Create a node and translate it. |
| 63 | + $node = $this->createNode([ |
| 64 | + 'type' => 'bundle_with_section_field', |
| 65 | + 'title' => 'The untranslated node title', |
| 66 | + ]); |
| 67 | + $node->addTranslation('es', [ |
| 68 | + 'title' => 'The translated node title', |
| 69 | + ]); |
| 70 | + $node->save(); |
| 71 | + |
| 72 | + $this->drupalLogin($this->createUser([ |
| 73 | + 'configure any layout', |
| 74 | + 'translate interface', |
| 75 | + ])); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Tests that custom blocks are available for translated entities. |
| 80 | + */ |
| 81 | + public function testCustomBlocks() { |
| 82 | + // Check translated and untranslated entities before translating the string. |
| 83 | + $this->assertCustomBlocks('node/1'); |
| 84 | + $this->assertCustomBlocks('es/node/1'); |
| 85 | + |
| 86 | + // Translate the 'Inline blocks' string used as a category in |
| 87 | + // \Drupal\layout_builder\Controller\ChooseBlockController::inlineBlockList(). |
| 88 | + $this->drupalPostForm('admin/config/regional/translate', ['string' => 'Inline blocks'], 'Filter'); |
| 89 | + $this->drupalPostForm(NULL, ['Translated string (Spanish)' => 'Bloques en linea'], 'Save translations'); |
| 90 | + |
| 91 | + // Check translated and untranslated entities after translating the string. |
| 92 | + $this->assertCustomBlocks('node/1'); |
| 93 | + $this->assertCustomBlocks('es/node/1'); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Asserts that custom blocks are available. |
| 98 | + * |
| 99 | + * @param string $url |
| 100 | + * The URL for a Layout Builder enabled entity. |
| 101 | + */ |
| 102 | + protected function assertCustomBlocks($url) { |
| 103 | + $page = $this->getSession()->getPage(); |
| 104 | + $assert_session = $this->assertSession(); |
| 105 | + |
| 106 | + $this->drupalGet($url); |
| 107 | + $page->clickLink('Layout'); |
| 108 | + $page->clickLink('Add Block'); |
| 109 | + $page->clickLink('Create custom block'); |
| 110 | + $assert_session->linkExists('First type'); |
| 111 | + $assert_session->linkExists('Second type'); |
| 112 | + } |
| 113 | + |
| 114 | +} |
0 commit comments