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