Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / block_content / tests / src / Functional / BlockContentTranslationUITest.php
1 <?php
2
3 namespace Drupal\Tests\block_content\Functional;
4
5 use Drupal\block_content\Entity\BlockContent;
6 use Drupal\block_content\Entity\BlockContentType;
7 use Drupal\Tests\content_translation\Functional\ContentTranslationUITestBase;
8
9 /**
10  * Tests the block content translation UI.
11  *
12  * @group block_content
13  */
14 class BlockContentTranslationUITest extends ContentTranslationUITestBase {
15
16   /**
17    * Modules to enable.
18    *
19    * @var array
20    */
21   public static $modules = [
22     'language',
23     'content_translation',
24     'block',
25     'field_ui',
26     'block_content',
27   ];
28
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   /**
43    * {@inheritdoc}
44    */
45   protected function setUp() {
46     $this->entityTypeId = 'block_content';
47     $this->bundle = 'basic';
48     $this->testLanguageSelector = FALSE;
49     parent::setUp();
50
51     $this->drupalPlaceBlock('page_title_block');
52   }
53
54   /**
55    * {@inheritdoc}
56    */
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,
62       'revision' => FALSE,
63     ]);
64     $bundle->save();
65   }
66
67   /**
68    * {@inheritdoc}
69    */
70   public function getTranslatorPermissions() {
71     return array_merge(parent::getTranslatorPermissions(), [
72       'translate any entity',
73       'access administration pages',
74       'administer blocks',
75       'administer block_content fields',
76     ]);
77   }
78
79   /**
80    * Creates a custom block.
81    *
82    * @param bool|string $title
83    *   (optional) Title of block. When no value is given uses a random name.
84    *   Defaults to FALSE.
85    * @param bool|string $bundle
86    *   (optional) Bundle name. When no value is given, defaults to
87    *   $this->bundle. Defaults to FALSE.
88    *
89    * @return \Drupal\block_content\Entity\BlockContent
90    *   Created custom block.
91    */
92   protected function createBlockContent($title = FALSE, $bundle = FALSE) {
93     $title = $title ?: $this->randomMachineName();
94     $bundle = $bundle ?: $this->bundle;
95     $block_content = BlockContent::create([
96       'info' => $title,
97       'type' => $bundle,
98       'langcode' => 'en',
99     ]);
100     $block_content->save();
101     return $block_content;
102   }
103
104   /**
105    * {@inheritdoc}
106    */
107   protected function getNewEntityValues($langcode) {
108     return ['info' => mb_strtolower($this->randomMachineName())] + parent::getNewEntityValues($langcode);
109   }
110
111   /**
112    * Returns an edit array containing the values to be posted.
113    */
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]);
120       }
121     }
122     return $edit;
123   }
124
125   /**
126    * {@inheritdoc}
127    */
128   protected function doTestBasicTranslation() {
129     parent::doTestBasicTranslation();
130
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);
138     $entity->save();
139     $entity->addTranslation('it', $values);
140
141     try {
142       $message = 'Blocks can have translations with the same "info" value.';
143       $entity->save();
144       $this->pass($message);
145     }
146     catch (\Exception $e) {
147       $this->fail($message);
148     }
149
150     // Check that the translate operation link is shown.
151     $this->drupalGet('admin/structure/block/block-content');
152     $this->assertLinkByHref('block/' . $entity->id() . '/translations');
153   }
154
155   /**
156    * Test that no metadata is stored for a disabled bundle.
157    */
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,
164       'revision' => FALSE,
165     ]);
166     $bundle->save();
167
168     // Create a block content for each bundle.
169     $enabled_block_content = $this->createBlockContent();
170     $disabled_block_content = $this->createBlockContent(FALSE, $bundle->id());
171
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));
175   }
176
177   /**
178    * {@inheritdoc}
179    */
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();
186
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);
193
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(),
198         ]);
199         $this->assertRaw($title);
200       }
201     }
202   }
203
204 }