You are here

public function KeyValueEntityStorageTest::testSaveRenameConfigEntity in Drupal 8

@covers ::save @covers ::doSave

@depends testSaveConfigEntity

File

core/tests/Drupal/Tests/Core/Entity/KeyValueStore/KeyValueEntityStorageTest.php, line 376

Class

KeyValueEntityStorageTest
@coversDefaultClass \Drupal\Core\Entity\KeyValueStore\KeyValueEntityStorage @group Entity

Namespace

Drupal\Tests\Core\Entity\KeyValueStore

Code

public function testSaveRenameConfigEntity(ConfigEntityInterface $entity) {
  $this->entityType
    ->expects($this
    ->once())
    ->method('getClass')
    ->will($this
    ->returnValue(get_class($entity)));
  $this
    ->setUpKeyValueEntityStorage();
  $this->moduleHandler
    ->expects($this
    ->at(0))
    ->method('getImplementations')
    ->with('entity_load')
    ->will($this
    ->returnValue([]));
  $this->moduleHandler
    ->expects($this
    ->at(1))
    ->method('getImplementations')
    ->with('test_entity_type_load')
    ->will($this
    ->returnValue([]));
  $expected = [
    'id' => 'foo',
  ];
  $entity
    ->expects($this
    ->once())
    ->method('toArray')
    ->will($this
    ->returnValue($expected));
  $this->keyValueStore
    ->expects($this
    ->exactly(2))
    ->method('has')
    ->with('foo')
    ->will($this
    ->returnValue(TRUE));
  $this->keyValueStore
    ->expects($this
    ->once())
    ->method('getMultiple')
    ->with([
    'foo',
  ])
    ->will($this
    ->returnValue([
    [
      'id' => 'foo',
    ],
  ]));
  $this->keyValueStore
    ->expects($this
    ->once())
    ->method('delete')
    ->with('foo');
  $this->keyValueStore
    ->expects($this
    ->once())
    ->method('set')
    ->with('bar', $expected);

  // Performing a rename does not change the original ID until saving.
  $this
    ->assertSame('foo', $entity
    ->getOriginalId());
  $entity
    ->set('id', 'bar');
  $this
    ->assertSame('foo', $entity
    ->getOriginalId());
  $return = $this->entityStorage
    ->save($entity);
  $this
    ->assertSame(SAVED_UPDATED, $return);
  $this
    ->assertSame('bar', $entity
    ->getOriginalId());
}