class FieldTypePluginManagerTest

Same name in this branch
  1. 11.x core/modules/field/tests/src/Kernel/FieldTypePluginManagerTest.php \Drupal\Tests\field\Kernel\FieldTypePluginManagerTest
Same name and namespace in other branches
  1. 9 core/modules/field/tests/src/Kernel/FieldTypePluginManagerTest.php \Drupal\Tests\field\Kernel\FieldTypePluginManagerTest
  2. 8.9.x core/modules/field/tests/src/Kernel/FieldTypePluginManagerTest.php \Drupal\Tests\field\Kernel\FieldTypePluginManagerTest
  3. 10 core/modules/field/tests/src/Kernel/FieldTypePluginManagerTest.php \Drupal\Tests\field\Kernel\FieldTypePluginManagerTest
  4. 10 core/tests/Drupal/Tests/Core/Field/FieldTypePluginManagerTest.php \Drupal\Tests\Core\Field\FieldTypePluginManagerTest

@coversDefaultClass \Drupal\Core\Field\FieldTypePluginManager
@group Field

Hierarchy

Expanded class hierarchy of FieldTypePluginManagerTest

File

core/tests/Drupal/Tests/Core/Field/FieldTypePluginManagerTest.php, line 22

Namespace

Drupal\Tests\Core\Field
View source
class FieldTypePluginManagerTest extends UnitTestCase {
  
  /**
   * The field type plugin manager.
   *
   * @var \Drupal\Core\Field\FieldTypePluginManager
   */
  protected FieldTypePluginManager $fieldTypeManager;
  
  /**
   * A mocked module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $moduleHandler;
  
  /**
   * A mocked module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $fieldTypeCategoryManager;
  
  /**
   * A mocked plugin discovery.
   *
   * @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $discovery;
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $container = new ContainerBuilder();
    $current_user = $this->prophesize(AccountInterface::class);
    $container->set('current_user', $current_user->reveal());
    $container->set('string_translation', $this->getStringTranslationStub());
    \Drupal::setContainer($container);
    $cache_backend = $this->prophesize(CacheBackendInterface::class);
    $this->moduleHandler = $this->prophesize(ModuleHandlerInterface::class);
    $this->moduleHandler
      ->alter('field_info', Argument::any())
      ->willReturn(NULL);
    $typed_data_manager = $this->prophesize(TypedDataManager::class);
    $this->fieldTypeCategoryManager = $this->prophesize(FieldTypeCategoryManagerInterface::class);
    $this->fieldTypeManager = new FieldTypePluginManager(new \ArrayObject(), $cache_backend->reveal(), $this->moduleHandler
      ->reveal(), $typed_data_manager->reveal(), $this->fieldTypeCategoryManager
      ->reveal());
    $this->fieldTypeManager
      ->setStringTranslation($this->getStringTranslationStub());
    $this->discovery = $this->prophesize(DiscoveryInterface::class);
    $property = new \ReflectionProperty(FieldTypePluginManager::class, 'discovery');
    $property->setAccessible(TRUE);
    $property->setValue($this->fieldTypeManager, $this->discovery
      ->reveal());
  }
  
  /**
   * @covers ::getGroupedDefinitions
   */
  public function testGetGroupedDefinitions() : void {
    $this->discovery
      ->getDefinitions()
      ->willReturn([
      'telephone' => [
        'category' => 'general',
        'label' => 'Telephone',
        'id' => 'telephone',
      ],
      'string' => [
        'category' => 'text',
        'label' => 'Text (plain)',
        'id' => 'string',
      ],
      'integer' => [
        'category' => 'number',
        'label' => 'Number (integer)',
        'id' => 'integer',
      ],
      'float' => [
        'id' => 'float',
        'label' => 'Number (float)',
        'category' => 'number',
      ],
    ]);
    $this->fieldTypeCategoryManager
      ->getDefinitions()
      ->willReturn([
      'general' => [
        'label' => 'General',
        'id' => 'general',
      ],
      'number' => [
        'label' => 'Number 🦥',
        'id' => 'number',
      ],
      'text' => [
        'label' => 'Text 🐈',
        'id' => 'text',
      ],
      'empty_group' => [
        'label' => 'Empty 🦗',
        'id' => 'empty_group',
      ],
    ]);
    $grouped_definitions = $this->fieldTypeManager
      ->getGroupedDefinitions();
    $this->assertEquals([
      'General',
      'Number 🦥',
      'Text 🐈',
    ], array_keys($grouped_definitions));
    $grouped_definitions = $this->fieldTypeManager
      ->getGroupedDefinitions(NULL, 'label', 'id');
    $this->assertEquals([
      'general',
      'number',
      'text',
    ], array_keys($grouped_definitions));
  }
  
  /**
   * @covers ::getGroupedDefinitions
   */
  public function testGetGroupedDefinitionsInvalid() : void {
    $this->discovery
      ->getDefinitions()
      ->willReturn([
      'string' => [
        'category' => 'text',
        'label' => 'Text (plain)',
        'id' => 'string',
      ],
    ]);
    $this->fieldTypeCategoryManager
      ->getDefinitions()
      ->willReturn([
      'general' => [
        'label' => 'General',
        'id' => 'general',
      ],
    ]);
    $zend_assertions_default = ini_get('zend.assertions');
    // Test behavior when assertions are not enabled.
    ini_set('zend.assertions', 0);
    $grouped_definitions = $this->fieldTypeManager
      ->getGroupedDefinitions();
    $this->assertEquals([
      'General',
    ], array_keys($grouped_definitions));
    // Test behavior when assertions are enabled.
    ini_set('zend.assertions', 1);
    $this->expectException(\AssertionError::class);
    try {
      $this->fieldTypeManager
        ->getGroupedDefinitions();
    } catch (\Exception $e) {
      // Reset the original assert values.
      ini_set('zend.assertions', $zend_assertions_default);
      throw $e;
    }
  }
  
  /**
   * @covers ::getGroupedDefinitions
   */
  public function testGetGroupedDefinitionsEmpty() : void {
    $this->fieldTypeCategoryManager
      ->getDefinitions()
      ->willReturn([]);
    $this->assertEquals([], $this->fieldTypeManager
      ->getGroupedDefinitions([]));
  }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
FieldTypePluginManagerTest::$discovery protected property A mocked plugin discovery.
FieldTypePluginManagerTest::$fieldTypeCategoryManager protected property A mocked module handler.
FieldTypePluginManagerTest::$fieldTypeManager protected property The field type plugin manager.
FieldTypePluginManagerTest::$moduleHandler protected property A mocked module handler.
FieldTypePluginManagerTest::setUp protected function Overrides UnitTestCase::setUp
FieldTypePluginManagerTest::testGetGroupedDefinitions public function @covers ::getGroupedDefinitions[[api-linebreak]]
FieldTypePluginManagerTest::testGetGroupedDefinitionsEmpty public function @covers ::getGroupedDefinitions[[api-linebreak]]
FieldTypePluginManagerTest::testGetGroupedDefinitionsInvalid public function @covers ::getGroupedDefinitions[[api-linebreak]]
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
UnitTestCase::$root protected property The app root.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setDebugDumpHandler public static function Registers the dumper CLI handler when the DebugDump extension is enabled.

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