BlockContentTranslationUITest.php 4.13 KB
Newer Older
1
2
<?php

3
namespace Drupal\Tests\block_content\Functional;
4

5
use Drupal\block_content\Entity\BlockContentType;
6
use Drupal\Tests\content_translation\Functional\ContentTranslationUITestBase;
7
8

/**
9
 * Tests the block content translation UI.
10
11
 *
 * @group block_content
12
 */
13
class BlockContentTranslationUITest extends ContentTranslationUITestBase {
14
15
16
17
18
19

  /**
   * Modules to enable.
   *
   * @var array
   */
20
  protected static $modules = [
21
    'language',
22
    'content_translation',
23
24
    'block',
    'field_ui',
25
    'block_content',
26
  ];
27

28
29
30
31
32
  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'classy';

33
34
35
36
37
  /**
   * {@inheritdoc}
   */
  protected $defaultCacheContexts = [
    'languages:language_interface',
38
    'session',
39
    'theme',
40
41
    'url.path',
    'url.query_args',
42
43
44
45
    'user.permissions',
    'user.roles:authenticated',
  ];

46
  /**
47
   * {@inheritdoc}
48
   */
49
  protected function setUp(): void {
50
    $this->entityTypeId = 'block_content';
51
52
53
    $this->bundle = 'basic';
    $this->testLanguageSelector = FALSE;
    parent::setUp();
54
55

    $this->drupalPlaceBlock('page_title_block');
56
57
  }

58
59
60
61
62
  /**
   * {@inheritdoc}
   */
  protected function setupBundle() {
    // Create the basic bundle since it is provided by standard.
63
    $bundle = BlockContentType::create([
64
65
      'id' => $this->bundle,
      'label' => $this->bundle,
66
      'revision' => FALSE,
67
    ]);
68
69
70
    $bundle->save();
  }

71
  /**
72
   * {@inheritdoc}
73
74
   */
  public function getTranslatorPermissions() {
75
    return array_merge(parent::getTranslatorPermissions(), [
76
77
78
      'translate any entity',
      'access administration pages',
      'administer blocks',
79
      'administer block_content fields',
80
    ]);
81
82
83
  }

  /**
84
   * {@inheritdoc}
85
86
   */
  protected function getNewEntityValues($langcode) {
87
    return ['info' => mb_strtolower($this->randomMachineName())] + parent::getNewEntityValues($langcode);
88
89
  }

90
91
92
93
94
95
96
97
98
99
100
101
102
103
  /**
   * Returns an edit array containing the values to be posted.
   */
  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;
  }

104
105
106
107
108
109
110
111
112
113
  /**
   * {@inheritdoc}
   */
  protected function doTestBasicTranslation() {
    parent::doTestBasicTranslation();

    // Ensure that a block translation can be created using the same description
    // as in the original language.
    $default_langcode = $this->langcodes[0];
    $values = $this->getNewEntityValues($default_langcode);
114
    $storage = \Drupal::entityTypeManager()->getStorage($this->entityTypeId);
115
    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
116
    $entity = $storage->create(['type' => 'basic'] + $values);
117
118
119
120
121
122
123
124
125
126
127
    $entity->save();
    $entity->addTranslation('it', $values);

    try {
      $message = 'Blocks can have translations with the same "info" value.';
      $entity->save();
      $this->pass($message);
    }
    catch (\Exception $e) {
      $this->fail($message);
    }
128
129
130
131

    // Check that the translate operation link is shown.
    $this->drupalGet('admin/structure/block/block-content');
    $this->assertLinkByHref('block/' . $entity->id() . '/translations');
132
133
  }

134
135
136
137
  /**
   * {@inheritdoc}
   */
  protected function doTestTranslationEdit() {
138
139
140
141
    $storage = $this->container->get('entity_type.manager')
      ->getStorage($this->entityTypeId);
    $storage->resetCache([$this->entityId]);
    $entity = $storage->load($this->entityId);
142
143
144
145
146
    $languages = $this->container->get('language_manager')->getLanguages();

    foreach ($this->langcodes as $langcode) {
      // We only want to test the title for non-english translations.
      if ($langcode != 'en') {
147
        $options = ['language' => $languages[$langcode]];
148
        $url = $entity->toUrl('edit-form', $options);
149
150
        $this->drupalGet($url);

151
        $title = t('<em>Edit @type</em> @title [%language translation]', [
152
153
154
          '@type' => $entity->bundle(),
          '@title' => $entity->getTranslation($langcode)->label(),
          '%language' => $languages[$langcode]->getName(),
155
        ]);
156
157
158
159
160
        $this->assertRaw($title);
      }
    }
  }

161
}