function TranslationsTest::setUp

Same name in other branches
  1. 9 core/modules/media_library/tests/src/FunctionalJavascript/TranslationsTest.php \Drupal\Tests\media_library\FunctionalJavascript\TranslationsTest::setUp()
  2. 10 core/modules/media_library/tests/src/FunctionalJavascript/TranslationsTest.php \Drupal\Tests\media_library\FunctionalJavascript\TranslationsTest::setUp()
  3. 11.x core/modules/media_library/tests/src/FunctionalJavascript/TranslationsTest.php \Drupal\Tests\media_library\FunctionalJavascript\TranslationsTest::setUp()

Overrides BrowserTestBase::setUp

File

core/modules/media_library/tests/src/FunctionalJavascript/TranslationsTest.php, line 45

Class

TranslationsTest
Tests media library for translatable media.

Namespace

Drupal\Tests\media_library\FunctionalJavascript

Code

protected function setUp() {
    parent::setUp();
    // Create some languages.
    foreach ([
        'nl',
        'es',
    ] as $langcode) {
        ConfigurableLanguage::createFromLangcode($langcode)->save();
    }
    // Create an image media type and article node type.
    $this->createMediaType('image', [
        'id' => 'image',
    ]);
    $this->drupalCreateContentType([
        'type' => 'article',
        'name' => 'Article',
    ]);
    // Make the media translatable and ensure the change is picked up.
    \Drupal::service('content_translation.manager')->setEnabled('media', 'image', TRUE);
    // Create a media reference field on articles.
    $this->createEntityReferenceField('node', 'article', 'field_media', 'Media', 'media', 'default', [
        'target_bundles' => [
            'image',
        ],
    ], FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
    // Add the media field to the form display.
    \Drupal::service('entity_display.repository')->getFormDisplay('node', 'article')
        ->setComponent('field_media', [
        'type' => 'media_library_widget',
    ])
        ->save();
    // Create a file to user for our images.
    $image = File::create([
        'uri' => $this->getTestFiles('image')[0]->uri,
    ]);
    $image->setPermanent();
    $image->save();
    // Create a translated and untranslated media item in each language.
    $media_items = [
        [
            'nl' => 'Eekhoorn',
            'es' => 'Ardilla',
        ],
        [
            'es' => 'Zorro',
            'nl' => 'Vos',
        ],
        [
            'nl' => 'Hert',
        ],
        [
            'es' => 'Tejón',
        ],
    ];
    foreach ($media_items as $translations) {
        $default_langcode = key($translations);
        $default_name = array_shift($translations);
        $media = Media::create([
            'name' => $default_name,
            'bundle' => 'image',
            'field_media_image' => $image,
            'langcode' => $default_langcode,
        ]);
        foreach ($translations as $langcode => $name) {
            $media->addTranslation($langcode, [
                'name' => $name,
            ]);
        }
        $media->save();
    }
    $user = $this->drupalCreateUser([
        'access administration pages',
        'access content',
        'access media overview',
        'edit own article content',
        'create article content',
        'administer media',
    ]);
    $this->drupalLogin($user);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.