3 namespace Drupal\Tests\Core\Field;
5 use Drupal\Core\DependencyInjection\ContainerBuilder;
6 use Drupal\Core\Field\BaseFieldDefinition;
7 use Drupal\Core\Field\FieldTypePluginManager;
8 use Drupal\Core\TypedData\TypedDataManagerInterface;
9 use Drupal\Tests\UnitTestCase;
12 * Provides setup method for testing base field definitions.
14 abstract class BaseFieldDefinitionTestBase extends UnitTestCase {
17 * The field definition used in this test.
19 * @var \Drupal\Core\Field\BaseFieldDefinition
21 protected $definition;
26 protected function setUp() {
29 // getModuleAndPath() returns an array of the module name and directory.
30 list($module_name, $module_dir) = $this->getModuleAndPath();
32 $namespaces = new \ArrayObject();
33 $namespaces["Drupal\\$module_name"] = $module_dir . '/src';
35 $module_handler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
36 $module_handler->expects($this->once())
37 ->method('moduleExists')
39 ->will($this->returnValue(TRUE));
40 $typed_data_manager = $this->getMock(TypedDataManagerInterface::class);
41 $plugin_manager = new FieldTypePluginManager(
43 $this->getMock('Drupal\Core\Cache\CacheBackendInterface'),
48 $container = new ContainerBuilder();
49 $container->set('plugin.manager.field.field_type', $plugin_manager);
50 // The 'string_translation' service is used by the @Translation annotation.
51 $container->set('string_translation', $this->getStringTranslationStub());
52 \Drupal::setContainer($container);
54 $this->definition = BaseFieldDefinition::create($this->getPluginId());
58 * Returns the plugin ID of the tested field type.
63 abstract protected function getPluginId();
66 * Returns the module name and the module directory for the plugin.
68 * drupal_get_path() cannot be used here, because it is not available in
69 * Drupal PHPUnit tests.
72 * A one-dimensional array containing the following strings:
74 * - The module directory, e.g. DRUPAL_CORE . 'core/modules/path'.
76 abstract protected function getModuleAndPath();