class OptionsAllowedValuesTest
Same name and namespace in other branches
- 11.x core/modules/options/tests/src/Kernel/OptionsAllowedValuesTest.php \Drupal\Tests\options\Kernel\OptionsAllowedValuesTest
Tests the caching of options allowed values.
Attributes
#[Group('options')]
#[RunTestsInSeparateProcesses]
Hierarchy
- class \Drupal\Tests\DrupalTestCase uses \Drupal\Tests\DrupalTestCaseTrait extends \PHPUnit\Framework\TestCase
- class \Drupal\KernelTests\KernelTestBase implements \Drupal\Core\DependencyInjection\ServiceProviderInterface uses \Drupal\KernelTests\AssertContentTrait, \Drupal\Tests\RandomGeneratorTrait, \Drupal\Tests\ConfigTestTrait, \Drupal\Tests\ExtensionListTestTrait, \Prophecy\PhpUnit\ProphecyTrait, \Drupal\Tests\BrowserHtmlDebugTrait, \Drupal\Tests\HttpKernelUiHelperTrait extends \Drupal\Tests\DrupalTestCase
- class \Drupal\Tests\field\Kernel\FieldKernelTestBase extends \Drupal\KernelTests\KernelTestBase
- class \Drupal\Tests\options\Kernel\OptionsFieldUnitTestBase extends \Drupal\Tests\field\Kernel\FieldKernelTestBase
- class \Drupal\Tests\options\Kernel\OptionsAllowedValuesTest extends \Drupal\Tests\options\Kernel\OptionsFieldUnitTestBase
- class \Drupal\Tests\options\Kernel\OptionsFieldUnitTestBase extends \Drupal\Tests\field\Kernel\FieldKernelTestBase
- class \Drupal\Tests\field\Kernel\FieldKernelTestBase extends \Drupal\KernelTests\KernelTestBase
- class \Drupal\KernelTests\KernelTestBase implements \Drupal\Core\DependencyInjection\ServiceProviderInterface uses \Drupal\KernelTests\AssertContentTrait, \Drupal\Tests\RandomGeneratorTrait, \Drupal\Tests\ConfigTestTrait, \Drupal\Tests\ExtensionListTestTrait, \Prophecy\PhpUnit\ProphecyTrait, \Drupal\Tests\BrowserHtmlDebugTrait, \Drupal\Tests\HttpKernelUiHelperTrait extends \Drupal\Tests\DrupalTestCase
Expanded class hierarchy of OptionsAllowedValuesTest
File
-
core/
modules/ options/ tests/ src/ Kernel/ OptionsAllowedValuesTest.php, line 16
Namespace
Drupal\Tests\options\KernelView source
class OptionsAllowedValuesTest extends OptionsFieldUnitTestBase {
/**
* Tests that each field is cached in its own item with its own cache tags.
*/
public function testCachedPerField() : void {
$service = $this->container
->get(OptionsAllowedValuesInterface::class);
$cache = $this->container
->get('cache.memory');
// Create a second list field on the same entity type.
$second_storage = FieldStorageConfig::create([
'field_name' => 'test_options_2',
'entity_type' => 'entity_test',
'type' => 'list_integer',
'cardinality' => 1,
'settings' => [
'allowed_values' => [
10 => 'Ten',
20 => 'Twenty',
],
],
]);
$second_storage->save();
// Starting from a cold cache, request the allowed values of both fields.
$this->assertSame([
1 => 'One',
2 => 'Two',
3 => 'Three',
], $service->getAllowedValues($this->fieldStorage));
$this->assertSame([
10 => 'Ten',
20 => 'Twenty',
], $service->getAllowedValues($second_storage));
// Each field is cached in a separate item tagged with the cache tags of
// its own field storage.
$first_item = $cache->get('options.allowed_values:entity_test:test_options:any');
$second_item = $cache->get('options.allowed_values:entity_test:test_options_2:any');
$this->assertSame([
1 => 'One',
2 => 'Two',
3 => 'Three',
], $first_item->data);
$this->assertSame([
10 => 'Ten',
20 => 'Twenty',
], $second_item->data);
$this->assertContains('config:field.storage.entity_test.test_options', $first_item->tags);
$this->assertContains('config:field.storage.entity_test.test_options_2', $second_item->tags);
// Updating the first field storage invalidates its cached allowed values,
// even though the allowed values of another field were cached later. The
// second field stays cached.
$this->fieldStorage
->setSetting('allowed_values', [
1 => 'First',
2 => 'Second',
])
->save();
$this->assertFalse($cache->get('options.allowed_values:entity_test:test_options:any'));
$this->assertNotFalse($cache->get('options.allowed_values:entity_test:test_options_2:any'));
$this->assertSame([
1 => 'First',
2 => 'Second',
], $service->getAllowedValues($this->fieldStorage));
$this->assertSame([
10 => 'Ten',
20 => 'Twenty',
], $service->getAllowedValues($second_storage));
}
/**
* Tests that the field storage form submit resets the cached values.
*/
public function testSubmitFieldStorageUpdate() : void {
$service = $this->container
->get(OptionsAllowedValuesInterface::class);
$this->assertSame([
1 => 'One',
2 => 'Two',
3 => 'Three',
], $service->getAllowedValues($this->fieldStorage));
// Change the setting without saving: the cached values are still returned.
$this->fieldStorage
->setSetting('allowed_values', [
1 => 'First',
]);
$this->assertSame([
1 => 'One',
2 => 'Two',
3 => 'Three',
], $service->getAllowedValues($this->fieldStorage));
// The field storage form submit handler resets all cached allowed values.
ListItemBase::submitFieldStorageUpdate();
$this->assertSame([
1 => 'First',
], $service->getAllowedValues($this->fieldStorage));
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.