summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorwebchick2018-07-27 22:33:41 (GMT)
committerwebchick2018-07-27 22:33:41 (GMT)
commit830defe683becff2b38a0e4ed23187ad1a9f2338 (patch)
tree8acea4a968e46541a1c85182e417bac824236157
parenta02c8a610f0b61d6dd16bd05763abdd2cec9beec (diff)
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
-rw-r--r--core/modules/media/src/MediaForm.php15
-rw-r--r--core/modules/media/tests/src/Functional/MediaRevisionTest.php4
-rw-r--r--core/modules/media/tests/src/Functional/MediaUiFunctionalTest.php46
-rw-r--r--core/modules/media/tests/src/FunctionalJavascript/MediaDisplayTest.php6
-rw-r--r--core/modules/media/tests/src/FunctionalJavascript/MediaJavascriptTestBase.php17
-rw-r--r--core/modules/media/tests/src/FunctionalJavascript/MediaSourceAudioVideoTest.php14
-rw-r--r--core/modules/media/tests/src/FunctionalJavascript/MediaSourceFileTest.php5
-rw-r--r--core/modules/media/tests/src/FunctionalJavascript/MediaSourceImageTest.php5
-rw-r--r--core/modules/media/tests/src/FunctionalJavascript/MediaSourceOEmbedVideoTest.php6
9 files changed, 108 insertions, 10 deletions
diff --git a/core/modules/media/src/MediaForm.php b/core/modules/media/src/MediaForm.php
index 6f860e2..45cff5f 100644
--- a/core/modules/media/src/MediaForm.php
+++ b/core/modules/media/src/MediaForm.php
@@ -59,9 +59,9 @@ class MediaForm extends ContentEntityForm {
*/
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 @@ class MediaForm extends ContentEntityForm {
$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;
}
diff --git a/core/modules/media/tests/src/Functional/MediaRevisionTest.php b/core/modules/media/tests/src/Functional/MediaRevisionTest.php
index 9a8ce5a..8291d2f 100644
--- a/core/modules/media/tests/src/Functional/MediaRevisionTest.php
+++ b/core/modules/media/tests/src/Functional/MediaRevisionTest.php
@@ -88,7 +88,7 @@ class MediaRevisionTest extends MediaFunctionalTestBase {
$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 @@ class MediaRevisionTest extends MediaFunctionalTestBase {
$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
diff --git a/core/modules/media/tests/src/Functional/MediaUiFunctionalTest.php b/core/modules/media/tests/src/Functional/MediaUiFunctionalTest.php
index f45a33e..fe39fd8 100644
--- a/core/modules/media/tests/src/Functional/MediaUiFunctionalTest.php
+++ b/core/modules/media/tests/src/Functional/MediaUiFunctionalTest.php
@@ -71,6 +71,7 @@ class MediaUiFunctionalTest extends MediaFunctionalTestBase {
->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 @@ class MediaUiFunctionalTest extends MediaFunctionalTestBase {
->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 @@ class MediaUiFunctionalTest extends MediaFunctionalTestBase {
$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')
@@ -434,6 +437,49 @@ class MediaUiFunctionalTest extends MediaFunctionalTestBase {
}
/**
+ * 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.
*
* @param string[] $texts
diff --git a/core/modules/media/tests/src/FunctionalJavascript/MediaDisplayTest.php b/core/modules/media/tests/src/FunctionalJavascript/MediaDisplayTest.php
index 591643b..19ebf9d 100644
--- a/core/modules/media/tests/src/FunctionalJavascript/MediaDisplayTest.php
+++ b/core/modules/media/tests/src/FunctionalJavascript/MediaDisplayTest.php
@@ -83,6 +83,9 @@ class MediaDisplayTest extends MediaJavascriptTestBase {
->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 @@ class MediaDisplayTest extends MediaJavascriptTestBase {
$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')));
diff --git a/core/modules/media/tests/src/FunctionalJavascript/MediaJavascriptTestBase.php b/core/modules/media/tests/src/FunctionalJavascript/MediaJavascriptTestBase.php
index f8c04d1..390e220 100644
--- a/core/modules/media/tests/src/FunctionalJavascript/MediaJavascriptTestBase.php
+++ b/core/modules/media/tests/src/FunctionalJavascript/MediaJavascriptTestBase.php
@@ -43,4 +43,21 @@ abstract class MediaJavascriptTestBase extends WebDriverTestBase {
$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');
+ }
+
}
diff --git a/core/modules/media/tests/src/FunctionalJavascript/MediaSourceAudioVideoTest.php b/core/modules/media/tests/src/FunctionalJavascript/MediaSourceAudioVideoTest.php
index 4ad3c5b..2195f74 100644
--- a/core/modules/media/tests/src/FunctionalJavascript/MediaSourceAudioVideoTest.php
+++ b/core/modules/media/tests/src/FunctionalJavascript/MediaSourceAudioVideoTest.php
@@ -39,7 +39,7 @@ class MediaSourceAudioVideoTest extends MediaSourceTestBase {
$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 @@ class MediaSourceAudioVideoTest extends MediaSourceTestBase {
$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 @@ class MediaSourceAudioVideoTest extends MediaSourceTestBase {
$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 @@ class MediaSourceAudioVideoTest extends MediaSourceTestBase {
$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']");
}
diff --git a/core/modules/media/tests/src/FunctionalJavascript/MediaSourceFileTest.php b/core/modules/media/tests/src/FunctionalJavascript/MediaSourceFileTest.php
index 708a941..ad509b0 100644
--- a/core/modules/media/tests/src/FunctionalJavascript/MediaSourceFileTest.php
+++ b/core/modules/media/tests/src/FunctionalJavascript/MediaSourceFileTest.php
@@ -57,7 +57,10 @@ class MediaSourceFileTest extends MediaSourceTestBase {
$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');
diff --git a/core/modules/media/tests/src/FunctionalJavascript/MediaSourceImageTest.php b/core/modules/media/tests/src/FunctionalJavascript/MediaSourceImageTest.php
index 8adb98a..faf58c1 100644
--- a/core/modules/media/tests/src/FunctionalJavascript/MediaSourceImageTest.php
+++ b/core/modules/media/tests/src/FunctionalJavascript/MediaSourceImageTest.php
@@ -52,7 +52,10 @@ class MediaSourceImageTest extends MediaSourceTestBase {
$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');
diff --git a/core/modules/media/tests/src/FunctionalJavascript/MediaSourceOEmbedVideoTest.php b/core/modules/media/tests/src/FunctionalJavascript/MediaSourceOEmbedVideoTest.php
index fbdfe6d..cb7ae2a 100644
--- a/core/modules/media/tests/src/FunctionalJavascript/MediaSourceOEmbedVideoTest.php
+++ b/core/modules/media/tests/src/FunctionalJavascript/MediaSourceOEmbedVideoTest.php
@@ -100,7 +100,11 @@ class MediaSourceOEmbedVideoTest extends MediaSourceTestBase {
$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);