acb7346db0d3b9f1792e417fce7f709a47c49a42
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Field / BaseFieldDefinitionTestBase.php
1 <?php
2
3 namespace Drupal\Tests\Core\Field;
4
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;
10
11 /**
12  * Provides setup method for testing base field definitions.
13  */
14 abstract class BaseFieldDefinitionTestBase extends UnitTestCase {
15
16   /**
17    * The field definition used in this test.
18    *
19    * @var \Drupal\Core\Field\BaseFieldDefinition
20    */
21   protected $definition;
22
23   /**
24    * {@inheritdoc}
25    */
26   protected function setUp() {
27     parent::setUp();
28
29     // getModuleAndPath() returns an array of the module name and directory.
30     list($module_name, $module_dir) = $this->getModuleAndPath();
31
32     $namespaces = new \ArrayObject();
33     $namespaces["Drupal\\$module_name"] = $module_dir . '/src';
34
35     $module_handler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
36     $module_handler->expects($this->once())
37       ->method('moduleExists')
38       ->with($module_name)
39       ->will($this->returnValue(TRUE));
40     $typed_data_manager = $this->getMock(TypedDataManagerInterface::class);
41     $plugin_manager = new FieldTypePluginManager(
42       $namespaces,
43       $this->getMock('Drupal\Core\Cache\CacheBackendInterface'),
44       $module_handler,
45       $typed_data_manager
46     );
47
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);
53
54     $this->definition = BaseFieldDefinition::create($this->getPluginId());
55   }
56
57   /**
58    * Returns the plugin ID of the tested field type.
59    *
60    * @return string
61    *   The plugin ID.
62    */
63   abstract protected function getPluginId();
64
65   /**
66    * Returns the module name and the module directory for the plugin.
67    *
68    * drupal_get_path() cannot be used here, because it is not available in
69    * Drupal PHPUnit tests.
70    *
71    * @return array
72    *   A one-dimensional array containing the following strings:
73    *   - The module name.
74    *   - The module directory, e.g. DRUPAL_CORE . 'core/modules/path'.
75    */
76   abstract protected function getModuleAndPath();
77
78 }