Skip to content
Snippets Groups Projects
Commit a116715c authored by Angie Byron's avatar Angie Byron
Browse files

Issue #2974203 by seanB, jan.stoeckler, phenaproxima, marcoscano, chr.fritsch,...

Issue #2974203 by seanB, jan.stoeckler, phenaproxima, marcoscano, chr.fritsch, lauriii, tstoeckler, ifrik, starshaped, ckrina: Redirect back to media list after creating a media entity

(cherry picked from commit 830defe6)
parent 954f7d59
No related branches found
No related tags found
No related merge requests found
Showing with 108 additions and 10 deletions
......@@ -59,9 +59,9 @@ public function form(array $form, FormStateInterface $form_state) {
*/
public function save(array $form, FormStateInterface $form_state) {
$saved = parent::save($form, $form_state);
$context = ['@type' => $this->entity->bundle(), '%label' => $this->entity->label()];
$context = ['@type' => $this->entity->bundle(), '%label' => $this->entity->label(), 'link' => $this->entity->toLink($this->t('View'))->toString()];
$logger = $this->logger('media');
$t_args = ['@type' => $this->entity->bundle->entity->label(), '%label' => $this->entity->label()];
$t_args = ['@type' => $this->entity->bundle->entity->label(), '%label' => $this->entity->toLink($this->entity->label())->toString()];
if ($saved === SAVED_NEW) {
$logger->notice('@type: added %label.', $context);
......@@ -72,7 +72,16 @@ public function save(array $form, FormStateInterface $form_state) {
$this->messenger()->addStatus($this->t('@type %label has been updated.', $t_args));
}
$form_state->setRedirectUrl($this->entity->toUrl('canonical'));
// Redirect the user to the media overview if the user has the 'access media
// overview' permission. If not, redirect to the canonical URL of the media
// item.
if ($this->currentUser()->hasPermission('access media overview')) {
$form_state->setRedirectUrl($this->entity->toUrl('collection'));
}
else {
$form_state->setRedirectUrl($this->entity->toUrl());
}
return $saved;
}
......
......@@ -88,7 +88,7 @@ public function testFileMediaRevision() {
$page->fillField('Name', 'Foobar');
$page->attachFileToField('File', $this->container->get('file_system')->realpath($uri));
$page->pressButton('Save');
$assert->addressMatches('/^\/media\/[0-9]+$/');
$assert->addressEquals('admin/content/media');
// The media item was just created, so it should only have one revision.
$media = $this->container
......@@ -137,7 +137,7 @@ public function testImageMediaRevision() {
$page->fillField('Name', 'Foobar');
$page->attachFileToField('Image', $this->root . '/core/modules/media/tests/fixtures/example_1.jpeg');
$page->pressButton('Save');
$assert->addressMatches('/^\/media\/[0-9]+$/');
$assert->addressEquals('admin/content/media');
// The media item was just created, so it should only have one revision.
$media = $this->container
......
......@@ -71,6 +71,7 @@ public function testMediaWithOnlyOneMediaType() {
->loadUnchanged($media_id);
$this->assertSame($media->getRevisionLogMessage(), $revision_log_message);
$this->assertSame($media->getName(), $media_name);
$this->drupalGet('media/' . $media_id);
$assert_session->titleEquals($media_name . ' | Drupal');
// Tests media edit form.
......@@ -87,6 +88,7 @@ public function testMediaWithOnlyOneMediaType() {
->getStorage('media')
->loadUnchanged($media_id);
$this->assertSame($media->getName(), $media_name2);
$this->drupalGet('media/' . $media_id);
$assert_session->titleEquals($media_name2 . ' | Drupal');
// Test that there is no empty vertical tabs element, if the container is
......@@ -113,6 +115,7 @@ public function testMediaWithOnlyOneMediaType() {
$page->fillField('name[0][value]', $media_name);
$page->fillField('revision_log_message[0][value]', $revision_log_message);
$page->pressButton('Save');
$this->drupalGet('media/' . $media_id);
$assert_session->titleEquals($media_name . ' | Drupal');
/** @var \Drupal\media\MediaInterface $media */
$media = $this->container->get('entity_type.manager')
......@@ -433,6 +436,49 @@ public function testMediaReferenceWidget($cardinality, array $media_type_create_
}
}
/**
* Tests the redirect URL after creating a media item.
*/
public function testMediaCreateRedirect() {
$session = $this->getSession();
$page = $session->getPage();
$assert_session = $this->assertSession();
$this->createMediaType('test', [
'queue_thumbnail_downloads' => FALSE,
]);
// Test a redirect to the media canonical URL for a user without the 'access
// media overview' permission.
$this->drupalLogin($this->drupalCreateUser([
'view media',
'create media',
]));
$this->drupalGet('media/add');
$page->fillField('name[0][value]', $this->randomMachineName());
$page->fillField('field_media_test[0][value]', $this->randomString());
$page->pressButton('Save');
$media_id = $this->container->get('entity_type.manager')
->getStorage('media')
->getQuery()
->execute();
$media_id = reset($media_id);
$assert_session->addressEquals('media/' . $media_id);
// Test a redirect to the media overview for a user with the 'access media
// overview' permission.
$this->drupalLogin($this->drupalCreateUser([
'view media',
'create media',
'access media overview',
]));
$this->drupalGet('media/add');
$page->fillField('name[0][value]', $this->randomMachineName());
$page->fillField('field_media_test[0][value]', $this->randomString());
$page->pressButton('Save');
$assert_session->addressEquals('admin/content/media');
}
/**
* Asserts that the given texts are present exactly once.
*
......
......@@ -83,6 +83,9 @@ public function testMediaDisplay() {
->execute();
$image_media_id = reset($image_media_id);
// Go to the media entity view.
$this->drupalGet('/media/' . $image_media_id);
// Here we expect to see only the image, nothing else.
// Assert only one element in the content region.
$this->assertSame(1, count($page->findAll('css', '.media--type-image > div')));
......@@ -104,6 +107,9 @@ public function testMediaDisplay() {
$this->assertNotEmpty($result);
$page->pressButton('Save');
// Go to the media entity view.
$this->drupalGet($this->assertLinkToCreatedMedia());
// Here we expect to see only the linked filename.
// Assert only one element in the content region.
$this->assertSame(1, count($page->findAll('css', 'article.media--type-file > div')));
......
......@@ -43,4 +43,21 @@ protected function waitUntilVisible($selector, $timeout = 1000, $message = '') {
$this->assertJsCondition($condition, $timeout, $message);
}
/**
* Asserts that a link to a new media item is displayed in the messages area.
*
* @return string
* The link URL.
*/
protected function assertLinkToCreatedMedia() {
$assert_session = $this->assertSession();
$selector = '.messages a';
// Get the canonical media entity URL from the creation message.
$link = $assert_session->elementExists('css', $selector);
$assert_session->elementAttributeExists('css', $selector, 'href');
return $link->getAttribute('href');
}
}
......@@ -39,7 +39,7 @@ public function testAudioTypeCreation() {
$formatter = $display->getComponent($field_name)['type'];
$this->assertSame('file_audio', $formatter);
// Create a media asset and verify that the <audio> tag is present.
// Create a media asset.
file_put_contents('public://file.mp3', str_repeat('t', 10));
$file = File::create([
'uri' => 'public://file.mp3',
......@@ -54,7 +54,12 @@ public function testAudioTypeCreation() {
$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']");
}
......@@ -83,7 +88,7 @@ public function testVideoTypeCreation() {
$formatter = $display->getComponent($field_name)['type'];
$this->assertSame('file_video', $formatter);
// Create a media asset and verify that the <video> tag is present.
// Create a media asset.
file_put_contents('public://file.mp4', str_repeat('t', 10));
$file = File::create([
'uri' => 'public://file.mp4',
......@@ -98,7 +103,12 @@ public function testVideoTypeCreation() {
$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 Video media asset has been created.");
$this->drupalGet($this->assertLinkToCreatedMedia());
// Verify that the <video> tag is present on the media entity view.
$assert_session->elementExists('css', "video > source[type='video/mp4']");
}
......
......@@ -57,7 +57,10 @@ public function testMediaFileSource() {
$this->assertNotEmpty($result);
$page->pressButton('Save');
$assert_session->addressEquals('media/1');
$assert_session->addressEquals('admin/content/media');
// Get the media entity view URL from the creation message.
$this->drupalGet($this->assertLinkToCreatedMedia());
// Make sure the thumbnail is displayed.
$assert_session->elementAttributeContains('css', '.image-style-thumbnail', 'src', 'generic.png');
......
......@@ -52,7 +52,10 @@ public function testMediaImageSource() {
$page->fillField("{$source_field_id}[0][alt]", 'Image Alt Text 1');
$page->pressButton('Save');
$assert_session->addressEquals('media/1');
$assert_session->addressEquals('admin/content/media');
// Get the media entity view URL from the creation message.
$this->drupalGet($this->assertLinkToCreatedMedia());
// Make sure the thumbnail is displayed from uploaded image.
$assert_session->elementAttributeContains('css', '.image-style-thumbnail', 'src', 'example_1.jpeg');
......
......@@ -100,7 +100,11 @@ public function testMediaOEmbedVideoSource() {
$assert_session->fieldExists('Remote video URL')->setValue($video_url);
$assert_session->buttonExists('Save')->press();
$assert_session->addressEquals('media/1');
$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);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment