function LinearHistoryTest::testDelete

Same name and namespace in other branches
  1. 10 core/tests/Drupal/Tests/Core/Config/Checkpoint/LinearHistoryTest.php \Drupal\Tests\Core\Config\Checkpoint\LinearHistoryTest::testDelete()

Tests delete.

@legacy-covers ::delete

File

core/tests/Drupal/Tests/Core/Config/Checkpoint/LinearHistoryTest.php, line 133

Class

LinearHistoryTest
Tests Drupal\Core\Config\Checkpoint\LinearHistory.

Namespace

Drupal\Tests\Core\Config\Checkpoint

Code

public function testDelete() : void {
  // Create a real State object so that we can manipulate it.
  $state = new State(new KeyValueMemoryFactory(), new NullBackend(''), new NullLockBackend());
  $test_data = [
    'hash1' => new Checkpoint('hash1', 'One', 1701539510, NULL),
    'hash2' => new Checkpoint('hash2', 'Two', 1701539520, 'hash1'),
    'hash3' => new Checkpoint('hash3', 'Three', 1701539530, 'hash2'),
  ];
  $state->set(self::CHECKPOINT_KEY, $test_data);
  $time = $this->prophesize(TimeInterface::class);
  $checkpoints = new LinearHistory($state, $time->reveal());
  $this->assertCount(3, $checkpoints);
  $this->assertSame('hash3', $checkpoints->getActiveCheckpoint()?->id);
  $checkpoints->delete('hash2');
  $this->assertCount(1, $checkpoints);
  $this->assertSame('hash3', $checkpoints->getActiveCheckpoint()?->id);
  // We can call getParents without an exception.
  foreach ($checkpoints->getParents('hash3') as $parent) {
    $this->fail('Checkpoint should not have a parent ' . $parent->id);
  }
  $this->assertNull($checkpoints->getActiveCheckpoint()->parent);
  // Now if we delete also the third one, the active one will be null.
  $checkpoints->delete('hash3');
  $this->assertCount(0, $checkpoints);
  $this->assertNull($checkpoints->getActiveCheckpoint());
  $this->assertNull($state->get(self::CHECKPOINT_KEY));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.