createMediaType example

/** * {@inheritdoc} */
  protected static $modules = ['field', 'media', 'media_test_source'];

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();
    $this->entity = $this->createMediaType('test');
  }

}
->getEditable('media.settings')
      ->set('standalone_url', TRUE)
      ->save(TRUE);
    $this->container->get('router.builder')->rebuild();
  }

  /** * {@inheritdoc} */
  protected function createEntity() {
    // Create a media type.     $mediaType = $this->createMediaType('test');

    // Create a media item.     $media = Media::create([
      'bundle' => $mediaType->id(),
      'name' => 'Unnamed',
    ]);
    $media->save();

    return $media;
  }

  

    $this->container->set('current_user', $user);

    $this->image = File::create([
      'uri' => $this->getTestFiles('image')[0]->uri,
      'uid' => 2,
    ]);
    $this->image->setPermanent();
    $this->image->save();

    // Create a sample media entity to be embedded.     $media_type = $this->createMediaType('image', ['id' => 'image']);
    EntityViewMode::create([
      'id' => 'media.foobar',
      'targetEntityType' => 'media',
      'status' => TRUE,
      'enabled' => TRUE,
      'label' => $this->randomMachineName(),
    ])->save();
    EntityViewDisplay::create([
      'targetEntityType' => 'media',
      'bundle' => $media_type->id(),
      'mode' => 'foobar',
      
protected function setUp(): void {
    parent::setUp();

    $this->installEntitySchema('user');
    $this->installEntitySchema('file');
    $this->installSchema('file', 'file_usage');
    $this->installSchema('system', 'sequences');
    $this->installEntitySchema('media');
    $this->installConfig(['field', 'system', 'image', 'file', 'media']);

    // Create a test media type.     $this->testMediaType = $this->createMediaType('test');
    // Create a test media type with constraints.     $this->testConstraintsMediaType = $this->createMediaType('test_constraints');

    $this->user = User::create([
      'name' => 'username',
      'status' => 1,
    ]);
    $this->user->save();
    $this->container->get('current_user')->setAccount($this->user);
  }

  
protected function setUp(): void {
    parent::setUp();
    // This is needed to provide the user cache context for a below assertion.     $this->drupalPlaceBlock('local_tasks_block');
  }

  /** * Tests some access control functionality. */
  public function testMediaAccess() {
    $assert_session = $this->assertSession();
    $media_type = $this->createMediaType('test');

    \Drupal::configFactory()
      ->getEditable('media.settings')
      ->set('standalone_url', TRUE)
      ->save(TRUE);

    $this->container->get('router.builder')->rebuild();

    // Create media.     $media = Media::create([
      'bundle' => $media_type->id(),
      

  protected function setUp(): void {
    $this->entityTypeId = 'media';
    $this->bundle = 'test';
    parent::setUp();
  }

  /** * {@inheritdoc} */
  public function setupBundle() {
    $this->createMediaType('test', [
      'id' => $this->bundle,
      'queue_thumbnail_downloads' => FALSE,
    ]);
  }

  /** * {@inheritdoc} */
  protected function getTranslatorPermissions() {
    return array_merge(parent::getTranslatorPermissions()[
      'administer media',
      
'locale',
    'media_library',
  ];

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();

    // Create a sample media entity to be embedded.     $this->createMediaType('image', ['id' => 'image', 'label' => 'Image']);
  }

  /** * Integration test to ensure that CKEditor 5 Plugins translations are loaded. */
  public function test(): void {
    $page = $this->getSession()->getPage();
    $assert_session = $this->assertSession();

    $this->createNewTextFormat($page$assert_session);
    $this->assertNotEmpty($assert_session->waitForElement('css', '.ckeditor5-toolbar-item-drupalMedia'));
    

  protected $defaultTheme = 'stark';

  /** * Tests that the oEmbed field widget shows the configured help text. */
  public function testFieldWidgetHelpText() {
    $account = $this->drupalCreateUser(['create media']);
    $this->drupalLogin($account);

    $media_type = $this->createMediaType('oembed:video');
    $source_field = $media_type->getSource()
      ->getSourceFieldDefinition($media_type)
      ->getName();

    /** @var \Drupal\field\Entity\FieldConfig $field */
    $field = FieldConfig::loadByName('media', $media_type->id()$source_field);
    $field->setDescription('This is help text for oEmbed field.')
      ->save();

    $this->drupalGet('media/add/' . $media_type->id());
    $assert_session = $this->assertSession();
    

    ];
  }

  /** * Tests that oEmbed media types' display can be configured correctly. */
  public function testDisplayConfiguration() {
    $account = $this->drupalCreateUser(['administer media display']);
    $this->drupalLogin($account);

    $media_type = $this->createMediaType('oembed:video');
    $this->drupalGet('/admin/structure/media/manage/' . $media_type->id() . '/display');
    $assert = $this->assertSession();
    $assert->statusCodeEquals(200);
    // Test that the formatter doesn't try to check applicability for fields     // which do not have a specific target bundle.     // @see https://www.drupal.org/project/drupal/issues/2976795.     $assert->pageTextNotContains('Can only flip STRING and INTEGER values!');
  }

  /** * Tests the oEmbed field formatter. * * @param string $url * The canonical URL of the media asset to test. * @param string $resource_url * The oEmbed resource URL of the media asset to test. * @param array $formatter_settings * Settings for the oEmbed field formatter. * @param array $selectors * An array of arrays. Each key is a CSS selector targeting an element in * the rendered output, and each value is an array of attributes, keyed by * name, that the element is expected to have. * @param bool $self_closing * Indicator if the HTML element is self closing i.e. <p/> vs <p></p>. * * @dataProvider providerRender */

  public function testAccess(array $permissions, array $entity_values$operation, AccessResultInterface $expected_result, array $expected_cache_contexts, array $expected_cache_tags) {
    // Set a fixed ID so the type specific permissions match.     $media_type = $this->createMediaType('test', [
      'id' => 'test',
    ]);

    $user = $this->createUser($permissions);

    $entity_values += [
      'status' => FALSE,
      'uid' => $user->id(),
      'bundle' => $media_type->id(),
    ];

    
/** * Tests the media thumbnail field formatter. */
  public function testRender() {
    $this->drupalLogin($this->adminUser);

    /** @var \Drupal\node\NodeStorage $node_storage */
    $node_storage = $this->container->get('entity_type.manager')->getStorage('node');

    // Create an image media type for testing the formatter.     $this->createMediaType('image', ['id' => 'image']);

    // Create an article content type.     $this->drupalCreateContentType([
      'type' => 'article',
      'name' => 'Article',
    ]);

    // Creates an entity reference field for media.     $field_storage = FieldStorageConfig::create([
      'field_name' => 'field_media_reference',
      'type' => 'entity_reference',
      
protected function setUp(): void {
    parent::setUp();
    $this->installEntitySchema('file');
    $this->installEntitySchema('user');
  }

  /** * @covers ::validate */
  public function testValidateEmptySource() {
    $media = Media::create([
      'bundle' => $this->createMediaType('oembed:video')->id(),
    ]);

    $constraint = new OEmbedResourceConstraint();

    // The media item has an empty source value, so the constraint validator     // should add a violation and return early before invoking the URL resolver.     $context = $this->prophesize(ExecutionContextInterface::class);
    $context->addViolation($constraint->invalidResourceMessage)->shouldBeCalled();

    $url_resolver = $this->prophesize(UrlResolverInterface::class);
    $url_resolver->getProviderByUrl(Argument::any())->shouldNotBeCalled();

    
protected static $modules = ['media'];

  /** * {@inheritdoc} */
  protected $defaultTheme = 'stark';

  /** * Tests template suggestions from media_theme_suggestions_media(). */
  public function testMediaThemeHookSuggestions() {
    $media_type = $this->createMediaType('test', [
      'queue_thumbnail_downloads' => FALSE,
    ]);

    // Create media item to be rendered.     $media = Media::create([
      'bundle' => $media_type->id(),
      'name' => 'Unnamed',
    ]);
    $media->save();
    $view_mode = 'full';

    
/** * {@inheritdoc} */
  protected $defaultTheme = 'stark';

  /** * Tests that the 'media_access' query tag is respected by Views. */
  public function testMediaEntityQueryAccess() {
    $this->container->get('module_installer')->install(['media']);

    $media_type = $this->createMediaType('test');
    $source_field = $media_type->getSource()
      ->getSourceFieldDefinition($media_type)
      ->getName();

    $hidden_media = Media::create([
      'bundle' => $media_type->id(),
      // This UUID should prevent this media item from being visible in the       // view.       // @see views_test_access_query_media_access_alter()       'uuid' => 'hidden-media',
      'name' => $this->randomString(),
      
FilterFormat::load('test_format')
      ))
    ));

    // Note that media_install() grants 'view media' to all users by default.     $this->adminUser = $this->drupalCreateUser([
      'use text format test_format',
      'bypass node access',
    ]);

    // Create a sample media entity to be embedded.     $this->createMediaType('image', ['id' => 'image']);
    File::create([
      'uri' => $this->getTestFiles('image')[0]->uri,
    ])->save();
    $this->media = Media::create([
      'bundle' => 'image',
      'name' => 'Screaming hairy armadillo',
      'field_media_image' => [
        [
          'target_id' => 1,
          'alt' => 'default alt',
          'title' => 'default title',
        ],
Home | Imprint | This part of the site doesn't use cookies.