Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / comment / tests / src / Kernel / CommentBundlesTest.php
1 <?php
2
3 namespace Drupal\Tests\comment\Kernel;
4
5 use Drupal\comment\Entity\CommentType;
6 use Drupal\KernelTests\KernelTestBase;
7
8 /**
9  * Tests that comment bundles behave as expected.
10  *
11  * @group comment
12  */
13 class CommentBundlesTest extends KernelTestBase {
14
15   /**
16    * {@inheritdoc}
17    */
18   public static $modules = ['comment', 'node', 'taxonomy', 'user'];
19
20   /**
21    * Entity type ids to use for target_entity_type_id on comment bundles.
22    *
23    * @var array
24    */
25   protected $targetEntityTypes;
26
27   /**
28    * @var \Drupal\Core\Entity\EntityFieldManagerInterface
29    */
30   protected $entityFieldManager;
31
32   /**
33    * {@inheritdoc}
34    */
35   protected function setUp() {
36     parent::setUp();
37
38     $this->entityFieldManager = $this->container->get('entity_field.manager');
39
40     $this->installEntitySchema('comment');
41
42     // Create multiple comment bundles,
43     // each of which has a different target entity type.
44     $this->targetEntityTypes = [
45       'comment' => 'Comment',
46       'node' => 'Node',
47       'taxonomy_term' => 'Taxonomy Term',
48     ];
49     foreach ($this->targetEntityTypes as $id => $label) {
50       CommentType::create([
51         'id' => 'comment_on_' . $id,
52         'label' => 'Comment on ' . $label,
53         'target_entity_type_id' => $id,
54       ])->save();
55     }
56   }
57
58   /**
59    * Test that the entity_id field is set correctly for each comment bundle.
60    */
61   public function testEntityIdField() {
62     $field_definitions = [];
63
64     foreach (array_keys($this->targetEntityTypes) as $id) {
65       $bundle = 'comment_on_' . $id;
66       $field_definitions[$bundle] = $this->entityFieldManager
67         ->getFieldDefinitions('comment', $bundle);
68     }
69     // Test that the value of the entity_id field for each bundle is correct.
70     foreach ($field_definitions as $bundle => $definition) {
71       $entity_type_id = str_replace('comment_on_', '', $bundle);
72       $target_type = $definition['entity_id']->getSetting('target_type');
73       $this->assertEquals($entity_type_id, $target_type);
74
75       // Verify that the target type remains correct
76       // in the deeply-nested object properties.
77       $nested_target_type = $definition['entity_id']->getItemDefinition()->getFieldDefinition()->getSetting('target_type');
78       $this->assertEquals($entity_type_id, $nested_target_type);
79     }
80
81   }
82
83 }