Issue #2850022 by Sam152, Berdir: Duplicating a non-default revision should produce a default revision for a newly created entity
parent
6018d048bb
commit
3bd5be0d33
|
@ -312,7 +312,9 @@ abstract class ContentEntityBase extends Entity implements \IteratorAggregate, C
|
||||||
if (isset($new_value)) {
|
if (isset($new_value)) {
|
||||||
$this->isDefaultRevision = (bool) $new_value;
|
$this->isDefaultRevision = (bool) $new_value;
|
||||||
}
|
}
|
||||||
return $return;
|
// New entities should always ensure at least one default revision exists,
|
||||||
|
// creating an entity without a default revision is an invalid state.
|
||||||
|
return $this->isNew() || $return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -0,0 +1,55 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Drupal\KernelTests\Core\Entity;
|
||||||
|
|
||||||
|
use Drupal\entity_test\Entity\EntityTestRev;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test entity duplication.
|
||||||
|
*
|
||||||
|
* @group Entity
|
||||||
|
*/
|
||||||
|
class EntityDuplicateTest extends EntityKernelTestBase {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var \Drupal\Core\Entity\ContentEntityStorageInterface
|
||||||
|
*/
|
||||||
|
protected $entityTestRevStorage;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function setUp() {
|
||||||
|
parent::setUp();
|
||||||
|
$this->installEntitySchema('entity_test_rev');
|
||||||
|
$this->entityTestRevStorage = $this->container->get('entity_type.manager')->getStorage('entity_test_rev');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test duplicating a non-default revision.
|
||||||
|
*/
|
||||||
|
public function testDuplicateNonDefaultRevision() {
|
||||||
|
$entity = EntityTestRev::create([
|
||||||
|
'name' => 'First Revision',
|
||||||
|
]);
|
||||||
|
$entity->save();
|
||||||
|
$first_revision_id = $entity->getRevisionId();
|
||||||
|
|
||||||
|
$entity->setNewRevision(TRUE);
|
||||||
|
$entity->name = 'Second Revision';
|
||||||
|
$entity->save();
|
||||||
|
|
||||||
|
$duplicate_first_revision = $this->entityTestRevStorage->loadRevision($first_revision_id)->createDuplicate();
|
||||||
|
$this->assertTrue($duplicate_first_revision->isDefaultRevision(), 'Duplicating a non-default revision creates a default revision.');
|
||||||
|
$this->assertEquals($duplicate_first_revision->label(), 'First Revision');
|
||||||
|
$duplicate_first_revision->save();
|
||||||
|
|
||||||
|
$duplicate_first_revision->name = 'Updated name';
|
||||||
|
$duplicate_first_revision->save();
|
||||||
|
|
||||||
|
$this->entityTestRevStorage->resetCache();
|
||||||
|
$duplicate_first_revision = EntityTestRev::load($duplicate_first_revision->id());
|
||||||
|
$this->assertEquals('Updated name', $duplicate_first_revision->label());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -182,8 +182,8 @@ class ContentEntityBaseUnitTest extends UnitTestCase {
|
||||||
->method('getFieldDefinitions')
|
->method('getFieldDefinitions')
|
||||||
->with($this->entityTypeId, $this->bundle)
|
->with($this->entityTypeId, $this->bundle)
|
||||||
->will($this->returnValue($this->fieldDefinitions));
|
->will($this->returnValue($this->fieldDefinitions));
|
||||||
$this->entity = $this->getMockForAbstractClass('\Drupal\Core\Entity\ContentEntityBase', array($values, $this->entityTypeId, $this->bundle));
|
|
||||||
|
|
||||||
|
$this->entity = $this->getMockForAbstractClass('\Drupal\Core\Entity\ContentEntityBase', array($values, $this->entityTypeId, $this->bundle), '', TRUE, TRUE, TRUE, ['isNew']);
|
||||||
$values['defaultLangcode'] = array(LanguageInterface::LANGCODE_DEFAULT => LanguageInterface::LANGCODE_NOT_SPECIFIED);
|
$values['defaultLangcode'] = array(LanguageInterface::LANGCODE_DEFAULT => LanguageInterface::LANGCODE_NOT_SPECIFIED);
|
||||||
$this->entityUnd = $this->getMockForAbstractClass('\Drupal\Core\Entity\ContentEntityBase', array($values, $this->entityTypeId, $this->bundle));
|
$this->entityUnd = $this->getMockForAbstractClass('\Drupal\Core\Entity\ContentEntityBase', array($values, $this->entityTypeId, $this->bundle));
|
||||||
}
|
}
|
||||||
|
@ -262,6 +262,12 @@ class ContentEntityBaseUnitTest extends UnitTestCase {
|
||||||
$this->assertTrue($this->entity->isDefaultRevision(FALSE));
|
$this->assertTrue($this->entity->isDefaultRevision(FALSE));
|
||||||
// The last call changed the return value for this call.
|
// The last call changed the return value for this call.
|
||||||
$this->assertFalse($this->entity->isDefaultRevision());
|
$this->assertFalse($this->entity->isDefaultRevision());
|
||||||
|
// The revision for a new entity should always be the default revision.
|
||||||
|
$this->entity->expects($this->any())
|
||||||
|
->method('isNew')
|
||||||
|
->will($this->returnValue(TRUE));
|
||||||
|
$this->entity->isDefaultRevision(FALSE);
|
||||||
|
$this->assertTrue($this->entity->isDefaultRevision());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue