3 namespace Drupal\field\Tests\Update;
5 use Drupal\Core\Config\Config;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\node\Entity\Node;
8 use Drupal\system\Tests\Update\UpdatePathTestBase;
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__ . '/../../../tests/fixtures/update/drupal-8.views_entity_reference_plugins-2429191.php',
43 * Tests field_update_8001().
45 * @see field_update_8001()
47 public function testFieldUpdate8001() {
48 // Load the 'node.field_image' field storage config, and check that is has
49 // a 'target_bundle' setting.
50 $config = $this->configFactory->get('field.storage.node.field_image');
51 $settings = $config->get('settings');
52 $this->assertTrue(array_key_exists('target_bundle', $settings));
57 // Reload the config, and check that the 'target_bundle' setting has been
59 $config = $this->configFactory->get('field.storage.node.field_image');
60 $settings = $config->get('settings');
61 $this->assertFalse(array_key_exists('target_bundle', $settings));
65 * Tests field_update_8002().
67 * @see field_update_8002()
69 public function testFieldUpdate8002() {
70 // Check that 'entity_reference' is the provider and a dependency of the
71 // test field storage .
72 $field_storage = $this->configFactory->get('field.storage.node.field_ref_views_select_2429191');
73 $this->assertIdentical($field_storage->get('module'), 'entity_reference');
74 $this->assertEntityRefDependency($field_storage, TRUE);
76 // Check that 'entity_reference' is a dependency of the test field.
77 $field = $this->configFactory->get('field.field.node.article.field_ref_views_select_2429191');
78 $this->assertEntityRefDependency($field, TRUE);
80 // Check that 'entity_reference' is a dependency of the test view.
81 $view = $this->configFactory->get('views.view.entity_reference_plugins_2429191');
82 $this->assertEntityRefDependency($view, TRUE);
87 // Check that 'entity_reference' is no longer a dependency of the test field
89 $field_storage = $this->configFactory->get('field.storage.node.field_ref_views_select_2429191');
90 $this->assertIdentical($field_storage->get('module'), 'core');
91 $this->assertEntityRefDependency($field_storage, FALSE);
92 $field = $this->configFactory->get('field.field.node.article.field_ref_views_select_2429191');
93 $this->assertEntityRefDependency($field, FALSE);
94 $view = $this->configFactory->get('views.view.entity_reference_plugins_2429191');
95 $this->assertEntityRefDependency($view, FALSE);
97 // Check that field selection, based on the view, still works. It only
98 // selects nodes whose title contains 'foo'.
99 $node_1 = Node::create(['type' => 'article', 'title' => 'foobar']);
101 $node_2 = Node::create(['type' => 'article', 'title' => 'barbaz']);
103 $field = FieldConfig::load('node.article.field_ref_views_select_2429191');
104 $selection = \Drupal::service('plugin.manager.entity_reference_selection')->getSelectionHandler($field);
105 $referencable = $selection->getReferenceableEntities();
106 $this->assertEqual(array_keys($referencable['article']), [$node_1->id()]);
110 * Tests field_update_8003().
112 * @see field_update_8003()
114 public function testFieldUpdate8003() {
118 // Check that the new 'auto_create_bundle' setting is populated correctly.
119 $field = $this->configFactory->get('field.field.node.article.field_ref_autocreate_2412569');
120 $handler_settings = $field->get('settings.handler_settings');
122 $expected_target_bundles = ['tags' => 'tags', 'test' => 'test'];
123 $this->assertEqual($handler_settings['target_bundles'], $expected_target_bundles);
125 $this->assertTrue($handler_settings['auto_create']);
126 $this->assertEqual($handler_settings['auto_create_bundle'], 'tags');
130 * Asserts that a config depends on 'entity_reference' or not
132 * @param \Drupal\Core\Config\Config $config
133 * The config to test.
134 * @param bool $present
135 * TRUE to test that entity_reference is present, FALSE to test that it is
138 protected function assertEntityRefDependency(Config $config, $present) {
139 $dependencies = $config->get('dependencies');
140 $dependencies += ['module' => []];
141 $this->assertEqual(in_array('entity_reference', $dependencies['module']), $present);