Commit 3bd5be0d authored by catch's avatar catch

Issue #2850022 by Sam152, Berdir: Duplicating a non-default revision should...

Issue #2850022 by Sam152, Berdir: Duplicating a non-default revision should produce a default revision for a newly created entity
parent 6018d048
......@@ -312,7 +312,9 @@ public function isDefaultRevision($new_value = NULL) {
if (isset($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;
}
/**
......
<?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 @@ protected function setUp() {
->method('getFieldDefinitions')
->with($this->entityTypeId, $this->bundle)
->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);
$this->entityUnd = $this->getMockForAbstractClass('\Drupal\Core\Entity\ContentEntityBase', array($values, $this->entityTypeId, $this->bundle));
}
......@@ -262,6 +262,12 @@ public function testIsDefaultRevision() {
$this->assertTrue($this->entity->isDefaultRevision(FALSE));
// The last call changed the return value for this call.
$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());
}
/**
......
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