function StateTest::testGetValuesSetDuringRequest

Tests the ::getValuesSetDuringRequest() method.

@legacy-covers ::getValuesSetDuringRequest

File

core/tests/Drupal/Tests/Core/State/StateTest.php, line 390

Class

StateTest
Tests Drupal\Core\State\State.

Namespace

Drupal\Tests\Core\State

Code

public function testGetValuesSetDuringRequest() : void {
  // Confirm getValuesSetDuringRequest() returns the correct values for
  // new keys set in state by setMultiple() during the request.
  $values = [
    'key1' => 'value1',
    'key2' => 'value2',
    'key3' => 'value3',
  ];
  $this->state
    ->setMultiple($values);
  $this->assertSame([
    'value' => 'value1',
    'original' => NULL,
  ], $this->state
    ->getValuesSetDuringRequest('key1'));
  $this->assertSame([
    'value' => 'value2',
    'original' => NULL,
  ], $this->state
    ->getValuesSetDuringRequest('key2'));
  $this->assertSame([
    'value' => 'value3',
    'original' => NULL,
  ], $this->state
    ->getValuesSetDuringRequest('key3'));
  // Confirm that getValuesSetDuringRequest() returns NULL for key not set in
  // state during the request.
  $this->assertNull($this->state
    ->getValuesSetDuringRequest('key4'));
  // Confirm that getValuesSetDuringRequest() returns the correct values for
  // new keys set in state by setMultiple() during the request and that
  // values for keys previously set during the request are not overwritten.
  $nonOverwritingValues = [
    'key4' => 'value4',
    'key5' => 'value5',
    'key6' => 'value6',
  ];
  $this->state
    ->setMultiple($nonOverwritingValues);
  $this->assertSame([
    'value' => 'value1',
    'original' => NULL,
  ], $this->state
    ->getValuesSetDuringRequest('key1'));
  $this->assertSame([
    'value' => 'value2',
    'original' => NULL,
  ], $this->state
    ->getValuesSetDuringRequest('key2'));
  $this->assertSame([
    'value' => 'value3',
    'original' => NULL,
  ], $this->state
    ->getValuesSetDuringRequest('key3'));
  $this->assertSame([
    'value' => 'value4',
    'original' => NULL,
  ], $this->state
    ->getValuesSetDuringRequest('key4'));
  $this->assertSame([
    'value' => 'value5',
    'original' => NULL,
  ], $this->state
    ->getValuesSetDuringRequest('key5'));
  $this->assertSame([
    'value' => 'value6',
    'original' => NULL,
  ], $this->state
    ->getValuesSetDuringRequest('key6'));
  // Confirm getValuesSetDuringRequest() returns the correct new values for
  // keys set by setMultiple() again in state during the request.
  $overwritingValues = [
    'key5' => 'new-value-5',
    'key6' => 'new-value-6',
  ];
  $this->state
    ->setMultiple($overwritingValues);
  $this->assertSame([
    'value' => 'new-value-5',
    'original' => NULL,
  ], $this->state
    ->getValuesSetDuringRequest('key5'));
  $this->assertSame([
    'value' => 'new-value-6',
    'original' => NULL,
  ], $this->state
    ->getValuesSetDuringRequest('key6'));
  // Confirm getValuesSetDuringRequest() returns the correct new value for
  // a key set in state by set() during the request.
  $this->state
    ->set('key4', 'new-value-4');
  $this->assertSame([
    'value' => 'new-value-4',
    'original' => NULL,
  ], $this->state
    ->getValuesSetDuringRequest('key4'));
}

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