3 namespace Drupal\Tests\field\Functional\Update;
5 use Drupal\Core\Config\Config;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\FunctionalTests\Update\UpdatePathTestBase;
8 use Drupal\node\Entity\Node;
11 * Tests that field settings are properly updated during database updates.
15 class FieldUpdateTest extends UpdatePathTestBase {
18 * The config factory service.
20 * @var \Drupal\Core\Config\ConfigFactoryInterface
22 protected $configFactory;
27 protected function setUp() {
29 $this->configFactory = $this->container->get('config.factory');
35 protected function setDatabaseDumpFiles() {
36 $this->databaseDumpFiles = [
37 __DIR__ . '/../../../../../system/tests/fixtures/update/drupal-8.bare.standard.php.gz',
38 __DIR__ . '/../../../fixtures/update/drupal-8.views_entity_reference_plugins-2429191.php',
39 __DIR__ . '/../../../fixtures/update/drupal-8.remove_handler_submit_setting-2715589.php',
44 * Tests field_update_8001().
46 * @see field_update_8001()
48 public function testFieldUpdate8001() {
49 // Load the 'node.field_image' field storage config, and check that is has
50 // a 'target_bundle' setting.
51 $config = $this->configFactory->get('field.storage.node.field_image');
52 $settings = $config->get('settings');
53 $this->assertTrue(array_key_exists('target_bundle', $settings));
58 // Reload the config, and check that the 'target_bundle' setting has been
60 $config = $this->configFactory->get('field.storage.node.field_image');
61 $settings = $config->get('settings');
62 $this->assertFalse(array_key_exists('target_bundle', $settings));
66 * Tests field_update_8002().
68 * @see field_update_8002()
70 public function testFieldUpdate8002() {
71 // Check that 'entity_reference' is the provider and a dependency of the
72 // test field storage .
73 $field_storage = $this->configFactory->get('field.storage.node.field_ref_views_select_2429191');
74 $this->assertIdentical($field_storage->get('module'), 'entity_reference');
75 $this->assertEntityRefDependency($field_storage, TRUE);
77 // Check that 'entity_reference' is a dependency of the test field.
78 $field = $this->configFactory->get('field.field.node.article.field_ref_views_select_2429191');
79 $this->assertEntityRefDependency($field, TRUE);
81 // Check that 'entity_reference' is a dependency of the test view.
82 $view = $this->configFactory->get('views.view.entity_reference_plugins_2429191');
83 $this->assertEntityRefDependency($view, TRUE);
88 // Check that 'entity_reference' is no longer a dependency of the test field
90 $field_storage = $this->configFactory->get('field.storage.node.field_ref_views_select_2429191');
91 $this->assertIdentical($field_storage->get('module'), 'core');
92 $this->assertEntityRefDependency($field_storage, FALSE);
93 $field = $this->configFactory->get('field.field.node.article.field_ref_views_select_2429191');
94 $this->assertEntityRefDependency($field, FALSE);
95 $view = $this->configFactory->get('views.view.entity_reference_plugins_2429191');
96 $this->assertEntityRefDependency($view, FALSE);
98 // Check that field selection, based on the view, still works. It only
99 // selects nodes whose title contains 'foo'.
100 $node_1 = Node::create(['type' => 'article', 'title' => 'foobar']);
102 $node_2 = Node::create(['type' => 'article', 'title' => 'barbaz']);
104 $field = FieldConfig::load('node.article.field_ref_views_select_2429191');
105 $selection = \Drupal::service('plugin.manager.entity_reference_selection')->getSelectionHandler($field);
106 $referencable = $selection->getReferenceableEntities();
107 $this->assertEqual(array_keys($referencable['article']), [$node_1->id()]);
111 * Tests field_update_8003().
113 * @see field_update_8003()
115 public function testFieldUpdate8003() {
119 // Check that the new 'auto_create_bundle' setting is populated correctly.
120 $field = $this->configFactory->get('field.field.node.article.field_ref_autocreate_2412569');
121 $handler_settings = $field->get('settings.handler_settings');
123 $expected_target_bundles = ['tags' => 'tags', 'test' => 'test'];
124 $this->assertEqual($handler_settings['target_bundles'], $expected_target_bundles);
126 $this->assertTrue($handler_settings['auto_create']);
127 $this->assertEqual($handler_settings['auto_create_bundle'], 'tags');
131 * Asserts that a config depends on 'entity_reference' or not
133 * @param \Drupal\Core\Config\Config $config
134 * The config to test.
135 * @param bool $present
136 * TRUE to test that entity_reference is present, FALSE to test that it is
139 protected function assertEntityRefDependency(Config $config, $present) {
140 $dependencies = $config->get('dependencies');
141 $dependencies += ['module' => []];
142 $this->assertEqual(in_array('entity_reference', $dependencies['module']), $present);
146 * Tests field_post_update_remove_handler_submit_setting().
148 * @see field_post_update_remove_handler_submit_setting()
150 public function testEntityReferenceFieldConfigCleanUpdate() {
151 $field_config = $this->config('field.field.node.article.field_tags');
152 // Check that 'handler_submit' key exists in field config settings.
153 $this->assertEquals('Change handler', $field_config->get('settings.handler_submit'));
157 $field_config = $this->config('field.field.node.article.field_tags');
158 // Check that 'handler_submit' has been removed from field config settings.
159 $this->assertArrayNotHasKey('handler_submit', $field_config->get('settings'));