Mercurial > hg > isophonics-drupal-site
comparison core/modules/field/tests/src/Kernel/WidgetPluginManagerTest.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\Tests\field\Kernel; | |
4 | |
5 use Drupal\Core\Field\BaseFieldDefinition; | |
6 | |
7 /** | |
8 * Tests the field widget manager. | |
9 * | |
10 * @group field | |
11 */ | |
12 class WidgetPluginManagerTest extends FieldKernelTestBase { | |
13 | |
14 /** | |
15 * Tests that the widget definitions alter hook works. | |
16 */ | |
17 public function testWidgetDefinitionAlter() { | |
18 $widget_definition = \Drupal::service('plugin.manager.field.widget')->getDefinition('test_field_widget_multiple'); | |
19 | |
20 // Test if hook_field_widget_info_alter is being called. | |
21 $this->assertTrue(in_array('test_field', $widget_definition['field_types']), "The 'test_field_widget_multiple' widget is enabled for the 'test_field' field type in field_test_field_widget_info_alter()."); | |
22 } | |
23 | |
24 /** | |
25 * Tests that getInstance falls back on default if current is not applicable. | |
26 * | |
27 * @see \Drupal\field\Tests\FormatterPluginManagerTest::testNotApplicableFallback() | |
28 */ | |
29 public function testNotApplicableFallback() { | |
30 /** @var \Drupal\Core\Field\WidgetPluginManager $widget_plugin_manager */ | |
31 $widget_plugin_manager = \Drupal::service('plugin.manager.field.widget'); | |
32 | |
33 $base_field_definition = BaseFieldDefinition::create('test_field') | |
34 // Set a name that will make isApplicable() return TRUE. | |
35 ->setName('field_multiwidgetfield'); | |
36 | |
37 $widget_options = [ | |
38 'field_definition' => $base_field_definition, | |
39 'form_mode' => 'default', | |
40 'configuration' => [ | |
41 'type' => 'test_field_widget_multiple', | |
42 ], | |
43 ]; | |
44 | |
45 $instance = $widget_plugin_manager->getInstance($widget_options); | |
46 $this->assertEqual($instance->getPluginId(), 'test_field_widget_multiple'); | |
47 | |
48 // Now do the same but with machine name field_onewidgetfield, because that | |
49 // makes isApplicable() return FALSE. | |
50 $base_field_definition->setName('field_onewidgetfield'); | |
51 $instance = $widget_plugin_manager->getInstance($widget_options); | |
52 | |
53 // Instance should be default widget. | |
54 $this->assertNotEqual($instance->getPluginId(), 'test_field_widget_multiple'); | |
55 $this->assertEqual($instance->getPluginId(), 'test_field_widget'); | |
56 } | |
57 | |
58 } |