Newer
Older
Angie Byron
committed
<?php
Alex Pott
committed
Angie Byron
committed
namespace Drupal\Tests\Core\Field;
use Drupal\Core\DependencyInjection\ContainerBuilder;
Alex Pott
committed
use Drupal\Core\Field\BaseFieldDefinition;
Angie Byron
committed
use Drupal\Core\Field\FieldTypePluginManager;
use Drupal\Core\TypedData\TypedDataManagerInterface;
Angie Byron
committed
use Drupal\Tests\UnitTestCase;
/**
Alex Pott
committed
* Provides setup method for testing base field definitions.
Angie Byron
committed
*/
Alex Pott
committed
abstract class BaseFieldDefinitionTestBase extends UnitTestCase {
Angie Byron
committed
/**
* The field definition used in this test.
*
Alex Pott
committed
* @var \Drupal\Core\Field\BaseFieldDefinition
Angie Byron
committed
*/
protected $definition;
/**
* {@inheritdoc}
*/
Alex Pott
committed
protected function setUp() {
// getModuleAndPath() returns an array of the module name and directory.
list($module_name, $module_dir) = $this->getModuleAndPath();
$namespaces = new \ArrayObject();
$namespaces["Drupal\\$module_name"] = $module_dir . '/src';
Angie Byron
committed
$module_handler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
$module_handler->expects($this->once())
->method('moduleExists')
->with($module_name)
->will($this->returnValue(TRUE));
$typed_data_manager = $this->getMock(TypedDataManagerInterface::class);
Angie Byron
committed
$plugin_manager = new FieldTypePluginManager(
$namespaces,
$this->getMock('Drupal\Core\Cache\CacheBackendInterface'),
Alex Pott
committed
$module_handler,
$typed_data_manager
Angie Byron
committed
);
$container = new ContainerBuilder();
$container->set('plugin.manager.field.field_type', $plugin_manager);
// The 'string_translation' service is used by the @Translation annotation.
$container->set('string_translation', $this->getStringTranslationStub());
\Drupal::setContainer($container);
Alex Pott
committed
$this->definition = BaseFieldDefinition::create($this->getPluginId());
Angie Byron
committed
}
/**
* Returns the plugin ID of the tested field type.
*
* @return string
* The plugin ID.
*/
abstract protected function getPluginId();
/**
* Returns the module name and the module directory for the plugin.
Angie Byron
committed
*
* drupal_get_path() cannot be used here, because it is not available in
* Drupal PHPUnit tests.
Angie Byron
committed
*
* @return array
* A one-dimensional array containing the following strings:
* - The module name.
* - The module directory, e.g. DRUPAL_CORE . 'core/modules/path'.
Angie Byron
committed
*/
abstract protected function getModuleAndPath();