You are here

trait SchedulerMediaSetupTrait in Scheduler 2.x

Additional setup trait for Scheduler tests that use Media.

This builds on the standard SchedulerSetupTrait.

Hierarchy

2 files declare their use of SchedulerMediaSetupTrait
SchedulerBrowserTestBase.php in tests/src/Functional/SchedulerBrowserTestBase.php
SchedulerJavascriptTestBase.php in tests/src/FunctionalJavascript/SchedulerJavascriptTestBase.php

File

tests/src/Traits/SchedulerMediaSetupTrait.php, line 12

Namespace

Drupal\Tests\scheduler\Traits
View source
trait SchedulerMediaSetupTrait {
  use MediaTypeCreationTrait;

  /**
   * The internal name of the standard media type created for testing.
   *
   * @var string
   */
  protected $mediaTypeName = 'test_video';

  /**
   * The readable label of the standard media type created for testing.
   *
   * @var string
   */
  protected $mediaTypeLabel = 'Test Video';

  /**
   * The media type object which is enabled for scheduling.
   *
   * @var \Drupal\media\MediaTypeInterface
   */
  protected $mediaType;

  /**
   * The internal name of the media type not enabled for scheduling.
   *
   * @var string
   */
  protected $nonSchedulerMediaTypeName = 'test_audio_not_enabled';

  /**
   * The readable label of the media type not enabled for scheduling.
   *
   * @var string
   */
  protected $nonSchedulerMediaTypeLabel = 'Test Audio - not for scheduling';

  /**
   * The media type object which is not enabled for scheduling.
   *
   * @var \Drupal\media\MediaTypeInterface
   */
  protected $nonSchedulerMediaType;

  /**
   * The media entity storage.
   *
   * @var \Drupal\Core\Entity\ContentEntityStorageInterface
   */
  protected $mediaStorage;

  /**
   * Set common properties, define content types and create users.
   */
  public function schedulerMediaSetUp() {

    // Create a test media type for video that is enabled for scheduling.

    /** @var \Drupal\media\Entity\MediaTypeInterface $mediaType */
    $this->mediaType = $this
      ->createMediaType('video_file', [
      'id' => $this->mediaTypeName,
      'label' => $this->mediaTypeLabel,
    ]);

    // Add scheduler functionality to the video media type.
    $this->mediaType
      ->setThirdPartySetting('scheduler', 'publish_enable', TRUE)
      ->setThirdPartySetting('scheduler', 'unpublish_enable', TRUE)
      ->save();

    // Create a test media type for audio which is not enabled for scheduling.

    /** @var \Drupal\media\Entity\MediaTypeInterface $nonSchedulerMediaType */
    $this->nonSchedulerMediaType = $this
      ->createMediaType('audio_file', [
      'id' => $this->nonSchedulerMediaTypeName,
      'label' => $this->nonSchedulerMediaTypeLabel,
    ]);

    // Define mediaStorage for use in many tests.

    /** @var MediaStorageInterface $mediaStorage */
    $this->mediaStorage = $this->container
      ->get('entity_type.manager')
      ->getStorage('media');

    // Add extra permisssions to the role assigned to the adminUser.
    $this
      ->addPermissionsToUser($this->adminUser, [
      'create ' . $this->mediaTypeName . ' media',
      'edit any ' . $this->mediaTypeName . ' media',
      'delete any ' . $this->mediaTypeName . ' media',
      'create ' . $this->nonSchedulerMediaTypeName . ' media',
      'edit any ' . $this->nonSchedulerMediaTypeName . ' media',
      'delete any ' . $this->nonSchedulerMediaTypeName . ' media',
      'access media overview',
      'view own unpublished media',
      'schedule publishing of media',
      'view scheduled media',
    ]);

    // Add extra permisssions to the role assigned to the schedulerUser.
    $this
      ->addPermissionsToUser($this->schedulerUser, [
      'create ' . $this->mediaTypeName . ' media',
      'edit own ' . $this->mediaTypeName . ' media',
      'delete own ' . $this->mediaTypeName . ' media',
      'view own unpublished media',
      'schedule publishing of media',
    ]);

    // By default, media items cannot be viewed directly, and the url media/mid
    // gives a 404 not found. Changing this setting makes debugging the tests
    // easier. It is also required for the meta information test.
    $configFactory = $this->container
      ->get('config.factory');
    $configFactory
      ->getEditable('media.settings')
      ->set('standalone_url', TRUE)
      ->save(TRUE);
    $this->container
      ->get('router.builder')
      ->rebuild();

    // Set the media file attachments to be optional not required, to simplify
    // editing and saving media entities.
    $configFactory
      ->getEditable('field.field.media.test_video.field_media_video_file')
      ->set('required', FALSE)
      ->save(TRUE);
    $configFactory
      ->getEditable('field.field.media.test_audio_not_enabled.field_media_audio_file')
      ->set('required', FALSE)
      ->save(TRUE);
  }

  /**
   * Creates a media entity.
   *
   * @param array $values
   *   The values to use for the entity.
   *
   * @return \Drupal\media\MediaInterface
   *   The created media object.
   */
  public function createMediaItem(array $values) {

    // Provide defaults for the critical values.
    $values += [
      'bundle' => $this->mediaTypeName,
      'name' => $this
        ->randomstring(12),
    ];

    /** @var \Drupal\media\MediaInterface $media */
    $media = $this->mediaStorage
      ->create($values);
    $media
      ->save();
    return $media;
  }

  /**
   * Gets a media item from storage.
   *
   * For nodes, there is drupalGetNodeByTitle() but nothing similar exists to
   * help Media testing. But this function goes one better - if a name is given,
   * then a match will be attempted on the name, and fail if none found. But if
   * no name is supplied then the media entity with the highest id value (the
   * newest item created) is returned, as this is often what is required.
   *
   * @param string $name
   *   Optional name text to match on. If given and no match, returns NULL.
   *   If no $name is given then returns the media with the highest id value.
   *
   * @return \Drupal\media\MediaInterface
   *   The media object.
   */
  public function getMediaItem(string $name = NULL) {
    $query = $this->mediaStorage
      ->getQuery()
      ->accessCheck(FALSE)
      ->sort('mid', 'DESC');
    if (!empty($name)) {
      $query
        ->condition('name', $name);
    }
    $result = $query
      ->execute();
    if (count($result)) {
      $media_id = reset($result);
      return $this->mediaStorage
        ->load($media_id);
    }
    else {
      return NULL;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MediaTypeCreationTrait::createMediaType protected function Create a media type for a source plugin.
SchedulerMediaSetupTrait::$mediaStorage protected property The media entity storage.
SchedulerMediaSetupTrait::$mediaType protected property The media type object which is enabled for scheduling.
SchedulerMediaSetupTrait::$mediaTypeLabel protected property The readable label of the standard media type created for testing.
SchedulerMediaSetupTrait::$mediaTypeName protected property The internal name of the standard media type created for testing.
SchedulerMediaSetupTrait::$nonSchedulerMediaType protected property The media type object which is not enabled for scheduling.
SchedulerMediaSetupTrait::$nonSchedulerMediaTypeLabel protected property The readable label of the media type not enabled for scheduling.
SchedulerMediaSetupTrait::$nonSchedulerMediaTypeName protected property The internal name of the media type not enabled for scheduling.
SchedulerMediaSetupTrait::createMediaItem public function Creates a media entity.
SchedulerMediaSetupTrait::getMediaItem public function Gets a media item from storage.
SchedulerMediaSetupTrait::schedulerMediaSetUp public function Set common properties, define content types and create users.