Upgraded imagemagick and manually altered pdf to image module to handle changes....
[yaffs-website] / web / modules / contrib / entity / tests / src / Kernel / BundlePluginTest.php
1 <?php
2
3 namespace Drupal\Tests\entity\Kernel;
4
5 use Drupal\entity_module_bundle_plugin_test\Entity\EntityTestBundlePlugin;
6 use Drupal\KernelTests\KernelTestBase;
7
8 /**
9  * Tests the bundle plugin API.
10  *
11  * @group entity
12  */
13 class BundlePluginTest extends KernelTestBase {
14
15   /**
16    * {@inheritdoc}
17    */
18   public static $modules = [
19     'system',
20     'entity',
21   ];
22
23   /**
24    * {@inheritdoc}
25    */
26   protected function setUp() {
27     parent::setUp();
28
29     $this->installSchema('system', 'router');
30
31     // Install the modules properly. Putting them into static::$modules doesn't trigger the install
32     // hooks, like hook_modules_installed, so entity_modules_installed is not triggered().
33     /** @var \Drupal\Core\Extension\ModuleInstallerInterface $module_installer */
34     $module_installer = \Drupal::service('module_installer');
35     $module_installer->install(['entity_module_bundle_plugin_test', 'entity_module_bundle_plugin_examples_test']);
36   }
37
38   /**
39    * Tests the bundle plugins.
40    */
41   public function testPluginBundles() {
42     $bundled_entity_types = entity_get_bundle_plugin_entity_types();
43     /** @var \Drupal\Core\Entity\EntityTypeInterface $entity_type */
44     $entity_type = $bundled_entity_types['entity_test_bundle_plugin'];
45     $this->assertEquals('entity_test_bundle_plugin', $entity_type->id());
46     $this->assertTrue($entity_type->hasHandlerClass('bundle_plugin'));
47
48     /** @var \Drupal\Core\Entity\EntityTypeBundleInfo $entity_type_bundle_info */
49     $entity_type_bundle_info = \Drupal::service('entity_type.bundle.info');
50     $bundle_info = $entity_type_bundle_info->getBundleInfo('entity_test_bundle_plugin');
51     $this->assertEquals(2, count($bundle_info));
52     $this->assertArrayHasKey('first', $bundle_info);
53     $this->assertArrayHasKey('second', $bundle_info);
54     $this->assertEquals('First', $bundle_info['first']['label']);
55     $this->assertEquals('Some description', $bundle_info['first']['description']);
56
57     /** @var \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager */
58     $entity_field_manager = \Drupal::service('entity_field.manager');
59     $field_storage_definitions = $entity_field_manager->getFieldStorageDefinitions('entity_test_bundle_plugin');
60     $this->assertArrayHasKey('first_mail', $field_storage_definitions);
61     $this->assertArrayHasKey('second_mail', $field_storage_definitions);
62     $first_field_definitions = $entity_field_manager->getFieldDefinitions('entity_test_bundle_plugin', 'first');
63     $this->assertArrayHasKey('first_mail', $first_field_definitions);
64     $this->assertArrayNotHasKey('second_mail', $first_field_definitions);
65     $second_field_definitions = $entity_field_manager->getFieldDefinitions('entity_test_bundle_plugin', 'second');
66     $this->assertArrayNotHasKey('first_mail', $second_field_definitions);
67     $this->assertArrayHasKey('second_mail', $second_field_definitions);
68
69     $first_entity = EntityTestBundlePlugin::create([
70       'type' => 'first',
71       'first_mail' => 'admin@test.com',
72     ]);
73     $first_entity->save();
74     $first_entity = EntityTestBundlePlugin::load($first_entity->id());
75     $this->assertEquals('admin@test.com', $first_entity->first_mail->value);
76
77     $second_entity = EntityTestBundlePlugin::create([
78       'type' => 'second',
79       'second_mail' => 'admin@example.com',
80     ]);
81     $second_entity->save();
82     $second_entity = EntityTestBundlePlugin::load($second_entity->id());
83     $this->assertEquals('admin@example.com', $second_entity->second_mail->value);
84
85     // Also test entity queries.
86     $result = \Drupal::entityTypeManager()->getStorage('entity_test_bundle_plugin')
87       ->getQuery()
88       ->condition('second_mail', 'admin@example.com')
89       ->execute();
90     $this->assertEquals([$second_entity->id() => $second_entity->id()], $result);
91
92     $result = \Drupal::entityTypeManager()->getStorage('entity_test_bundle_plugin')
93       ->getQuery()
94       ->condition('type', 'first')
95       ->execute();
96     $this->assertEquals([$first_entity->id() => $first_entity->id()], $result);
97
98   }
99
100   /**
101    * Tests the uninstallation for a bundle provided by a module.
102    */
103   public function testBundlePluginModuleUninstallation() {
104     /** @var \Drupal\Core\Extension\ModuleInstallerInterface $module_installer */
105     $module_installer = \Drupal::service('module_installer');
106
107     // One should be possible to uninstall without any actual content.
108     $violations = $module_installer->validateUninstall(['entity_module_bundle_plugin_examples_test']);
109     $this->assertEmpty($violations);
110
111     $first_entity = EntityTestBundlePlugin::create([
112       'type' => 'first',
113       'first_mail' => 'admin@test.com',
114     ]);
115     $first_entity->save();
116     $second_entity = EntityTestBundlePlugin::create([
117       'type' => 'second',
118       'second_mail' => 'admin@example.com',
119     ]);
120     $second_entity->save();
121
122     $violations = $module_installer->validateUninstall(['entity_module_bundle_plugin_examples_test']);
123     $this->assertCount(1, $violations);
124     $this->assertCount(1, $violations['entity_module_bundle_plugin_examples_test']);
125     $this->assertEquals('There is data for the bundle Second on the entity type Entity test bundle plugin. Please remove all content before uninstalling the module.', $violations['entity_module_bundle_plugin_examples_test'][0]);
126
127     $second_entity->delete();
128
129     // The first entity is defined by entity_module_bundle_plugin_test, so it should be possible
130     // to uninstall the module providing the second bundle plugin.
131     $violations = $module_installer->validateUninstall(['entity_module_bundle_plugin_examples_test']);
132     $this->assertEmpty($violations);
133
134     $module_installer->uninstall(['entity_module_bundle_plugin_examples_test']);
135
136     // The first entity is provided by entity_module_bundle_plugin_test which we cannot uninstall,
137     // until all the entities are deleted.
138     $violations = $module_installer->validateUninstall(['entity_module_bundle_plugin_test']);
139     $this->assertNotEmpty($violations);
140
141     $first_entity->delete();
142     $violations = $module_installer->validateUninstall(['entity_module_bundle_plugin_test']);
143     $this->assertEmpty($violations);
144   }
145
146 }