b9175b127794a2d229732567c0299438a7f7befe
[yaffs-website] / web / core / modules / field / tests / src / Functional / Update / FieldUpdateTest.php
1 <?php
2
3 namespace Drupal\Tests\field\Functional\Update;
4
5 use Drupal\Core\Config\Config;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\FunctionalTests\Update\UpdatePathTestBase;
8 use Drupal\node\Entity\Node;
9
10 /**
11  * Tests that field settings are properly updated during database updates.
12  *
13  * @group field
14  */
15 class FieldUpdateTest extends UpdatePathTestBase {
16
17   /**
18    * The config factory service.
19    *
20    * @var \Drupal\Core\Config\ConfigFactoryInterface
21    */
22   protected $configFactory;
23
24   /**
25    * {@inheritdoc}
26    */
27   protected function setUp() {
28     parent::setUp();
29     $this->configFactory = $this->container->get('config.factory');
30   }
31
32   /**
33    * {@inheritdoc}
34    */
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',
40     ];
41   }
42
43   /**
44    * Tests field_update_8001().
45    *
46    * @see field_update_8001()
47    */
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));
54
55     // Run updates.
56     $this->runUpdates();
57
58     // Reload the config, and check that the 'target_bundle' setting has been
59     // removed.
60     $config = $this->configFactory->get('field.storage.node.field_image');
61     $settings = $config->get('settings');
62     $this->assertFalse(array_key_exists('target_bundle', $settings));
63   }
64
65   /**
66    * Tests field_update_8002().
67    *
68    * @see field_update_8002()
69    */
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);
76
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);
80
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);
84
85     // Run updates.
86     $this->runUpdates();
87
88     // Check that 'entity_reference' is no longer a dependency of the test field
89     // and view.
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);
97
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']);
101     $node_1->save();
102     $node_2 = Node::create(['type' => 'article', 'title' => 'barbaz']);
103     $node_2->save();
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()]);
108   }
109
110   /**
111    * Tests field_update_8003().
112    *
113    * @see field_update_8003()
114    */
115   public function testFieldUpdate8003() {
116     // Run updates.
117     $this->runUpdates();
118
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');
122
123     $expected_target_bundles = ['tags' => 'tags', 'test' => 'test'];
124     $this->assertEqual($handler_settings['target_bundles'], $expected_target_bundles);
125
126     $this->assertTrue($handler_settings['auto_create']);
127     $this->assertEqual($handler_settings['auto_create_bundle'], 'tags');
128   }
129
130   /**
131    * Asserts that a config depends on 'entity_reference' or not
132    *
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
137    *   absent.
138    */
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);
143   }
144
145   /**
146    * Tests field_post_update_remove_handler_submit_setting().
147    *
148    * @see field_post_update_remove_handler_submit_setting()
149    */
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'));
154
155     $this->runUpdates();
156
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'));
160   }
161
162 }