Commit 48f9801e authored by webchick's avatar webchick

Issue #2897009 by vaplas, seanB, phenaproxima, harsha012, Wim Leers,...

Issue #2897009 by vaplas, seanB, phenaproxima, harsha012, Wim Leers, cburschka, bojanz: MediaInterface is missing setName() and getName()
parent fcf01cc8
......@@ -85,6 +85,35 @@ class Media extends EditorialContentEntityBase implements MediaInterface {
use StringTranslationTrait;
/**
* {@inheritdoc}
*/
public function getName() {
$name = $this->get('name');
if ($name->isEmpty()) {
$media_source = $this->getSource();
return $media_source->getMetadata($this, $media_source->getPluginDefinition()['default_name_metadata_attribute']);
}
else {
return $name->value;
}
}
/**
* {@inheritdoc}
*/
public function label() {
return $this->getName();
}
/**
* {@inheritdoc}
*/
public function setName($name) {
return $this->set('name', $name);
}
/**
* {@inheritdoc}
*/
......@@ -288,10 +317,9 @@ public function preSave(EntityStorageInterface $storage) {
}
}
// Try to set a default name for this media item if no label is
// provided.
if (!$translation->label()) {
$translation->set('name', $media_source->getMetadata($translation, $media_source->getPluginDefinition()['default_name_metadata_attribute']));
// Try to set a default name for this media item if no name is provided.
if ($translation->get('name')->isEmpty()) {
$translation->setName($translation->getName());
}
// Set thumbnail.
......
......@@ -13,6 +13,24 @@
*/
interface MediaInterface extends ContentEntityInterface, EntityChangedInterface, RevisionLogInterface, EntityOwnerInterface, EntityPublishedInterface {
/**
* Gets the media item name.
*
* @return string
* The name of the media item.
*/
public function getName();
/**
* Sets the media item name.
*
* @param string $name
* The name of the media item.
*
* @return $this
*/
public function setName($name);
/**
* Returns the media item creation timestamp.
*
......
......@@ -65,7 +65,7 @@ public function testRevisions() {
$assert->statusCodeEquals(200);
// Confirm the revision page shows the correct title.
$assert->pageTextContains($media->label());
$assert->pageTextContains($media->getName());
// Confirm that the last revision is the default revision.
$this->assertTrue($media->isDefaultRevision(), 'Last revision is the default.');
......@@ -168,7 +168,7 @@ public function testImageMediaRevision() {
* A media object with up to date revision information.
*/
protected function createMediaRevision(MediaInterface $media) {
$media->set('name', $this->randomMachineName());
$media->setName($this->randomMachineName());
$media->setNewRevision();
$media->save();
return $media;
......
......@@ -67,7 +67,7 @@ public function testMediaWithOnlyOneMediaType() {
->getStorage('media')
->loadUnchanged($media_id);
$this->assertEquals($media->getRevisionLogMessage(), $revision_log_message);
$this->assertEquals($media->label(), $media_name);
$this->assertEquals($media->getName(), $media_name);
$assert_session->titleEquals($media_name . ' | Drupal');
// Tests media edit form.
......@@ -83,7 +83,7 @@ public function testMediaWithOnlyOneMediaType() {
$media = $this->container->get('entity_type.manager')
->getStorage('media')
->loadUnchanged($media_id);
$this->assertEquals($media->label(), $media_name2);
$this->assertEquals($media->getName(), $media_name2);
$assert_session->titleEquals($media_name2 . ' | Drupal');
// Test that there is no empty vertical tabs element, if the container is
......@@ -171,12 +171,12 @@ public function testMediaWithMultipleMediaTypes() {
// Go to first media item.
$this->drupalGet('media/' . $first_media_item->id());
$assert_session->statusCodeEquals(200);
$assert_session->pageTextContains($first_media_item->label());
$assert_session->pageTextContains($first_media_item->getName());
// Go to second media item.
$this->drupalGet('media/' . $second_media_item->id());
$assert_session->statusCodeEquals(200);
$assert_session->pageTextContains($second_media_item->label());
$assert_session->pageTextContains($second_media_item->getName());
}
}
......@@ -57,7 +57,7 @@ public function testMediaFileSource() {
// Load the media and check if the label was properly populated.
$media = Media::load(1);
$this->assertEquals($test_filename, $media->label());
$this->assertEquals($test_filename, $media->getName());
// Test the MIME type icon.
$icon_base = \Drupal::config('media.settings')->get('icon_base_uri');
......
......@@ -72,7 +72,7 @@ public function testMediaImageSource() {
// Load the media and check that all fields are properly populated.
$media = Media::load(1);
$this->assertEquals('example_1.jpeg', $media->label());
$this->assertEquals('example_1.jpeg', $media->getName());
$this->assertEquals('200', $media->get('field_string_width')->value);
$this->assertEquals('89', $media->get('field_string_height')->value);
}
......
......@@ -50,7 +50,7 @@ public function testMediaEntityCreation() {
$this->assertInstanceOf(MediaInterface::class, Media::load($media->id()), 'The new media item has not been created in the database.');
$this->assertEquals($this->testMediaType->id(), $media->bundle(), 'The media item was not created with the correct type.');
$this->assertEquals('Unnamed', $media->label(), 'The media item was not created with the correct name.');
$this->assertEquals('Unnamed', $media->getName(), 'The media item was not created with the correct name.');
$source_field_name = $media->bundle->entity->getSource()->getSourceFieldDefinition($media->bundle->entity)->getName();
$this->assertEquals('Nation of sheep, ruled by wolves, owned by pigs.', $media->get($source_field_name)->value, 'Source returns incorrect source field value.');
}
......
......@@ -25,8 +25,11 @@ public function testDefaultName() {
$media_source = $media->getSource();
$this->assertEquals('default_name', $media_source->getPluginDefinition()['default_name_metadata_attribute'], 'Default metadata attribute is not used for the default name.');
$this->assertEquals('media:' . $media->bundle() . ':' . $media->uuid(), $media_source->getMetadata($media, 'default_name'), 'Value of the default name metadata attribute does not look correct.');
$this->assertEquals('media:' . $media->bundle() . ':' . $media->uuid(), $media->getName(), 'Default name was not used correctly by getName().');
$this->assertEquals($media->getName(), $media->label(), 'Default name and label are not the same.');
$media->save();
$this->assertEquals('media:' . $media->bundle() . ':' . $media->uuid(), $media->label(), 'Default name was not set correctly.');
$this->assertEquals('media:' . $media->bundle() . ':' . $media->uuid(), $media->getName(), 'Default name was not saved correctly.');
$this->assertEquals($media->getName(), $media->label(), 'The label changed during save.');
// Make sure that the user-supplied name is used.
/** @var \Drupal\media\MediaInterface $media */
......@@ -39,7 +42,8 @@ public function testDefaultName() {
$this->assertEquals('default_name', $media_source->getPluginDefinition()['default_name_metadata_attribute'], 'Default metadata attribute is not used for the default name.');
$this->assertEquals('media:' . $media->bundle() . ':' . $media->uuid(), $media_source->getMetadata($media, 'default_name'), 'Value of the default name metadata attribute does not look correct.');
$media->save();
$this->assertEquals($name, $media->label(), 'User-supplied name was not set correctly.');
$this->assertEquals($name, $media->getName(), 'User-supplied name was not set correctly.');
$this->assertEquals($media->getName(), $media->label(), 'The user-supplied name does not match the label.');
// Change the default name attribute and see if it is used to set the name.
$name = 'Old Major';
......@@ -51,7 +55,8 @@ public function testDefaultName() {
$this->assertEquals('alternative_name', $media_source->getPluginDefinition()['default_name_metadata_attribute'], 'Correct metadata attribute is not used for the default name.');
$this->assertEquals($name, $media_source->getMetadata($media, 'alternative_name'), 'Value of the default name metadata attribute does not look correct.');
$media->save();
$this->assertEquals($name, $media->label(), 'Default name was not set correctly.');
$this->assertEquals($name, $media->getName(), 'Default name was not set correctly.');
$this->assertEquals($media->getName(), $media->label(), 'The default name does not match the label.');
}
/**
......@@ -263,7 +268,7 @@ public function testThumbnail() {
'field_media_test' => 'some_value',
]);
$media->save();
$this->assertEquals('Boxer', $media->label(), 'Correct name was not set on the media entity.');
$this->assertEquals('Boxer', $media->getName(), 'Correct name was not set on the media entity.');
$this->assertEquals('This will be title.', $media->thumbnail->title, 'Title text was not set on the thumbnail.');
$this->assertEquals('This will be alt.', $media->thumbnail->alt, 'Alt text was not set on the thumbnail.');
}
......@@ -293,7 +298,7 @@ public function testConstraints() {
$this->assertEquals('Inappropriate text.', $violations->get(0)->getMessage(), 'Incorrect constraint validation message found.');
// Fix the violation and make sure it is not reported anymore.
$media->set('name', 'I love Drupal!');
$media->setName('I love Drupal!');
$violations = $media->validate();
$this->assertCount(0, $violations, 'Expected number of validations not found.');
......@@ -301,6 +306,7 @@ public function testConstraints() {
$this->assertEmpty($media->id(), 'Entity ID was found.');
$media->save();
$this->assertNotEmpty($media->id(), 'Entity ID was not found.');
$this->assertSame($media->getName(), 'I love Drupal!');
// Test source field constraints.
\Drupal::state()->set('media_source_test_field_constraints', [
......
......@@ -2,6 +2,7 @@
namespace Drupal\views;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\Tags;
use Drupal\Core\Routing\RouteProviderInterface;
......@@ -799,7 +800,7 @@ public function setDisplay($display_id = NULL) {
// Ensure the requested display exists.
if (!$this->displayHandlers->has($display_id)) {
debug(format_string('setDisplay() called with invalid display ID "@display".', ['@display' => $display_id]));
trigger_error(new FormattableMarkup('setDisplay() called with invalid display ID "@display".', ['@display' => $display_id]));
return FALSE;
}
......
......@@ -196,8 +196,12 @@ public function testSetDisplayWithInvalidDisplay() {
$view->initDisplay();
// Error is triggered while calling the wrong display.
$this->setExpectedException(\PHPUnit_Framework_Error::class);
$view->setDisplay('invalid');
try {
$view->setDisplay('invalid');
}
catch (\PHPUnit_Framework_Error $e) {
$this->assertEquals('setDisplay() called with invalid display ID "invalid".', $e->getMessage());
}
$this->assertEqual($view->current_display, 'default', 'If setDisplay is called with an invalid display id the default display should be used.');
$this->assertEqual(spl_object_hash($view->display_handler), spl_object_hash($view->displayHandlers->get('default')));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment