be31a09351c5fab895d6350798e935f5f1916ab7
[yaffs-website] / web / core / modules / field / src / Tests / Update / FieldUpdateTest.php
1 <?php
2
3 namespace Drupal\field\Tests\Update;
4
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;
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__ . '/../../../tests/fixtures/update/drupal-8.views_entity_reference_plugins-2429191.php',
39     ];
40   }
41
42   /**
43    * Tests field_update_8001().
44    *
45    * @see field_update_8001()
46    */
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));
53
54     // Run updates.
55     $this->runUpdates();
56
57     // Reload the config, and check that the 'target_bundle' setting has been
58     // removed.
59     $config = $this->configFactory->get('field.storage.node.field_image');
60     $settings = $config->get('settings');
61     $this->assertFalse(array_key_exists('target_bundle', $settings));
62   }
63
64   /**
65    * Tests field_update_8002().
66    *
67    * @see field_update_8002()
68    */
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);
75
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);
79
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);
83
84     // Run updates.
85     $this->runUpdates();
86
87     // Check that 'entity_reference' is no longer a dependency of the test field
88     // and view.
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);
96
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']);
100     $node_1->save();
101     $node_2 = Node::create(['type' => 'article', 'title' => 'barbaz']);
102     $node_2->save();
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()]);
107   }
108
109   /**
110    * Tests field_update_8003().
111    *
112    * @see field_update_8003()
113    */
114   public function testFieldUpdate8003() {
115     // Run updates.
116     $this->runUpdates();
117
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');
121
122     $expected_target_bundles = ['tags' => 'tags', 'test' => 'test'];
123     $this->assertEqual($handler_settings['target_bundles'], $expected_target_bundles);
124
125     $this->assertTrue($handler_settings['auto_create']);
126     $this->assertEqual($handler_settings['auto_create_bundle'], 'tags');
127   }
128
129   /**
130    * Asserts that a config depends on 'entity_reference' or not
131    *
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
136    *   absent.
137    */
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);
142   }
143
144 }