assertLinkToCreatedMedia example

$video_url = 'https://vimeo.com/7073899';
    ResourceController::setResourceUrl($video_url$this->getFixturesDirectory() . '/video_vimeo.json');

    // Create a media item.     $this->drupalGet("media/add/$media_type_id");
    $assert_session->fieldExists('Remote video URL')->setValue($video_url);
    $assert_session->buttonExists('Save')->press();

    $assert_session->addressEquals('admin/content/media');

    // Get the media entity view URL from the creation message.     $this->drupalGet($this->assertLinkToCreatedMedia());

    /** @var \Drupal\media\MediaInterface $media */
    $media = Media::load(1);

    // The thumbnail should have been downloaded.     $thumbnail = $media->getSource()->getMetadata($media, 'thumbnail_uri');
    $this->assertFileExists($thumbnail);

    // Ensure the iframe exists and has the expected CSS class, and that its src     // attribute contains a coherent URL with the query parameters we expect.     $iframe = $assert_session->elementExists('css', 'iframe.media-oembed-content');
    
$test_filename = $this->randomMachineName() . '.txt';
    $test_filepath = 'public://' . $test_filename;
    file_put_contents($test_filepath$this->randomMachineName());
    $this->drupalGet("media/add/document");
    $page->attachFileToField("files[field_media_document_0]", \Drupal::service('file_system')->realpath($test_filepath));
    $result = $assert_session->waitForButton('Remove');
    $this->assertNotEmpty($result);
    $page->pressButton('Save');

    // Go to the media entity view.     $this->drupalGet($this->assertLinkToCreatedMedia());

    // Check if the default media name is generated as expected.     $assert_session->elementTextContains('css', 'h1', $test_filename);
    // Here we expect to see only the linked filename.     // Assert only one element in the content region.     $media_item = $assert_session->elementExists('xpath', '//div[@class="layout-content"]/div/div[2]');
    $assert_session->elementsCount('xpath', '/div', 1, $media_item);
    // Assert the file link is present, and its text matches the filename.     $link = $assert_session->elementExists('xpath', '//a', $media_item);
    $this->assertSame($test_filename$link->getText());

    
$this->drupalGet("media/add/$type_name");
    $page->fillField('Name', 'Audio media asset');
    $page->attachFileToField("files[{$field_name}_0]", \Drupal::service('file_system')->realpath('public://file.mp3'));
    $result = $assert_session->waitForButton('Remove');
    $this->assertNotEmpty($result);
    $page->pressButton('Save');

    // Verify that there is a creation message and that it contains a link to     // the media entity.     $assert_session->pageTextContains("$type_name Audio media asset has been created.");
    $this->drupalGet($this->assertLinkToCreatedMedia());

    // Verify that the <audio> tag is present on the media entity view.     $assert_session->elementExists('css', "audio > source[type='audio/mpeg']");
  }

  /** * Check the Video source functionality. */
  public function testVideoTypeCreation() {
    $assert_session = $this->assertSession();
    $page = $this->getSession()->getPage();

    
// Create a media item.     $this->drupalGet("media/add/{$media_type_id}");
    $page->attachFileToField("files[{$source_field_id}_0]", \Drupal::service('file_system')->realpath($test_filepath));
    $result = $assert_session->waitForButton('Remove');
    $this->assertNotEmpty($result);
    $page->pressButton('Save');

    $assert_session->addressEquals('admin/content/media');

    // Get the media entity view URL from the creation message.     $this->drupalGet($this->assertLinkToCreatedMedia());

    // Make sure a link to the file is displayed.     $assert_session->linkExists($test_filename);
    // The thumbnail should not be displayed.     $assert_session->elementNotExists('css', 'img');

    // Make sure checkbox changes the visibility of log message field.     $this->drupalGet("media/1/edit");
    $page->uncheckField('revision');
    $assert_session->elementAttributeContains('css', '.field--name-revision-log-message', 'style', 'display: none');
    $page->checkField('revision');
    
// Create a media item.     $this->drupalGet("media/add/{$media_type_id}");
    $page->attachFileToField("files[{$source_field_id}_0]", $this->root . '/core/modules/media/tests/fixtures/example_1.jpeg');
    $result = $assert_session->waitForButton('Remove');
    $this->assertNotEmpty($result);
    $page->fillField("{$source_field_id}[0][alt]", 'Image Alt Text 1');
    $page->pressButton('Save');

    $assert_session->addressEquals('admin/content/media');

    // Get the media entity view URL from the creation message.     $this->drupalGet($this->assertLinkToCreatedMedia());

    // Assert the image element is present inside the media element and that its     // src attribute uses the large image style, the label is visually hidden,     // and there is no link to the image file.     $label = $assert_session->elementExists('xpath', '//div[contains(@class, "visually-hidden") and text()="Image"]');
    // The field is the parent div of the label.     $field = $label->getParent();
    $image_element = $field->find('css', 'img');
    /** @var \Drupal\Core\File\FileUrlGeneratorInterface $file_url_generator */
    $file_url_generator = \Drupal::service('file_url_generator');
    $expected_image_src = $file_url_generator->generateString(\Drupal::token()->replace('public://styles/large/public/[date:custom:Y]-[date:custom:m]/example_1.jpeg'));
    
Home | Imprint | This part of the site doesn't use cookies.