|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\Tests\Core\Field; |
| 4 | + |
| 5 | +use Drupal\Component\Plugin\Discovery\DiscoveryInterface; |
| 6 | +use Drupal\Core\Cache\CacheBackendInterface; |
| 7 | +use Drupal\Core\DependencyInjection\ContainerBuilder; |
| 8 | +use Drupal\Core\Extension\ModuleHandlerInterface; |
| 9 | +use Drupal\Core\Field\FieldTypeCategoryManagerInterface; |
| 10 | +use Drupal\Core\Field\FieldTypePluginManager; |
| 11 | +use Drupal\Core\Session\AccountInterface; |
| 12 | +use Drupal\Core\TypedData\TypedDataManager; |
| 13 | +use Drupal\Tests\UnitTestCase; |
| 14 | +use Prophecy\Argument; |
| 15 | + |
| 16 | +/** |
| 17 | + * @coversDefaultClass \Drupal\Core\Field\FieldTypePluginManager |
| 18 | + * @group Field |
| 19 | + */ |
| 20 | +class FieldTypePluginManagerTest extends UnitTestCase { |
| 21 | + |
| 22 | + /** |
| 23 | + * The field type plugin manager. |
| 24 | + * |
| 25 | + * @var \Drupal\Core\Field\FieldTypePluginManager |
| 26 | + */ |
| 27 | + protected FieldTypePluginManager $fieldTypeManager; |
| 28 | + |
| 29 | + /** |
| 30 | + * A mocked module handler. |
| 31 | + * |
| 32 | + * @var \Drupal\Core\Extension\ModuleHandlerInterface|\Prophecy\Prophecy\ObjectProphecy |
| 33 | + */ |
| 34 | + protected $moduleHandler; |
| 35 | + |
| 36 | + /** |
| 37 | + * A mocked module handler. |
| 38 | + * |
| 39 | + * @var \Drupal\Core\Extension\ModuleHandlerInterface|\Prophecy\Prophecy\ObjectProphecy |
| 40 | + */ |
| 41 | + protected $fieldTypeCategoryManager; |
| 42 | + |
| 43 | + /** |
| 44 | + * A mocked plugin discovery. |
| 45 | + * |
| 46 | + * @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface|\Prophecy\Prophecy\ObjectProphecy |
| 47 | + */ |
| 48 | + protected $discovery; |
| 49 | + |
| 50 | + /** |
| 51 | + * {@inheritdoc} |
| 52 | + */ |
| 53 | + protected function setUp(): void { |
| 54 | + parent::setUp(); |
| 55 | + |
| 56 | + $container = new ContainerBuilder(); |
| 57 | + $current_user = $this->prophesize(AccountInterface::class); |
| 58 | + $container->set('current_user', $current_user->reveal()); |
| 59 | + $container->set('string_translation', $this->getStringTranslationStub()); |
| 60 | + \Drupal::setContainer($container); |
| 61 | + |
| 62 | + $cache_backend = $this->prophesize(CacheBackendInterface::class); |
| 63 | + $this->moduleHandler = $this->prophesize(ModuleHandlerInterface::class); |
| 64 | + $this->moduleHandler->alter('field_info', Argument::any())->willReturn(NULL); |
| 65 | + $typed_data_manager = $this->prophesize(TypedDataManager::class); |
| 66 | + $this->fieldTypeCategoryManager = $this->prophesize(FieldTypeCategoryManagerInterface::class); |
| 67 | + |
| 68 | + $this->fieldTypeManager = new FieldTypePluginManager(new \ArrayObject(), $cache_backend->reveal(), $this->moduleHandler->reveal(), $typed_data_manager->reveal(), $this->fieldTypeCategoryManager->reveal()); |
| 69 | + $this->fieldTypeManager->setStringTranslation($this->getStringTranslationStub()); |
| 70 | + |
| 71 | + $this->discovery = $this->prophesize(DiscoveryInterface::class); |
| 72 | + $property = new \ReflectionProperty(FieldTypePluginManager::class, 'discovery'); |
| 73 | + $property->setAccessible(TRUE); |
| 74 | + $property->setValue($this->fieldTypeManager, $this->discovery->reveal()); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @covers ::getGroupedDefinitions |
| 79 | + */ |
| 80 | + public function testGetGroupedDefinitions() { |
| 81 | + $this->discovery->getDefinitions()->willReturn([ |
| 82 | + 'telephone' => [ |
| 83 | + 'category' => 'general', |
| 84 | + 'label' => 'Telephone', |
| 85 | + 'id' => 'telephone', |
| 86 | + ], |
| 87 | + 'string' => [ |
| 88 | + 'category' => 'text', |
| 89 | + 'label' => 'Text (plain)', |
| 90 | + 'id' => 'string', |
| 91 | + ], |
| 92 | + 'integer' => [ |
| 93 | + 'category' => 'number', |
| 94 | + 'label' => 'Number (integer)', |
| 95 | + 'id' => 'integer', |
| 96 | + ], |
| 97 | + 'float' => [ |
| 98 | + 'id' => 'float', |
| 99 | + 'label' => 'Number (float)', |
| 100 | + 'category' => 'number', |
| 101 | + ], |
| 102 | + ]); |
| 103 | + |
| 104 | + $this->fieldTypeCategoryManager->getDefinitions()->willReturn([ |
| 105 | + 'general' => [ |
| 106 | + 'label' => 'General', |
| 107 | + 'id' => 'general', |
| 108 | + ], |
| 109 | + 'number' => [ |
| 110 | + 'label' => 'Number 🦥', |
| 111 | + 'id' => 'number', |
| 112 | + ], |
| 113 | + 'text' => [ |
| 114 | + 'label' => 'Text 🐈', |
| 115 | + 'id' => 'text', |
| 116 | + ], |
| 117 | + 'empty_group' => [ |
| 118 | + 'label' => 'Empty 🦗', |
| 119 | + 'id' => 'empty_group', |
| 120 | + ], |
| 121 | + ]); |
| 122 | + |
| 123 | + $grouped_definitions = $this->fieldTypeManager->getGroupedDefinitions(); |
| 124 | + $this->assertEquals(['General', 'Number 🦥', 'Text 🐈'], array_keys($grouped_definitions)); |
| 125 | + |
| 126 | + $grouped_definitions = $this->fieldTypeManager->getGroupedDefinitions(NULL, 'label', 'id'); |
| 127 | + $this->assertEquals(['general', 'number', 'text'], array_keys($grouped_definitions)); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * @covers ::getGroupedDefinitions |
| 132 | + */ |
| 133 | + public function testGetGroupedDefinitionsInvalid() { |
| 134 | + $this->discovery->getDefinitions()->willReturn([ |
| 135 | + 'string' => [ |
| 136 | + 'category' => 'text', |
| 137 | + 'label' => 'Text (plain)', |
| 138 | + 'id' => 'string', |
| 139 | + ], |
| 140 | + ]); |
| 141 | + |
| 142 | + $this->fieldTypeCategoryManager->getDefinitions()->willReturn([ |
| 143 | + 'general' => [ |
| 144 | + 'label' => 'General', |
| 145 | + 'id' => 'general', |
| 146 | + ], |
| 147 | + ]); |
| 148 | + |
| 149 | + $zend_assertions_default = ini_get('zend.assertions'); |
| 150 | + $assert_active_default = assert_options(ASSERT_ACTIVE); |
| 151 | + |
| 152 | + // Test behavior when assertions are not enabled. |
| 153 | + ini_set('zend.assertions', 0); |
| 154 | + assert_options(ASSERT_ACTIVE, 0); |
| 155 | + $grouped_definitions = $this->fieldTypeManager->getGroupedDefinitions(); |
| 156 | + $this->assertEquals(['General'], array_keys($grouped_definitions)); |
| 157 | + |
| 158 | + // Test behavior when assertions are enabled. |
| 159 | + ini_set('zend.assertions', 1); |
| 160 | + assert_options(ASSERT_ACTIVE, 1); |
| 161 | + $this->expectException(\AssertionError::class); |
| 162 | + try { |
| 163 | + $this->fieldTypeManager->getGroupedDefinitions(); |
| 164 | + } |
| 165 | + catch (\Exception $e) { |
| 166 | + // Reset the original assert values. |
| 167 | + ini_set('zend.assertions', $zend_assertions_default); |
| 168 | + assert_options(ASSERT_ACTIVE, $assert_active_default); |
| 169 | + |
| 170 | + throw $e; |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * @covers ::getGroupedDefinitions |
| 176 | + */ |
| 177 | + public function testGetGroupedDefinitionsEmpty() { |
| 178 | + $this->fieldTypeCategoryManager->getDefinitions()->willReturn([]); |
| 179 | + $this->assertEquals([], $this->fieldTypeManager->getGroupedDefinitions([])); |
| 180 | + } |
| 181 | + |
| 182 | +} |
0 commit comments