Mercurial > hg > isophonics-drupal-site
comparison core/modules/media/tests/src/Kernel/MediaTranslationTest.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 129ea1e6d783 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\Tests\media\Kernel; | |
4 | |
5 use Drupal\Component\Render\FormattableMarkup; | |
6 use Drupal\language\Entity\ConfigurableLanguage; | |
7 | |
8 /** | |
9 * Tests multilanguage fields logic. | |
10 * | |
11 * @group media | |
12 */ | |
13 class MediaTranslationTest extends MediaKernelTestBase { | |
14 | |
15 /** | |
16 * Modules to install. | |
17 * | |
18 * @var array | |
19 */ | |
20 public static $modules = ['language']; | |
21 | |
22 /** | |
23 * The test media translation type. | |
24 * | |
25 * @var \Drupal\media\MediaTypeInterface | |
26 */ | |
27 protected $testTranslationMediaType; | |
28 | |
29 /** | |
30 * {@inheritdoc} | |
31 */ | |
32 protected function setUp() { | |
33 parent::setUp(); | |
34 | |
35 $this->installConfig(['language']); | |
36 | |
37 // Create a test media type for translations. | |
38 $this->testTranslationMediaType = $this->createMediaType('test_translation'); | |
39 | |
40 for ($i = 0; $i < 3; ++$i) { | |
41 $language_id = 'l' . $i; | |
42 ConfigurableLanguage::create([ | |
43 'id' => $language_id, | |
44 'label' => $this->randomString(), | |
45 ])->save(); | |
46 file_put_contents('public://' . $language_id . '.png', ''); | |
47 } | |
48 } | |
49 | |
50 /** | |
51 * Test translatable fields storage/retrieval. | |
52 */ | |
53 public function testTranslatableFieldSaveLoad() { | |
54 /** @var \Drupal\Core\Entity\EntityTypeInterface $entity_type */ | |
55 $entity_type = $this->container->get('entity_type.manager')->getDefinition('media'); | |
56 $this->assertTrue($entity_type->isTranslatable(), 'Media is translatable.'); | |
57 | |
58 // Prepare the field translations. | |
59 $source_field_definition = $this->testTranslationMediaType->getSource()->getSourceFieldDefinition($this->testTranslationMediaType); | |
60 $source_field_storage = $source_field_definition->getFieldStorageDefinition(); | |
61 /** @var \Drupal\Core\Entity\Sql\SqlContentEntityStorage $media_storage */ | |
62 $media_storage = $this->container->get('entity_type.manager')->getStorage('media'); | |
63 /** @var \Drupal\media\Entity\Media $media */ | |
64 $media = $media_storage->create([ | |
65 'bundle' => $this->testTranslationMediaType->id(), | |
66 'name' => 'Unnamed', | |
67 ]); | |
68 | |
69 $field_translations = []; | |
70 $available_langcodes = array_keys($this->container->get('language_manager')->getLanguages()); | |
71 $media->set('langcode', reset($available_langcodes)); | |
72 foreach ($available_langcodes as $langcode) { | |
73 $values = []; | |
74 for ($i = 0; $i < $source_field_storage->getCardinality(); $i++) { | |
75 $values[$i]['value'] = $this->randomString(); | |
76 } | |
77 $field_translations[$langcode] = $values; | |
78 $translation = $media->hasTranslation($langcode) ? $media->getTranslation($langcode) : $media->addTranslation($langcode); | |
79 $translation->{$source_field_definition->getName()}->setValue($field_translations[$langcode]); | |
80 } | |
81 | |
82 // Save and reload the field translations. | |
83 $media->save(); | |
84 $media_storage->resetCache(); | |
85 $media = $media_storage->load($media->id()); | |
86 | |
87 // Check if the correct source field values were saved/loaded. | |
88 foreach ($field_translations as $langcode => $items) { | |
89 /** @var \Drupal\media\MediaInterface $media_translation */ | |
90 $media_translation = $media->getTranslation($langcode); | |
91 $result = TRUE; | |
92 foreach ($items as $delta => $item) { | |
93 $result = $result && $item['value'] == $media_translation->{$source_field_definition->getName()}[$delta]->value; | |
94 } | |
95 $this->assertTrue($result, new FormattableMarkup('%language translation field value not correct.', ['%language' => $langcode])); | |
96 $this->assertEquals('public://' . $langcode . '.png', $media_translation->getSource()->getMetadata($media_translation, 'thumbnail_uri'), new FormattableMarkup('%language translation thumbnail metadata attribute is not correct.', ['%language' => $langcode])); | |
97 $this->assertEquals('public://' . $langcode . '.png', $media_translation->get('thumbnail')->entity->getFileUri(), new FormattableMarkup('%language translation thumbnail value is not correct.', ['%language' => $langcode])); | |
98 $this->assertEquals('Test Thumbnail ' . $langcode, $media_translation->getSource()->getMetadata($media_translation, 'test_thumbnail_alt'), new FormattableMarkup('%language translation thumbnail alt metadata attribute is not correct.', ['%language' => $langcode])); | |
99 $this->assertEquals('Test Thumbnail ' . $langcode, $media_translation->get('thumbnail')->alt, new FormattableMarkup('%language translation thumbnail alt value is not correct.', ['%language' => $langcode])); | |
100 } | |
101 } | |
102 | |
103 } |