function ContentModerationTest::setUp

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

Overrides BrowserTestBase::setUp

File

core/modules/media_library/tests/src/FunctionalJavascript/ContentModerationTest.php, line 74

Class

ContentModerationTest
Tests media library integration with content moderation.

Namespace

Drupal\Tests\media_library\FunctionalJavascript

Code

protected function setUp() : void {
    parent::setUp();
    // Create an image media type and article node type.
    $this->createMediaType('image', [
        'id' => 'image',
    ]);
    $this->drupalCreateContentType([
        'type' => 'article',
        'name' => 'Article',
    ]);
    // 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.
    $form_display = \Drupal::service('entity_display.repository')->getFormDisplay('node', 'article', 'default');
    $form_display->setComponent('field_media', [
        'type' => 'media_library_widget',
    ])
        ->save();
    // Configure the "Editorial" workflow to apply to image media.
    $workflow = $this->createEditorialWorkflow();
    $workflow->getTypePlugin()
        ->addEntityTypeAndBundle('media', 'image');
    $workflow->save();
    $image = File::create([
        'uri' => $this->getTestFiles('image')[0]->uri,
    ]);
    $image->setPermanent();
    $image->save();
    // Create a draft, published and archived media item.
    $draft_media = Media::create([
        'name' => 'Hoglet',
        'bundle' => 'image',
        'field_media_image' => $image,
        'moderation_state' => 'draft',
    ]);
    $draft_media->save();
    $published_media = Media::create([
        'name' => 'Panda',
        'bundle' => 'image',
        'field_media_image' => $image,
        'moderation_state' => 'published',
    ]);
    $published_media->save();
    $archived_media = Media::create([
        'name' => 'Mammoth',
        'bundle' => 'image',
        'field_media_image' => $image,
        'moderation_state' => 'archived',
    ]);
    $archived_media->save();
    // Create some users for our tests. We want to check with user 1, a media
    // administrator with 'administer media' permissions, a user that has the
    // 'view media' permissions, a user that can 'view media' and 'view own
    // unpublished media', and a user that has 'view media' and 'view any
    // unpublished content' permissions.
    $this->userAdmin = $this->drupalCreateUser([
        'access administration pages',
        'access content',
        'access media overview',
        'edit own article content',
        'create article content',
        'administer media',
    ]);
    $this->userViewer = $this->drupalCreateUser([
        'access administration pages',
        'access content',
        'access media overview',
        'edit own article content',
        'create article content',
        'view media',
        'create media',
    ]);
    $this->userViewOwnUnpublished = $this->drupalCreateUser([
        'access administration pages',
        'access content',
        'access media overview',
        'edit own article content',
        'create article content',
        'view media',
        'view own unpublished media',
        'create media',
    ]);
    $this->userViewAnyUnpublished = $this->drupalCreateUser([
        'access administration pages',
        'access content',
        'access media overview',
        'edit own article content',
        'create article content',
        'view media',
        'create media',
        'view any unpublished content',
    ]);
}

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